UNPKG

3.23 kBJavaScriptView Raw
1#!/usr/bin/env node
2/*
3** GemstoneJS -- Gemstone JavaScript Technology Stack
4** Copyright (c) 2016-2018 Gemstone Project <http://gemstonejs.com>
5** Licensed under Apache License 2.0 <https://spdx.org/licenses/Apache-2.0>
6*/
7
8import chalk from "chalk"
9import dashdash from "dashdash"
10import Gemstone from "../lib/gemstone-tool-api"
11import pkg from "../package.json"
12
13/*
14 * STEP 1: Parse global options
15 */
16
17const options = [
18 { names: [ "help", "h" ], type: "bool", "default": false,
19 help: "Print tool usage and exit." },
20 { names: [ "version" ], type: "bool", "default": false,
21 help: "Print tool version and exit." },
22 { names: [ "verbose", "v" ], env: "GEMTOOL_VERBOSE", type: "bool", "default": false,
23 help: "Enable verbosity in outputs." },
24 { names: [ "color", "c" ], env: "GEMTOOL_COLOR", type: "bool", "default": chalk.supportsColor,
25 help: "Enable colors in outputs." },
26 { names: [ "plugins", "p" ], type: "string", "default": "",
27 help: "Plugins to load.", helpArg: "NAME[,...]" }
28]
29const parser = dashdash.createParser({
30 options: options,
31 interspersed: false
32})
33let opts
34try {
35 opts = parser.parse(process.argv)
36}
37catch (ex) {
38 process.stderr.write(`gemstone: ERROR: ${ex.message}\n`)
39 process.exit(1)
40}
41
42/*
43 * STEP 2: Execute global options
44 */
45
46if (opts.help) {
47 let help = parser.help().trimRight()
48 process.stdout.write(
49 "gemstone: USAGE: gemstone [options] command [arguments]\n" +
50 "options:\n" +
51 help + "\n")
52 process.exit(0)
53}
54else if (opts.version) {
55 process.stdout.write(`${pkg.name} ${pkg.version}\n`)
56 process.exit(0)
57}
58
59/*
60 * STEP 3: Load API and the requested plugins
61 */
62
63let gemstone = new Gemstone({
64 verbose: opts.verbose,
65 color: opts.color
66})
67if (opts.plugins !== "") {
68 let plugins = opts.plugins.split(/,/)
69 gemstone.use(plugins)
70}
71
72/*
73 * STEP 4: Parse command-line arguments
74 */
75
76let args = opts._args
77
78if (args.length === 0)
79 args = [ "help" ]
80
81let pluginName = args.shift()
82let pluginOpts = {}
83let pluginArgs = []
84
85const convertValue = (val) => {
86 if (val.match(/^(?:true|false)$/))
87 val = (val === "true")
88 else if (val.match(/^[+-]?[0-9]+$/))
89 val = parseInt(val, 10)
90 else if (val.match(/^[+-]?[0-9]*\.?[0-9]+(?:[eE][+-]?[0-9]+)?$/))
91 val = parseFloat(val)
92 return val
93}
94
95while (args.length > 0) {
96 let arg = args.shift()
97 let m
98 if ((m = arg.match(/^([a-zA-Z_][a-zA-Z0-9_-]*)=(.*)$/)) !== null) {
99 let [ , key, val ] = m
100 if (val.match(/[^\\],/)) {
101 val = val
102 .replace(/\\,/g, "\uE000")
103 .split(/,/)
104 .map((val) => convertValue(val.replace(/\uE000/g, ",")))
105 }
106 else
107 val = convertValue(val.replace(/\\,/g, ","))
108 pluginOpts[key] = val
109 }
110 else
111 pluginArgs.push(convertValue(arg))
112}
113
114/*
115 * STEP 5: Pass-through execution to Gemstone API
116 */
117
118gemstone.exec(pluginName, pluginOpts, ...pluginArgs).then((output) => {
119 process.stdout.write(output)
120 process.exit(0)
121}).catch((err) => {
122 process.stderr.write(`gemstone: ERROR: ${err.stack}\n`)
123 process.exit(1)
124})
125