Skip to content

Commit

Permalink
🆕 Add node-supported flags to commander options
Browse files Browse the repository at this point in the history
  • Loading branch information
SteelBrain committed Oct 6, 2020
1 parent c76d39f commit 664e430
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"clean": "rimraf lib",
"prepare": "yarn build",
"build": "yarn clean && babel src --out-dir lib",
"watch": "yarn build -- -w",
"watch": "yarn build -w",
"lint": "(eslint . ) && (prettier --list-different src/*.js)"
},
"dependencies": {
Expand Down
50 changes: 24 additions & 26 deletions src/cli/index.js
Expand Up @@ -4,9 +4,19 @@ import get from 'lodash/get'
import program from 'commander'

import main from '..'
import { logError, SUPPORTED_FLAGS } from '../helpers'
import { logError } from '../helpers'
import manifest from '../../package.json'

// [ arg for cli, name in commander, has arg ]
const NODE_FLAGS = [
['debug-port', 'debugPort', true],
['inspect-port', 'inspectPort', true],
['inspect', 'inspect', false],
['inspect-brk', 'inspectBrk', false],
['inspect-publish-uid', 'inspectPublishUid', true],
['enable-source-maps', 'enableSourceMaps', false],
]

program
.version(manifest.version)
.description(manifest.description)
Expand All @@ -30,11 +40,17 @@ program
)
.option('--typescript', 'Enables typescript support by processing .ts and .tsx files')
.on('--help', () => {
console.log('\nArguments after -- will be passed as-are to the program specified in -x flag')
console.log('Supported NodeJS CLI flags: ', SUPPORTED_FLAGS.join(', '))
console.log('\nArguments after -- will be passed as-are to programs executed through -x')
})
.allowUnknownOption()
.parse(process.argv)

NODE_FLAGS.forEach(([nodeFlag, hasOption]) => {
program.option(
`--${nodeFlag}${hasOption ? ' <arg>' : ''}`,
'Passthrough flag for Node.js runtime for programs executed through -x',
)
})

program.parse(process.argv)

if (program.args.length < 1) {
program.outputHelp()
Expand All @@ -46,27 +62,9 @@ if (typeof program.outputDirectory === 'undefined') {
process.exit(1)
}

const nodeFlags = []
SUPPORTED_FLAGS.forEach((item) => {
const flagIndex = program.rawArgs.indexOf(item)
if (flagIndex !== -1) {
let flagValue = program.rawArgs[flagIndex + 1] || true
if (
typeof flagValue === 'undefined' ||
(typeof flagValue === 'string' &&
(flagValue.startsWith('-') || program.args.includes(flagValue)))
) {
// ^ If it's got no value, or value is an option or value is present in args
// Then the value must not be own, so treat this as a boolean
flagValue = true
}
nodeFlags.push(item)
if (flagValue !== true) {
// ^ True values are unnecessary to specify
nodeFlags.push(flagValue)
}
}
})
const nodeFlags = NODE_FLAGS.filter((item) => program[item[1]] != null)
.map((item) => (item[2] ? [`--${item[0]}`, program[item[1]]] : [`--${item[0]}`]))
.flat()

const config = {
root: get(program, 'root', process.cwd()),
Expand Down
8 changes: 0 additions & 8 deletions src/helpers.js
Expand Up @@ -8,14 +8,6 @@ import debounce from 'lodash/debounce'
import resolveFrom from 'resolve-from'
import AdapterFileAsync from 'lowdb/adapters/FileAsync'

export const SUPPORTED_FLAGS = [
'--debug-port',
'--inspect-port',
'--inspect',
'--inspect-brk',
'--inspect-publish-uid',
'--enable-source-maps',
]
export class CLIError extends Error {}

export function getSha1(contents) {
Expand Down

0 comments on commit 664e430

Please sign in to comment.