-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use common-bin parse execArgv
- Loading branch information
Showing
3 changed files
with
19 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,27 @@ | ||
'use strict'; | ||
|
||
const BaseCommand = require('common-bin'); | ||
const changeCase = require('change-case'); | ||
const parser = require('yargs-parser'); | ||
|
||
class Command extends BaseCommand { | ||
/** | ||
* normalize context | ||
* @param {Object} context - { cwd, argv, rawArgv } | ||
* @return {Object} return with { cwd, argv, execArgv, rawArgv } | ||
*/ | ||
constructor(rawArgv) { | ||
super(rawArgv); | ||
this.parserOptions = { | ||
execArgv: true, | ||
removeAlias: true, | ||
}; | ||
} | ||
|
||
get context() { | ||
const context = super.context; | ||
const argv = context.argv; | ||
|
||
let execArgvObj = {}; | ||
let debugPort; | ||
|
||
// extract from command argv | ||
debugPort = findDebugPort(argv); | ||
execArgvObj = extractExecArgv(argv); | ||
|
||
// extract from WebStorm env `$NODE_DEBUG_OPTION` | ||
if (context.env.NODE_DEBUG_OPTION) { | ||
console.log('Use $NODE_DEBUG_OPTION: %s', context.env.NODE_DEBUG_OPTION); | ||
const argvFromEnv = parser(context.env.NODE_DEBUG_OPTION); | ||
debugPort = findDebugPort(argvFromEnv); | ||
Object.assign(execArgvObj, extractExecArgv(argvFromEnv)); | ||
} | ||
|
||
context.execArgv = this.helper.unparseArgv(execArgvObj); | ||
context.execArgvObj = execArgvObj; | ||
context.debug = debugPort; | ||
// compatible | ||
if (context.debugPort) context.debug = context.debugPort; | ||
|
||
// remove unuse args | ||
argv.$0 = undefined; | ||
context.argv.$0 = undefined; | ||
|
||
return context; | ||
} | ||
} | ||
|
||
function match(key, arr) { | ||
return arr.some(x => x instanceof RegExp ? x.test(key) : x === key); // eslint-disable-line no-confusing-arrow | ||
} | ||
|
||
function findDebugPort(argv) { | ||
let debugPort; | ||
|
||
for (const key of Object.keys(argv)) { | ||
if (match(key, [ /^debug.*/, /^inspect.*/ ]) && typeof argv[key] === 'number') { | ||
debugPort = argv[key]; | ||
} | ||
} | ||
return debugPort; | ||
} | ||
|
||
// pick and remove all execArgv from origin `argv` | ||
function extractExecArgv(argv) { | ||
const execArgvObj = {}; | ||
for (const key of Object.keys(argv)) { | ||
// debug / debug-brk / debug-port / inspect / inspect-brk / inspect-port | ||
if (match(key, [ /^debug.*/, /^inspect.*/, 'es_staging', 'expose_debug_as', /^harmony.*/ ])) { | ||
execArgvObj[key] = argv[key]; | ||
// remove from origin obj | ||
argv[key] = undefined; | ||
// also remove `debugBrk` | ||
argv[changeCase.camelCase(key)] = undefined; | ||
} | ||
} | ||
return execArgvObj; | ||
} | ||
|
||
module.exports = Command; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters