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
37 changes: 37 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import prompts from 'prompts';

import { version as pkgVersion } from '../package.json';
import cli from '../src';
import OpenAPICommand from '../src/cmds/openapi';
import conf from '../src/lib/configstore';

import getAPIMock from './helpers/get-api-mock';
Expand Down Expand Up @@ -108,6 +109,42 @@ 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', 'owlbert@readme.io');
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 () => {
const openapi = new OpenAPICommand();
const key = '123456';

await expect(openapi.run({ key, create: true, update: true })).rejects.toStrictEqual(
new Error('The `--create` and `--update` options cannot be used simultaneously. Please use one or the other!')
);
darrenyong marked this conversation as resolved.
Show resolved Hide resolved

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('Command not found.');
darrenyong marked this conversation as resolved.
Show resolved Hide resolved

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