Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(baseCommand): inform user which project they're making changes to #643

Merged
merged 10 commits into from
Oct 27, 2022
Merged
35 changes: 35 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,41 @@ describe('cli', () => {
versionMock.done();
});

describe('logged-in user notifications', () => {
let consoleInfoSpy;
const getCommandOutput = () => {
return [consoleInfoSpy.mock.calls.join('\n\n')].filter(Boolean).join('\n\n');
};

beforeEach(() => {
conf.set('email', '[email protected]');
conf.set('project', 'owlbert');
conf.set('apiKey', '123456');
consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
});

afterEach(() => {
consoleInfoSpy.mockRestore();
conf.clear();
});

it('should inform a logged in user which project is being updated', async () => {
await expect(cli(['openapi', '--create', '--update'])).rejects.toThrow(
'The `--create` and `--update` options cannot be used simultaneously. Please use one or the other!'
);

expect(getCommandOutput()).toMatch('is currently logged in, using the stored API key for this project:');
});

it('should not inform a logged in user when they pass their own key', async () => {
await expect(cli(['openapi', '--create', '--update', '--key=asdf'])).rejects.toThrow(
'The `--create` and `--update` options cannot be used simultaneously. Please use one or the other!'
);

expect(getCommandOutput()).toBe('');
});
});

it('should error with `rdme oas` arguments passed in', async () => {
await expect(cli(['oas', 'endpoint'])).rejects.toThrow(/.*/);
});
Expand Down
13 changes: 13 additions & 0 deletions src/lib/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type commands from '../cmds';
import type { CommandLineOptions } from 'command-line-args';
import type { OptionDefinition } from 'command-line-usage';

import chalk from 'chalk';

import configstore from './configstore';
import isCI from './isCI';
import { debug, info, warn } from './logger';
Expand Down Expand Up @@ -93,6 +95,17 @@ export default class Command {
Command.debug(`opts: ${JSON.stringify(opts)}`);

if (this.args.some(arg => arg.name === 'key')) {
if (opts.key && configstore.get('apiKey') === opts.key) {
info(
`🔑 ${configstore.get(
'email'
)} is currently logged in, using the stored API key for this project: ${chalk.blue(
configstore.get('project')
)}`,
false
);
}

if (!opts.key) {
if (isCI()) {
throw new Error('No project API key provided. Please use `--key`.');
Expand Down