From 9ae604591dbb7e82aff0668d2055ed9d69bb920a Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Wed, 24 Feb 2021 21:51:39 +0100 Subject: [PATCH] fix(CLI): Recognize "-s" as "--stage" alias, when expected --- lib/cli/resolve-input.js | 9 ++++++- test/unit/lib/cli/resolve-input.test.js | 34 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/cli/resolve-input.js b/lib/cli/resolve-input.js index 5b6793568..785878cd4 100644 --- a/lib/cli/resolve-input.js +++ b/lib/cli/resolve-input.js @@ -5,12 +5,14 @@ const memoizee = require('memoizee'); const parseArgs = require('./parse-args'); +const customSAliasCommands = new Set(['config credentials'], ['config tabcompletion install']); + module.exports = memoizee(() => { const args = process.argv.slice(2); const baseArgsSchema = { boolean: new Set(['help', 'help-interactive', 'v', 'version']), - string: new Set(['app', 'config', 'org']), + string: new Set(['app', 'config', 'org', 'stage']), alias: new Map([ ['c', 'config'], ['h', 'help'], @@ -29,6 +31,11 @@ module.exports = memoizee(() => { baseArgsSchema.boolean.delete('v'); baseArgsSchema.alias.set('v', 'version'); } + if (!customSAliasCommands.has(command)) { + // Unfortunately, there are few command for which "-s" aliases different param than "--stage" + // This handling ensures we do not break those commands + baseArgsSchema.alias.set('s', 'stage'); + } options = parseArgs(args, baseArgsSchema); diff --git a/test/unit/lib/cli/resolve-input.test.js b/test/unit/lib/cli/resolve-input.test.js index 7414fd96f..74891a400 100644 --- a/test/unit/lib/cli/resolve-input.test.js +++ b/test/unit/lib/cli/resolve-input.test.js @@ -63,6 +63,40 @@ describe('test/unit/lib/cli/resolve-input.test.js', () => { }); }); }); + + describe('"-s" handling', () => { + describe('Normal command', () => { + let data; + before(() => { + resolveInput.clear(); + data = overrideArgv( + { + args: ['serverless', 'cmd1', 'cmd2', '-s', 'stage'], + }, + () => resolveInput() + ); + }); + it('should recognize stage alias', async () => { + expect(data.options.stage).to.equal('stage'); + }); + }); + describe('Command with custom -s alias', () => { + let data; + before(() => { + resolveInput.clear(); + data = overrideArgv( + { + args: ['serverless', 'config', 'credentials', '-s', 'stage'], + }, + () => resolveInput() + ); + }); + it('should recognize stage alias', async () => { + expect(data.options).to.not.have.property('stage'); + }); + }); + }); + describe('when no commands', () => { let data; before(() => {