Skip to content

fix convert behavior, better error message for invalid input #5063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 29 additions & 37 deletions scripts/rescript_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ rescript convert -- it converts the current directory

var child_process = require("child_process");
var path = require("path");
var fs = require("fs");

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

/**
*
* @param {string} file
* @param {string} bsc_exe
* assume the file is convertible
*/
function handleOneFile(file, bsc_exe) {
// console.log(`processing ${arg}`);
var nextExt = file.endsWith("i") ? ".resi" : ".res";
child_process.execFile(
bsc_exe,
["-o", file.substr(0, file.lastIndexOf(".")) + nextExt, "-format", file],
(error, stdout, stderr) => {
if (error === null) {
// todo
fs.unlink(file, () => {
//ignore
});
} else {
// todo error handling
console.error(`Error when converting ${file}`);
console.log(stderr);
}
}
);
}
/**
* @param {string[]} argv
* @param {string} bsb_exe
Expand Down Expand Up @@ -66,26 +93,7 @@ function main(argv, bsb_exe, bsc_exe) {
files = output.stdout.split("\n").map((x) => x.trim());
for (let file of files) {
if (shouldConvert(file)) {
// console.log(`processing ${arg}`);
var nextExt = file.endsWith("i") ? ".resi" : ".res";
child_process.execFile(
path.join(__dirname, process.platform, "bsc.exe"),
[
"-o",
file.substr(0, file.lastIndexOf(".")) + nextExt,
"-format",
file,
],
(error, stdout, stderr) => {
if (error === null) {
// todo
} else {
// todo error handling
console.error(`Error when converting ${file}`);
console.log(stderr);
}
}
);
handleOneFile(file, bsc_exe);
}
}
} else {
Expand All @@ -97,23 +105,7 @@ function main(argv, bsb_exe, bsc_exe) {
}
}
files.forEach((file) => {
var nextExt = file.endsWith("i") ? ".resi" : ".res";
child_process.execFile(
bsc_exe,
[
"-o",
file.substr(0, file.lastIndexOf(".")) + nextExt,
"-format",
file,
],
(error, stdout, stderr) => {
if (error === null) {
} else {
console.error(`Error when converting ${file}`);
console.log(stderr);
}
}
);
handleOneFile(file, bsc_exe);
});
}
} catch (e) {
Expand Down
42 changes: 32 additions & 10 deletions scripts/rescript_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@ in the stdout (in rescript syntax)`,
"Formatting the whole project ",
],
];
var formattedExtensions = [".res", ".resi", ".ml", ".mli", ".re", ".rei"];
var formattedStdExtensions = [".res", ".resi", ".ml", ".mli", ".re", ".rei"];
var formattedFileExtensions = [".res", ".resi"];
var convertedExtensions = [".ml", ".mli", ".re", ".rei"];
/**
*
* @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 = [];
Expand All @@ -47,6 +60,9 @@ async function readStdin() {
* @param {string} bsc_exe
*/
function main(argv, bsb_exe, bsc_exe) {
var isSupportedFile = hasExtension(formattedFileExtensions);
var isSupportedStd = hasExtension(formattedStdExtensions);
var isSupportedConvert = hasExtension(convertedExtensions);
try {
/**
* @type {string[]}
Expand Down Expand Up @@ -75,7 +91,7 @@ function main(argv, bsb_exe, bsc_exe) {
}
files = output.stdout.split("\n").map((x) => x.trim());
for (let arg of files) {
if (arg.endsWith(".res") || arg.endsWith(".resi")) {
if (isSupportedFile(arg)) {
// console.log(`processing ${arg}`);
child_process.execFile(
bsc_exe,
Expand All @@ -92,7 +108,7 @@ function main(argv, bsb_exe, bsc_exe) {
}
}
} else if (use_stdin) {
if (formattedExtensions.some((x) => use_stdin.endsWith(x))) {
if (isSupportedStd(use_stdin)) {
var crypto = require("crypto");
var os = require("os");
var filename = path.join(
Expand All @@ -107,7 +123,7 @@ function main(argv, bsb_exe, bsc_exe) {
["-format", filename],
(error, stdout, stderr) => {
if (error === null) {
console.log(stdout);
console.log(stdout.trimEnd());
} else {
console.log(stderr);
process.exit(2);
Expand All @@ -117,21 +133,27 @@ function main(argv, bsb_exe, bsc_exe) {
})();
} else {
console.error(`Unsupported exetnsion ${use_stdin}`);
console.error(`Supported extensions: ${formattedExtensions} `);
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((x) => x.endsWith(".res") || x.endsWith(".resi"));
files = fs.readdirSync(process.cwd()).filter(isSupportedFile);
}

for (let i = 0; i < files.length; ++i) {
let file = files[i];
if (!(file.endsWith(".res") || file.endsWith(".resi"))) {
console.error(`don't know what do with ${file}`);
if (!isSupportedFile(file)) {
if (isSupportedConvert(file)) {
console.error(
`You need convert subcommand to handle such file extensions`
);
} else {
console.error(`Don't know what do with ${file}`);
}
console.error(`Supported extensions: ${formattedFileExtensions}`)
process.exit(2);
}
}
Expand Down