UNPKG

7.84 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.run = run;
7Object.defineProperty(exports, "init", {
8 enumerable: true,
9 get: function () {
10 return _initCompat.default;
11 }
12});
13exports.bin = void 0;
14
15function _chalk() {
16 const data = _interopRequireDefault(require("chalk"));
17
18 _chalk = function () {
19 return data;
20 };
21
22 return data;
23}
24
25function _child_process() {
26 const data = _interopRequireDefault(require("child_process"));
27
28 _child_process = function () {
29 return data;
30 };
31
32 return data;
33}
34
35function _commander() {
36 const data = _interopRequireDefault(require("commander"));
37
38 _commander = function () {
39 return data;
40 };
41
42 return data;
43}
44
45function _leven() {
46 const data = _interopRequireDefault(require("leven"));
47
48 _leven = function () {
49 return data;
50 };
51
52 return data;
53}
54
55function _path() {
56 const data = _interopRequireDefault(require("path"));
57
58 _path = function () {
59 return data;
60 };
61
62 return data;
63}
64
65function _cliTools() {
66 const data = require("@react-native-community/cli-tools");
67
68 _cliTools = function () {
69 return data;
70 };
71
72 return data;
73}
74
75var _commands = require("./commands");
76
77var _initCompat = _interopRequireDefault(require("./commands/init/initCompat"));
78
79var _config = _interopRequireDefault(require("./tools/config"));
80
81function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
82
83const pkgJson = require("../package.json");
84
85_commander().default.usage('<command> [options]').option('--version', 'Print CLI version').option('--verbose', 'Increase logging verbosity');
86
87_commander().default.arguments('<command>').action(cmd => {
88 printUnknownCommand(cmd);
89 process.exit(1);
90});
91
92const handleError = err => {
93 _cliTools().logger.enable();
94
95 if (_commander().default.verbose) {
96 _cliTools().logger.error(err.message);
97 } else {
98 // Some error messages (esp. custom ones) might have `.` at the end already.
99 const message = err.message.replace(/\.$/, '');
100
101 _cliTools().logger.error(`${message}.`);
102 }
103
104 if (err.stack) {
105 _cliTools().logger.log(err.stack);
106 }
107
108 if (!_commander().default.verbose) {
109 _cliTools().logger.info(_chalk().default.dim(`Run CLI with ${_chalk().default.reset('--verbose')} ${_chalk().default.dim('flag for more details.')}`));
110 }
111
112 process.exit(1);
113};
114/**
115 * Custom printHelpInformation command inspired by internal Commander.js
116 * one modified to suit our needs
117 */
118
119
120function printHelpInformation(examples, pkg) {
121 let cmdName = this._name;
122
123 const argsList = this._args.map(arg => arg.required ? `<${arg.name}>` : `[${arg.name}]`).join(' ');
124
125 if (this._alias) {
126 cmdName = `${cmdName}|${this._alias}`;
127 }
128
129 const sourceInformation = pkg ? [`${_chalk().default.bold('Source:')} ${pkg.name}@${pkg.version}`, ''] : [];
130 let output = [_chalk().default.bold(`react-native ${cmdName} ${argsList}`), this._description ? `\n${this._description}\n` : '', ...sourceInformation, `${_chalk().default.bold('Options:')}`, this.optionHelp().replace(/^/gm, ' ')];
131
132 if (examples && examples.length > 0) {
133 const formattedUsage = examples.map(example => ` ${example.desc}: \n ${_chalk().default.cyan(example.cmd)}`).join('\n\n');
134 output = output.concat([_chalk().default.bold('\nExample usage:'), formattedUsage]);
135 }
136
137 return output.join('\n').concat('\n');
138}
139
140function printUnknownCommand(cmdName) {
141 const availableCommands = _commander().default.commands.map(cmd => cmd._name);
142
143 const suggestion = availableCommands.find(cmd => {
144 return (0, _leven().default)(cmd, cmdName) < cmd.length * 0.4;
145 });
146 let errorMsg = `Unrecognized command "${_chalk().default.bold(cmdName)}".`;
147
148 if (suggestion) {
149 errorMsg += ` Did you mean "${suggestion}"?`;
150 }
151
152 if (cmdName) {
153 _cliTools().logger.error(errorMsg);
154
155 _cliTools().logger.info(`Run ${_chalk().default.bold('"react-native --help"')} to see a list of all available commands.`);
156 } else {
157 _commander().default.outputHelp();
158 }
159}
160/**
161 * Custom type assertion needed for the `makeCommand` conditional
162 * types to be properly resolved.
163 */
164
165
166const isDetachedCommand = command => {
167 return command.detached === true;
168};
169/**
170 * Attaches a new command onto global `commander` instance.
171 *
172 * Note that this function takes additional argument of `Config` type in case
173 * passed `command` needs it for its execution.
174 */
175
176
177function attachCommand(command, ...rest) {
178 const cmd = _commander().default.command(command.name).action(async function handleAction(...args) {
179 const passedOptions = this.opts();
180 const argv = Array.from(args).slice(0, -1);
181
182 try {
183 if (isDetachedCommand(command)) {
184 await command.func(argv, passedOptions);
185 } else {
186 await command.func(argv, rest[0], passedOptions);
187 }
188 } catch (error) {
189 handleError(error);
190 }
191 });
192
193 if (command.description) {
194 cmd.description(command.description);
195 }
196
197 cmd.helpInformation = printHelpInformation.bind(cmd, command.examples, command.pkg);
198
199 for (const opt of command.options || []) {
200 cmd.option(opt.name, opt.description, opt.parse || (val => val), typeof opt.default === 'function' ? opt.default(rest[0]) : opt.default);
201 }
202}
203
204async function run() {
205 try {
206 await setupAndRun();
207 } catch (e) {
208 handleError(e);
209 }
210}
211
212async function setupAndRun() {
213 // Commander is not available yet
214 // when we run `config`, we don't want to output anything to the console. We
215 // expect it to return valid JSON
216 if (process.argv.includes('config')) {
217 _cliTools().logger.disable();
218 }
219
220 _cliTools().logger.setVerbose(process.argv.includes('--verbose')); // We only have a setup script for UNIX envs currently
221
222
223 if (process.platform !== 'win32') {
224 const scriptName = 'setup_env.sh';
225
226 const absolutePath = _path().default.join(__dirname, '..', scriptName);
227
228 try {
229 _child_process().default.execFileSync(absolutePath, {
230 stdio: 'pipe'
231 });
232 } catch (error) {
233 _cliTools().logger.warn(`Failed to run environment setup script "${scriptName}"\n\n${_chalk().default.red(error)}`);
234
235 _cliTools().logger.info(`React Native CLI will continue to run if your local environment matches what React Native expects. If it does fail, check out "${absolutePath}" and adjust your environment to match it.`);
236 }
237 }
238
239 for (const command of _commands.detachedCommands) {
240 attachCommand(command);
241 }
242
243 try {
244 const config = (0, _config.default)();
245
246 _cliTools().logger.enable();
247
248 for (const command of [..._commands.projectCommands, ...config.commands]) {
249 attachCommand(command, config);
250 }
251 } catch (error) {
252 /**
253 * When there is no `package.json` found, the CLI will enter `detached` mode and a subset
254 * of commands will be available. That's why we don't throw on such kind of error.
255 */
256 if (error.message.includes("We couldn't find a package.json")) {
257 _cliTools().logger.debug(error.message);
258
259 _cliTools().logger.debug('Failed to load configuration of your project. Only a subset of commands will be available.');
260 } else {
261 throw new (_cliTools().CLIError)('Failed to load configuration of your project.', error);
262 }
263 }
264
265 _commander().default.parse(process.argv);
266
267 if (_commander().default.rawArgs.length === 2) {
268 _commander().default.outputHelp();
269 } // We handle --version as a special case like this because both `commander`
270 // and `yargs` append it to every command and we don't want to do that.
271 // E.g. outside command `init` has --version flag and we want to preserve it.
272
273
274 if (_commander().default.args.length === 0 && _commander().default.rawArgs.includes('--version')) {
275 console.log(pkgJson.version);
276 }
277}
278
279const bin = require.resolve("./bin");
280
281exports.bin = bin;
282
283//# sourceMappingURL=index.js.map
\No newline at end of file