Fix bug where command flags were stolen by commander

This commit is contained in:
Todd Bluhm 2019-05-05 22:27:37 -05:00
parent b1ad7e4902
commit 2dafb745f7
No known key found for this signature in database
GPG Key ID: 9CF312607477B8AB
6 changed files with 57 additions and 30 deletions

View File

@ -1,5 +1,8 @@
# Changelog
## 9.0.1 - Unreleased
## 9.0.1
- **BREAKING**: Fixed major bug that required passing `--` inorder to pass flags to the command.
Normally I release major breaking changes as major versions, but this was a bug and no documentation
anywhere states using `--` as intended or official behavior.
- **Change**: Fixed some documentation issues
- **Change**: `npm run lint` command now includes calling `tsc` to check for typescript errors

View File

@ -1,5 +1,7 @@
import { Command } from 'commander';
import { EnvCmdOptions } from './types';
/**
* Parses the arguments passed into the cli
*/
export declare function parseArgs(args: string[]): EnvCmdOptions;
export declare function parseArgsUsingCommander(args: string[]): Command;

32
dist/parse-args.js vendored
View File

@ -6,20 +6,12 @@ const utils_1 = require("./utils");
* Parses the arguments passed into the cli
*/
function parseArgs(args) {
const program = new commander_1.Command();
program
.version('9.0.0', '-v, --version')
.usage('[options] <command> [...args]')
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc(|.js|.json)')
.option('-e, --environments [env1,env2,...]', 'The rc file environment(s) to use', utils_1.parseArgList)
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
.option('--no-override', 'Do not override existing environment variables')
.option('--use-shell', 'Execute the command in a new shell with the given environment')
.parse(['_', '_', ...args]);
// get the command and command args
// Get the command and command args
let program = parseArgsUsingCommander(args);
const command = program.args[0];
const commandArgs = program.args.slice(1);
const commandArgs = args.splice(args.indexOf(command) + 1);
// Reprocess the args with the command and command args removed
program = parseArgsUsingCommander(args.slice(0, args.indexOf(command)));
const noOverride = !program.override;
const useShell = !!program.useShell;
let rc;
@ -46,3 +38,17 @@ function parseArgs(args) {
};
}
exports.parseArgs = parseArgs;
function parseArgsUsingCommander(args) {
const program = new commander_1.Command();
return program
.version('9.0.1', '-v, --version')
.usage('[options] <command> [...args]')
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc(|.js|.json)')
.option('-e, --environments [env1,env2,...]', 'The rc file environment(s) to use', utils_1.parseArgList)
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
.option('--no-override', 'Do not override existing environment variables')
.option('--use-shell', 'Execute the command in a new shell with the given environment')
.parse(['_', '_', ...args]);
}
exports.parseArgsUsingCommander = parseArgsUsingCommander;

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "env-cmd",
"version": "9.0.0",
"version": "9.0.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -6,21 +6,13 @@ import { parseArgList } from './utils'
* Parses the arguments passed into the cli
*/
export function parseArgs (args: string[]): EnvCmdOptions {
const program = new Command()
program
.version('9.0.0', '-v, --version')
.usage('[options] <command> [...args]')
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc(|.js|.json)')
.option('-e, --environments [env1,env2,...]', 'The rc file environment(s) to use', parseArgList)
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
.option('--no-override', 'Do not override existing environment variables')
.option('--use-shell', 'Execute the command in a new shell with the given environment')
.parse(['_', '_', ...args])
// get the command and command args
// Get the command and command args
let program = parseArgsUsingCommander(args)
const command = program.args[0]
const commandArgs = program.args.slice(1)
const commandArgs = args.splice(args.indexOf(command) + 1)
// Reprocess the args with the command and command args removed
program = parseArgsUsingCommander(args.slice(0, args.indexOf(command)))
const noOverride = !program.override
const useShell = !!program.useShell
@ -49,3 +41,17 @@ export function parseArgs (args: string[]): EnvCmdOptions {
}
}
}
export function parseArgsUsingCommander (args: string[]): Command {
const program = new Command()
return program
.version('9.0.1', '-v, --version')
.usage('[options] <command> [...args]')
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc(|.js|.json)')
.option('-e, --environments [env1,env2,...]', 'The rc file environment(s) to use', parseArgList)
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
.option('--no-override', 'Do not override existing environment variables')
.option('--use-shell', 'Execute the command in a new shell with the given environment')
.parse(['_', '_', ...args])
}

View File

@ -4,7 +4,7 @@ import { parseArgs } from '../src/parse-args'
describe('parseArgs', (): void => {
const command = 'command'
const commandArgs = ['cmda1', 'cmda2']
const commandArgs = ['cmda1', 'cmda2', '--cmda3', '-4', 'cmda4']
const environments = ['development', 'production']
const rcFilePath = './.env-cmdrc'
const envFilePath = './.env'
@ -30,6 +30,16 @@ describe('parseArgs', (): void => {
assert.sameOrderedMembers(res.commandArgs, commandArgs)
})
it('should parse multiple command arguments even if they use the same options flags as env-cmd',
(): void => {
const commandFlags = ['-f', './other-file', '--use-shell', '-r']
const res = parseArgs(['-e', environments[0], command, ...commandFlags])
assert.sameOrderedMembers(res.commandArgs, commandFlags)
assert.notOk(res.options!.useShell)
assert.notOk(res.envFile)
}
)
it('should parse override option', (): void => {
const res = parseArgs(['-e', environments[0], '--no-override', command, ...commandArgs])
assert.exists(res.options)