Skip to content

Commit 93ea39b

Browse files
committed
tweaks, better error message for invalid input
1 parent 7d6b3f5 commit 93ea39b

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

scripts/rescript_format.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,20 @@ in the stdout (in rescript syntax)`,
3333
"Formatting the whole project ",
3434
],
3535
];
36-
var formattedExtensions = [".res", ".resi", ".ml", ".mli", ".re", ".rei"];
36+
var formattedStdExtensions = [".res", ".resi", ".ml", ".mli", ".re", ".rei"];
37+
var formattedFileExtensions = [".res", ".resi"];
38+
var convertedExtensions = [".ml", ".mli", ".re", ".rei"];
39+
/**
40+
*
41+
* @param {string[]} extensions
42+
*/
43+
function hasExtension(extensions) {
44+
/**
45+
* @param {string} x
46+
*/
47+
var pred = (x) => extensions.some((ext) => x.endsWith(ext));
48+
return pred;
49+
}
3750
async function readStdin() {
3851
var stream = process.stdin;
3952
const chunks = [];
@@ -47,6 +60,9 @@ async function readStdin() {
4760
* @param {string} bsc_exe
4861
*/
4962
function main(argv, bsb_exe, bsc_exe) {
63+
var isSupportedFile = hasExtension(formattedFileExtensions);
64+
var isSupportedStd = hasExtension(formattedStdExtensions);
65+
var isSupportedConvert = hasExtension(convertedExtensions);
5066
try {
5167
/**
5268
* @type {string[]}
@@ -75,7 +91,7 @@ function main(argv, bsb_exe, bsc_exe) {
7591
}
7692
files = output.stdout.split("\n").map((x) => x.trim());
7793
for (let arg of files) {
78-
if (arg.endsWith(".res") || arg.endsWith(".resi")) {
94+
if (isSupportedFile(arg)) {
7995
// console.log(`processing ${arg}`);
8096
child_process.execFile(
8197
bsc_exe,
@@ -92,7 +108,7 @@ function main(argv, bsb_exe, bsc_exe) {
92108
}
93109
}
94110
} else if (use_stdin) {
95-
if (formattedExtensions.some((x) => use_stdin.endsWith(x))) {
111+
if (isSupportedStd(use_stdin)) {
96112
var crypto = require("crypto");
97113
var os = require("os");
98114
var filename = path.join(
@@ -107,7 +123,7 @@ function main(argv, bsb_exe, bsc_exe) {
107123
["-format", filename],
108124
(error, stdout, stderr) => {
109125
if (error === null) {
110-
console.log(stdout);
126+
console.log(stdout.trimEnd());
111127
} else {
112128
console.log(stderr);
113129
process.exit(2);
@@ -117,21 +133,27 @@ function main(argv, bsb_exe, bsc_exe) {
117133
})();
118134
} else {
119135
console.error(`Unsupported exetnsion ${use_stdin}`);
120-
console.error(`Supported extensions: ${formattedExtensions} `);
136+
console.error(`Supported extensions: ${formattedStdExtensions} `);
121137
process.exit(2);
122138
}
123139
} else {
124140
if (files.length === 0) {
125141
// none of argumets set
126142
// format the current directory
127-
files = fs
128-
.readdirSync(process.cwd())
129-
.filter((x) => x.endsWith(".res") || x.endsWith(".resi"));
143+
files = fs.readdirSync(process.cwd()).filter(isSupportedFile);
130144
}
145+
131146
for (let i = 0; i < files.length; ++i) {
132147
let file = files[i];
133-
if (!(file.endsWith(".res") || file.endsWith(".resi"))) {
134-
console.error(`don't know what do with ${file}`);
148+
if (!isSupportedFile(file)) {
149+
if (isSupportedConvert(file)) {
150+
console.error(
151+
`You need convert subcommand to handle such file extensions`
152+
);
153+
} else {
154+
console.error(`Don't know what do with ${file}`);
155+
}
156+
console.error(`Supported extensions: ${formattedFileExtensions}`)
135157
process.exit(2);
136158
}
137159
}

0 commit comments

Comments
 (0)