forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliEntry.js
93 lines (75 loc) · 2.36 KB
/
cliEntry.js
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
const cli = require('commander');
const Config = require('./util/Config');
const childProcess = require('child_process');
const Promise = require('promise');
const path = require('path');
const fs = require('fs');
const gracefulFs = require('graceful-fs');
const init = require('./init/init');
const commands = require('./commands');
const assertRequiredOptions = require('./util/assertRequiredOptions');
const pkg = require('../package.json');
const defaultConfig = require('./default.config');
import type { Command } from './commands';
// graceful-fs helps on getting an error when we run out of file
// descriptors. When that happens it will enqueue the operation and retry it.
gracefulFs.gracefulify(fs);
cli.version(pkg.version);
const defaultOptParser = (val) => val;
const handleError = (err) => {
console.error();
console.error(err.message || err);
console.error();
process.exit(1);
};
const addCommand = (command: Command, config: Config) => {
const options = command.options || [];
const cmd = cli
.command(command.name)
.usage(command.usage)
.description(command.description)
.action(function runAction() {
const passedOptions = this.opts();
const argv: Array<string> = arguments;
Promise.resolve()
.then(() => {
assertRequiredOptions(options, passedOptions);
return command.func(argv, config, passedOptions);
})
.catch(handleError);
});
options
.forEach(opt => cmd.option(
opt.command,
opt.description,
opt.parse || defaultOptParser,
typeof opt.default === 'function' ? opt.default(config) : opt.default,
));
};
function run() {
const config = Config.get(__dirname, defaultConfig);
const setupEnvScript = /^win/.test(process.platform)
? 'setup_env.bat'
: 'setup_env.sh';
childProcess.execFileSync(path.join(__dirname, setupEnvScript));
commands.forEach(cmd => addCommand(cmd, config));
cli.parse(process.argv);
if (!cli.args.length) {
cli.help();
}
}
module.exports = {
run: run,
init: init,
};