файл пакета→ .shtukbase/packages/lenses/actions/kit.mjs
actions/kit.mjs
44 строки, 2.3 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // Finds the lens kit and runs it on the project in the working directory. The kit (kit.js, kit.css, the
- // templates, the build and the server) lives in this package, in kit/: an install puts it at
- // .shtukbase/packages/lenses/kit/. SHTUK_KIT, when set, points to another copy of the kit (an override for kit work).
- import { spawn, spawnSync } from "node:child_process";
- import { existsSync } from "node:fs";
- import { dirname, join, resolve } from "node:path";
- import { fileURLToPath } from "node:url";
- /** The package's own copy of the kit: kit/ next to actions/. */
- export const OWN_KIT = resolve(dirname(fileURLToPath(import.meta.url)), "..", "kit");
- const runner = process.versions.bun ? process.execPath : "bun";
- /** The kit folder (SHTUK_KIT, else the package's own copy), or null after saying what is wrong. */
- export function kitDir() {
- const dir = process.env.SHTUK_KIT ? resolve(process.env.SHTUK_KIT) : OWN_KIT;
- if (!existsSync(join(dir, "bin", "shtukbase-lenses.js"))) {
- const why = process.env.SHTUK_KIT ? "SHTUK_KIT does not point to a kit" : "the lenses package is not fully installed, reinstall it";
- console.log(`kit not found in ${dir}: ${why}`);
- return null;
- }
- return dir;
- }
- /** Runs the kit's lens command line (`build` or `check`) on the project; returns its exit code. */
- export function lenses(cmd) {
- const dir = kitDir();
- if (!dir) return 2;
- const r = spawnSync(runner, [join(dir, "bin", "shtukbase-lenses.js"), cmd, "--root", process.cwd()], { encoding: "utf8" });
- // Findings go to stdout, one per line; the kit writes its complaint to stderr.
- for (const line of `${r.stdout ?? ""}${r.stderr ?? ""}`.split("\n")) if (line.trim()) console.log(line);
- return r.status ?? 1;
- }
- /** Runs the kit's server on the project until it stops; a signal is passed on, so no server is left behind. */
- export function serve(port) {
- const dir = kitDir();
- if (!dir) return Promise.resolve(2);
- console.log(`project ${process.cwd()} is open at http://127.0.0.1:${port}/ (Ctrl-C to stop)`);
- return new Promise((done) => {
- const child = spawn(runner, [join(dir, "server", "serve.ts"), process.cwd(), String(port)], { stdio: "inherit" });
- for (const sig of ["SIGINT", "SIGTERM"]) process.on(sig, () => child.kill(sig));
- child.on("exit", (code, sig) => done(code ?? (sig ? 0 : 1)));
- });
- }