-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat-rescript.js
More file actions
38 lines (31 loc) · 953 Bytes
/
format-rescript.js
File metadata and controls
38 lines (31 loc) · 953 Bytes
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
/* eslint-disable no-console */
const { basename } = require("path");
const { readFileSync, writeFileSync, lstatSync } = require("fs");
const { execSync } = require("child_process");
const fileNames = process.argv.slice(2);
if (!fileNames.length) {
throw new Error(`Usage: ${basename(__filename)} <fileName> [<fileName>...]`);
}
let hasError = false;
fileNames.forEach(fileName => {
if (!fileName.endsWith(".res")) {
throw new Error(`Not a rescript file: ${fileName}`);
}
if (lstatSync(fileName).isSymbolicLink()) {
console.log(`Ignoring symlink ${fileName}`);
return;
}
const contents = readFileSync(fileName).toString();
try {
const formattedCode = execSync(`node_modules/.bin/bsc -format ${fileName}`);
if (formattedCode !== contents) {
writeFileSync(fileName, formattedCode);
console.log("Formatted", fileName);
}
} catch {
hasError = true;
}
});
if (hasError) {
process.exit(1);
}