-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcli.ts
59 lines (56 loc) · 1.63 KB
/
cli.ts
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
import { CompilerError } from "./exceptions";
import compiler from "./main";
import fs from "fs";
let path = process.argv[process.argv.length - 1].replace(/\/$/, "");
let inputFiles: Array<{ fileName: string; file: string[] }>;
if (path.endsWith(".jack")) {
inputFiles = [
{
fileName: path
.split("/")
.pop()!
.replace(/\.jack$/, ""),
file: fs.readFileSync(path, "utf-8").split("\n"),
},
];
} else {
const JackOS = fs
.readdirSync("os")
.filter((OSFileName: string) => OSFileName.endsWith(".jack"))
.map((OSFileName: string) => `os/${OSFileName}`);
const inputFileNames = fs
.readdirSync(path)
.filter((fileName: string) => fileName.endsWith(".jack"))
.map((fileName: string) => `${path}/${fileName}`);
inputFiles = inputFileNames
.concat(
JackOS.filter(
(OSFileName: string) =>
!inputFileNames.find(
(inputFileName: string) =>
inputFileName.split("/").pop() === OSFileName.split("/").pop()
)
)
)
.map((fileName: string) => ({
fileName: fileName
.split("/")
.pop()!
.replace(/\.jack$/, ""),
file: fs.readFileSync(fileName, "utf-8").split("\n") as string[],
}));
}
const out = compiler(inputFiles, true);
if (out instanceof CompilerError) {
console.error(out.toString());
} else {
out.forEach((fileData) => {
let outputFile: string;
if (path.endsWith(".jack")) {
outputFile = path.replace(/\.jack$/, ".vm");
} else {
outputFile = `${path}/${fileData.fileName}.vm`;
}
fs.writeFileSync(outputFile, fileData.VMCode.join("\n"));
});
}