-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwjr.js
105 lines (89 loc) · 3.45 KB
/
nwjr.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/env node
var fs = require('fs-extra');
var path = require('path');
var child_process = require('child_process');
// If nwjr ever breaks due to changes in NW.js's executable naming system, modify these
var MAC_EXECUTABLE = 'nwjs.app';
var LINUX_EXECUTABLE = 'nw';
var WIN_EXECUTABLE = 'nw.exe';
var HELP_TEXT = "\nnwjr is a utility to quickly run and test NW.js apps during development.\n" +
"usage: nwjr (-s /path/to/NW.js/executables | /path/to/NW.js/app)\n" +
" -s: Run in setup mode to copy and store NW.js files (run again to update NW.js)\n"
// Reading in arguments
var argv = require('minimist')(process.argv.slice(2));
// If setup mode requested
if (argv['s']) {
// If nothing else passed in, minimist sets -s == true
if (typeof argv['s'] === 'boolean') {
console.log(HELP_TEXT);
process.exit(-1);
}
// Setup stuff
try {
switch (process.platform) {
case "darwin":
fs.statSync(path.resolve(argv['s'], MAC_EXECUTABLE)).isFile();
break;
case "linux":
fs.statSync(path.resolve(argv['s'], LINUX_EXECUTABLE)).isFile();
break;
case "win32":
fs.statSync(path.resolve(argv['s'], WIN_EXECUTABLE)).isFile();
break;
}
console.log("\nCopying NW.js executable files...");
// Replacing any existing executable directory and copy new files
fs.removeSync('executable');
fs.copySync(path.resolve(argv['s']), path.resolve('.', 'executable'));
console.log("NW.js files accepted. You may now delete your folder.\n");
process.exit();
}
// If setup directory doesn't exist or is missing platform-specific NW.js executables
catch(err) {
if (err.code === "ENOENT") {
console.log("Either that directory does not exist, or it's missing appropriate NW.js files.\n");
process.exit(1);
}
}
}
// If no -s, but 1 argument (presumably app location)
if (argv['_'].length == 1) {
// Check if NW.js executable files have been set up
try {
fs.statSync(path.resolve('.', 'executable')).isDirectory()
}
catch(err) {
if (err.code === "ENOENT") {
console.log("NW.js executables not found. Run setup using -s first.\n");
process.exit(-1);
}
}
var pathToApp = path.resolve(argv['_'][0]);
// Run platform specific executable
try {
// To check if location and package.json exist
fs.statSync(path.resolve(pathToApp, 'package.json')).isFile()
console.log("Launching NW.js app...\n");
if (process.platform === "darwin") {
var nwjsExecLocation = path.resolve('.', 'executable', MAC_EXECUTABLE + '/Contents/MacOS/nwjs');
child_process.exec(nwjsExecLocation + " " + pathToApp);
process.exit();
}
else if (process.platform === "win32" || process.platform === "linux") {
child_process.exec(path.resolve('.', 'executable', LINUX_EXECUTABLE) + " " + pathToApp);
process.exit();
}
}
// If package.json or app folder was not found
catch(err) {
if (err.code === "ENOENT") {
console.log("Directory does not exist or package.json not found. Are you sure that's an NW.js app?\n");
process.exit(1);
}
}
}
// If no -s and something weird
else {
console.log(HELP_TEXT);
process.exit(-1);
}