-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_cmd.js
97 lines (73 loc) · 2.69 KB
/
run_cmd.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
const path = require("path");
require('shelljs/global');
// http://adilapapaya.com/docs/shelljs/
// https://documentup.com/shelljs/shelljs
function RunCmd(filename, res) {
var info = '';
// var spawn = require('child_process').spawn,
// ls = spawn('cmd.exe', ['/c', filename]);
// var name = filename.replace(/^.*[\\\/]/, '');
//path.basename('/foo/bar/baz/asdf/quux.html')
var dir = path.dirname(filename);
var version = exec('node --version', {silent: true}).stdout;
if (!which('docker')) {
info = 'Sorry, this script requires DOCKER';
console.log(info);
echo(info);
exit(1);
}
if (!which('docker-compose')) {
info = 'Sorry, this script requires DOCKER';
console.log(info);
echo(info);
exit(1);
}
var stdout = '';
// var partition = 'e:';
// var command = 'cd ' + dir + ' && ' + filename;
// var command = 'bash ' + filename;
var command = filename;
console.log('command');
console.log(command);
// var configs = {};
// var configs = {silent:true,async:false};
console.log('dir');
console.log(dir);
if (cd(dir).code !== 0) {
echo('Error: Change DIR failed');
exit(1);
}
try {
var version = exec('node --version', {silent: true}).stdout;
console.log(version);
// var child = exec(command, {async:true});
// child.stdout.on('data', function(data) {
// /* ... do something with data ... */
// });
exec(command, function (code, stdout, stderr) {
console.log('Exit code:', code);
console.log('Program output:', stdout);
console.log('Program stderr:', stderr);
code = '<span style="color: gray">' + code + '</span>' + '<br/>';
stdout = stdout.replace(/\r\n|\r|\n/, '<br/>');
stdout = '<span style="color: darkblue">' + stdout + '</span>' + '<br/>';
stderr = stderr.replace(/\r\n|\r|\n/, '<br/>');
stderr = '<span style="color: darkred">' + stderr + '</span>' + '<br/>';
res.send('<br/>' + code + stdout + stderr + '<br/>');
});
// // stdout = exec( command, configs ).stdout;
// if (exec(command, configs).code !== 0) {
// echo('Error: Script failed');
// exit(1);
// }
echo('--------------------------------------------------------');
} catch (err) {
stdout = err.stack;
// ^ will output the "unexpected" result of: elsewhere has failed
}
// Print out the result
console.log(stdout);
// stdout = stdout.replace(/\r\n|\r|\n/, '<br/>');
// res.send(stdout);
}
module.exports = RunCmd;