-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
executable file
·124 lines (99 loc) · 2.57 KB
/
main.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
#! /usr/bin/env node
const fs = require("fs");
const readline = require("readline");
const util = require("util");
const { spawn } = require("child_process");
const tmpfiles = [];
const tmpfile = (callback) => {
const tmpath = "/tmp/.xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx.cmake".replace(
/[xy]/g,
(c) => {
const r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
}
);
tmpfiles.push(tmpath);
fs.open(tmpath, "w", 0666, (err, fd) => {
callback(err, fd, tmpath);
});
};
process.on("exit", () => {
tmpfiles.forEach((file) => {
fs.unlinkSync(file);
});
});
const stdin = process.openStdin();
const prompt = "cmake $ ";
const cmake_bin_path = "cmake"; // replace with your cmake path
const rli = readline.createInterface(process.stdin, process.stdout);
rli.setPrompt(prompt);
rli.prompt();
stdin.on("data", (chunk) => {
rli.write(chunk);
});
rli.on("line", (cmd) => {
if (!cmd) {
rli.prompt();
return;
}
if (cmd.indexOf(".") == 0) {
return replCommand(cmd);
}
cmd = trimWhitespace(cmd);
// cmd = regexpEscape(cmd)
cmd = cmd.replace("&", "_CMAKE_repl_OUT__");
tmpfile((err, fd, tmpath) => {
const cmake_code =
cmd +
"\n" +
"foreach(i ${_CMAKE_repl_OUT__})\n" +
" message(STATUS ${i})\n" +
"endforeach()\n";
if (err) {
console.log("error while creating temp file", err);
throw err;
}
const buf = new Buffer.from(cmake_code);
fs.write(fd, buf, 0, buf.length, null, (err, written) => {
if (err) {
throw err;
}
if (written < buf.length) {
throw new Error("write was not atomic, too lazy to implemente");
}
fs.close(fd, () => {
const cmake = spawn(cmake_bin_path, ["-P", tmpath]);
const out_data = "";
cmake.stdout.on("data", (data) => {
out_data += data;
});
cmake.stderr.on("data", (data) => {
console.log(data.toString().trim());
});
cmake.on("exit", (_) => {
console.log(out_data.toString().trim());
rli.prompt();
});
cmake.on("error", (e) => {
console.log("error", e);
console.log(e);
rli.prompt();
});
});
});
});
});
rli.on("close", (_) => {
stdin.destroy();
});
function trimWhitespace(cmd) {
const trimmer = /^\s*(.+)\s*$/m,
matches = trimmer.exec(cmd);
if (matches && matches.length === 2) {
return matches[1];
}
}
function regexpEscape(s) {
return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}