-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
177 lines (154 loc) · 4.36 KB
/
Copy pathcli.js
File metadata and controls
177 lines (154 loc) · 4.36 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
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
#!/usr/bin/env node
"use strict";
const fs = require("node:fs/promises");
const path = require("node:path");
const process = require("node:process");
const {
SUPPORTED_LANGUAGES,
detectLanguage,
instrumentSource,
} = require("./instrument");
const { transformSourceMarkers } = require("./markerToLog");
function printHelp() {
console.log(`AutologInsert Node.js (AST-based)
Usage:
node ./src/cli.js [options] [source-file]
Options:
-i <file> Input file. Defaults to stdin.
-o <file> Output file. Defaults to stdout.
-markers-to-logs
-logs Insert //$$...$$ markers, then transform them into mklog2seq-compatible log statements.
-c Treat input as C.
-cpp Treat input as C++.
-csharp | -cs Treat input as C#.
-java Treat input as Java.
-javascript | -js Treat input as JavaScript.
-php Treat input as PHP.
-VC Alias of -c.
-h | -help Show this help.
Supported languages:
${SUPPORTED_LANGUAGES.join(", ")}
`);
}
function parseArgs(argv) {
const options = {
input: "stdin",
output: "stdout",
language: null,
mode: "instrument",
passthrough: [],
};
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
switch (arg) {
case "-i":
index += 1;
options.input = argv[index] ?? "stdin";
break;
case "-o":
index += 1;
options.output = argv[index] ?? "stdout";
break;
case "-markers-to-logs":
case "-logs":
options.mode = "markers-to-logs";
break;
case "-c":
case "-cpp":
case "-csharp":
case "-cs":
case "-java":
case "-javascript":
case "-js":
case "-php":
case "-VC":
options.language = detectLanguage({
explicitFlag: arg,
inputPath: options.input !== "stdin" ? options.input : null,
});
break;
case "-h":
case "-help":
case "--help":
options.help = true;
break;
default:
if (arg.startsWith("-")) {
options.passthrough.push(arg);
} else if (options.input === "stdin") {
options.input = arg;
} else {
options.passthrough.push(arg);
}
break;
}
}
return options;
}
async function readInput(inputPath) {
if (inputPath === "stdin") {
const chunks = [];
for await (const chunk of process.stdin) {
chunks.push(chunk);
}
return Buffer.concat(chunks).toString("utf8");
}
return fs.readFile(inputPath, "utf8");
}
async function writeOutput(outputPath, content) {
if (outputPath === "stdout") {
process.stdout.write(content);
return;
}
await fs.writeFile(outputPath, content, "utf8");
}
async function main() {
const options = parseArgs(process.argv.slice(2));
if (options.help) {
printHelp();
return;
}
if (options.passthrough.length > 0) {
console.error(
`Ignored options: ${options.passthrough.join(", ")}`
);
}
const resolvedLanguage =
options.language ||
detectLanguage({
explicitFlag: null,
inputPath: options.input !== "stdin" ? options.input : null,
});
if (!resolvedLanguage) {
console.error(
"Unable to detect the input language. Pass -c, -cpp, -csharp, -java, -javascript, or -php."
);
process.exitCode = 1;
return;
}
const source = await readInput(options.input);
const result = options.mode === "markers-to-logs"
? (() => {
const instrumented = instrumentSource(source, resolvedLanguage, {
filePath: options.input === "stdin" ? null : path.resolve(options.input),
});
const transformed = transformSourceMarkers(instrumented.code, resolvedLanguage);
return {
code: transformed.code,
warnings: [...instrumented.warnings, ...transformed.warnings],
};
})()
: instrumentSource(source, resolvedLanguage, {
filePath: options.input === "stdin" ? null : path.resolve(options.input),
});
await writeOutput(options.output, result.code);
if (result.warnings.length > 0) {
for (const warning of result.warnings) {
console.error(`warning: ${warning}`);
}
}
}
main().catch((error) => {
console.error(error && error.message ? error.message : String(error));
process.exitCode = 1;
});