@linemap/studio 0.0.0-stage → 0.2.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/index.mjs ADDED
@@ -0,0 +1,33 @@
1
+ // The linemap studio as a ready-made page: dist/linemap.html, built once by build.mjs
2
+ // (before publishing), here only filled in. Nothing in this file needs a bundler.
3
+ // studioHtml(flows) the page with these flows baked in ([] = the empty studio)
4
+ // embed(html, flows) the same, on a page you already have as a string
5
+ // serve({ root, ... }) a local server: the studio at /studio.html and the DOT files under root
6
+ // announce, openBrowser what `linemap studio` prints, and --open
7
+ import fs from 'node:fs';
8
+
9
+ export { serve, dotFiles, announce, openBrowser } from './serve.mjs';
10
+
11
+ export const TEMPLATE = new URL('./dist/linemap.html', import.meta.url);
12
+
13
+ // The flows' slot in the page, empty; it comes before the script, so the first match is the slot.
14
+ const SLOT = '<script type="application/json" id="linemap-flows">[]</script>';
15
+
16
+ // flows: [{ name, text }]. `<` is escaped in the JSON, so a flow cannot end the script element.
17
+ export function embed(html, flows = []) {
18
+ const at = html.indexOf(SLOT);
19
+ if (at < 0) throw new Error('not a linemap studio page: no empty flows slot');
20
+ const json = JSON.stringify(flows.map(({ name, text }) => ({ name, text }))).replace(/</g, '\\u003c');
21
+ return html.slice(0, at) + SLOT.replace('[]', json) + html.slice(at + SLOT.length);
22
+ }
23
+
24
+ export function studioHtml(flows = []) {
25
+ let html;
26
+ try {
27
+ html = fs.readFileSync(TEMPLATE, 'utf8');
28
+ } catch (e) {
29
+ if (e.code !== 'ENOENT') throw e;
30
+ throw new Error('the studio page is not built (in the linemap repository: pnpm build)');
31
+ }
32
+ return embed(html, flows);
33
+ }
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@linemap/studio",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "The linemap studio as one self-contained HTML file, and the few lines that bake DOT flows into it. The CLI (`linemap export`, `linemap studio`) uses this.",
5
+ "keywords": [
6
+ "flowchart",
7
+ "diagram",
8
+ "transit-map",
9
+ "metro-map",
10
+ "dot",
11
+ "graphviz",
12
+ "svg",
13
+ "html"
14
+ ],
15
+ "license": "MPL-2.0",
16
+ "homepage": "https://github.com/mergezalot/linemap#readme",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/mergezalot/linemap.git",
20
+ "directory": "packages/studio"
21
+ },
22
+ "bugs": "https://github.com/mergezalot/linemap/issues",
23
+ "type": "module",
24
+ "exports": "./index.mjs",
25
+ "files": [
26
+ "index.mjs",
27
+ "serve.mjs",
28
+ "dist/linemap.html"
29
+ ],
30
+ "engines": {
31
+ "node": ">=22"
32
+ },
33
+ "devDependencies": {
34
+ "@linemap/core": "0.2.0",
35
+ "esbuild": "0.28.2",
36
+ "playwright": "1.65.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "scripts": {
42
+ "build": "node build.mjs",
43
+ "test": "node build.mjs && node test/smoke.mjs"
44
+ }
6
45
  }
package/serve.mjs ADDED
@@ -0,0 +1,90 @@
1
+ // A local server for the studio: the page at /studio.html (/ redirects there, keeping ?src=) and the
2
+ // DOT files (.dot, .gv) under root, nothing else. `page` returns the HTML on every request, so a
3
+ // caller can rebuild it in between. Listens on 127.0.0.1 only; port 0 = any free port.
4
+ // Resolves with the listening server, rejects if it cannot listen (e.g. EADDRINUSE).
5
+ import fs from 'node:fs';
6
+ import { spawn } from 'node:child_process';
7
+ import http from 'node:http';
8
+ import path from 'node:path';
9
+
10
+ const DOT = /\.(dot|gv)$/i;
11
+
12
+ export function serve({ root, page, port = 8000, onError = console.error }) {
13
+ root = path.resolve(root);
14
+ const server = http.createServer(async (req, res) => {
15
+ const url = new URL(req.url, 'http://x');
16
+ try {
17
+ if (url.pathname === '/') {
18
+ res.writeHead(302, { location: '/studio.html' + url.search });
19
+ return res.end();
20
+ }
21
+ let body, type;
22
+ if (url.pathname === '/studio.html') {
23
+ body = await page();
24
+ type = 'text/html; charset=utf-8';
25
+ } else {
26
+ const file = path.join(root, decodeURIComponent(url.pathname));
27
+ if (!file.startsWith(root + path.sep) || !DOT.test(file)) throw Object.assign(new Error('not served'), { code: 'ENOENT' });
28
+ body = fs.readFileSync(file);
29
+ type = 'text/plain; charset=utf-8';
30
+ }
31
+ res.writeHead(200, {
32
+ 'content-type': type,
33
+ 'cache-control': 'no-store', // so "watch" sees every change
34
+ });
35
+ res.end(req.method === 'HEAD' ? undefined : body);
36
+ } catch (err) {
37
+ const missing = err.code === 'ENOENT' || err.code === 'EISDIR' || err instanceof URIError;
38
+ res.writeHead(missing ? 404 : 500, { 'content-type': 'text/plain; charset=utf-8' });
39
+ res.end(missing ? `not found: ${url.pathname}\n` : `${err.message}\n`);
40
+ if (!missing) onError(err);
41
+ }
42
+ });
43
+ return new Promise((resolve, reject) => {
44
+ server.once('error', reject);
45
+ server.listen(port, '127.0.0.1', () => {
46
+ server.off('error', reject);
47
+ resolve(server);
48
+ });
49
+ });
50
+ }
51
+
52
+ // What a serving command prints: where it serves, the studio's URL, and one ?src= link per DOT file.
53
+ export function announce(server, root) {
54
+ const base = `http://localhost:${server.address().port}/studio.html`;
55
+ const files = dotFiles(root);
56
+ const src = (f) => `${base}?src=${f.split('/').map(encodeURIComponent).join('/')}`;
57
+ return {
58
+ text: [`serving the studio and the DOT files in ${path.resolve(root)} (Ctrl+C stops)`, base, ...files.map(src)].join('\n '),
59
+ // the one to open: the only flow, or the start screen
60
+ url: files.length === 1 ? src(files[0]) : base,
61
+ };
62
+ }
63
+
64
+ // Opens a URL in the default browser; quietly does nothing where that fails.
65
+ export function openBrowser(url) {
66
+ const [cmd, args] = process.platform === 'darwin' ? ['open', [url]]
67
+ : process.platform === 'win32' ? ['cmd', ['/c', 'start', '', url]]
68
+ : ['xdg-open', [url]];
69
+ try { spawn(cmd, args, { stdio: 'ignore', detached: true }).on('error', () => {}).unref(); } catch { /* no browser */ }
70
+ }
71
+
72
+ // DOT files under root, relative and with / separators, for listing ?src= links. Skips hidden
73
+ // folders and node_modules; stops at `max`.
74
+ export function dotFiles(root, max = 20) {
75
+ const found = [];
76
+ const walk = (dir, rel) => {
77
+ let entries;
78
+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
79
+ entries.sort((a, b) => a.name.localeCompare(b.name));
80
+ for (const e of entries) {
81
+ if (found.length >= max) return;
82
+ if (e.name.startsWith('.') || e.name === 'node_modules') continue;
83
+ const r = rel ? `${rel}/${e.name}` : e.name;
84
+ if (e.isDirectory()) walk(path.join(dir, e.name), r);
85
+ else if (DOT.test(e.name)) found.push(r);
86
+ }
87
+ };
88
+ walk(path.resolve(root), '');
89
+ return found;
90
+ }