forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrescript
executable file
·134 lines (115 loc) · 3.18 KB
/
rescript
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
129
130
131
132
133
134
#!/usr/bin/env node
//@ts-check
"use strict";
/**
* This script is supposed to be running in project root directory
* It matters since we need read .sourcedirs(location)
* and its content are file/directories with regard to project root
*/
var { bsc_exe, rescript_exe } = require("./cli/bin_path.js");
var bsb = require("./cli/rescript_bsb.js");
var cwd = process.cwd();
process.env.BSB_PROJECT_ROOT = cwd;
if (process.env.FORCE_COLOR === undefined) {
if (require("tty").isatty(1)) {
process.env.FORCE_COLOR = "1";
process.env.NINJA_ANSI_FORCED = "1";
}
} else {
if (process.env.FORCE_COLOR === "1" && process.env.NINJA_ANSI_FORCED === undefined) {
process.env.NINJA_ANSI_FORCED = "1";
}
if (process.argv.includes("-verbose")) {
console.log(`FORCE_COLOR: "${process.env.FORCE_COLOR}"`);
}
}
const helpMessage = `Usage: rescript <options> <subcommand>
\`rescript\` is equivalent to \`rescript build\`
Options:
-v, -version display version number
-h, -help display help
Subcommands:
build
clean
format
dump
help
Run \`rescript <subcommand> -h\` for subcommand help. Examples:
rescript build -h
rescript format -h`;
function onUncaughtException(err) {
console.error("Uncaught Exception", err);
bsb.releaseBuild();
process.exit(1);
}
function exitProcess() {
bsb.releaseBuild();
process.exit(0);
}
process.on("uncaughtException", onUncaughtException);
// OS signal handlers
// Ctrl+C
process.on("SIGINT", exitProcess);
// kill pid
try {
process.on("SIGUSR1", exitProcess);
process.on("SIGUSR2", exitProcess);
process.on("SIGTERM", exitProcess);
process.on("SIGHUP", exitProcess);
} catch (_e) {
// Deno might throw an error here, see https://github.com/denoland/deno/issues/9995
// TypeError: Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).
}
const args = process.argv.slice(2);
const argPatterns = {
help: ['help', '-h', '-help', '--help'],
version: ['version', '-v', '-version', '--version'],
};
const helpArgIndex = args.findIndex(arg => argPatterns.help.includes(arg));
const firstPositionalArgIndex = args.findIndex(arg => !arg.startsWith("-"));
if (helpArgIndex !== -1 && (firstPositionalArgIndex === -1 || helpArgIndex <= firstPositionalArgIndex)) {
console.log(helpMessage);
} else if (argPatterns.version.includes(args[0])) {
console.log(require("./package.json").version);
} else if (firstPositionalArgIndex !== -1) {
const subcmd = args[firstPositionalArgIndex];
const subcmdArgs = args.slice(firstPositionalArgIndex + 1);
switch (subcmd) {
case "info": {
bsb.info(subcmdArgs);
break;
}
case "clean": {
bsb.clean(subcmdArgs);
break;
}
case "build": {
bsb.build(subcmdArgs);
break;
}
case "format": {
require("./cli/rescript_format.js").main(
subcmdArgs,
rescript_exe,
bsc_exe
);
break;
}
case "dump": {
require("./cli/rescript_dump.js").main(
subcmdArgs,
rescript_exe,
bsc_exe
);
break;
}
default: {
console.error(
`Error: Unknown command "${subcmd}".\n${helpMessage}`
);
process.exit(2);
}
}
} else {
bsb.build(args);
}