From 1bb244822dd657a7f128dbefb50b92d1d227a5fb Mon Sep 17 00:00:00 2001 From: Darren Yong <41130056+darrenyong@users.noreply.github.com> Date: Thu, 27 Oct 2022 16:45:50 -0700 Subject: [PATCH] feat(baseCommand): inform user which project they're making changes to (#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 --- __tests__/index.test.ts | 35 +++++++++++++++++++++++++++++++++++ src/lib/baseCommand.ts | 13 +++++++++++++ 2 files changed, 48 insertions(+) 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`.');