-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.ts
86 lines (71 loc) · 2.16 KB
/
build.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
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
import { Command, flags as flagDefs } from "@oclif/command";
import { ExitCodes } from "../lib/utils";
import { dirname, isAbsolute, join, normalize } from "path";
import { existsSync, promises } from "fs";
import { isFile, ensureDir } from "../lib/utils";
const { writeFile } = promises;
import { MaxJSCompiler } from "../lib/maxJSCompiler";
const parseCLIPath = (input: string): string => isAbsolute(input) ? input : normalize(join(process.cwd(), input));
export default class Build extends Command {
static description = "Build and bundle a Project for usage within the [js] or the [jsui] object";
static examples = [
];
static flags = {
help: flagDefs.help({ "char": "h" }),
force: flagDefs.boolean({
"char": "f",
description: "Force overwrite the output file",
"default": false,
hidden: false,
required: false
}),
output: flagDefs.string({
"char": "o",
description: "Output generated file",
hidden: false,
multiple: false,
parse: parseCLIPath,
required: false
})
};
static args = [
{
name: "file",
required: true,
description: "InputFile",
hidden: false,
parse: parseCLIPath
}
];
async run(): Promise<void> {
const { args, flags } = this.parse(Build);
let code = "";
try {
const compiler: MaxJSCompiler = new MaxJSCompiler({ filepath: args.file });
await compiler.setup();
code = await compiler.output();
} catch (err) {
return void this.error(`Failed to compile JS Code: ${err.message}`, {
code: err.code,
exit: ExitCodes.error
});
}
if (!flags.output) return void process.stdout.write(code);
if (existsSync(flags.output)) {
// not a file
if (!(await isFile(flags.output))) {
return void this.error(`Output file ${flags.output} can't be overwritten as it doesn't seem to be a file.`, {
exit: ExitCodes.no_file_overwrite
});
}
// no force remove
if (!flags.force) {
return void this.error(`Output file ${flags.output} already exists. Please remove it or use the --force option.`, {
exit: ExitCodes.no_overwrite
});
}
}
await ensureDir(dirname(flags.output));
await writeFile(flags.output, code, { encoding: "utf8", flag: "w" });
}
}