|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// @ts-check |
| 4 | + |
| 5 | +// This script is supposed to be running in project root directory |
| 6 | +// It matters since we need read .sourcedirs(location) |
| 7 | +// and its content are file/directories with regard to project root |
| 8 | + |
| 9 | +import * as fs from "node:fs"; |
| 10 | +import * as tty from "node:tty"; |
| 11 | + |
| 12 | +import { bsc_exe, rescript_legacy_exe } from "./common/bins.js"; |
| 13 | +import * as bsb from "./common/bsb.js"; |
| 14 | + |
| 15 | +const cwd = process.cwd(); |
| 16 | +process.env.BSB_PROJECT_ROOT = cwd; |
| 17 | + |
| 18 | +if (process.env.FORCE_COLOR === undefined) { |
| 19 | + if (tty.isatty(1)) { |
| 20 | + process.env.FORCE_COLOR = "1"; |
| 21 | + process.env.NINJA_ANSI_FORCED = "1"; |
| 22 | + } |
| 23 | +} else { |
| 24 | + if ( |
| 25 | + process.env.FORCE_COLOR === "1" && |
| 26 | + process.env.NINJA_ANSI_FORCED === undefined |
| 27 | + ) { |
| 28 | + process.env.NINJA_ANSI_FORCED = "1"; |
| 29 | + } |
| 30 | + if (process.argv.includes("-verbose")) { |
| 31 | + console.log(`FORCE_COLOR: "${process.env.FORCE_COLOR}"`); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const helpMessage = `Usage: rescript <options> <subcommand> |
| 36 | +
|
| 37 | +\`rescript\` is equivalent to \`rescript build\` |
| 38 | +
|
| 39 | +Options: |
| 40 | + -v, -version display version number |
| 41 | + -h, -help display help |
| 42 | +
|
| 43 | +Subcommands: |
| 44 | + build |
| 45 | + clean |
| 46 | + format |
| 47 | + dump |
| 48 | + help |
| 49 | +
|
| 50 | +Run \`rescript <subcommand> -h\` for subcommand help. Examples: |
| 51 | + rescript build -h |
| 52 | + rescript format -h`; |
| 53 | + |
| 54 | +function onUncaughtException(err) { |
| 55 | + console.error("Uncaught Exception", err); |
| 56 | + bsb.releaseBuild(); |
| 57 | + process.exit(1); |
| 58 | +} |
| 59 | + |
| 60 | +function exitProcess() { |
| 61 | + bsb.releaseBuild(); |
| 62 | + process.exit(0); |
| 63 | +} |
| 64 | + |
| 65 | +process.on("uncaughtException", onUncaughtException); |
| 66 | + |
| 67 | +// OS signal handlers |
| 68 | +// Ctrl+C |
| 69 | +process.on("SIGINT", exitProcess); |
| 70 | +// kill pid |
| 71 | +try { |
| 72 | + process.on("SIGUSR1", exitProcess); |
| 73 | + process.on("SIGUSR2", exitProcess); |
| 74 | + process.on("SIGTERM", exitProcess); |
| 75 | + process.on("SIGHUP", exitProcess); |
| 76 | +} catch (_e) { |
| 77 | + // Deno might throw an error here, see https://github.com/denoland/deno/issues/9995 |
| 78 | + // TypeError: Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK). |
| 79 | +} |
| 80 | + |
| 81 | +const args = process.argv.slice(2); |
| 82 | +const argPatterns = { |
| 83 | + help: ["help", "-h", "-help", "--help"], |
| 84 | + version: ["version", "-v", "-version", "--version"], |
| 85 | +}; |
| 86 | + |
| 87 | +const helpArgIndex = args.findIndex(arg => argPatterns.help.includes(arg)); |
| 88 | +const firstPositionalArgIndex = args.findIndex(arg => !arg.startsWith("-")); |
| 89 | + |
| 90 | +if ( |
| 91 | + helpArgIndex !== -1 && |
| 92 | + (firstPositionalArgIndex === -1 || helpArgIndex <= firstPositionalArgIndex) |
| 93 | +) { |
| 94 | + console.log(helpMessage); |
| 95 | +} else if (argPatterns.version.includes(args[0])) { |
| 96 | + const packageSpec = JSON.parse( |
| 97 | + fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8"), |
| 98 | + ); |
| 99 | + |
| 100 | + console.log(packageSpec.version); |
| 101 | +} else if (firstPositionalArgIndex !== -1) { |
| 102 | + const subcmd = args[firstPositionalArgIndex]; |
| 103 | + const subcmdArgs = args.slice(firstPositionalArgIndex + 1); |
| 104 | + |
| 105 | + switch (subcmd) { |
| 106 | + case "info": { |
| 107 | + bsb.info(subcmdArgs); |
| 108 | + break; |
| 109 | + } |
| 110 | + case "clean": { |
| 111 | + bsb.clean(subcmdArgs); |
| 112 | + break; |
| 113 | + } |
| 114 | + case "build": { |
| 115 | + bsb.build(subcmdArgs); |
| 116 | + break; |
| 117 | + } |
| 118 | + case "format": { |
| 119 | + const mod = await import("./rescript-legacy/format.js"); |
| 120 | + await mod.main(subcmdArgs, rescript_legacy_exe, bsc_exe); |
| 121 | + break; |
| 122 | + } |
| 123 | + case "dump": { |
| 124 | + const mod = await import("./rescript-legacy/dump.js"); |
| 125 | + mod.main(subcmdArgs, rescript_legacy_exe, bsc_exe); |
| 126 | + break; |
| 127 | + } |
| 128 | + default: { |
| 129 | + console.error(`Error: Unknown command "${subcmd}".\n${helpMessage}`); |
| 130 | + process.exit(2); |
| 131 | + } |
| 132 | + } |
| 133 | +} else { |
| 134 | + bsb.build(args); |
| 135 | +} |
0 commit comments