Skip to content

Commit 799f01b

Browse files
committed
support check formatting
1 parent f7451ad commit 799f01b

File tree

1 file changed

+81
-27
lines changed

1 file changed

+81
-27
lines changed

scripts/rescript_format.js

+81-27
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ var format_usage = `Usage: rescript format <options> [files]
55
\`rescript format\` formats the current directory
66
`;
77
var child_process = require("child_process");
8+
var util = require("node:util");
9+
var asyncExecFile = util.promisify(child_process.execFile);
810
var path = require("path");
911
var fs = require("fs");
12+
var asyncFs = fs.promises;
1013
/**
1114
* @type {arg.stringref}
1215
*/
@@ -17,6 +20,11 @@ var stdin = { val: undefined };
1720
*/
1821
var format = { val: undefined };
1922

23+
/**
24+
* @type {arg.boolref}
25+
*/
26+
var check = { val: undefined };
27+
2028
/**
2129
* @type{arg.specs}
2230
*/
@@ -27,12 +35,16 @@ var specs = [
2735
`[.res|.resi|.ml|.mli] Read the code from stdin and print
2836
the formatted code to stdout in ReScript syntax`,
2937
],
30-
// ml|mli
3138
[
3239
"-all",
3340
{ kind: "Unit", data: { kind: "Unit_set", data: format } },
3441
"Format the whole project ",
3542
],
43+
[
44+
"-check",
45+
{ kind: "Unit", data: { kind: "Unit_set", data: check } },
46+
"Check formatting only",
47+
],
3648
];
3749
var formattedStdExtensions = [".res", ".resi", ".ml", ".mli"];
3850
var formattedFileExtensions = [".res", ".resi"];
@@ -55,12 +67,33 @@ async function readStdin() {
5567
return Buffer.concat(chunks).toString("utf8");
5668
}
5769

70+
/**
71+
*
72+
* @param {string} file
73+
* @returns void
74+
*/
75+
function printIncorrectlyFormattedFile(file) {
76+
console.error("[format check]", file);
77+
}
78+
79+
/**
80+
*
81+
* @param {number} incorrectlyFormattedFiles
82+
* @returns void
83+
*/
84+
function printFormatCheckResult(incorrectlyFormattedFiles) {
85+
if (incorrectlyFormattedFiles > 0){
86+
console.error(`${incorrectlyFormattedFiles} file${incorrectlyFormattedFiles > 1 ? "s":""} listed above need${incorrectlyFormattedFiles > 1 ? "": "s"} formatting.`);
87+
process.exit(3);
88+
}
89+
}
90+
5891
/**
5992
* @param {string[]} argv
6093
* @param {string} rescript_exe
6194
* @param {string} bsc_exe
6295
*/
63-
function main(argv, rescript_exe, bsc_exe) {
96+
async function main(argv, rescript_exe, bsc_exe) {
6497
var isSupportedFile = hasExtension(formattedFileExtensions);
6598
var isSupportedStd = hasExtension(formattedStdExtensions);
6699

@@ -95,26 +128,33 @@ function main(argv, rescript_exe, bsc_exe) {
95128
process.exit(2);
96129
}
97130
files = output.stdout.split("\n").map(x => x.trim());
98-
var hasError = false;
99-
for (let arg of files) {
100-
if (isSupportedFile(arg)) {
101-
// console.log(`processing ${arg}`);
102-
child_process.execFile(
103-
bsc_exe,
104-
["-o", arg, "-format", arg],
105-
(error, _stdout, stderr) => {
106-
if (error !== null) {
107-
console.error(stderr);
108-
hasError = true;
131+
var incorrectlyFormattedFiles = 0;
132+
try {
133+
const _promises = await Promise.all(files.map(async (file) => {
134+
if (isSupportedFile(file)) {
135+
// console.log(`processing ${arg}`);
136+
const flags = check.val ? ["-format", file] : ["-o", file, "-format", file];
137+
const {stdout} = await asyncExecFile(bsc_exe,flags);
138+
if (check.val){
139+
const original = await asyncFs.readFile(file, 'utf-8');
140+
if (original != stdout){
141+
printIncorrectlyFormattedFile(file);
142+
incorrectlyFormattedFiles++;
109143
}
110144
}
111-
);
112-
}
113-
}
114-
if (hasError) {
145+
}
146+
return null;
147+
}));
148+
} catch (err) {
149+
console.error(err);
115150
process.exit(2);
116151
}
152+
printFormatCheckResult(incorrectlyFormattedFiles);
117153
} else if (use_stdin) {
154+
if (check.val){
155+
console.error("format -stdin cannot be used with -check flag");
156+
process.exit(2);
157+
}
118158
if (isSupportedStd(use_stdin)) {
119159
var crypto = require("crypto");
120160
var os = require("os");
@@ -144,7 +184,7 @@ function main(argv, rescript_exe, bsc_exe) {
144184
);
145185
})();
146186
} else {
147-
console.error(`Unsupported exetnsion ${use_stdin}`);
187+
console.error(`Unsupported extension ${use_stdin}`);
148188
console.error(`Supported extensions: ${formattedStdExtensions} `);
149189
process.exit(2);
150190
}
@@ -164,20 +204,34 @@ function main(argv, rescript_exe, bsc_exe) {
164204
}
165205
}
166206
var hasError = false;
207+
var incorrectlyFormattedFiles = 0;
167208
files.forEach(file => {
168-
var write = isSupportedFile(file);
209+
var write = isSupportedFile(file) && !check.val;
169210
var flags = write ? ["-o", file, "-format", file] : ["-format", file];
170-
child_process.execFile(bsc_exe, flags, (error, stdout, stderr) => {
171-
if (error === null) {
172-
if (!write) {
173-
process.stdout.write(stdout);
211+
try {
212+
const formatted = child_process.execFileSync(bsc_exe, flags);
213+
if (!write) {
214+
if (check.val) {
215+
try {
216+
const original = fs.readFileSync(file, 'utf-8');
217+
if (original != formatted){
218+
printIncorrectlyFormattedFile(file);
219+
incorrectlyFormattedFiles++;
220+
}
221+
} catch (err){
222+
console.error(err);
223+
hasError = true;
224+
}
225+
} else {
226+
process.stdout.write(formatted);
174227
}
175-
} else {
176-
console.error(stderr);
177-
hasError = true;
178228
}
179-
});
229+
} catch (err) {
230+
console.error(err);
231+
hasError = true;
232+
}
180233
});
234+
printFormatCheckResult(incorrectlyFormattedFiles);
181235
if (hasError) {
182236
process.exit(2);
183237
}

0 commit comments

Comments
 (0)