-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
index.dev.js
executable file
·193 lines (160 loc) · 5.36 KB
/
index.dev.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env node
import * as Insect from "./output/Insect/index.js";
import * as path from "path";
import * as xdgBasedir from "xdg-basedir";
var insectEnv = Insect.initialEnvironment;
function usage() {
console.log("Usage: insect [EXPR]");
process.exit(1);
}
function runInsect(fmt, line) {
var lineTrimmed = line.trim();
if (lineTrimmed === "" || lineTrimmed[0] === "#") {
return undefined;
}
// Run insect
var res = Insect.repl(fmt)(insectEnv)(line);
// Update environment
insectEnv = res.newEnv;
return res;
}
// Top-level await is not supported in Node 12 and earlier.
(async function() {
if (process.env.INSECT_NO_RC !== "true") {
var [util, lineReader, os] = await Promise.all([import("util"), import("line-reader"), import("os")]);
var rcFilePaths = [path.join(xdgBasedir.config, "insect/insectrc"), path.join(os.homedir(), ".insectrc")];
var eachLine = util.promisify(lineReader.eachLine);
for (const rcFilePath of rcFilePaths) {
try {
await eachLine(rcFilePath, function(line) {
var res = runInsect(Insect.fmtPlain, line);
// We really only care when it breaks
if (res && res.msgType === "error") {
console.error(res.msg);
process.exit(1);
}
});
break;
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
}
}
}
// Handle command line arguments
var args = process.argv.slice(2);
if (args[0] === "-h" || args[0] === "--help") {
usage();
} else if (args.length !== 0) {
// Execute a single command
var res = runInsect(Insect.fmtPlain, args.join(" "));
if (res.msgType === "value" || res.msgType === "info") {
console.log(res.msg);
} else if (res.msgType === "error") {
console.error(res.msg);
process.exit(1);
}
process.exit(0);
}
var interactive = process.stdin.isTTY;
if (interactive) {
var [fs, clipboardy, readline] = await Promise.all([import("fs"), import("clipboardy"), import("readline")]);
// Create `xdgBasedir.data` if it doesn't already exist.
// See https://github.com/sharkdp/insect/issues/364.
try {
fs.mkdirSync(xdgBasedir.data, { recursive: true });
} catch (err) {
if (err.code !== "EEXIST") {
throw err;
}
}
// Open the history file for reading and appending.
var historyFd = fs.openSync(path.join(xdgBasedir.data, "insect-history"), 'a+');
var maxHistoryLength = 5000;
// Set up REPL
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
history: fs.readFileSync(historyFd, "utf8").split("\n").slice(0, -1).reverse().slice(0, maxHistoryLength),
historySize: maxHistoryLength,
completer: function(line) {
var identifiers = Insect.identifiers(insectEnv);
var keywords =
identifiers.concat(Insect.functions(insectEnv), Insect.supportedUnits, Insect.commands);
var lastWord = line;
if (line.trim() !== "") {
var words = line.split(/\b/);
lastWord = words[words.length - 1];
keywords = keywords.filter(function(kw) {
return kw.indexOf(lastWord) === 0;
});
}
return [keywords, lastWord];
},
});
var prompt = '\x1b[01m>>>\x1b[0m ';
// The visual length of the prompt (4) needs to be set explicitly for
// older versions of node:
rl.setPrompt(prompt, 4);
rl.on('line', function(line) {
var res = runInsect(Insect.fmtConsole, line);
if (res) {
if (res.msgType === "quit") {
process.exit(0);
} else if (res.msgType === "clear") {
process.stdout.write('\x1Bc');
} else if (res.msgType === "copy") {
if (res.msg === "") {
console.log("\nNo result to copy.\n");
} else {
clipboardy.writeSync(res.msg);
console.log("\nCopied result '" + res.msg + "' to clipboard.\n");
}
} else {
console.log(res.msg + "\n");
}
}
rl.prompt();
}).on('SIGINT', function() {
rl.clearLine();
rl.prompt();
}).on('close', function() {
process.exit(0);
});
// `createWriteStream` doesn't care about the first argument if it's given
// `fd`.
var historyStream = fs.createWriteStream(undefined, {fd: historyFd});
var oldAddHistory = rl._addHistory;
// TODO: figure out how to do this without resorting to Node.js internals.
rl._addHistory = function() {
var last = rl.history[0];
var line = oldAddHistory.call(rl);
if (line && line !== last) {
historyStream.write(line + "\n");
}
return line;
};
rl.prompt();
} else {
if (typeof lineReader === "undefined") {
var lineReader = await import("line-reader");
}
// Read from non-interactive stream (shell pipe)
lineReader.eachLine(process.stdin, function(line) {
var res = runInsect(Insect.fmtPlain, line);
if (res) {
// Only output values and halt on errors. Ignore 'info' and 'value-set'
// message types.
if (res.msgType === "value") {
console.log(res.msg);
} else if (res.msgType == "error") {
console.error(res.msg);
process.exit(1);
} else if (res.msgType == "quit") {
process.exit(0);
}
}
});
}
}());