Skip to content

Commit

Permalink
feat(baseCommand): inform user which project they're making changes to (
Browse files Browse the repository at this point in the history
#643)

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

* feat(baseCommand): add check to surface notification only if user is logged in

* feat(baseCommand): update logic for surfacing project notification

* test: add test for surfacing project notifications

* refactor: copy changes

* Merge branch 'main' into darren/rm-5582-inform-user-were-using-their-stored

* refactor: update test to use `openapi` command after `docs:single` refactor

* refactor: pass options in correctly

Co-authored-by: Kanad Gupta <kanad@readme.io>
  • Loading branch information
darrenyong and kanadgupta authored Oct 27, 2022
1 parent d412a04 commit 1bb2448
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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', '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 () => {
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

0 comments on commit 1bb2448

Please sign in to comment.