@bigmistqke/workpad 0.20.0 → 0.20.1
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 +11 -7
- package/bin/workpad.ts +6 -0
- package/dist/bin/workpad.js +5 -0
- package/dist/plugins/claude/doctor.js +15 -5
- package/dist/src/entries.js +6 -2
- package/dist/src/files.js +26 -3
- package/dist/src/hub.js +6 -3
- package/dist/src/loader.js +4 -4
- package/dist/src/server.js +45 -20
- package/package.json +1 -1
- package/plugins/claude/doctor.ts +11 -3
- package/src/entries.ts +18 -7
- package/src/files.ts +28 -2
- package/src/hub.ts +16 -8
- package/src/loader.ts +6 -6
- package/src/server.ts +71 -29
package/README.md
CHANGED
|
@@ -159,10 +159,11 @@ gets `-2`. The server looks for projects again every few seconds, so a new
|
|
|
159
159
|
worktree with a `.workpad/` shows up, and a removed one goes, without a
|
|
160
160
|
restart.
|
|
161
161
|
|
|
162
|
-
An entry's `
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
162
|
+
An entry's `files` is the default `files` of each of its projects, with a
|
|
163
|
+
config and without: `"files": false` serves none of their own files and
|
|
164
|
+
watches only their `.workpad/`, and `"files": { "include": [...], "exclude":
|
|
165
|
+
[...] }` says which, as in a project's config (below). A project's config
|
|
166
|
+
that sets `files` still has its way. Left out, a project serves its files, as
|
|
166
167
|
without the machine config.
|
|
167
168
|
|
|
168
169
|
- **`/` is the overview of every project,** as a project's own overview is of
|
|
@@ -227,7 +228,8 @@ without the machine config.
|
|
|
227
228
|
`url --full`). `$WORKPAD_URL` does the same for a project's own server
|
|
228
229
|
started with it. By default `http://localhost:<port>`.
|
|
229
230
|
- `"files": false` on an entry (see above) keeps its projects from
|
|
230
|
-
watching more than their `.workpad
|
|
231
|
+
watching more than their `.workpad/`; `"files": { "exclude": ["**/dist"]
|
|
232
|
+
}` leaves out what nothing needs to show. A page's own folder is watched
|
|
231
233
|
besides, once it is opened, so a page in a folder made and written into
|
|
232
234
|
at once (which a container's mount may say nothing of) still goes live.
|
|
233
235
|
- **A project per checkout.** A folder is a project once it has a
|
|
@@ -572,8 +574,10 @@ export default defineConfig({
|
|
|
572
574
|
```
|
|
573
575
|
|
|
574
576
|
Without `include`, all files are shown but what `exclude` names.
|
|
575
|
-
`node_modules` and `.git` are always left out, at any depth
|
|
576
|
-
watched.
|
|
577
|
+
`node_modules` and `.git` are always left out, at any depth (in folders
|
|
578
|
+
whose name starts with a dot too), and never watched. On Linux each folder
|
|
579
|
+
that can hold something shown is watched on its own, so what is left out
|
|
580
|
+
costs no watches; `WORKPAD_WATCH=folders` does the same elsewhere.
|
|
577
581
|
|
|
578
582
|
## Building a site
|
|
579
583
|
|
package/bin/workpad.ts
CHANGED
|
@@ -195,6 +195,12 @@ async function ensureServer(): Promise<string> {
|
|
|
195
195
|
)
|
|
196
196
|
return base
|
|
197
197
|
}
|
|
198
|
+
// the first ask timed out and this one was answered: the server is slow (busy), not missing the project
|
|
199
|
+
const mine = server?.projects.find(p => p.dir === proj.dir)
|
|
200
|
+
if (server && mine) {
|
|
201
|
+
console.error(`workpad: the shared server at ${server.origin} was slow to answer; it serves this project`)
|
|
202
|
+
return `${server.origin}${mine.base}`
|
|
203
|
+
}
|
|
198
204
|
const at = recordedOrigin(machineServerFile())
|
|
199
205
|
if (!server && at && (await stuckAt(at))) throw new Error(stuck(at, 'its login service log'))
|
|
200
206
|
throw new Error(
|
package/dist/bin/workpad.js
CHANGED
|
@@ -140,6 +140,11 @@ async function ensureServer() {
|
|
|
140
140
|
if (server && compareVersions(VERSION, server.version) > 0) console.error(`workpad: the shared server runs ${server.version}, this is ${VERSION}; restart it to upgrade (\`workpad serve\` takes it over)`);
|
|
141
141
|
return base;
|
|
142
142
|
}
|
|
143
|
+
const mine = server?.projects.find((p) => p.dir === proj.dir);
|
|
144
|
+
if (server && mine) {
|
|
145
|
+
console.error(`workpad: the shared server at ${server.origin} was slow to answer; it serves this project`);
|
|
146
|
+
return `${server.origin}${mine.base}`;
|
|
147
|
+
}
|
|
143
148
|
const at = recordedOrigin(machineServerFile());
|
|
144
149
|
if (!server && at && await stuckAt(at)) throw new Error(stuck(at, "its login service log"));
|
|
145
150
|
throw new Error(server ? `the shared server at ${server.origin} does not serve ${proj.dir}; is it in an include of ${machineDir()}/config.json?` : `the shared server is not running. It is started outside the agents' sandboxes: as a login service, or with \`workpad serve\` in a terminal, outside any project`);
|
|
@@ -18,9 +18,15 @@ function readJson(file) {
|
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* `$VAR`, `${VAR}` and `${VAR:-default}` in a command, its args or env as Claude Code expands them
|
|
23
|
+
* (the default when VAR is unset or empty); CLAUDE_PROJECT_DIR is the project.
|
|
24
|
+
*/
|
|
22
25
|
function expand(s, env) {
|
|
23
|
-
return s.replace(/\$\{(\w+)
|
|
26
|
+
return s.replace(/\$\{(\w+)(?::-([^}]*))?\}|\$(\w+)/g, (_, a, fallback, b) => {
|
|
27
|
+
const value = env[a ?? b];
|
|
28
|
+
return fallback !== void 0 && !value ? fallback : value ?? "";
|
|
29
|
+
});
|
|
24
30
|
}
|
|
25
31
|
/** Start the channel's command, ask it to initialize, and stop it: whether it answered, or why not. */
|
|
26
32
|
function probe(command, args, cwd, env) {
|
|
@@ -182,10 +188,14 @@ async function channelChecks(pages, claude, home = os.homedir()) {
|
|
|
182
188
|
fix: install
|
|
183
189
|
});
|
|
184
190
|
else {
|
|
185
|
-
const
|
|
191
|
+
const base = {
|
|
186
192
|
...process.env,
|
|
187
|
-
CLAUDE_PROJECT_DIR: dir
|
|
188
|
-
|
|
193
|
+
CLAUDE_PROJECT_DIR: dir
|
|
194
|
+
};
|
|
195
|
+
const own = Object.entries(entry.env ?? {}).map(([k, v]) => [k, expand(String(v), base)]);
|
|
196
|
+
const env = {
|
|
197
|
+
...base,
|
|
198
|
+
...Object.fromEntries(own)
|
|
189
199
|
};
|
|
190
200
|
const why = await probe(expand(entry.command, env), (entry.args ?? []).map((a) => expand(a, env)), dir, env);
|
|
191
201
|
const shown = [entry.command, ...entry.args ?? []].join(" ");
|
package/dist/src/entries.js
CHANGED
|
@@ -69,11 +69,15 @@ function readMachineConfig(dir = machineDir()) {
|
|
|
69
69
|
if (!Array.isArray(v) || v.some((x) => typeof x !== "string")) throw new Error(`${file}: entries.${name}.${what} must be a list of globs`);
|
|
70
70
|
return v;
|
|
71
71
|
};
|
|
72
|
-
|
|
72
|
+
const files = e?.files;
|
|
73
|
+
if (files != null && typeof files !== "boolean" && (typeof files !== "object" || Array.isArray(files))) throw new Error(`${file}: entries.${name}.files must be true, false or { "include": [...], "exclude": [...] }`);
|
|
73
74
|
entries[name] = {
|
|
74
75
|
include: list(e?.include, "include"),
|
|
75
76
|
exclude: list(e?.exclude, "exclude"),
|
|
76
|
-
...
|
|
77
|
+
...files != null && { files: typeof files === "boolean" ? files : {
|
|
78
|
+
...files.include != null && { include: list(files.include, "files.include") },
|
|
79
|
+
...files.exclude != null && { exclude: list(files.exclude, "files.exclude") }
|
|
80
|
+
} }
|
|
77
81
|
};
|
|
78
82
|
}
|
|
79
83
|
const port = raw.port == null ? void 0 : Number(raw.port);
|
package/dist/src/files.js
CHANGED
|
@@ -2,7 +2,30 @@ import path from "node:path";
|
|
|
2
2
|
// src/files.ts
|
|
3
3
|
/** Left out always: what a repository keeps for its tools. */
|
|
4
4
|
const ALWAYS_EXCLUDED = ["**/node_modules", "**/.git"];
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Does `rel` match `g`? As `path.posix.matchesGlob`, but a `**` takes in folders whose name starts
|
|
7
|
+
* with a dot as well: matchesGlob's doesn't, so `**\/node_modules` left out nothing under `.claude/`,
|
|
8
|
+
* and an install in a worktree kept there reached the watcher as thousands of changes. Segment by
|
|
9
|
+
* segment: `**` stands for any number of folders, every other segment is matched by matchesGlob.
|
|
10
|
+
*/
|
|
11
|
+
function glob(rel, g) {
|
|
12
|
+
const parts = g.split("/");
|
|
13
|
+
const segs = rel.split("/");
|
|
14
|
+
const seen = /* @__PURE__ */ new Set();
|
|
15
|
+
const at = (i, j) => {
|
|
16
|
+
if (i === parts.length) return j === segs.length;
|
|
17
|
+
const key = `${i},${j}`;
|
|
18
|
+
if (seen.has(key)) return false;
|
|
19
|
+
seen.add(key);
|
|
20
|
+
if (parts[i] === "**") {
|
|
21
|
+
for (let k = j; k <= segs.length; k++) if (at(i + 1, k)) return true;
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return j < segs.length && path.posix.matchesGlob(segs[j], parts[i]) && at(i + 1, j + 1);
|
|
25
|
+
};
|
|
26
|
+
return at(0, 0);
|
|
27
|
+
}
|
|
28
|
+
const ALWAYS_NAMES = new Set(ALWAYS_EXCLUDED.map((g) => g.slice(3)));
|
|
6
29
|
/** The path and each folder above it: `a/b/c` → `a/b/c`, `a/b`, `a`. */
|
|
7
30
|
function* upward(rel) {
|
|
8
31
|
for (let p = rel; p && p !== "."; p = path.posix.dirname(p)) yield p;
|
|
@@ -28,7 +51,7 @@ function fileFilter(files) {
|
|
|
28
51
|
const config = typeof files === "object" && files ? files : {};
|
|
29
52
|
const exclude = [...ALWAYS_EXCLUDED, ...config.exclude ?? []];
|
|
30
53
|
const include = config.include?.length ? config.include : null;
|
|
31
|
-
const excluded = (rel) => [...upward(rel)].some((p) => exclude.some((g) => glob(p, g)));
|
|
54
|
+
const excluded = (rel) => rel.split("/").some((s) => ALWAYS_NAMES.has(s)) || [...upward(rel)].some((p) => exclude.some((g) => glob(p, g)));
|
|
32
55
|
const included = (rel) => !include || [...upward(rel)].some((p) => include.some((g) => glob(p, g)));
|
|
33
56
|
return {
|
|
34
57
|
key: JSON.stringify({
|
|
@@ -44,4 +67,4 @@ function fileFilter(files) {
|
|
|
44
67
|
}
|
|
45
68
|
};
|
|
46
69
|
}
|
|
47
|
-
export { ALWAYS_EXCLUDED, fileFilter };
|
|
70
|
+
export { ALWAYS_EXCLUDED, fileFilter, glob };
|
package/dist/src/hub.js
CHANGED
|
@@ -46,7 +46,7 @@ function createHub({ dir = machineDir(), state = stateDir(), home, interval, onS
|
|
|
46
46
|
};
|
|
47
47
|
function sync(projects) {
|
|
48
48
|
setImmediate(() => touch());
|
|
49
|
-
const wanted = new Map(projects.map((p) => [`${p.base}\n${p.dir}\n${p.files ??
|
|
49
|
+
const wanted = new Map(projects.map((p) => [`${p.base}\n${p.dir}\n${JSON.stringify(p.files ?? null)}`, p]));
|
|
50
50
|
for (const [key, s] of served) if (!wanted.has(key)) {
|
|
51
51
|
for (const el of entryListens) release(s, el);
|
|
52
52
|
for (const ul of unfollowedListens) letGo(s, ul);
|
|
@@ -99,14 +99,17 @@ function createHub({ dir = machineDir(), state = stateDir(), home, interval, onS
|
|
|
99
99
|
});
|
|
100
100
|
let touching;
|
|
101
101
|
const changes = /* @__PURE__ */ new Set();
|
|
102
|
+
let toldAt = 0;
|
|
102
103
|
const touch = () => {
|
|
103
|
-
|
|
104
|
+
if (touching) return;
|
|
104
105
|
touching = setTimeout(() => {
|
|
106
|
+
touching = void 0;
|
|
107
|
+
toldAt = Date.now();
|
|
105
108
|
root.touch();
|
|
106
109
|
const from = [...changes];
|
|
107
110
|
changes.clear();
|
|
108
111
|
for (const s of served.values()) if (from.some((b) => b !== s.base)) s.server.elsewhere();
|
|
109
|
-
}, 150);
|
|
112
|
+
}, Math.max(150, toldAt + 1e3 - Date.now()));
|
|
110
113
|
};
|
|
111
114
|
const changed = (base) => {
|
|
112
115
|
changes.add(base);
|
package/dist/src/loader.js
CHANGED
|
@@ -87,8 +87,8 @@ function checkPlugin(e, where) {
|
|
|
87
87
|
*/
|
|
88
88
|
const RETRY_FAILED_MS = 5e3;
|
|
89
89
|
/**
|
|
90
|
-
* `files`:
|
|
91
|
-
* entry can say
|
|
90
|
+
* `files`: which of its own files the project serves when its config doesn't say (a machine config's
|
|
91
|
+
* entry can say it for all of its projects; see EntryConfig).
|
|
92
92
|
*/
|
|
93
93
|
function createConfigLoader(proj, { pages: withPages = true, files: filesDefault = true } = {}) {
|
|
94
94
|
let cached = null;
|
|
@@ -140,8 +140,8 @@ function createConfigLoader(proj, { pages: withPages = true, files: filesDefault
|
|
|
140
140
|
forReader: new Map([...declared("forReader")].map((type) => [type, requires.get(type) ?? []])),
|
|
141
141
|
requires,
|
|
142
142
|
publicDirs,
|
|
143
|
-
files: config?.files
|
|
144
|
-
fileFilter: fileFilter(config?.files),
|
|
143
|
+
files: (config?.files ?? filesDefault) !== false,
|
|
144
|
+
fileFilter: fileFilter(config?.files ?? filesDefault),
|
|
145
145
|
build: config?.build ?? {},
|
|
146
146
|
commands: configCommands(config?.commands, {
|
|
147
147
|
taken: RESERVED,
|
package/dist/src/server.js
CHANGED
|
@@ -22,6 +22,13 @@ import fs from "node:fs";
|
|
|
22
22
|
import path from "node:path";
|
|
23
23
|
import crypto from "node:crypto";
|
|
24
24
|
// src/server.ts
|
|
25
|
+
/**
|
|
26
|
+
* Watch a project's files folder by folder (each that can hold something shown, at any depth), not
|
|
27
|
+
* a recursive watch per top-level folder: where a recursive watch costs a watch per folder below
|
|
28
|
+
* it, whatever it holds (Linux's inotify). `$WORKPAD_WATCH`: `folders` or `recursive`, else by
|
|
29
|
+
* platform. See setFiles.
|
|
30
|
+
*/
|
|
31
|
+
const WATCH_BY_FOLDER = process.env.WORKPAD_WATCH ? process.env.WORKPAD_WATCH === "folders" : process.platform !== "darwin" && process.platform !== "win32";
|
|
25
32
|
const ENGINE_DIR = PACKAGE_DIR;
|
|
26
33
|
const MIME = {
|
|
27
34
|
".css": "text/css",
|
|
@@ -63,7 +70,7 @@ const API_LISTS = [
|
|
|
63
70
|
"subscriptions",
|
|
64
71
|
"agents"
|
|
65
72
|
];
|
|
66
|
-
function createServer(project, { base = "", entry, label, name, files: filesOption, filesDefault, overview, onChange, origins = () => [] } = {}) {
|
|
73
|
+
function createServer(project, { base = "", entry, label, name, files: filesOption, filesDefault, watchByFolder = WATCH_BY_FOLDER, overview, onChange, origins = () => [] } = {}) {
|
|
67
74
|
const projectName = () => name ?? project.name;
|
|
68
75
|
const loader = createConfigLoader(project, { files: filesDefault });
|
|
69
76
|
const auth = createAuth(project.wp, base, origins);
|
|
@@ -645,15 +652,24 @@ function createServer(project, { base = "", entry, label, name, files: filesOpti
|
|
|
645
652
|
let FILES = null;
|
|
646
653
|
let FILTER = fileFilter();
|
|
647
654
|
let projectWatchers = /* @__PURE__ */ new Map();
|
|
655
|
+
let watchRound = 0;
|
|
648
656
|
function setFiles(files, filter = fileFilter()) {
|
|
649
657
|
if (files === FILES && filter.key === FILTER.key) return;
|
|
650
658
|
FILES = files;
|
|
651
659
|
FILTER = filter;
|
|
660
|
+
const round = ++watchRound;
|
|
652
661
|
for (const w of projectWatchers.values()) {
|
|
653
662
|
w.close();
|
|
654
663
|
watchers.delete(w);
|
|
655
664
|
}
|
|
656
665
|
projectWatchers = /* @__PURE__ */ new Map();
|
|
666
|
+
const drop = (key) => {
|
|
667
|
+
for (const [k, w] of projectWatchers) if (k === key || k.startsWith(key + "/")) {
|
|
668
|
+
w.close();
|
|
669
|
+
watchers.delete(w);
|
|
670
|
+
projectWatchers.delete(k);
|
|
671
|
+
}
|
|
672
|
+
};
|
|
657
673
|
const add = (key, dir, recursive, prefix) => {
|
|
658
674
|
try {
|
|
659
675
|
const w = fs.watch(dir, { recursive }, (evt, filename) => {
|
|
@@ -661,32 +677,41 @@ function createServer(project, { base = "", entry, label, name, files: filesOpti
|
|
|
661
677
|
const name = filename.toString().replace(/\\/g, "/");
|
|
662
678
|
fileChanged(prefix + name);
|
|
663
679
|
if (!recursive && evt === "rename") {
|
|
664
|
-
const
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
watchers.delete(old);
|
|
668
|
-
projectWatchers.delete(name);
|
|
669
|
-
}
|
|
670
|
-
watchFolder(name);
|
|
680
|
+
const child = prefix + name;
|
|
681
|
+
drop(child);
|
|
682
|
+
watchFolder(child);
|
|
671
683
|
}
|
|
672
684
|
});
|
|
673
|
-
w.on("error", () =>
|
|
674
|
-
w.close();
|
|
675
|
-
watchers.delete(w);
|
|
676
|
-
projectWatchers.delete(key);
|
|
677
|
-
});
|
|
685
|
+
w.on("error", () => drop(key));
|
|
678
686
|
projectWatchers.set(key, w);
|
|
679
687
|
watchers.add(w);
|
|
680
688
|
} catch {}
|
|
681
689
|
};
|
|
682
|
-
const
|
|
683
|
-
if (projectWatchers.has(name) || name.includes("/") || name !== ".workpad" && !FILTER.mayHold(name)) return;
|
|
684
|
-
const dir = path.join(project.dir, name);
|
|
685
|
-
let isDir = false;
|
|
690
|
+
const isDir = (dir) => {
|
|
686
691
|
try {
|
|
687
|
-
|
|
688
|
-
} catch {
|
|
689
|
-
|
|
692
|
+
return fs.statSync(dir).isDirectory();
|
|
693
|
+
} catch {
|
|
694
|
+
return false;
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
const watchFolder = (rel) => {
|
|
698
|
+
if (round !== watchRound || projectWatchers.has(rel)) return;
|
|
699
|
+
if (rel !== ".workpad" && !FILTER.mayHold(rel)) return;
|
|
700
|
+
if (!!rel.includes("/") && !watchByFolder) return;
|
|
701
|
+
const dir = path.join(project.dir, rel);
|
|
702
|
+
if (!isDir(dir)) return;
|
|
703
|
+
if (rel === ".workpad" || !watchByFolder) return add(rel, dir, true, rel + "/");
|
|
704
|
+
add(rel, dir, false, rel + "/");
|
|
705
|
+
walk(rel);
|
|
706
|
+
};
|
|
707
|
+
const walk = async (rel) => {
|
|
708
|
+
let ents;
|
|
709
|
+
try {
|
|
710
|
+
ents = await fs.promises.readdir(path.join(project.dir, rel), { withFileTypes: true });
|
|
711
|
+
} catch {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
for (const ent of ents) if (ent.isDirectory()) watchFolder(rel ? `${rel}/${ent.name}` : ent.name);
|
|
690
715
|
};
|
|
691
716
|
if (!files) return add(".workpad", project.wp, true, ".workpad/");
|
|
692
717
|
add("", project.dir, false, "");
|
package/package.json
CHANGED
package/plugins/claude/doctor.ts
CHANGED
|
@@ -40,9 +40,15 @@ function readJson(file: string): { value: any; why?: string } | null {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* `$VAR`, `${VAR}` and `${VAR:-default}` in a command, its args or env as Claude Code expands them
|
|
45
|
+
* (the default when VAR is unset or empty); CLAUDE_PROJECT_DIR is the project.
|
|
46
|
+
*/
|
|
44
47
|
export function expand(s: string, env: Record<string, string | undefined>): string {
|
|
45
|
-
return s.replace(/\$\{(\w+)
|
|
48
|
+
return s.replace(/\$\{(\w+)(?::-([^}]*))?\}|\$(\w+)/g, (_, a, fallback, b) => {
|
|
49
|
+
const value = env[a ?? b]
|
|
50
|
+
return fallback !== undefined && !value ? fallback : (value ?? '')
|
|
51
|
+
})
|
|
46
52
|
}
|
|
47
53
|
|
|
48
54
|
/** Start the channel's command, ask it to initialize, and stop it: whether it answered, or why not. */
|
|
@@ -177,7 +183,9 @@ export async function channelChecks(
|
|
|
177
183
|
else if (mcp.value === undefined) lines.push({ mark: 'fail', text: `${mcpFile}: ${mcp.why}`, fix: 'fix the file' })
|
|
178
184
|
else if (!entry?.command) lines.push({ mark: 'fail', text: `${mcpFile} has no "${SERVER}" server`, fix: install })
|
|
179
185
|
else {
|
|
180
|
-
const
|
|
186
|
+
const base = { ...process.env, CLAUDE_PROJECT_DIR: dir }
|
|
187
|
+
const own = Object.entries((entry.env ?? {}) as Record<string, string>).map(([k, v]) => [k, expand(String(v), base)])
|
|
188
|
+
const env = { ...base, ...Object.fromEntries(own) }
|
|
181
189
|
const command = expand(entry.command, env)
|
|
182
190
|
const args = ((entry.args ?? []) as string[]).map(a => expand(a, env))
|
|
183
191
|
const why = await probe(command, args, dir, env)
|
package/src/entries.ts
CHANGED
|
@@ -14,16 +14,18 @@
|
|
|
14
14
|
import fs from 'node:fs'
|
|
15
15
|
import os from 'node:os'
|
|
16
16
|
import path from 'node:path'
|
|
17
|
+
import type { FilesConfig } from './files.ts'
|
|
17
18
|
|
|
18
19
|
/** One entry of the machine config: which folders are its projects. */
|
|
19
20
|
export interface EntryConfig {
|
|
20
21
|
include: string[]
|
|
21
22
|
exclude?: string[]
|
|
22
23
|
/**
|
|
23
|
-
*
|
|
24
|
-
* false
|
|
24
|
+
* which of their own files its projects serve and watch, when a project's config doesn't say, as
|
|
25
|
+
* a project's config has it: false none (only `.workpad/` is watched), `{ include, exclude }`
|
|
26
|
+
* some (default: true, all but node_modules and .git)
|
|
25
27
|
*/
|
|
26
|
-
files?: boolean
|
|
28
|
+
files?: boolean | FilesConfig
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
/** The machine config: the shared server's port and its entries. */
|
|
@@ -49,7 +51,7 @@ export interface EntryProject {
|
|
|
49
51
|
/** the URL path it is served under, without a trailing slash: /<entry>/<path> */
|
|
50
52
|
base: string
|
|
51
53
|
/** its entry's `files`, when the entry sets it */
|
|
52
|
-
files?: boolean
|
|
54
|
+
files?: boolean | FilesConfig
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
/**
|
|
@@ -130,12 +132,21 @@ export function readMachineConfig(dir = machineDir()): MachineConfig | null {
|
|
|
130
132
|
throw new Error(`${file}: entries.${name}.${what} must be a list of globs`)
|
|
131
133
|
return v
|
|
132
134
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
+
const files = e?.files
|
|
136
|
+
if (files != null && typeof files !== 'boolean' && (typeof files !== 'object' || Array.isArray(files)))
|
|
137
|
+
throw new Error(`${file}: entries.${name}.files must be true, false or { "include": [...], "exclude": [...] }`)
|
|
135
138
|
entries[name] = {
|
|
136
139
|
include: list(e?.include, 'include'),
|
|
137
140
|
exclude: list(e?.exclude, 'exclude'),
|
|
138
|
-
...(
|
|
141
|
+
...(files != null && {
|
|
142
|
+
files:
|
|
143
|
+
typeof files === 'boolean'
|
|
144
|
+
? files
|
|
145
|
+
: {
|
|
146
|
+
...(files.include != null && { include: list(files.include, 'files.include') }),
|
|
147
|
+
...(files.exclude != null && { exclude: list(files.exclude, 'files.exclude') }),
|
|
148
|
+
},
|
|
149
|
+
}),
|
|
139
150
|
}
|
|
140
151
|
}
|
|
141
152
|
const port = raw.port == null ? undefined : Number(raw.port)
|
package/src/files.ts
CHANGED
|
@@ -26,7 +26,32 @@ export interface FileFilter {
|
|
|
26
26
|
mayHold(rel: string): boolean
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Does `rel` match `g`? As `path.posix.matchesGlob`, but a `**` takes in folders whose name starts
|
|
31
|
+
* with a dot as well: matchesGlob's doesn't, so `**\/node_modules` left out nothing under `.claude/`,
|
|
32
|
+
* and an install in a worktree kept there reached the watcher as thousands of changes. Segment by
|
|
33
|
+
* segment: `**` stands for any number of folders, every other segment is matched by matchesGlob.
|
|
34
|
+
*/
|
|
35
|
+
export function glob(rel: string, g: string): boolean {
|
|
36
|
+
const parts = g.split('/')
|
|
37
|
+
const segs = rel.split('/')
|
|
38
|
+
const seen = new Set<string>()
|
|
39
|
+
const at = (i: number, j: number): boolean => {
|
|
40
|
+
if (i === parts.length) return j === segs.length
|
|
41
|
+
const key = `${i},${j}`
|
|
42
|
+
if (seen.has(key)) return false
|
|
43
|
+
seen.add(key)
|
|
44
|
+
if (parts[i] === '**') {
|
|
45
|
+
for (let k = j; k <= segs.length; k++) if (at(i + 1, k)) return true
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
return j < segs.length && path.posix.matchesGlob(segs[j]!, parts[i]!) && at(i + 1, j + 1)
|
|
49
|
+
}
|
|
50
|
+
return at(0, 0)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// the always-excluded by folder name: checked first, it settles most of a burst cheaply
|
|
54
|
+
const ALWAYS_NAMES = new Set(ALWAYS_EXCLUDED.map(g => g.slice('**/'.length)))
|
|
30
55
|
|
|
31
56
|
/** The path and each folder above it: `a/b/c` → `a/b/c`, `a/b`, `a`. */
|
|
32
57
|
function* upward(rel: string) {
|
|
@@ -57,7 +82,8 @@ export function fileFilter(files?: boolean | FilesConfig): FileFilter {
|
|
|
57
82
|
const exclude = [...ALWAYS_EXCLUDED, ...(config.exclude ?? [])]
|
|
58
83
|
const include = config.include?.length ? config.include : null
|
|
59
84
|
// left out: it, or a folder it is in, matches an exclude
|
|
60
|
-
const excluded = (rel: string) =>
|
|
85
|
+
const excluded = (rel: string) =>
|
|
86
|
+
rel.split('/').some(s => ALWAYS_NAMES.has(s)) || [...upward(rel)].some(p => exclude.some(g => glob(p, g)))
|
|
61
87
|
// taken in: it, or a folder it is in, matches an include
|
|
62
88
|
const included = (rel: string) => !include || [...upward(rel)].some(p => include.some(g => glob(p, g)))
|
|
63
89
|
return {
|
package/src/hub.ts
CHANGED
|
@@ -151,7 +151,7 @@ export function createHub({
|
|
|
151
151
|
// projects come and go: the overview lists them
|
|
152
152
|
setImmediate(() => touch())
|
|
153
153
|
// a project whose entry's files changed is made again with it
|
|
154
|
-
const wanted = new Map(projects.map(p => [`${p.base}\n${p.dir}\n${p.files ??
|
|
154
|
+
const wanted = new Map(projects.map(p => [`${p.base}\n${p.dir}\n${JSON.stringify(p.files ?? null)}`, p]))
|
|
155
155
|
for (const [key, s] of served)
|
|
156
156
|
if (!wanted.has(key)) {
|
|
157
157
|
for (const el of entryListens) release(s, el)
|
|
@@ -210,14 +210,22 @@ export function createHub({
|
|
|
210
210
|
// the projects that changed since: each other project's open pages are told, for their menu of
|
|
211
211
|
// what waits elsewhere (pushed, so no page has to ask every project now and then)
|
|
212
212
|
const changes = new Set<string>()
|
|
213
|
+
// told 150 ms after the first change, then at most once a second: a burst of changes (an install
|
|
214
|
+
// writing into a shown folder) re-renders the overview's clients once a second, not at each pause
|
|
215
|
+
let toldAt = 0
|
|
213
216
|
const touch = () => {
|
|
214
|
-
|
|
215
|
-
touching = setTimeout(
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
217
|
+
if (touching) return
|
|
218
|
+
touching = setTimeout(
|
|
219
|
+
() => {
|
|
220
|
+
touching = undefined
|
|
221
|
+
toldAt = Date.now()
|
|
222
|
+
root.touch()
|
|
223
|
+
const from = [...changes]
|
|
224
|
+
changes.clear()
|
|
225
|
+
for (const s of served.values()) if (from.some(b => b !== s.base)) s.server.elsewhere()
|
|
226
|
+
},
|
|
227
|
+
Math.max(150, toldAt + 1000 - Date.now()),
|
|
228
|
+
)
|
|
221
229
|
}
|
|
222
230
|
const changed = (base: string) => {
|
|
223
231
|
changes.add(base)
|
package/src/loader.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// the pages (how markdown is rendered with their blocks).
|
|
5
5
|
import fs from 'node:fs'
|
|
6
6
|
import path from 'node:path'
|
|
7
|
-
import { fileFilter, type FileFilter } from './files.ts'
|
|
7
|
+
import { fileFilter, type FileFilter, type FilesConfig } from './files.ts'
|
|
8
8
|
import { fileURLToPath } from 'node:url'
|
|
9
9
|
import { importFresh, importOwn } from './load.ts'
|
|
10
10
|
import { createPages, type Pages } from './pages/index.ts'
|
|
@@ -126,12 +126,12 @@ function checkPlugin(e: Plugin, where: string | null) {
|
|
|
126
126
|
const RETRY_FAILED_MS = 5000
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
|
-
* `files`:
|
|
130
|
-
* entry can say
|
|
129
|
+
* `files`: which of its own files the project serves when its config doesn't say (a machine config's
|
|
130
|
+
* entry can say it for all of its projects; see EntryConfig).
|
|
131
131
|
*/
|
|
132
132
|
export function createConfigLoader(
|
|
133
133
|
proj: Project,
|
|
134
|
-
{ pages: withPages = true, files: filesDefault = true }: { pages?: boolean; files?: boolean } = {},
|
|
134
|
+
{ pages: withPages = true, files: filesDefault = true }: { pages?: boolean; files?: boolean | FilesConfig } = {},
|
|
135
135
|
) {
|
|
136
136
|
let cached: Promise<LoadedConfig> | null = null
|
|
137
137
|
let failedAt: number | null = null
|
|
@@ -193,8 +193,8 @@ export function createConfigLoader(
|
|
|
193
193
|
forReader: new Map([...declared('forReader')].map(type => [type, requires.get(type) ?? []])),
|
|
194
194
|
requires,
|
|
195
195
|
publicDirs,
|
|
196
|
-
files: config?.files
|
|
197
|
-
fileFilter: fileFilter(config?.files),
|
|
196
|
+
files: (config?.files ?? filesDefault) !== false,
|
|
197
|
+
fileFilter: fileFilter(config?.files ?? filesDefault),
|
|
198
198
|
build: config?.build ?? {},
|
|
199
199
|
commands: configCommands(config?.commands, { taken: RESERVED, plugins: plugins.map(p => p.name), where: configFile }),
|
|
200
200
|
}
|
package/src/server.ts
CHANGED
|
@@ -74,7 +74,17 @@ import {
|
|
|
74
74
|
type ProjectStatus,
|
|
75
75
|
} from './status.ts'
|
|
76
76
|
import { formsIn, messageForms } from './pages/markdown.ts'
|
|
77
|
-
import { fileFilter, type FileFilter } from './files.ts'
|
|
77
|
+
import { fileFilter, type FileFilter, type FilesConfig } from './files.ts'
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Watch a project's files folder by folder (each that can hold something shown, at any depth), not
|
|
81
|
+
* a recursive watch per top-level folder: where a recursive watch costs a watch per folder below
|
|
82
|
+
* it, whatever it holds (Linux's inotify). `$WORKPAD_WATCH`: `folders` or `recursive`, else by
|
|
83
|
+
* platform. See setFiles.
|
|
84
|
+
*/
|
|
85
|
+
const WATCH_BY_FOLDER = process.env.WORKPAD_WATCH
|
|
86
|
+
? process.env.WORKPAD_WATCH === 'folders'
|
|
87
|
+
: process.platform !== 'darwin' && process.platform !== 'win32'
|
|
78
88
|
import { titleOf } from './pages/render.ts'
|
|
79
89
|
import { createRegistry, follows, holdStream, listenerOf } from './listeners.ts'
|
|
80
90
|
import { inPublic } from './public.ts'
|
|
@@ -189,8 +199,10 @@ export interface ServerOptions {
|
|
|
189
199
|
name?: string
|
|
190
200
|
/** serve and watch the project's files, over the config's `files` */
|
|
191
201
|
files?: boolean
|
|
192
|
-
/**
|
|
193
|
-
filesDefault?: boolean
|
|
202
|
+
/** which of the project's files are served and watched when its config doesn't say (its entry's `files`); default true */
|
|
203
|
+
filesDefault?: boolean | FilesConfig
|
|
204
|
+
/** watch the project's files folder by folder, not recursively (default: by platform, WATCH_BY_FOLDER) */
|
|
205
|
+
watchByFolder?: boolean
|
|
194
206
|
/** the overview at / lists these instead of the project's pages (the shared server's own project) */
|
|
195
207
|
overview?: () => Promise<Pick<PagesSource, 'projects' | 'note'>>
|
|
196
208
|
/** a page of the project changed, or its log grew: the shared server's overview follows it */
|
|
@@ -208,6 +220,7 @@ export function createServer(
|
|
|
208
220
|
name,
|
|
209
221
|
files: filesOption,
|
|
210
222
|
filesDefault,
|
|
223
|
+
watchByFolder = WATCH_BY_FOLDER,
|
|
211
224
|
overview,
|
|
212
225
|
onChange,
|
|
213
226
|
origins = () => [],
|
|
@@ -885,65 +898,94 @@ export function createServer(
|
|
|
885
898
|
// With files on, the whole project folder is watched (repository views stay live); with files off,
|
|
886
899
|
// only .workpad/, so a project in a large folder such as the home folder stays cheap. A folder
|
|
887
900
|
// that holds nothing shown (node_modules, .git, what the config's `files` leaves out: src/files.ts)
|
|
888
|
-
// is never watched: an install or a checkout changes thousands of files there that nothing shows
|
|
889
|
-
//
|
|
890
|
-
//
|
|
891
|
-
//
|
|
892
|
-
// watched
|
|
901
|
+
// is never watched: an install or a checkout changes thousands of files there that nothing shows.
|
|
902
|
+
// How, by platform (watchByFolder): on Linux a recursive watch is one inotify watch per folder
|
|
903
|
+
// below it, node_modules and nested checkouts' dependencies too (10 checkouts: 113,899 watches,
|
|
904
|
+
// the first request blocked for minutes while they were set up), so each folder that can hold
|
|
905
|
+
// something shown is watched on its own, at any depth, found without blocking, and one made later
|
|
906
|
+
// too. On macOS a recursive watch is a single FSEvents stream, cheap however deep, and a watch
|
|
907
|
+
// per folder would rebuild it at every one added: the project folder is watched without
|
|
908
|
+
// recursing, each folder in it that can hold something shown recursively, and what is left out
|
|
909
|
+
// deeper down (a package's own node_modules) is passed over (fileChanged), cheaply.
|
|
893
910
|
let FILES: boolean | null = null
|
|
894
911
|
// which of the project's files are shown (the config's `files`, src/files.ts)
|
|
895
912
|
let FILTER: FileFilter = fileFilter()
|
|
913
|
+
// a watcher per folder watched, by its path in the project folder ('' the project folder itself)
|
|
896
914
|
let projectWatchers = new Map<string, fs.FSWatcher>()
|
|
915
|
+
// counts each setFiles: a walk still finding folders for an earlier one stops
|
|
916
|
+
let watchRound = 0
|
|
897
917
|
function setFiles(files: boolean, filter: FileFilter = fileFilter()) {
|
|
898
918
|
if (files === FILES && filter.key === FILTER.key) return
|
|
899
919
|
FILES = files
|
|
900
920
|
FILTER = filter
|
|
921
|
+
const round = ++watchRound
|
|
901
922
|
for (const w of projectWatchers.values()) {
|
|
902
923
|
w.close()
|
|
903
924
|
watchers.delete(w)
|
|
904
925
|
}
|
|
905
926
|
projectWatchers = new Map()
|
|
927
|
+
const drop = (key: string) => {
|
|
928
|
+
for (const [k, w] of projectWatchers)
|
|
929
|
+
if (k === key || k.startsWith(key + '/')) {
|
|
930
|
+
w.close()
|
|
931
|
+
watchers.delete(w)
|
|
932
|
+
projectWatchers.delete(k)
|
|
933
|
+
}
|
|
934
|
+
}
|
|
906
935
|
const add = (key: string, dir: string, recursive: boolean, prefix: string) => {
|
|
907
936
|
try {
|
|
908
937
|
const w = fs.watch(dir, { recursive }, (evt, filename) => {
|
|
909
938
|
if (!filename) return
|
|
910
939
|
const name = filename.toString().replace(/\\/g, '/')
|
|
911
940
|
fileChanged(prefix + name)
|
|
912
|
-
// a folder made, removed or made again in
|
|
941
|
+
// a folder made, removed or made again in a folder watched on its own: watched afresh, if it is there
|
|
913
942
|
if (!recursive && evt === 'rename') {
|
|
914
|
-
const
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
watchers.delete(old)
|
|
918
|
-
projectWatchers.delete(name)
|
|
919
|
-
}
|
|
920
|
-
watchFolder(name)
|
|
943
|
+
const child = prefix + name
|
|
944
|
+
drop(child)
|
|
945
|
+
watchFolder(child)
|
|
921
946
|
}
|
|
922
947
|
})
|
|
923
948
|
// a folder that goes: its watcher with it
|
|
924
|
-
w.on('error', () =>
|
|
925
|
-
w.close()
|
|
926
|
-
watchers.delete(w)
|
|
927
|
-
projectWatchers.delete(key)
|
|
928
|
-
})
|
|
949
|
+
w.on('error', () => drop(key))
|
|
929
950
|
projectWatchers.set(key, w)
|
|
930
951
|
watchers.add(w)
|
|
931
952
|
} catch {
|
|
932
953
|
/* gone meanwhile */
|
|
933
954
|
}
|
|
934
955
|
}
|
|
935
|
-
const
|
|
936
|
-
// .workpad always; of the rest, a folder that can hold something shown
|
|
937
|
-
if (projectWatchers.has(name) || name.includes('/') || (name !== '.workpad' && !FILTER.mayHold(name))) return
|
|
938
|
-
const dir = path.join(project.dir, name)
|
|
939
|
-
let isDir = false
|
|
956
|
+
const isDir = (dir: string) => {
|
|
940
957
|
try {
|
|
941
|
-
|
|
942
|
-
} catch {
|
|
943
|
-
|
|
958
|
+
return fs.statSync(dir).isDirectory()
|
|
959
|
+
} catch {
|
|
960
|
+
return false
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
// a folder in the project folder (`src`, `src/lib` by folder): watched, if it can hold something shown
|
|
964
|
+
const watchFolder = (rel: string) => {
|
|
965
|
+
if (round !== watchRound || projectWatchers.has(rel)) return
|
|
966
|
+
if (rel !== '.workpad' && !FILTER.mayHold(rel)) return
|
|
967
|
+
const top = !rel.includes('/')
|
|
968
|
+
if (!top && !watchByFolder) return
|
|
969
|
+
const dir = path.join(project.dir, rel)
|
|
970
|
+
if (!isDir(dir)) return
|
|
971
|
+
// .workpad recursively, everywhere: it is small, and its pages' folders come and go
|
|
972
|
+
if (rel === '.workpad' || !watchByFolder) return add(rel, dir, true, rel + '/')
|
|
973
|
+
add(rel, dir, false, rel + '/')
|
|
974
|
+
void walk(rel)
|
|
975
|
+
}
|
|
976
|
+
// the folders in a folder watched on its own, each watched in turn; read off the event loop
|
|
977
|
+
const walk = async (rel: string) => {
|
|
978
|
+
let ents: fs.Dirent[]
|
|
979
|
+
try {
|
|
980
|
+
ents = await fs.promises.readdir(path.join(project.dir, rel), { withFileTypes: true })
|
|
981
|
+
} catch {
|
|
982
|
+
return
|
|
983
|
+
}
|
|
984
|
+
for (const ent of ents) if (ent.isDirectory()) watchFolder(rel ? `${rel}/${ent.name}` : ent.name)
|
|
944
985
|
}
|
|
945
986
|
if (!files) return add('.workpad', project.wp, true, '.workpad/')
|
|
946
987
|
add('', project.dir, false, '')
|
|
988
|
+
// the folders in the project folder at once, as it starts; deeper ones as they are found
|
|
947
989
|
for (const ent of fs.readdirSync(project.dir, { withFileTypes: true })) if (ent.isDirectory()) watchFolder(ent.name)
|
|
948
990
|
}
|
|
949
991
|
|