1 |
|
2 |
|
3 |
|
4 | import 'source-map-support/register';
|
5 |
|
6 | import usage from './actions/usage'
|
7 | import installI18N from './library/i18n';
|
8 | import {platform} from 'os';
|
9 | import MyError from './library/error';
|
10 | import readEnvSync from './actions/read-env';
|
11 | import {getCurrentDefault} from './actions/current-config';
|
12 | import shellSpawnSync from "./library/shell";
|
13 | import {applyGlobalEnv} from "./actions/apply-global-env";
|
14 |
|
15 | interface AvailableCmdOptions {
|
16 | [id: string]: boolean|string;
|
17 | }
|
18 |
|
19 | installI18N(() => {
|
20 | const args: string[] = process.argv.slice(2);
|
21 |
|
22 | if (!args.length) {
|
23 | throw usage();
|
24 | }
|
25 | let firstArg = args.shift();
|
26 |
|
27 | if (args.length === 0 && /^shell$/.test(firstArg)) {
|
28 | firstArg = platform() === 'win32' ? 'cmd.exe' : '/usr/bin/bash';
|
29 | }
|
30 |
|
31 | const isCommand = /^--/.test(firstArg);
|
32 | const isEnv = !isCommand && /^-/.test(firstArg);
|
33 |
|
34 | if (isCommand) {
|
35 | const cmd_options: AvailableCmdOptions = {};
|
36 | const cmd_args = args.filter(function (item) {
|
37 | if (/^--/.test(item)) {
|
38 | const match = /^--(.+?)(=(.+))?$/.exec(item);
|
39 | if (!match) {
|
40 | console.error('unknwon argument %s', item);
|
41 | process.exit(1);
|
42 | }
|
43 | cmd_options[match[1]] = match[3] === undefined ? true : match[3];
|
44 | return false;
|
45 | } else if (/^-/.test(item)) {
|
46 | if (cmd_options['envName']) {
|
47 | console.error('duplicate env argument %s', item);
|
48 | process.exit(1);
|
49 | }
|
50 | cmd_options['envName'] = item.replace(/^-/, '');
|
51 | } else {
|
52 | return true;
|
53 | }
|
54 | });
|
55 |
|
56 | try {
|
57 | const ret = require('./run_command')(firstArg.replace(/^--/, ''), cmd_args, cmd_options);
|
58 | if (ret === 999) {
|
59 | return;
|
60 | }
|
61 | process.exit(ret);
|
62 | } catch (e) {
|
63 | if (e instanceof MyError) {
|
64 | if (e.message !== 'ignore') {
|
65 | console.error(e.stack);
|
66 | }
|
67 | process.exit(1);
|
68 | } else {
|
69 | throw e;
|
70 | }
|
71 | }
|
72 | } else {
|
73 | let envName;
|
74 | if (isEnv) {
|
75 | envName = firstArg.replace(/^-/, '');
|
76 | } else {
|
77 | envName = getCurrentDefault();
|
78 | args.unshift(firstArg);
|
79 | }
|
80 |
|
81 | let config;
|
82 | try {
|
83 | config = readEnvSync(envName);
|
84 | } catch (err) {
|
85 | console.error(`can't load environment "${envName}":\n ${err.stack}`);
|
86 | process.exit(1);
|
87 | }
|
88 |
|
89 | applyGlobalEnv(config);
|
90 | const ret = shellSpawnSync(args);
|
91 | process.exit(ret);
|
92 | }
|
93 | });
|