fix(CLI): Recognize "-s" as "--stage" alias, when expected

This commit is contained in:
Mariusz Nowak 2021-02-24 21:51:39 +01:00 committed by Mariusz Nowak
parent 8db64a1f31
commit 9ae604591d
2 changed files with 42 additions and 1 deletions

View File

@ -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);

View File

@ -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(() => {