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