dslinter 0.0.27 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,136 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import {
3
+ defaultReportPath,
4
+ defaultServePort,
5
+ findViteRoot,
6
+ resolveBundledDashboardDir,
7
+ resolveViteBin,
8
+ } from "../lib/project-root.mjs";
9
+ import { spawnScanner } from "../lib/run-scanner.mjs";
10
+ import { waitForPort } from "../lib/wait-for-port.mjs";
11
+
12
+ /**
13
+ * @param {{
14
+ * scanPath: string;
15
+ * outputPath: string | null;
16
+ * scannerArgs: string[];
17
+ * servePort: number | null;
18
+ * }}
19
+ */
20
+ export async function runDevMode({ scanPath, outputPath, scannerArgs, servePort }) {
21
+ const port = servePort ?? defaultServePort();
22
+ const reportPath = defaultReportPath(scanPath, outputPath);
23
+ const viteRoot = findViteRoot(process.cwd());
24
+ const bundledDist = resolveBundledDashboardDir();
25
+
26
+ const args = [...scannerArgs];
27
+ const hasServe = args.some((a) => a === "--serve" || a.startsWith("--serve="));
28
+ if (!hasServe) {
29
+ args.push("--serve", String(port));
30
+ }
31
+ if (!args.some((a) => a === "--output" || a.startsWith("--output="))) {
32
+ args.push("--output", reportPath);
33
+ }
34
+ if (bundledDist && !args.some((a) => a === "--dashboard-static" || a.startsWith("--dashboard-static="))) {
35
+ args.push("--dashboard-static", bundledDist);
36
+ }
37
+
38
+ const scanner = await spawnScanner(args);
39
+ const children = [scanner];
40
+
41
+ const cleanup = (signal) => {
42
+ for (const child of children) {
43
+ if (child && !child.killed) child.kill(signal);
44
+ }
45
+ };
46
+
47
+ for (const sig of ["SIGINT", "SIGTERM", "SIGHUP"]) {
48
+ process.on(sig, () => cleanup(sig));
49
+ }
50
+
51
+ scanner.on("exit", (code, signal) => {
52
+ if (signal) cleanup("SIGTERM");
53
+ else if (code !== 0 && code !== null) process.exit(code);
54
+ });
55
+
56
+ try {
57
+ await waitForPort(port);
58
+ } catch (err) {
59
+ process.stderr.write(`${err instanceof Error ? err.message : err}\n`);
60
+ cleanup("SIGTERM");
61
+ process.exit(1);
62
+ }
63
+
64
+ if (bundledDist) {
65
+ const url = `http://127.0.0.1:${port}/`;
66
+ const lines = [
67
+ "",
68
+ "[dslinter] Bundled dashboard at",
69
+ ` ${url}`,
70
+ ` Report: http://127.0.0.1:${port}/dslint-report.json`,
71
+ ];
72
+ if (viteRoot) {
73
+ lines.push(
74
+ " Vite dev server also starting (use its URL for your app; proxy /dslint-report.json and /events to this port if needed).",
75
+ );
76
+ }
77
+ lines.push("");
78
+ process.stderr.write(lines.join("\n"));
79
+ if (!viteRoot) {
80
+ maybeOpenBrowser(url);
81
+ scanner.on("exit", (code) => process.exit(code ?? 0));
82
+ return;
83
+ }
84
+ }
85
+
86
+ if (!viteRoot) {
87
+ process.stderr.write(
88
+ [
89
+ "",
90
+ "[dslinter] No vite.config.* and no bundled dashboard-dist — scanner only (watch + serve).",
91
+ " Reinstall dslinter or run `pnpm --filter dslinter run build:dashboard` from the repo.",
92
+ " Or add a Vite app with proxy for /dslint-report.json and /events.",
93
+ ` Scanner: http://127.0.0.1:${port}/dslint-report.json`,
94
+ "",
95
+ ].join("\n"),
96
+ );
97
+ scanner.on("exit", (code) => process.exit(code ?? 0));
98
+ return;
99
+ }
100
+
101
+ const viteBin = resolveViteBin(viteRoot);
102
+ if (!viteBin) {
103
+ process.stderr.write(`dslinter: vite not installed in ${viteRoot}. Run npm install.\n`);
104
+ cleanup("SIGTERM");
105
+ process.exit(1);
106
+ }
107
+
108
+ const vite = spawn(process.execPath, [viteBin, "--mode", "serve"], {
109
+ cwd: viteRoot,
110
+ stdio: "inherit",
111
+ });
112
+ children.push(vite);
113
+
114
+ vite.on("exit", (code, signal) => {
115
+ cleanup("SIGTERM");
116
+ if (signal) process.kill(process.pid, signal);
117
+ else process.exit(code ?? 0);
118
+ });
119
+ }
120
+
121
+ /**
122
+ * @param {string} url
123
+ */
124
+ function maybeOpenBrowser(url) {
125
+ if (process.env.CI === "true" || process.env.CI === "1") return;
126
+ if (process.env.DSLINT_OPEN_BROWSER !== "1") return;
127
+
128
+ const platform = process.platform;
129
+ if (platform === "darwin") {
130
+ spawnSync("open", [url], { stdio: "ignore" });
131
+ } else if (platform === "win32") {
132
+ spawnSync("cmd", ["/c", "start", "", url], { stdio: "ignore" });
133
+ } else {
134
+ spawnSync("xdg-open", [url], { stdio: "ignore" });
135
+ }
136
+ }
@@ -0,0 +1,9 @@
1
+ import { runScannerSync } from "../lib/run-scanner.mjs";
2
+
3
+ /**
4
+ * @param {string[]} scannerArgs
5
+ */
6
+ export function runReportMode(scannerArgs) {
7
+ const code = runScannerSync(scannerArgs);
8
+ process.exit(code);
9
+ }