33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
#!/usr/bin/env node
|
|
import { spawnSync } from "node:child_process";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
export function runLighthouse({ spawn = spawnSync, stdout = process.stdout, stderr = process.stderr } = {}) {
|
|
const result = spawn("npx", ["--yes", "@lhci/cli@0.13.0", "autorun"], {
|
|
encoding: "utf8",
|
|
});
|
|
|
|
if (result.stdout) stdout.write(result.stdout);
|
|
if (result.stderr) stderr.write(result.stderr);
|
|
|
|
if (result.error?.code === "ENOENT") {
|
|
stdout.write("SKIP lhci not installable in this environment\n");
|
|
return 0;
|
|
}
|
|
|
|
const combinedOutput = `${result.stdout ?? ""}\n${result.stderr ?? ""}`;
|
|
if (combinedOutput.includes("Chrome installation not found")) {
|
|
stdout.write("SKIP Chrome not installed; Lighthouse CI requires a local Chrome/Chromium binary.\n");
|
|
return 0;
|
|
}
|
|
|
|
return result.status ?? 0;
|
|
}
|
|
|
|
const isCli = process.argv[1] && fileURLToPath(import.meta.url) === path.resolve(process.argv[1]);
|
|
|
|
if (isCli) {
|
|
process.exit(runLighthouse());
|
|
}
|