sitegen/run.js

71 lines
2 KiB
JavaScript

// This file allows using Node.js in combination with
// all available plugins. Usage: "node run <script>"
import * as util from "node:util";
import * as zlib from "node:zlib";
import process from "node:process";
if (!zlib.zstdCompress) {
const brand = process.versions.bun
? `bun ${process.versions.bun}`
: process.versions.deno
? `deno ${process.versions.deno}`
: null;
globalThis.console.error(
`sitegen depends on a node.js-compatibile runtime that supports zstd compression\n` +
`this is node.js version ${process.version}${
brand ? ` (${brand})` : ""
}\n\n` +
`get node.js --> https://nodejs.org/en/download/current`,
);
process.exit(1);
}
// Disable experimental warnings (Type Stripping, etc)
const { emit: originalEmit } = process;
const warnings = ["ExperimentalWarning"];
process.emit = function (event, error) {
return event === "warning" && warnings.includes(error.name)
? false
: originalEmit.apply(process, arguments);
};
// Init hooks
const hot = await import("./framework/hot.ts");
await import("#debug");
const console = hot.load("@paperclover/console");
globalThis.console["log"] = console.info;
globalThis.console.info = console.info;
globalThis.console.warn = console.warn;
globalThis.console.error = console.error;
globalThis.console.debug = console.scoped("dbg");
// Load with hooks
if (process.argv[1].startsWith(import.meta.filename.slice(0, -".js".length))) {
if (process.argv.length == 2) {
console.error("usage: node run <script> [...args]");
process.exit(1);
}
let found;
for (const dir of ["./", "./src/", "./framework/"]) {
try {
found = hot.resolveFrom(import.meta.filename, dir + process.argv[2]);
break;
} catch (e) {
continue;
}
}
if (!found) {
console.error("Cannot find script: " + process.argv[2]);
process.exit(1);
}
process.argv = [process.argv[0], ...process.argv.slice(2)];
try {
await hot.load(found).main?.();
} catch (e) {
console.error(util.inspect(e));
}
}
export { hot };