1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import chalk from "chalk"
|
9 | import dashdash from "dashdash"
|
10 | import Gemstone from "../lib/gemstone-tool-api"
|
11 | import pkg from "../package.json"
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const 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 | ]
|
29 | const parser = dashdash.createParser({
|
30 | options: options,
|
31 | interspersed: false
|
32 | })
|
33 | let opts
|
34 | try {
|
35 | opts = parser.parse(process.argv)
|
36 | }
|
37 | catch (ex) {
|
38 | process.stderr.write(`gemstone: ERROR: ${ex.message}\n`)
|
39 | process.exit(1)
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | if (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 | }
|
54 | else if (opts.version) {
|
55 | process.stdout.write(`${pkg.name} ${pkg.version}\n`)
|
56 | process.exit(0)
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | let gemstone = new Gemstone({
|
64 | verbose: opts.verbose,
|
65 | color: opts.color
|
66 | })
|
67 | if (opts.plugins !== "") {
|
68 | let plugins = opts.plugins.split(/,/)
|
69 | gemstone.use(plugins)
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | let args = opts._args
|
77 |
|
78 | if (args.length === 0)
|
79 | args = [ "help" ]
|
80 |
|
81 | let pluginName = args.shift()
|
82 | let pluginOpts = {}
|
83 | let pluginArgs = []
|
84 |
|
85 | const 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 |
|
95 | while (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 |
|
116 |
|
117 |
|
118 | gemstone.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 |
|