1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | 1x 28x 28x 28x 28x 28x 28x 28x 4x 24x 24x 3x 3x 21x 21x 2x 19x 16x 3x 19x 2x 2x 17x 17x 17x 17x 17x 17x 17x 1x 1x 16x 13x 13x 3x 28x 28x 28x 4x 4x 4x 4x 4x 2x 1x 1x 3x 3x 1x 1x 2x 2x 2x 2x 2x 24x 24x 371x 32x 2x 30x 28x 28x 27x 1x 21x 3x 21x 21x 1x 1x 20x 28x | import findUp from 'find-up' import yargs from 'yargs/yargs' import chalk from 'chalk' import {keyInYN} from 'readline-sync' import {includes, isEqual} from 'lodash' import {oneLine} from 'common-tags' import getLogger from '../get-logger' import { preloadModule, loadConfig, loadCLIConfig, initialize, help, specificHelpScript, } from '../bin-utils' import getCompletionScripts from './autocomplete-get-scripts' const log = getLogger() export default parse function parse(rawArgv) { let commandExecuted = false const configOption = { describe: oneLine` Config file to use (defaults to nearest package-scripts.yml or package-scripts.js) `, alias: 'c', default: getPSConfigFilepath(), } const baseOptions = { config: configOption, silent: { describe: 'Silent nps output', alias: 's', type: 'boolean', default: false, }, 'log-level': { describe: 'The log level to use', choices: ['error', 'warn', 'info', 'debug'], alias: 'l', default: 'info', }, require: { describe: 'Module to preload', alias: 'r', default: undefined, }, scripts: { describe: 'Log command text for script', type: 'boolean', default: true, }, 'help-style': { describe: 'Choose the level of detail displayed by the help command', choices: ['all', 'scripts', 'basic'], alias: 'y', default: 'all', }, } const yargsInstance = yargs(rawArgv) const parser = yargsInstance .config(getCLIConfig()) .usage('Usage: $0 [options] <script>...') .example('$0 test build', 'Runs the `test` script then the `build` script') .example( '$0 "test --cover" "build --prod"', oneLine` Runs the \`test\` script and forwards the "--cover" flag then the \`build\` script and forwards the "--prod" flag `, ) .help(false) .alias('h', 'help') .version() .alias('v', 'version') .options(baseOptions) .command(...getInitCommand()) .completion('completion', completionHandler) .exitProcess(shouldExitProcess()) const parsedArgv = parser.parse(rawArgv) if (commandExecuted) { return undefined } const invalidFlags = getInvalidFlags() if (invalidFlags.length) { log.error({ message: chalk.red( oneLine` You provided one or more invalid flags: ${invalidFlags.join(', ')}\n Did you forget to put your command in quotes? `, ), ref: 'invalid-flags', }) throw new Error(`invalid flag(s) passed: ${invalidFlags}`) } const psConfig = getPSConfig(parsedArgv) if (!psConfig) { return undefined } if (showHelp(parsedArgv._)) { return undefined } return {argv: parsedArgv, psConfig} // util functions // eslint-disable-next-line complexity function showHelp(specifiedScripts) { if (parsedArgv.help) { // if --help was specified, then yargs will show the default help log.info(help(psConfig)) return true } const helpStyle = String(psConfig.options['help-style']) const hasDefaultScript = Boolean(psConfig.scripts.default) const noScriptSpecifiedAndNoDefault = !specifiedScripts.length && !hasDefaultScript const hasHelpScript = Boolean(psConfig.scripts.help) const commandIsHelp = isEqual(specifiedScripts[0], 'help') && !hasHelpScript const shouldShowSpecificScriptHelp = commandIsHelp && specifiedScripts.length > 1 if (shouldShowSpecificScriptHelp) { log.info(specificHelpScript(psConfig, specifiedScripts[1])) return true } else if (commandIsHelp || noScriptSpecifiedAndNoDefault) { // Can't achieve 100% branch coverage without refactoring this showHelp() // function into ./index.js and re-working existing tests and such. Branch // options aren't relevant here either, so telling Istanbul to ignore. /* istanbul ignore next */ if (helpStyle === 'all') { parser.showHelp('log') } log.info(help(psConfig)) return true } return false } function getInitCommand() { const command = 'init' const description = 'automatically migrate from npm scripts to nps' return [command, description, getConfig, onInit] function getConfig(initYargs) { return initYargs.usage('Usage: $0 init [options]').options({ config: configOption, type: { describe: 'The type of config to generate', choices: ['js', 'yml'], default: 'js', }, }) } function onInit(initArgv) { commandExecuted = true const path = getPSConfigFilepath(initArgv) const fileExists = typeof path === 'string' && Boolean(findUp.sync(path)) if (fileExists) { if ( !keyInYN( chalk.yellow(`Do you want to overwrite your existing config file?`), ) ) { log.info( chalk.yellow( `Exiting. Please specify a different config file to use on init.`, ), ) return } } const init = initialize(initArgv.type) if (!init) { log.error(chalk.red('Unable to to find an existing package.json')) return } const packageScriptsPath = init.packageScriptsPath log.info( `Your scripts have been saved at ${chalk.green(packageScriptsPath)}`, ) log.info( chalk.gray( oneLine` Check out your scripts in there. Go ahead and update them and add descriptions to the ones that need it `, ), ) log.info( chalk.gray( oneLine` Your package.json scripts have also been updated. Run \`npm start help\` for help `, ), ) log.info( chalk.gray( oneLine` You may also want to install the package globally and installing autocomplete script. You can do so by running \n npm install --global nps \n nps completion >> <your-bash-profile-file> `, ), ) } } /* istanbul ignore next */ function completionHandler(currentInput, currentArgv) { commandExecuted = true const {scripts} = getPSConfig(currentArgv) || {} if (scripts) { return getCompletionScripts(scripts, currentInput) } return [] } function getInvalidFlags() { const customFlags = Object.keys(yargsInstance.getOptions().default) const allowedFlags = [ ...customFlags, 'v', 'version', 'h', 'help', '$0', '_', ] return Object.keys(parsedArgv).filter(key => !includes(allowedFlags, key)) } function getPSConfigFilepath({config} = {}) { if (config) { return config } return ( findUp.sync('package-scripts.js') || findUp.sync('package-scripts.yml') || findUp.sync('package-scripts.yaml') ) } } function getCLIConfig() { const configPath = findUp.sync('.npsrc') || findUp.sync('.npsrc.json') if (!configPath) { return {} } return loadCLIConfig(configPath) } function getPSConfig(parsedArgv) { if (parsedArgv.require) { preloadModule(parsedArgv.require) } const configFilepath = parsedArgv.config if (!configFilepath) { log.warn({ message: chalk.yellow( 'Unable to find a config file and none was specified.', ), ref: 'unable-to-find-config', }) return undefined } return loadConfig(configFilepath, parsedArgv._) } function shouldExitProcess(rawArgv) { return !(isEqual(rawArgv, ['-h']) || isEqual(rawArgv, ['--help'])) } |