sitegen/run.js

53 lines
1.5 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 process from "node:process";
// 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 };