-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
63 lines (52 loc) · 1.82 KB
/
index.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
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs');
const { normalize } = require('path');
const { compact } = require('lodash');
const chalk = require('chalk');
const { transpile } = require('./generator/transpiler');
const { compile } = require('./generator/compiler');
const { emulate } = require('./generator/emulator');
const { readCommandLine } = require('./generator/commandline');
const { watchProject } = require('./generator/watcher');
const { showMenu } = require('./generator/ui')
const { showEditor } = require('./editor/editor')
const commandLine = readCommandLine();
const handleErrors = result => {
if (!result.errors || !result.errors.length) {
return 0;
}
result.errors.forEach(({sourceName, line, message}) => {
console.error(chalk.red(compact([
sourceName && `${sourceName}.choice`,
line && `Error at line ${line}`,
message
]).join(': ')));
});
return -1;
}
const COMMANDS = { transpile, compile, emulate };
const executeCommands = async () => {
const commandNames = commandLine._.filter(command => command !== 'menu');
const commandsToExecute = compact((commandNames.length ? commandNames : ['transpile', 'compile', 'emulate'])
.map(command => COMMANDS[command]));
for (execute of commandsToExecute) {
const result = await execute(commandLine);
const exitCode = handleErrors(result);
if (exitCode) {
return { exitCode };
}
}
}
if (commandLine._.includes('menu')) {
showMenu(commandLine, executeCommands);
} else if (commandLine._.includes('edit')) {
showEditor(commandLine, executeCommands);
} else if (commandLine.watch) {
console.warn('The "watch" option is a bit unstable, right now.');
watchProject(commandLine, executeCommands);
} else {
executeCommands().then(finalResult => {
if (finalResult && finalResult.exitCode) {
process.exit(-1);
}
});
}