1 line
11 KiB
Plaintext
1 line
11 KiB
Plaintext
|
{"version":3,"names":["pkgJson","require","program","CommanderCommand","usage","version","option","handleError","err","logger","enable","opts","verbose","error","message","replace","stack","log","hasDebugMessages","info","chalk","dim","reset","process","exit","printExamples","examples","output","length","formattedUsage","map","example","desc","cyan","cmd","join","concat","bold","isDetachedCommand","command","detached","isAttachedCommand","attachCommand","config","name","action","handleAction","args","passedOptions","argv","Array","from","slice","func","Error","description","addHelpText","opt","options","parse","val","default","run","setupAndRun","e","includes","disable","setVerbose","platform","scriptName","absolutePath","path","__dirname","childProcess","execFileSync","stdio","warn","red","loadConfig","projectCommands","commands","debug","CLIError","detachedCommands","bin","resolve"],"sources":["../src/index.ts"],"sourcesContent":["import loadConfig from '@react-native-community/cli-config';\nimport {CLIError, logger} from '@react-native-community/cli-tools';\nimport type {\n Command,\n Config,\n DetachedCommand,\n} from '@react-native-community/cli-types';\nimport chalk from 'chalk';\nimport childProcess from 'child_process';\nimport {Command as CommanderCommand} from 'commander';\nimport path from 'path';\nimport {detachedCommands, projectCommands} from './commands';\n\nconst pkgJson = require('../package.json');\n\nconst program = new CommanderCommand()\n .usage('[command] [options]')\n .version(pkgJson.version, '-v', 'Output the current version')\n .option('--verbose', 'Increase logging verbosity');\n\nconst handleError = (err: Error) => {\n logger.enable();\n if (program.opts().verbose) {\n logger.error(err.message);\n } else {\n // Some error messages (esp. custom ones) might have `.` at the end already.\n const message = err.message.replace(/\\.$/, '');\n logger.error(`${message}.`);\n }\n if (err.stack) {\n logger.log(err.stack);\n }\n if (!program.opts().verbose && logger.hasDebugMessages()) {\n logger.info(\n chalk.dim(\n `Run CLI with ${chalk.reset('--verbose')} ${chalk.dim(\n 'flag for more details.',\n )}`,\n ),\n );\n }\n process.exit(1);\n};\n\nfunction printExamples(examples: Command['examples']) {\n let output: string[] = [];\n\n if (examples && examples.length > 0) {\n const formattedUsage = examples\n .map((example) => ` ${example.desc}: \\n ${chalk.cyan(example.cmd)}`)\n .join('\\n\\n');\n\n output = output.concat([chalk.bold('\\nExample usage:'), formattedUsage]);\n }\n\n return output.join('\\n').concat('\\n');\n}\n\n/**\n * Custom type assertion needed for the `makeCommand` conditional\n * types to be properly resolved.\n */\nfunction isDetachedCommand(\n command: Command<boolean>,\n): command is DetachedCommand {\n return command.detached === true;\n}\n\nfunction isAttachedCommand(\n command: Command<boolean>,\n): command is Command<false> {\n return !isDetachedCommand(command);\n}\n\n/**\n * Attaches a new command onto global `commander` instance.\n *\n * Note that this function takes additional argument of `Config` type in case\n * passed `command` needs it for its execution.\n */\nfunction attachCommand<C extends Command<boolean>>(\n command: C,\n config: C extends DetachedCommand ? Config | undefined : Config,\n): void {\n const cmd = program\n .command(command.name)\n .action(async function handleAction(\n this: CommanderCommand,\n ...args: string[]\n ) {\n const passedOptions = this.opts();\n const argv = Array.from(args).slice(0, -1);\n\n try {\n if (isDetachedCommand(command)) {\n await command.func(argv, passedOptions, config);\n } else if (isAttachedCommand(command)) {\n await command.func(argv, config, passedOptions);\n } else {\n throw new Error('A command must be either attached or detached');\n }\n } catch (error) {\n handleError(error as Error);\n }\n });\n\n if (command.descr
|