diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 8c3075466..c6ac6fd32 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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(/.*/); }); diff --git a/src/lib/baseCommand.ts b/src/lib/baseCommand.ts index 1152ea63b..e15d6e96c 100644 --- a/src/lib/baseCommand.ts +++ b/src/lib/baseCommand.ts @@ -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'; @@ -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`.');