-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
84 lines (54 loc) · 1.55 KB
/
main.js
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
const plib = require('path');
const fs = require('fs');
function main() {
// nwjs changes PWD. This is surprising, so change it back
process.chdir(nw.App.startPath);
// Invoke provided module
const floc = parseArgs(nw.App.argv);
require(floc);
}
const defaultConfigLoc = plib.resolve(__dirname, 'example/example.js');
function parseArgs(argv) {
if (
![0, 1, 2].includes(argv.length)
|| argv.includes('-h')
|| argv.includes('--help')
) {
help();
}
let configLoc = nw.App.argv[1];
if (!configLoc) {
console.info(`No config provided; defaulting to ${defaultConfigLoc}`);
configLoc = defaultConfigLoc;
}
try {
configLoc = plib.resolve(process.cwd(), configLoc);
} catch (e) {
abort(`Supplied argument '${configLoc}' must be a path`);
}
if (!fs.existsSync(configLoc))
abort(`Supplied path '${configLoc}' does not exist`);
return configLoc;
}
function help() {
console.log(String.raw`
Nifty launcher
USAGE:
nifty [<path-to-config>]
Run Nifty with the specified configuration.
If no configuration is provided, defaults to the following:
${defaultConfigLoc}
This default configuration is a good kickoff point for creating
your own custom config! Start with something like:
$ cp -r ${plib.dirname(defaultConfigLoc)} ./.
$ nifty ./${plib.join(plib.basename(plib.dirname(defaultConfigLoc)), plib.basename(defaultConfigLoc))}
nifty [-h|--help]
Show this help text
`);
abort();
}
function abort(str) {
str && console.error(str);
nw.App.quit();
}
main();