-
Notifications
You must be signed in to change notification settings - Fork 149
/
elm-app-cli.js
executable file
Β·148 lines (130 loc) Β· 3.56 KB
/
elm-app-cli.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env node
'use strict';
const path = require('path');
const spawn = require('cross-spawn');
const argv = require('minimist')(process.argv.slice(2));
const elmExecutable = require.resolve('elm/bin/elm');
const version = require('../package.json').version;
const elmVersion = require('elm/package.json').version;
const commands = argv._;
const elmCommands = [
'repl',
'init',
'reactor',
'make',
'install',
'bump',
'diff',
'publish'
];
if (commands.length === 0) {
help(version);
process.exit(1);
}
const script = commands[0];
switch (script) {
case 'build':
case 'eject':
case 'start':
if (argv['help']) {
help(version, 'start');
break;
}
let args = [];
const env = {};
Object.keys(argv || {}).forEach(key => {
if (key === 'browser') {
// `--no-browser` turns into `--browser false`
// See https://github.com/substack/minimist/issues/123
args = args.concat([argv[key] ? '--browser' : '--no-browser']);
}
if (key === 'debug' && argv[key] === false) {
env.ELM_DEBUGGER = 'false';
}
});
spawnSyncNode(path.resolve(__dirname, '../scripts', script), args, env);
break;
case 'create':
spawnSyncNode(path.resolve(__dirname, '../scripts/create'), commands[1]);
break;
case 'test': {
let args = [];
Object.keys(argv || {}).forEach(key => {
if (key !== '_' && key !== 'compiler') {
args = args.concat(['--' + key, argv[key]]);
}
});
args = args.concat(['--compiler', require.resolve('elm/bin/elm')]);
const cp = spawn.sync(require.resolve('elm-test/bin/elm-test'), args, {
stdio: 'inherit'
});
if (cp.status !== 0) {
process.exit(cp.status);
}
break;
}
default:
// Proxy elm-platform cli commands.
if (elmCommands.indexOf(script) !== -1) {
spawn.sync(elmExecutable, process.argv.slice(2), {
stdio: 'inherit'
});
break;
} else {
help(version);
process.exit(1);
}
}
/**
*Prints help message
*
* @param {string} version Package version
* @param {string} command Name of command for which to print help message
* @return {undefined}
*/
function help(version, command = '') {
switch (command) {
case 'start':
console.log();
console.log('Usage: elm-app start [options...]');
console.log();
console.log('where [options...] is any number of:');
console.log('--no-browser\tDo not open the browser automatically');
console.log('--no-debug\tDisable the elm time travel debugger');
console.log();
break;
case 'test':
// NOTE: Current implementation calls through to `elm-test` for help message
break;
default:
console.log();
console.log('Usage: elm-app <command>');
console.log();
console.log('where <command> is one of:');
console.log(' build, start, test, eject, ' + elmCommands.join(', '));
console.log();
console.log();
console.log('Elm ' + elmVersion);
console.log();
}
console.log(
'create-elm-app@' + version + ' ' + path.resolve(__dirname, '..')
);
}
/**
* Spawn separate node process with specified script
*
* @param {string} script Path to script
* @param {Array} args Script arguments
* @param {Object} env Overwrite process.env properties
* @return {undefined}
*/
function spawnSyncNode(script, args, env) {
const cp = spawn.sync('node', [script].concat(args || []), {
stdio: 'inherit',
env: Object.assign({}, process.env, env)
});
if (cp.status !== 0) {
process.exit(cp.status);
}
}