forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrescript_convert.js
128 lines (119 loc) · 2.83 KB
/
rescript_convert.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//@ts-check
var arg = require("./rescript_arg.js");
var format_usage = `Usage: rescript convert <options> [files]
\`rescript convert\` converts the current directory
**This command removes old OCaml files and creates new ReScript
files. Make sure your work is saved first!**
`;
var child_process = require("child_process");
var path = require("path");
var fs = require("fs");
/**
* @type {arg.boolref}
*/
var formatProject = { val: undefined };
/**
* @type{arg.specs}
*/
var specs = [
[
"-all",
{ kind: "Unit", data: { kind: "Unit_set", data: formatProject } },
"Convert the whole project",
],
];
/**
*
* @param {string} file
*/
function shouldConvert(file) {
return [".ml", ".mli"].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} rescript_exe
* @param {string} bsc_exe
*/
function main(argv, rescript_exe, bsc_exe) {
try {
/**
* @type {string[]}
*/
var files = [];
arg.parse_exn(format_usage, argv, specs, xs => {
files = xs;
});
var format_project = formatProject.val;
if (format_project) {
if (files.length !== 0) {
console.error("convert -all can not be in use with other flags");
process.exit(2);
}
// -all
// TODO: check the rest arguments
var output = child_process.spawnSync(
rescript_exe,
["info", "-list-files"],
{
encoding: "utf-8",
}
);
if (output.status !== 0) {
console.error(output.stdout);
console.error(output.stderr);
process.exit(2);
}
files = output.stdout.split("\n").map(x => x.trim());
for (let file of files) {
if (shouldConvert(file)) {
handleOneFile(file, bsc_exe);
}
}
} else {
for (let i = 0; i < files.length; ++i) {
let file = files[i];
if (!shouldConvert(file)) {
console.error(`don't know what do with ${file}`);
process.exit(2);
}
}
files.forEach(file => {
handleOneFile(file, bsc_exe);
});
}
} catch (e) {
if (e instanceof arg.ArgError) {
console.error(e.message);
process.exit(2);
} else {
throw e;
}
}
}
exports.main = main;