1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | "use strict";
|
9 |
|
10 | var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
|
11 |
|
12 | var _parseFloat2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/parse-float"));
|
13 |
|
14 | var _parseInt2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/parse-int"));
|
15 |
|
16 | var _chalk = _interopRequireDefault(require("chalk"));
|
17 |
|
18 | var _dashdash = _interopRequireDefault(require("dashdash"));
|
19 |
|
20 | var _gemstoneToolApi = _interopRequireDefault(require("../lib/gemstone-tool-api"));
|
21 |
|
22 | var _package = _interopRequireDefault(require("../package.json"));
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const options = [{
|
28 | names: ["help", "h"],
|
29 | type: "bool",
|
30 | "default": false,
|
31 | help: "Print tool usage and exit."
|
32 | }, {
|
33 | names: ["version"],
|
34 | type: "bool",
|
35 | "default": false,
|
36 | help: "Print tool version and exit."
|
37 | }, {
|
38 | names: ["verbose", "v"],
|
39 | env: "GEMTOOL_VERBOSE",
|
40 | type: "bool",
|
41 | "default": false,
|
42 | help: "Enable verbosity in outputs."
|
43 | }, {
|
44 | names: ["color", "c"],
|
45 | env: "GEMTOOL_COLOR",
|
46 | type: "bool",
|
47 | "default": _chalk.default.supportsColor,
|
48 | help: "Enable colors in outputs."
|
49 | }, {
|
50 | names: ["plugins", "p"],
|
51 | type: "string",
|
52 | "default": "",
|
53 | help: "Plugins to load.",
|
54 | helpArg: "NAME[,...]"
|
55 | }];
|
56 |
|
57 | const parser = _dashdash.default.createParser({
|
58 | options: options,
|
59 | interspersed: false
|
60 | });
|
61 |
|
62 | let opts;
|
63 |
|
64 | try {
|
65 | opts = parser.parse(process.argv);
|
66 | } catch (ex) {
|
67 | process.stderr.write(`gemstone: ERROR: ${ex.message}\n`);
|
68 | process.exit(1);
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | if (opts.help) {
|
76 | const help = parser.help().trimRight();
|
77 | process.stdout.write("gemstone: USAGE: gemstone [options] command [arguments]\n" + "options:\n" + help + "\n");
|
78 | process.exit(0);
|
79 | } else if (opts.version) {
|
80 | process.stdout.write(`${_package.default.name} ${_package.default.version}\n`);
|
81 | process.exit(0);
|
82 | }
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | const gemstone = new _gemstoneToolApi.default({
|
89 | verbose: opts.verbose,
|
90 | color: opts.color
|
91 | });
|
92 |
|
93 | if (opts.plugins !== "") {
|
94 | const plugins = opts.plugins.split(/,/);
|
95 | gemstone.use(plugins);
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | let args = opts._args;
|
103 | if (args.length === 0) args = ["help"];
|
104 | const pluginName = args.shift();
|
105 | const pluginOpts = {};
|
106 | const pluginArgs = [];
|
107 |
|
108 | const convertValue = val => {
|
109 | if (val.match(/^(?:true|false)$/)) val = val === "true";else if (val.match(/^[+-]?[0-9]+$/)) val = (0, _parseInt2.default)(val, 10);else if (val.match(/^[+-]?[0-9]*\.?[0-9]+(?:[eE][+-]?[0-9]+)?$/)) val = (0, _parseFloat2.default)(val);
|
110 | return val;
|
111 | };
|
112 |
|
113 | while (args.length > 0) {
|
114 | const arg = args.shift();
|
115 | let m;
|
116 |
|
117 | if ((m = arg.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)=(.*)$/)) !== null) {
|
118 | let [, key, val] = m;
|
119 |
|
120 | if (val.match(/[^\\],/)) {
|
121 | val = val.replace(/\\,/g, "\uE000").split(/,/).map(val => convertValue(val.replace(/\uE000/g, ",")));
|
122 | } else val = convertValue(val.replace(/\\,/g, ","));
|
123 |
|
124 | pluginOpts[key] = val;
|
125 | } else pluginArgs.push(convertValue(arg));
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | gemstone.exec(pluginName, pluginOpts, ...pluginArgs).then(output => {
|
133 | process.stdout.write(output);
|
134 | process.exit(0);
|
135 | }).catch(err => {
|
136 | process.stderr.write(`gemstone: ERROR: ${err.stack}\n`);
|
137 | process.exit(1);
|
138 | });
|