UNPKG

3.52 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/*
4** GemstoneJS -- Gemstone JavaScript Technology Stack
5** Copyright (c) 2016-2019 Gemstone Project <http://gemstonejs.com>
6** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>
7*/
8"use strict";
9
10var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
11
12var _parseFloat2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/parse-float"));
13
14var _parseInt2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/parse-int"));
15
16var _chalk = _interopRequireDefault(require("chalk"));
17
18var _dashdash = _interopRequireDefault(require("dashdash"));
19
20var _gemstoneToolApi = _interopRequireDefault(require("../lib/gemstone-tool-api"));
21
22var _package = _interopRequireDefault(require("../package.json"));
23
24/*
25 * STEP 1: Parse global options
26 */
27const 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
57const parser = _dashdash.default.createParser({
58 options: options,
59 interspersed: false
60});
61
62let opts;
63
64try {
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 * STEP 2: Execute global options
72 */
73
74
75if (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 * STEP 3: Load API and the requested plugins
85 */
86
87
88const gemstone = new _gemstoneToolApi.default({
89 verbose: opts.verbose,
90 color: opts.color
91});
92
93if (opts.plugins !== "") {
94 const plugins = opts.plugins.split(/,/);
95 gemstone.use(plugins);
96}
97/*
98 * STEP 4: Parse command-line arguments
99 */
100
101
102let args = opts._args;
103if (args.length === 0) args = ["help"];
104const pluginName = args.shift();
105const pluginOpts = {};
106const pluginArgs = [];
107
108const 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
113while (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 * STEP 5: Pass-through execution to Gemstone API
129 */
130
131
132gemstone.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});