-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoderunner.js
More file actions
85 lines (80 loc) · 2.56 KB
/
coderunner.js
File metadata and controls
85 lines (80 loc) · 2.56 KB
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
#!/usr/bin/env node
const { prompt } = require('inquirer');
const { writeFile, existsSync, mkdirSync } = require('fs');
const childProcess = require('child_process');
const { join } = require('path');
const rimraf = require('rimraf');
/*
https://stackoverflow.com/questions/22646996/how-do-i-run-a-node-js-script-from-within-another-node-js-script
function written by fshost
*/
const runScript = (scriptPath, callback) => {
// keep track of whether callback has been invoked to prevent multiple invocations
var invoked = false;
var process = childProcess.fork(scriptPath);
// listen for errors as they may prevent the exit event from firing
process.on('error', function (err) {
if (invoked) return;
invoked = true;
callback(err);
});
// execute the callback once the process has finished running
process.on('exit', function (code) {
if (invoked) return;
invoked = true;
var err = code === 0 ? null : new Error('exit code ' + code);
callback(err);
});
}
const installDependencies = (dependencies) => {
const codeDir = join(__dirname, '/code')
if (!existsSync(codeDir)) mkdirSync(codeDir)
if (!dependencies) return;
childProcess.execSync("npm install --prefix ./code " + dependencies, { stdio: [0, 1, 2] });
}
const promptCode = () => {
prompt([{
name: "dependencies",
type: "input",
message: "Enter a space seperated list of dependencies"
}, {
name: "code",
type: "editor",
message: "Write some Code"
}])
.then(({ code, dependencies }) => {
const path = join(__dirname, '/code/code.js');
installDependencies(dependencies);
writeFile(path, code, err => {
if (err) throw err;
runScript(path, err => {
if (err) throw err;
rimraf(join(__dirname, '/code'), err => {
if (err) throw err;
main()
});
});
});
});
}
const main = () => {
prompt([{
name: "main",
type: "list",
message: "What would you like to do?",
choices: ["Write Code", "Exit"]
}])
.then(({ main }) => {
switch (main) {
case "Write Code":
promptCode();
break;
case "Exit":
default:
console.log("Goodbye!");
process.exit();
break;
}
});
}
main();