From c0ce28cb845e054d56a19937d7870d91c60d2d66 Mon Sep 17 00:00:00 2001 From: Todd Bluhm Date: Mon, 17 Feb 2020 17:42:53 -0600 Subject: [PATCH] chore(package): updated ts-standard version - improved code readability of options parsing - added missing code coverage test cases --- CHANGELOG.md | 3 +++ dist/parse-args.js | 29 +++++++++++++++++++++++------ dist/parse-env-file.js | 2 +- package.json | 2 +- src/parse-args.ts | 30 ++++++++++++++++++++++++------ src/parse-env-file.ts | 2 +- test/parse-args.spec.ts | 12 ++++++++++++ 7 files changed, 65 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aac0efe..2562961 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 10.1.1 - Pending +- **Upgrade**: Updated devDependencies `ts-standard` + ## 10.1.0 - **Feature**: Added support for expanding vars using the `-x` flag. diff --git a/dist/parse-args.js b/dist/parse-args.js index 34ed8ba..544aad4 100644 --- a/dist/parse-args.js +++ b/dist/parse-args.js @@ -14,11 +14,28 @@ function parseArgs(args) { 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; - const expandEnvs = !!program.expandEnvs; - const verbose = !!program.verbose; - const silent = !!program.silent; + // Set values for provided options + let noOverride = false; + // In commander `no-` negates the original value `override` + if (program.override === false) { + noOverride = true; + } + let useShell = false; + if (program.useShell === true) { + useShell = true; + } + let expandEnvs = false; + if (program.expandEnvs === true) { + expandEnvs = true; + } + let verbose = false; + if (program.verbose === true) { + verbose = true; + } + let silent = false; + if (program.silent === true) { + silent = true; + } let rc; if (program.environments !== undefined && program.environments.length !== 0) { rc = { @@ -46,7 +63,7 @@ function parseArgs(args) { verbose } }; - if (verbose === true) { + if (verbose) { console.info(`Options: ${JSON.stringify(options, null, 0)}`); } return options; diff --git a/dist/parse-env-file.js b/dist/parse-env-file.js index a5b27d7..dc1c854 100644 --- a/dist/parse-env-file.js +++ b/dist/parse-env-file.js @@ -17,7 +17,7 @@ async function getEnvFileVars(envFilePath) { // Get the file extension const ext = path.extname(absolutePath).toLowerCase(); let env = {}; - if (REQUIRE_HOOK_EXTENSIONS.indexOf(ext) > -1) { + if (REQUIRE_HOOK_EXTENSIONS.includes(ext)) { const possiblePromise = require(absolutePath); /* eslint-disable-line */ env = utils_1.isPromise(possiblePromise) ? await possiblePromise : possiblePromise; } diff --git a/package.json b/package.json index 2994803..a855617 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "nyc": "^15.0.0", "sinon": "^8.0.0", "ts-node": "^8.0.0", - "ts-standard": "^4.0.0", + "ts-standard": "^5.0.0", "typescript": "^3.7.0" }, "nyc": { diff --git a/src/parse-args.ts b/src/parse-args.ts index 54b3ad1..892798b 100644 --- a/src/parse-args.ts +++ b/src/parse-args.ts @@ -16,11 +16,29 @@ export function parseArgs (args: string[]): EnvCmdOptions { // Reprocess the args with the command and command args removed program = parseArgsUsingCommander(args.slice(0, args.indexOf(command))) - const noOverride = !(program.override as boolean) - const useShell = !!(program.useShell as boolean) - const expandEnvs = !!(program.expandEnvs as boolean) - const verbose = !!(program.verbose as boolean) - const silent = !!(program.silent as boolean) + + // Set values for provided options + let noOverride = false + // In commander `no-` negates the original value `override` + if (program.override === false) { + noOverride = true + } + let useShell = false + if (program.useShell === true) { + useShell = true + } + let expandEnvs = false + if (program.expandEnvs === true) { + expandEnvs = true + } + let verbose = false + if (program.verbose === true) { + verbose = true + } + let silent = false + if (program.silent === true) { + silent = true + } let rc: any if (program.environments !== undefined && program.environments.length !== 0) { @@ -51,7 +69,7 @@ export function parseArgs (args: string[]): EnvCmdOptions { verbose } } - if (verbose === true) { + if (verbose) { console.info(`Options: ${JSON.stringify(options, null, 0)}`) } return options diff --git a/src/parse-env-file.ts b/src/parse-env-file.ts index c3326b0..2992272 100644 --- a/src/parse-env-file.ts +++ b/src/parse-env-file.ts @@ -18,7 +18,7 @@ export async function getEnvFileVars (envFilePath: string): Promise<{ [key: stri // Get the file extension const ext = path.extname(absolutePath).toLowerCase() let env = {} - if (REQUIRE_HOOK_EXTENSIONS.indexOf(ext) > -1) { + if (REQUIRE_HOOK_EXTENSIONS.includes(ext)) { const possiblePromise = require(absolutePath) /* eslint-disable-line */ env = isPromise(possiblePromise) ? await possiblePromise : possiblePromise } else { diff --git a/test/parse-args.spec.ts b/test/parse-args.spec.ts index 04889e9..79a8783 100644 --- a/test/parse-args.spec.ts +++ b/test/parse-args.spec.ts @@ -91,4 +91,16 @@ describe('parseArgs', (): void => { assert.exists(res.options!.verbose) assert.equal(logInfoStub.callCount, 1) }) + + it('should parse expandEnvs option', (): void => { + const res = parseArgs(['-f', envFilePath, '-x', command, ...commandArgs]) + assert.exists(res.envFile) + assert.isTrue(res.options!.expandEnvs) + }) + + it('should parse silent option', (): void => { + const res = parseArgs(['-f', envFilePath, '--silent', command, ...commandArgs]) + assert.exists(res.envFile) + assert.isTrue(res.options!.silent) + }) })