-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·97 lines (84 loc) · 2.22 KB
/
cli.js
File metadata and controls
executable file
·97 lines (84 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
"use strict";
const { env } = process;
if (env.DEBUG === undefined) {
env.DEBUG = "*";
}
const { load, watch } = require("./index.js");
const { inspect } = require("util");
const get = require("lodash/get.js");
const pick = require("lodash/pick.js");
const { stdout } = process;
let useJson = !stdout.isTTY;
function print(paths, config) {
if (paths.length !== 0) {
config = paths.length === 1 ? get(config, paths[0]) : pick(config, paths);
}
stdout.write(
useJson
? JSON.stringify(config, null, 2)
: inspect(config, {
colors: true,
depth: Infinity,
sorted: true,
}),
);
stdout.write("\n");
}
async function main(args) {
const cliOpts = {
_: [],
help: false,
paths: [],
watch: false,
};
for (let i = 0, n = args.length; i < n; ) {
const arg = args[i++];
if (arg[0] === "-") {
if (arg === "-h" || arg === "--help") {
cliOpts.help = true;
} else if (arg === "-j" || arg === "--json") {
useJson = true;
} else if (arg === "-w" || arg === "--watch") {
cliOpts.watch = true;
} else if (arg === "-p" || arg === "--path") {
if (i < n) {
cliOpts.paths.push(args[i++]);
} else {
throw new Error("missing argument for --path option");
}
} else {
throw new Error("unknown option: " + arg);
}
} else {
cliOpts._.push(arg);
}
}
if (cliOpts._.length === 0 || cliOpts.help) {
const { name, version } = require("./package.json");
return stdout.write(`Usage: ${name} [--json | -j] [--watch | -w] [-p <path>]... <appName> [<appDir>]
${name} v${version}
`);
}
const [appName, appDir] = cliOpts._;
const opts = {
appDir,
appName,
ignoreUnknownFormats: true,
initialLoad: true,
};
const printPaths = print.bind(undefined, cliOpts.paths);
if (cliOpts.watch) {
await watch(opts, (error, config) => {
console.log("--", new Date());
if (error !== undefined) {
console.warn(error);
return;
}
printPaths(config);
});
} else {
printPaths(await load(appName, opts));
}
}
main(process.argv.slice(2)).catch(console.error.bind(console, "FATAL:"));