-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (95 loc) · 3.19 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
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
const chalk = require('chalk');
const pjson = require('./package.json');
const commander = require('commander');
const inquirer = require('inquirer');
const fs = require('fs');
const midi = require('midi');
const chokidar = require('chokidar');
const runProgram = require('./dist/node/node/runner').runProgram;
console.log(chalk.green.bold(`Starting ${pjson.name}-${pjson.version}`));
const output = new midi.Output();
if (!output.getPortCount()) {
const error = (txt) => chalk.red(txt);
console.log(error('No MIDI output device found !'));
return 0;
}
let foundFile;
let foundOutput;
let foundEntry;
const codeProvider = {};
main();
async function main() {
commander
.version(pjson.version)
.option('-f, --file [file]', 'File to read from')
.option('-o, --output [output]', 'Midi output port to use')
.option('-e, --entry [entry]', 'The program\'s entry point')
.action(async options => {
const parsed = parseInt(options.output);
foundOutput = (isNaN(parsed) || parsed < 0 || parsed > output.getPortCount()) ? null : parsed;
if (foundOutput == null) {
printAvailableMidiOutputDevices();
}
while (foundOutput == null) {
await inquirer.prompt([{type: 'input', name: 'output', message: 'MIDI output'}])
.then(answers => {
const parsed = parseInt(answers.output);
foundOutput = (isNaN(parsed) || parsed < 0 || parsed > output.getPortCount()) ? null : parsed;
});
}
output.openPort(foundOutput);
foundFile = fs.existsSync(options.file) ? options.file : null;
while (foundFile == null) {
await inquirer.prompt([{type: 'input', name: 'file', message: 'Which file'}])
.then(answers => {
if (fs.existsSync(answers.file)) {
foundFile = answers.file
}
});
}
foundEntry = options.entry;
while (foundEntry == null) {
await inquirer.prompt([{type: 'input', name: 'entry', message: 'Which entry point'}])
.then(answers => {
if (answers.entry.trim().length > 0) {
foundEntry = answers.entry.trim();
}
});
}
chokidar
.watch(foundFile)
.on('change', () => {
codeProvider.code = fs.readFileSync(foundFile, 'utf8');
});
codeProvider.code = fs.readFileSync(foundFile, 'utf8');
const runnerOptions = {
codeProvider,
entryPoint: foundEntry,
midiOutput: output,
onProgramEnded: () => onProgramEnded(runnerOptions),
reportError,
};
runProgram(runnerOptions);
});
commander.parse(process.argv);
}
function printAvailableMidiOutputDevices() {
console.log('Available MIDI output devices :');
for (let i = 0; i < output.getPortCount(); ++i) {
console.log(`${i} : ${output.getPortName(i)}`);
}
}
async function onProgramEnded(runnerOptions) {
console.log('');
await inquirer.prompt([{type: 'confirm', name: 'confirm', message: 'Restart ?'}])
.then(answers => {
if (answers.confirm) {
runProgram(runnerOptions);
} else {
process.exit(0);
}
});
}
function reportError(...args) {
console.log(chalk.red(args));
}