sdtk-spec-kit 0.8.1 → 0.9.0
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.
- package/README.md +13 -12
- package/assets/manifest/toolkit-bundle.manifest.json +2 -2
- package/package.json +1 -1
- package/src/commands/atlas.js +55 -344
- package/src/commands/help.js +12 -35
- package/src/lib/atlas-delegate.js +109 -0
- package/assets/atlas/build_atlas.py +0 -880
- package/assets/atlas/doc_atlas_viewer_template.html +0 -3796
- package/assets/atlas/vendor/mermaid.min.js +0 -2029
- package/src/lib/atlas-config.js +0 -200
- package/src/lib/atlas-runner.js +0 -435
- package/src/lib/browser-open.js +0 -39
package/src/lib/atlas-config.js
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const { ValidationError } = require("./errors");
|
|
6
|
-
|
|
7
|
-
const CONFIG_SCHEMA_VERSION = 1;
|
|
8
|
-
const DEFAULT_PORT = 8765;
|
|
9
|
-
const DEFAULT_HOST = "127.0.0.1";
|
|
10
|
-
|
|
11
|
-
const DEFAULT_EXCLUDES = [
|
|
12
|
-
".git",
|
|
13
|
-
".sdtk/atlas",
|
|
14
|
-
"node_modules",
|
|
15
|
-
".venv",
|
|
16
|
-
"venv",
|
|
17
|
-
"dist",
|
|
18
|
-
"build",
|
|
19
|
-
"coverage",
|
|
20
|
-
".next",
|
|
21
|
-
".turbo",
|
|
22
|
-
".cache",
|
|
23
|
-
"__pycache__",
|
|
24
|
-
];
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Resolve Atlas configuration from CLI flags and optional persisted config.
|
|
28
|
-
*
|
|
29
|
-
* Priority: CLI flags > persisted config > defaults.
|
|
30
|
-
*
|
|
31
|
-
* @param {Object} flags - Parsed CLI flags.
|
|
32
|
-
* @returns {AtlasConfig}
|
|
33
|
-
*/
|
|
34
|
-
function resolveAtlasConfig(flags) {
|
|
35
|
-
const projectPath = flags["project-path"]
|
|
36
|
-
? path.resolve(flags["project-path"])
|
|
37
|
-
: process.cwd();
|
|
38
|
-
|
|
39
|
-
if (!fs.existsSync(projectPath) || !fs.statSync(projectPath).isDirectory()) {
|
|
40
|
-
throw new ValidationError(
|
|
41
|
-
`--project-path is not a valid directory: ${projectPath}`
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const defaultOutputDir = path.join(projectPath, ".sdtk", "atlas");
|
|
46
|
-
const outputDir = flags["output-dir"]
|
|
47
|
-
? path.resolve(flags["output-dir"])
|
|
48
|
-
: defaultOutputDir;
|
|
49
|
-
|
|
50
|
-
// Read persisted config if it exists
|
|
51
|
-
const configPath = path.join(outputDir, "config.json");
|
|
52
|
-
let persisted = {};
|
|
53
|
-
if (fs.existsSync(configPath)) {
|
|
54
|
-
try {
|
|
55
|
-
persisted = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
|
56
|
-
} catch (_) {
|
|
57
|
-
// ignore corrupt config, use defaults
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Resolve scan roots: CLI flag > persisted > [projectPath]
|
|
62
|
-
let scanRoots;
|
|
63
|
-
if (flags["scan-root"] && flags["scan-root"].length > 0) {
|
|
64
|
-
scanRoots = flags["scan-root"].map((r) => path.resolve(r));
|
|
65
|
-
} else if (persisted.scanRoots && Array.isArray(persisted.scanRoots) && persisted.scanRoots.length > 0) {
|
|
66
|
-
scanRoots = persisted.scanRoots.map((r) => path.resolve(r));
|
|
67
|
-
} else {
|
|
68
|
-
scanRoots = [projectPath];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const excludes =
|
|
72
|
-
persisted.excludes && Array.isArray(persisted.excludes)
|
|
73
|
-
? persisted.excludes
|
|
74
|
-
: DEFAULT_EXCLUDES.slice();
|
|
75
|
-
|
|
76
|
-
const host = flags["host"] || persisted.host || DEFAULT_HOST;
|
|
77
|
-
const port = flags["port"]
|
|
78
|
-
? parseInt(flags["port"], 10)
|
|
79
|
-
: persisted.port || DEFAULT_PORT;
|
|
80
|
-
|
|
81
|
-
if (isNaN(port) || port < 1 || port > 65535) {
|
|
82
|
-
throw new ValidationError(`Invalid --port value: ${flags["port"]}`);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return {
|
|
86
|
-
projectPath,
|
|
87
|
-
outputDir,
|
|
88
|
-
configPath,
|
|
89
|
-
scanRoots,
|
|
90
|
-
excludes,
|
|
91
|
-
host,
|
|
92
|
-
port,
|
|
93
|
-
verbose: !!flags.verbose,
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Write the persisted Atlas config to .sdtk/atlas/config.json.
|
|
99
|
-
* Creates the output directory if it does not exist.
|
|
100
|
-
*
|
|
101
|
-
* @param {Object} config - Atlas config object from resolveAtlasConfig.
|
|
102
|
-
*/
|
|
103
|
-
function writeAtlasConfig(config) {
|
|
104
|
-
fs.mkdirSync(config.outputDir, { recursive: true });
|
|
105
|
-
|
|
106
|
-
const payload = {
|
|
107
|
-
schemaVersion: CONFIG_SCHEMA_VERSION,
|
|
108
|
-
projectPath: config.projectPath,
|
|
109
|
-
outputDir: config.outputDir,
|
|
110
|
-
scanRoots: config.scanRoots,
|
|
111
|
-
excludes: config.excludes,
|
|
112
|
-
host: config.host,
|
|
113
|
-
port: config.port,
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
fs.writeFileSync(config.configPath, JSON.stringify(payload, null, 2) + "\n", "utf-8");
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Check whether Atlas has been initialized (config.json exists).
|
|
121
|
-
*
|
|
122
|
-
* @param {string} outputDir
|
|
123
|
-
* @returns {boolean}
|
|
124
|
-
*/
|
|
125
|
-
function isAtlasInitialized(outputDir) {
|
|
126
|
-
return fs.existsSync(path.join(outputDir, "config.json"));
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Check whether a successful Atlas build exists (index artifact present).
|
|
131
|
-
*
|
|
132
|
-
* @param {string} outputDir
|
|
133
|
-
* @returns {boolean}
|
|
134
|
-
*/
|
|
135
|
-
function isAtlasBuilt(outputDir) {
|
|
136
|
-
return (
|
|
137
|
-
fs.existsSync(path.join(outputDir, "SDTK_DOC_INDEX.json")) &&
|
|
138
|
-
fs.existsSync(path.join(outputDir, "viewer.html"))
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Read last build metadata from SDTK_DOC_INDEX.json if it exists.
|
|
144
|
-
*
|
|
145
|
-
* @param {string} outputDir
|
|
146
|
-
* @returns {{ generated: string, count: number } | null}
|
|
147
|
-
*/
|
|
148
|
-
function readBuildMeta(outputDir) {
|
|
149
|
-
const indexPath = path.join(outputDir, "SDTK_DOC_INDEX.json");
|
|
150
|
-
if (!fs.existsSync(indexPath)) return null;
|
|
151
|
-
try {
|
|
152
|
-
const data = JSON.parse(fs.readFileSync(indexPath, "utf-8"));
|
|
153
|
-
return { generated: data.generated || null, count: data.count || 0 };
|
|
154
|
-
} catch (_) {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Read last graph metadata from SDTK_DOC_GRAPH.json if it exists.
|
|
161
|
-
*
|
|
162
|
-
* @param {string} outputDir
|
|
163
|
-
* @returns {{ nodeCount: number, edgeCount: number } | null}
|
|
164
|
-
*/
|
|
165
|
-
function readGraphMeta(outputDir) {
|
|
166
|
-
const graphPath = path.join(outputDir, "SDTK_DOC_GRAPH.json");
|
|
167
|
-
if (!fs.existsSync(graphPath)) return null;
|
|
168
|
-
try {
|
|
169
|
-
const data = JSON.parse(fs.readFileSync(graphPath, "utf-8"));
|
|
170
|
-
return {
|
|
171
|
-
nodeCount: data.node_count || 0,
|
|
172
|
-
edgeCount: data.edge_count || 0,
|
|
173
|
-
};
|
|
174
|
-
} catch (_) {
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Resolve the path to the packaged atlas builder Python script.
|
|
181
|
-
*
|
|
182
|
-
* @returns {string} Absolute path to build_atlas.py.
|
|
183
|
-
*/
|
|
184
|
-
function resolveBuilderPath() {
|
|
185
|
-
const p = path.resolve(__dirname, "..", "..", "assets", "atlas", "build_atlas.py");
|
|
186
|
-
return p;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
module.exports = {
|
|
190
|
-
resolveAtlasConfig,
|
|
191
|
-
writeAtlasConfig,
|
|
192
|
-
isAtlasInitialized,
|
|
193
|
-
isAtlasBuilt,
|
|
194
|
-
readBuildMeta,
|
|
195
|
-
readGraphMeta,
|
|
196
|
-
resolveBuilderPath,
|
|
197
|
-
DEFAULT_EXCLUDES,
|
|
198
|
-
DEFAULT_PORT,
|
|
199
|
-
DEFAULT_HOST,
|
|
200
|
-
};
|
package/src/lib/atlas-runner.js
DELETED
|
@@ -1,435 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const http = require("http");
|
|
5
|
-
const https = require("https");
|
|
6
|
-
const net = require("net");
|
|
7
|
-
const path = require("path");
|
|
8
|
-
const { spawn, execFile } = require("child_process");
|
|
9
|
-
const { DependencyError, CliError } = require("./errors");
|
|
10
|
-
const { resolveBuilderPath } = require("./atlas-config");
|
|
11
|
-
const { openBrowser } = require("./browser-open");
|
|
12
|
-
|
|
13
|
-
const HEALTH_CHECK_RETRIES = 20;
|
|
14
|
-
const HEALTH_CHECK_INTERVAL_MS = 300;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Find an available Python executable.
|
|
18
|
-
* Returns 'python3' on Unix, 'python' on Windows as first try.
|
|
19
|
-
*
|
|
20
|
-
* @returns {Promise<string>} Resolved python executable name.
|
|
21
|
-
*/
|
|
22
|
-
function findPython() {
|
|
23
|
-
return new Promise((resolve, reject) => {
|
|
24
|
-
const candidates = process.platform === "win32"
|
|
25
|
-
? ["python", "python3"]
|
|
26
|
-
: ["python3", "python"];
|
|
27
|
-
|
|
28
|
-
let idx = 0;
|
|
29
|
-
function tryNext() {
|
|
30
|
-
if (idx >= candidates.length) {
|
|
31
|
-
reject(
|
|
32
|
-
new DependencyError(
|
|
33
|
-
"Python not found. Install Python 3.8+ and ensure it is in PATH.\n" +
|
|
34
|
-
"Atlas build requires Python to run the local builder."
|
|
35
|
-
)
|
|
36
|
-
);
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
const candidate = candidates[idx++];
|
|
40
|
-
execFile(candidate, ["--version"], { timeout: 5000 }, (err) => {
|
|
41
|
-
if (!err) {
|
|
42
|
-
resolve(candidate);
|
|
43
|
-
} else {
|
|
44
|
-
tryNext();
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
tryNext();
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Run the Atlas Python builder with explicit project/output args.
|
|
54
|
-
*
|
|
55
|
-
* @param {Object} config - Resolved AtlasConfig.
|
|
56
|
-
* @returns {Promise<{ docCount: number, nodeCount: number, edgeCount: number, generated: string }>}
|
|
57
|
-
*/
|
|
58
|
-
async function runBuild(config) {
|
|
59
|
-
const builderPath = resolveBuilderPath();
|
|
60
|
-
if (!fs.existsSync(builderPath)) {
|
|
61
|
-
throw new DependencyError(
|
|
62
|
-
`Atlas builder not found: ${builderPath}\n` +
|
|
63
|
-
"This is a packaging error. Reinstall sdtk-spec-kit."
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const pythonExe = await findPython();
|
|
68
|
-
|
|
69
|
-
const args = [
|
|
70
|
-
builderPath,
|
|
71
|
-
"--project-root",
|
|
72
|
-
config.projectPath,
|
|
73
|
-
"--output-dir",
|
|
74
|
-
config.outputDir,
|
|
75
|
-
];
|
|
76
|
-
|
|
77
|
-
for (const sr of config.scanRoots) {
|
|
78
|
-
args.push("--scan-root", sr);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
for (const ex of config.excludes) {
|
|
82
|
-
args.push("--exclude", ex);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (config.verbose) {
|
|
86
|
-
args.push("--verbose");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return new Promise((resolve, reject) => {
|
|
90
|
-
const child = spawn(pythonExe, args, {
|
|
91
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
let stdout = "";
|
|
95
|
-
let stderr = "";
|
|
96
|
-
|
|
97
|
-
child.stdout.on("data", (chunk) => {
|
|
98
|
-
const text = chunk.toString();
|
|
99
|
-
stdout += text;
|
|
100
|
-
// Stream output unless silent
|
|
101
|
-
process.stdout.write(text);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
child.stderr.on("data", (chunk) => {
|
|
105
|
-
const text = chunk.toString();
|
|
106
|
-
stderr += text;
|
|
107
|
-
process.stderr.write(text);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
child.on("error", (err) => {
|
|
111
|
-
if (err.code === "ENOENT") {
|
|
112
|
-
reject(
|
|
113
|
-
new DependencyError(
|
|
114
|
-
`Python executable not found: ${pythonExe}\n` +
|
|
115
|
-
"Install Python 3.8+ and ensure it is in PATH."
|
|
116
|
-
)
|
|
117
|
-
);
|
|
118
|
-
} else {
|
|
119
|
-
reject(new CliError(`Failed to start Atlas builder: ${err.message}`));
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
child.on("close", (code) => {
|
|
124
|
-
if (code !== 0) {
|
|
125
|
-
reject(
|
|
126
|
-
new CliError(
|
|
127
|
-
`Atlas build failed (exit code ${code}).\n` +
|
|
128
|
-
(stderr ? `Builder error output:\n${stderr}` : "See output above for details.")
|
|
129
|
-
)
|
|
130
|
-
);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Parse result JSON from stdout line
|
|
135
|
-
const resultLine = stdout.split("\n").find((l) => l.startsWith("[atlas:result] "));
|
|
136
|
-
if (resultLine) {
|
|
137
|
-
try {
|
|
138
|
-
const result = JSON.parse(resultLine.replace("[atlas:result] ", ""));
|
|
139
|
-
resolve({
|
|
140
|
-
docCount: result.doc_count || 0,
|
|
141
|
-
nodeCount: result.node_count || 0,
|
|
142
|
-
edgeCount: result.edge_count || 0,
|
|
143
|
-
generated: result.generated || "",
|
|
144
|
-
});
|
|
145
|
-
return;
|
|
146
|
-
} catch (_) {
|
|
147
|
-
// fall through to defaults
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
resolve({ docCount: 0, nodeCount: 0, edgeCount: 0, generated: "" });
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Check whether a port is in use (server is listening).
|
|
158
|
-
*
|
|
159
|
-
* @param {string} host
|
|
160
|
-
* @param {number} port
|
|
161
|
-
* @returns {Promise<boolean>}
|
|
162
|
-
*/
|
|
163
|
-
function isPortOpen(host, port) {
|
|
164
|
-
return new Promise((resolve) => {
|
|
165
|
-
const socket = net.createConnection({ host, port, timeout: 500 });
|
|
166
|
-
socket.once("connect", () => {
|
|
167
|
-
socket.destroy();
|
|
168
|
-
resolve(true);
|
|
169
|
-
});
|
|
170
|
-
socket.once("error", () => resolve(false));
|
|
171
|
-
socket.once("timeout", () => {
|
|
172
|
-
socket.destroy();
|
|
173
|
-
resolve(false);
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Wait for the local server to become reachable.
|
|
180
|
-
*
|
|
181
|
-
* @param {string} host
|
|
182
|
-
* @param {number} port
|
|
183
|
-
* @returns {Promise<void>}
|
|
184
|
-
*/
|
|
185
|
-
async function waitForServer(host, port) {
|
|
186
|
-
for (let i = 0; i < HEALTH_CHECK_RETRIES; i++) {
|
|
187
|
-
const ok = await isPortOpen(host, port);
|
|
188
|
-
if (ok) return;
|
|
189
|
-
await new Promise((r) => setTimeout(r, HEALTH_CHECK_INTERVAL_MS));
|
|
190
|
-
}
|
|
191
|
-
throw new CliError(
|
|
192
|
-
`Atlas viewer server did not start on http://${host}:${port}\n` +
|
|
193
|
-
"Try passing a different --port if the port is occupied."
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Probe a local Atlas endpoint and capture its HTTP status.
|
|
199
|
-
*
|
|
200
|
-
* @param {string} url
|
|
201
|
-
* @returns {Promise<{ ok: boolean, statusCode: number, body: string }>}
|
|
202
|
-
*/
|
|
203
|
-
function probeUrl(url) {
|
|
204
|
-
return new Promise((resolve) => {
|
|
205
|
-
let target;
|
|
206
|
-
try {
|
|
207
|
-
target = new URL(url);
|
|
208
|
-
} catch (_) {
|
|
209
|
-
resolve({ ok: false, statusCode: 0, body: "" });
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const transport = target.protocol === "https:" ? https : http;
|
|
214
|
-
const req = transport.request(
|
|
215
|
-
target,
|
|
216
|
-
{ method: "GET", timeout: 1200 },
|
|
217
|
-
(res) => {
|
|
218
|
-
let body = "";
|
|
219
|
-
res.setEncoding("utf8");
|
|
220
|
-
res.on("data", (chunk) => {
|
|
221
|
-
if (body.length < 512) {
|
|
222
|
-
body += chunk.slice(0, 512 - body.length);
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
res.on("end", () => {
|
|
226
|
-
const statusCode = res.statusCode || 0;
|
|
227
|
-
resolve({
|
|
228
|
-
ok: statusCode >= 200 && statusCode < 300,
|
|
229
|
-
statusCode,
|
|
230
|
-
body,
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
);
|
|
235
|
-
|
|
236
|
-
req.on("timeout", () => {
|
|
237
|
-
req.destroy();
|
|
238
|
-
resolve({ ok: false, statusCode: 0, body: "" });
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
req.on("error", () => {
|
|
242
|
-
resolve({ ok: false, statusCode: 0, body: "" });
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
req.end();
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Check whether an existing server on the requested port is a usable Atlas server.
|
|
251
|
-
*
|
|
252
|
-
* @param {string} host
|
|
253
|
-
* @param {number} port
|
|
254
|
-
* @param {string} viewerUrl
|
|
255
|
-
* @returns {Promise<{ reusable: boolean, health: { ok: boolean, statusCode: number }, viewer: { ok: boolean, statusCode: number } }>}
|
|
256
|
-
*/
|
|
257
|
-
async function probeExistingAtlasServer(host, port, viewerUrl) {
|
|
258
|
-
const health = await probeUrl(`http://${host}:${port}/api/health`);
|
|
259
|
-
const viewer = await probeUrl(`${viewerUrl}?embedded=1&probe=1`);
|
|
260
|
-
return {
|
|
261
|
-
reusable: health.ok && viewer.ok,
|
|
262
|
-
health: { ok: health.ok, statusCode: health.statusCode },
|
|
263
|
-
viewer: { ok: viewer.ok, statusCode: viewer.statusCode },
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Start a lightweight Node.js static file server for the atlas output dir.
|
|
269
|
-
* Binds to loopback by default. Prevents path traversal outside outputDir/projectPath.
|
|
270
|
-
*
|
|
271
|
-
* @param {string} host
|
|
272
|
-
* @param {number} port
|
|
273
|
-
* @param {string} outputDir - Directory to serve static atlas files from.
|
|
274
|
-
* @param {string} projectPath - Project root for /api/note path traversal bound.
|
|
275
|
-
* @returns {Promise<http.Server>}
|
|
276
|
-
*/
|
|
277
|
-
function startAtlasServer(host, port, outputDir, projectPath) {
|
|
278
|
-
return new Promise((resolve, reject) => {
|
|
279
|
-
const MIME_TYPES = {
|
|
280
|
-
".html": "text/html; charset=utf-8",
|
|
281
|
-
".js": "application/javascript; charset=utf-8",
|
|
282
|
-
".json": "application/json; charset=utf-8",
|
|
283
|
-
".css": "text/css; charset=utf-8",
|
|
284
|
-
".md": "text/plain; charset=utf-8",
|
|
285
|
-
".txt": "text/plain; charset=utf-8",
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
const server = http.createServer((req, res) => {
|
|
289
|
-
const url = req.url || "/";
|
|
290
|
-
|
|
291
|
-
// Health check endpoint
|
|
292
|
-
if (url === "/api/health" || url === "/api/health/") {
|
|
293
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
294
|
-
res.end(JSON.stringify({ ok: true }));
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// Serve full note content with path traversal protection
|
|
299
|
-
if (url.startsWith("/api/note")) {
|
|
300
|
-
const qs = new URL(url, `http://${host}:${port}`).searchParams;
|
|
301
|
-
const notePath = qs.get("path");
|
|
302
|
-
if (!notePath) {
|
|
303
|
-
res.writeHead(400, { "Content-Type": "application/json" });
|
|
304
|
-
res.end(JSON.stringify({ error: "Missing path parameter" }));
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// Path traversal protection: resolve and verify it stays within projectPath
|
|
309
|
-
const projectRoot = projectPath || outputDir;
|
|
310
|
-
const resolved = path.resolve(projectRoot, notePath);
|
|
311
|
-
if (!resolved.startsWith(projectRoot + path.sep) && resolved !== projectRoot) {
|
|
312
|
-
res.writeHead(403, { "Content-Type": "application/json" });
|
|
313
|
-
res.end(JSON.stringify({ error: "Access denied" }));
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (!fs.existsSync(resolved)) {
|
|
318
|
-
res.writeHead(404, { "Content-Type": "application/json" });
|
|
319
|
-
res.end(JSON.stringify({ error: "Not found" }));
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
try {
|
|
324
|
-
const content = fs.readFileSync(resolved, "utf-8");
|
|
325
|
-
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
|
|
326
|
-
res.end(content);
|
|
327
|
-
} catch (err) {
|
|
328
|
-
res.writeHead(500, { "Content-Type": "application/json" });
|
|
329
|
-
res.end(JSON.stringify({ error: "Read error" }));
|
|
330
|
-
}
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// Static files from outputDir
|
|
335
|
-
let filePath = url.split("?")[0];
|
|
336
|
-
if (filePath === "/" || filePath === "") {
|
|
337
|
-
filePath = "/viewer.html";
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
const resolved = path.resolve(outputDir, filePath.replace(/^\/+/, ""));
|
|
341
|
-
// Path traversal protection: must stay inside outputDir
|
|
342
|
-
if (!resolved.startsWith(outputDir + path.sep) && resolved !== outputDir) {
|
|
343
|
-
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
344
|
-
res.end("403 Forbidden");
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
if (!fs.existsSync(resolved) || fs.statSync(resolved).isDirectory()) {
|
|
349
|
-
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
350
|
-
res.end("404 Not Found");
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
const ext = path.extname(resolved).toLowerCase();
|
|
355
|
-
const contentType = MIME_TYPES[ext] || "application/octet-stream";
|
|
356
|
-
|
|
357
|
-
try {
|
|
358
|
-
const data = fs.readFileSync(resolved);
|
|
359
|
-
res.writeHead(200, { "Content-Type": contentType });
|
|
360
|
-
res.end(data);
|
|
361
|
-
} catch (err) {
|
|
362
|
-
res.writeHead(500, { "Content-Type": "text/plain" });
|
|
363
|
-
res.end("500 Internal Server Error");
|
|
364
|
-
}
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
server.once("error", (err) => {
|
|
368
|
-
if (err.code === "EADDRINUSE") {
|
|
369
|
-
reject(
|
|
370
|
-
new CliError(
|
|
371
|
-
`Port ${port} is already in use on ${host}.\n` +
|
|
372
|
-
"Pass --port <number> to use a different port, or stop the process using that port."
|
|
373
|
-
)
|
|
374
|
-
);
|
|
375
|
-
} else {
|
|
376
|
-
reject(new CliError(`Atlas server error: ${err.message}`));
|
|
377
|
-
}
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
server.listen(port, host, () => {
|
|
381
|
-
resolve(server);
|
|
382
|
-
});
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Open or reuse the local Atlas viewer server and optionally launch a browser.
|
|
388
|
-
*
|
|
389
|
-
* @param {Object} config - Resolved AtlasConfig.
|
|
390
|
-
* @param {boolean} noOpen - If true, skip browser launch.
|
|
391
|
-
* @returns {Promise<{ url: string, server: http.Server | null }>}
|
|
392
|
-
*/
|
|
393
|
-
async function openViewer(config, noOpen = false) {
|
|
394
|
-
const { host, port, outputDir } = config;
|
|
395
|
-
const viewerUrl = `http://${host}:${port}/viewer.html`;
|
|
396
|
-
|
|
397
|
-
// Check if a server is already running on this port
|
|
398
|
-
const alreadyRunning = await isPortOpen(host, port);
|
|
399
|
-
|
|
400
|
-
let server = null;
|
|
401
|
-
if (!alreadyRunning) {
|
|
402
|
-
console.log(`[atlas] Starting local atlas viewer on http://${host}:${port} ...`);
|
|
403
|
-
server = await startAtlasServer(host, port, outputDir, config.projectPath);
|
|
404
|
-
await waitForServer(host, port);
|
|
405
|
-
console.log(`[atlas] Viewer server ready: ${viewerUrl}`);
|
|
406
|
-
} else {
|
|
407
|
-
const probe = await probeExistingAtlasServer(host, port, viewerUrl);
|
|
408
|
-
if (!probe.reusable) {
|
|
409
|
-
throw new CliError(
|
|
410
|
-
`Port ${port} is already occupied by an incompatible Atlas server.\n` +
|
|
411
|
-
` Health endpoint status: ${probe.health.statusCode || "unreachable"}\n` +
|
|
412
|
-
` Viewer endpoint status: ${probe.viewer.statusCode || "unreachable"}\n` +
|
|
413
|
-
"Stop the existing process on that port, then rerun the command, or pass --port <number>."
|
|
414
|
-
);
|
|
415
|
-
}
|
|
416
|
-
console.log(`[atlas] Reusing existing server at http://${host}:${port}`);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
if (!noOpen) {
|
|
420
|
-
console.log("[atlas] Opening viewer in default browser...");
|
|
421
|
-
await openBrowser(viewerUrl);
|
|
422
|
-
} else {
|
|
423
|
-
console.log(`[atlas] Viewer URL: ${viewerUrl}`);
|
|
424
|
-
console.log("[atlas] --no-open specified; skipping browser launch.");
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
return { url: viewerUrl, server };
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
module.exports = {
|
|
431
|
-
runBuild,
|
|
432
|
-
openViewer,
|
|
433
|
-
startAtlasServer,
|
|
434
|
-
findPython,
|
|
435
|
-
};
|
package/src/lib/browser-open.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { execFile } = require("child_process");
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Open a URL in the default system browser.
|
|
7
|
-
* Cross-platform: Windows, macOS, Linux.
|
|
8
|
-
*
|
|
9
|
-
* @param {string} url
|
|
10
|
-
* @returns {Promise<void>}
|
|
11
|
-
*/
|
|
12
|
-
function openBrowser(url) {
|
|
13
|
-
return new Promise((resolve) => {
|
|
14
|
-
let cmd;
|
|
15
|
-
let args;
|
|
16
|
-
|
|
17
|
-
if (process.platform === "win32") {
|
|
18
|
-
// On Windows: cmd /c start "" <url>
|
|
19
|
-
cmd = "cmd";
|
|
20
|
-
args = ["/c", "start", "", url];
|
|
21
|
-
} else if (process.platform === "darwin") {
|
|
22
|
-
cmd = "open";
|
|
23
|
-
args = [url];
|
|
24
|
-
} else {
|
|
25
|
-
cmd = "xdg-open";
|
|
26
|
-
args = [url];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
execFile(cmd, args, { windowsHide: true }, (err) => {
|
|
30
|
-
if (err) {
|
|
31
|
-
// Non-fatal: browser open failure should not crash the CLI
|
|
32
|
-
console.error(`[atlas] Warning: could not open browser: ${err.message}`);
|
|
33
|
-
}
|
|
34
|
-
resolve();
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
module.exports = { openBrowser };
|