forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrescript_format.js
254 lines (236 loc) · 6.79 KB
/
rescript_format.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//@ts-check
var os = require("os");
var arg = require("./rescript_arg.js");
var format_usage = `Usage: rescript format <options> [files]
\`rescript format\` formats the current directory
`;
var child_process = require("child_process");
var util = require("util");
var asyncExecFile = util.promisify(child_process.execFile);
var path = require("path");
var fs = require("fs");
var asyncFs = fs.promises;
/**
* @type {arg.stringref}
*/
var stdin = { val: undefined };
/**
* @type {arg.boolref}
*/
var format = { val: undefined };
/**
* @type {arg.boolref}
*/
var check = { val: undefined };
/**
* @type{arg.specs}
*/
var specs = [
[
"-stdin",
{ kind: "String", data: { kind: "String_set", data: stdin } },
`[.res|.resi] Read the code from stdin and print
the formatted code to stdout in ReScript syntax`,
],
[
"-all",
{ kind: "Unit", data: { kind: "Unit_set", data: format } },
"Format the whole project ",
],
[
"-check",
{ kind: "Unit", data: { kind: "Unit_set", data: check } },
"Check formatting for file or the whole project. Use `-all` to check the whole project",
],
];
var formattedStdExtensions = [".res", ".resi"];
var formattedFileExtensions = [".res", ".resi"];
/**
*
* @param {string[]} extensions
*/
function hasExtension(extensions) {
/**
* @param {string} x
*/
var pred = x => extensions.some(ext => x.endsWith(ext));
return pred;
}
async function readStdin() {
var stream = process.stdin;
const chunks = [];
for await (const chunk of stream) chunks.push(chunk);
return Buffer.concat(chunks).toString("utf8");
}
const numThreads = os.cpus().length;
/**
* Splits an array into smaller chunks of a specified size.
*
* @template T
* @param {T[]} array - The array to split into chunks.
* @param {number} chunkSize - The size of each chunk.
* @returns {T[][]} - An array of chunks, where each chunk is an array of type T.
*/
function chunkArray(array, chunkSize) {
/** @type {T[][]} */
const result = [];
for (let i = 0; i < array.length; i += chunkSize) {
result.push(array.slice(i, i + chunkSize));
}
return result;
}
/**
* @param {string[]} files
* @param {string} bsc_exe
* @param {(x: string) => boolean} isSupportedFile
* @param {boolean} checkFormatting
*/
async function formatFiles(files, bsc_exe, isSupportedFile, checkFormatting) {
const supportedFiles = files.filter(isSupportedFile);
const batchSize = 4 * os.cpus().length;
const batches = chunkArray(supportedFiles, batchSize);
let incorrectlyFormattedFiles = 0;
try {
for (const batch of batches) {
await Promise.all(
batch.map(async file => {
const flags = checkFormatting
? ["-format", file]
: ["-o", file, "-format", file];
const { stdout } = await asyncExecFile(bsc_exe, flags);
if (check.val) {
const original = await asyncFs.readFile(file, "utf-8");
if (original != stdout) {
console.error("[format check]", file);
incorrectlyFormattedFiles++;
}
}
}),
);
}
} catch (err) {
console.error(err);
process.exit(2);
}
if (incorrectlyFormattedFiles > 0) {
if (incorrectlyFormattedFiles == 1) {
console.error("The file listed above needs formatting");
} else {
console.error(
`The ${incorrectlyFormattedFiles} files listed above need formatting`,
);
}
process.exit(3);
}
}
/**
* @param {string[]} argv
* @param {string} rescript_exe
* @param {string} bsc_exe
*/
async function main(argv, rescript_exe, bsc_exe) {
var isSupportedFile = hasExtension(formattedFileExtensions);
var isSupportedStd = hasExtension(formattedStdExtensions);
try {
/**
* @type {string[]}
*/
var files = [];
arg.parse_exn(format_usage, argv, specs, xs => {
files = xs;
});
var format_project = format.val;
var use_stdin = stdin.val;
// Only -check arg
// Require: -all or path to a file
if (check.val && !format_project && files.length == 0) {
console.error(
"format check require path to a file or use `-all` to check the whole project",
);
process.exit(2);
}
if (format_project) {
if (use_stdin || files.length !== 0) {
console.error("format -all can not be in use with other flags");
process.exit(2);
}
// -all
// TODO: check the rest arguments
var output = child_process.spawnSync(
rescript_exe,
["info", "-list-files"],
{
encoding: "utf-8",
},
);
if (output.status !== 0) {
console.error(output.stdout);
console.error(output.stderr);
process.exit(2);
}
files = output.stdout.split("\n").map(x => x.trim());
await formatFiles(files, bsc_exe, isSupportedFile, check.val);
} else if (use_stdin) {
if (check.val) {
console.error("format -stdin cannot be used with -check flag");
process.exit(2);
}
if (isSupportedStd(use_stdin)) {
var crypto = require("crypto");
var os = require("os");
var filename = path.join(
os.tmpdir(),
"rescript_" +
crypto.randomBytes(8).toString("hex") +
path.parse(use_stdin).base,
);
(async function () {
var content = await readStdin();
var fd = fs.openSync(filename, "wx", 0o600); // Avoid overwriting existing file
fs.writeFileSync(fd, content, "utf8");
fs.closeSync(fd);
process.addListener("exit", () => fs.unlinkSync(filename));
child_process.execFile(
bsc_exe,
["-format", filename],
(error, stdout, stderr) => {
if (error === null) {
process.stdout.write(stdout);
} else {
console.error(stderr);
process.exit(2);
}
},
);
})();
} else {
console.error(`Unsupported extension ${use_stdin}`);
console.error(`Supported extensions: ${formattedStdExtensions} `);
process.exit(2);
}
} else {
if (files.length === 0) {
// none of argumets set
// format the current directory
files = fs.readdirSync(process.cwd()).filter(isSupportedFile);
}
for (let i = 0; i < files.length; ++i) {
let file = files[i];
if (!isSupportedStd(file)) {
console.error(`Don't know what do with ${file}`);
console.error(`Supported extensions: ${formattedFileExtensions}`);
process.exit(2);
}
}
await formatFiles(files, bsc_exe, isSupportedFile, check.val);
}
} catch (e) {
if (e instanceof arg.ArgError) {
console.error(e.message);
process.exit(2);
} else {
throw e;
}
}
}
exports.main = main;