Skip to content

Commit b6418cc

Browse files
authored
Merge pull request #5063 from rescript-lang/convert_format_tweaks
fix convert behavior, better error message for invalid input
2 parents 564c7c4 + 93ea39b commit b6418cc

File tree

2 files changed

+61
-47
lines changed

2 files changed

+61
-47
lines changed

scripts/rescript_convert.js

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ rescript convert -- it converts the current directory
66

77
var child_process = require("child_process");
88
var path = require("path");
9+
var fs = require("fs");
910

1011
/**
1112
* @type {arg.boolref}
@@ -31,6 +32,32 @@ function shouldConvert(file) {
3132
return [".ml", ".mli", ".re", ".rei"].some((x) => file.endsWith(x));
3233
}
3334

35+
/**
36+
*
37+
* @param {string} file
38+
* @param {string} bsc_exe
39+
* assume the file is convertible
40+
*/
41+
function handleOneFile(file, bsc_exe) {
42+
// console.log(`processing ${arg}`);
43+
var nextExt = file.endsWith("i") ? ".resi" : ".res";
44+
child_process.execFile(
45+
bsc_exe,
46+
["-o", file.substr(0, file.lastIndexOf(".")) + nextExt, "-format", file],
47+
(error, stdout, stderr) => {
48+
if (error === null) {
49+
// todo
50+
fs.unlink(file, () => {
51+
//ignore
52+
});
53+
} else {
54+
// todo error handling
55+
console.error(`Error when converting ${file}`);
56+
console.log(stderr);
57+
}
58+
}
59+
);
60+
}
3461
/**
3562
* @param {string[]} argv
3663
* @param {string} bsb_exe
@@ -66,26 +93,7 @@ function main(argv, bsb_exe, bsc_exe) {
6693
files = output.stdout.split("\n").map((x) => x.trim());
6794
for (let file of files) {
6895
if (shouldConvert(file)) {
69-
// console.log(`processing ${arg}`);
70-
var nextExt = file.endsWith("i") ? ".resi" : ".res";
71-
child_process.execFile(
72-
path.join(__dirname, process.platform, "bsc.exe"),
73-
[
74-
"-o",
75-
file.substr(0, file.lastIndexOf(".")) + nextExt,
76-
"-format",
77-
file,
78-
],
79-
(error, stdout, stderr) => {
80-
if (error === null) {
81-
// todo
82-
} else {
83-
// todo error handling
84-
console.error(`Error when converting ${file}`);
85-
console.log(stderr);
86-
}
87-
}
88-
);
96+
handleOneFile(file, bsc_exe);
8997
}
9098
}
9199
} else {
@@ -97,23 +105,7 @@ function main(argv, bsb_exe, bsc_exe) {
97105
}
98106
}
99107
files.forEach((file) => {
100-
var nextExt = file.endsWith("i") ? ".resi" : ".res";
101-
child_process.execFile(
102-
bsc_exe,
103-
[
104-
"-o",
105-
file.substr(0, file.lastIndexOf(".")) + nextExt,
106-
"-format",
107-
file,
108-
],
109-
(error, stdout, stderr) => {
110-
if (error === null) {
111-
} else {
112-
console.error(`Error when converting ${file}`);
113-
console.log(stderr);
114-
}
115-
}
116-
);
108+
handleOneFile(file, bsc_exe);
117109
});
118110
}
119111
} catch (e) {

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)