@bigmistqke/workpad 0.0.0-stage → 0.18.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/LICENSE +21 -0
- package/README.md +781 -2
- package/bin/workpad.ts +1509 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1190 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2098 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +211 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +341 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +665 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +588 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +133 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +137 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +238 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +426 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +748 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
package/src/listeners.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Who listens, as a server holds it. Anyone listening for answers (`workpad listen`, a plugin's
|
|
2
|
+
// channel) holds a connection to /api/listen for as long as it runs: that connection is the
|
|
3
|
+
// listener, gone when it is. Any number listen on a page, and each hears every answer there: who
|
|
4
|
+
// they are (`agent`) is for the page and `status` to show. A project's server keeps its listeners in a
|
|
5
|
+
// registry; the shared server adds a `listen` without a page in one of an entry's projects to the
|
|
6
|
+
// registry of each project of the entry.
|
|
7
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
8
|
+
import type { Listener } from './define.ts'
|
|
9
|
+
import { covers } from './status.ts'
|
|
10
|
+
|
|
11
|
+
/** The listeners of one project. */
|
|
12
|
+
export interface Registry {
|
|
13
|
+
/** the listeners, those covering `page` when given; each a copy */
|
|
14
|
+
list(page?: string | null): Listener[]
|
|
15
|
+
/** hold `l` until the returned function is called */
|
|
16
|
+
add(l: Listener): () => void
|
|
17
|
+
/** call `fn` whenever a listener comes or goes; returns what stops it */
|
|
18
|
+
onChange(fn: () => void): () => void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function createRegistry(): Registry {
|
|
22
|
+
const listeners = new Set<Listener>()
|
|
23
|
+
const watchers = new Set<() => void>()
|
|
24
|
+
const changed = () => {
|
|
25
|
+
for (const fn of watchers) fn()
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
list: (page = null) =>
|
|
29
|
+
[...listeners].filter(l => page == null || covers(l, page)).map(({ page, type, agent }) => ({ page, type, agent })),
|
|
30
|
+
add(l) {
|
|
31
|
+
listeners.add(l)
|
|
32
|
+
changed()
|
|
33
|
+
return () => {
|
|
34
|
+
if (listeners.delete(l)) changed()
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
onChange(fn) {
|
|
38
|
+
watchers.add(fn)
|
|
39
|
+
return () => watchers.delete(fn)
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A project a `listen` over a whole entry covers (the shared server names them). */
|
|
45
|
+
export interface Followed {
|
|
46
|
+
/** the project's folder */
|
|
47
|
+
dir: string
|
|
48
|
+
/** where the server serves it: `/rg/develop` under the shared one */
|
|
49
|
+
base: string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What a `listen` over a whole entry is told (`event: projects`): every project it covers, now. */
|
|
53
|
+
export interface Snapshot {
|
|
54
|
+
projects: Followed[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The listener a /api/listen request asks for. */
|
|
58
|
+
export const listenerOf = (q: URLSearchParams): Listener => ({
|
|
59
|
+
page: q.get('page') || '',
|
|
60
|
+
type: q.get('type') || null,
|
|
61
|
+
agent: q.get('agent') || null,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Hold a /api/listen connection open as an event stream (with a ping, so nothing between closes it
|
|
66
|
+
* as idle). `send` writes an event on it; `onEnd` runs once, when the agent goes or `end` is called.
|
|
67
|
+
*/
|
|
68
|
+
export function holdStream(req: IncomingMessage, res: ServerResponse, onEnd: () => void) {
|
|
69
|
+
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' })
|
|
70
|
+
res.write(': listening\n\n')
|
|
71
|
+
const ping = setInterval(() => res.write(': ping\n\n'), 25000)
|
|
72
|
+
let ended = false
|
|
73
|
+
const finish = () => {
|
|
74
|
+
if (ended) return
|
|
75
|
+
ended = true
|
|
76
|
+
clearInterval(ping)
|
|
77
|
+
onEnd()
|
|
78
|
+
}
|
|
79
|
+
req.on('close', finish)
|
|
80
|
+
return {
|
|
81
|
+
send(event: string, data: unknown) {
|
|
82
|
+
if (!ended) res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`)
|
|
83
|
+
},
|
|
84
|
+
end() {
|
|
85
|
+
finish()
|
|
86
|
+
res.end()
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
}
|
package/src/load.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Loading a project's config with Node itself: Node runs the TypeScript of `config.ts`, and these
|
|
2
|
+
// hooks do the two things it cannot do alone.
|
|
3
|
+
//
|
|
4
|
+
// - `@bigmistqke/workpad` and its parts always mean this engine's own files, so a config works in a
|
|
5
|
+
// project without node_modules (a Rust repository, a home folder), and it always matches the
|
|
6
|
+
// server that runs it. Which file a subpath is comes from the package's `exports`, read afresh
|
|
7
|
+
// on every resolve, so a part added to the package works without restarting the server: its
|
|
8
|
+
// `source` (the TypeScript) when the engine runs from source, else its build.
|
|
9
|
+
// - Reloading. Node evaluates a module once per URL; the config and every local module it imports
|
|
10
|
+
// get a version in their URL, and a new version imports them again. Modules in node_modules are
|
|
11
|
+
// left alone: they don't change while the server runs.
|
|
12
|
+
import fs from 'node:fs'
|
|
13
|
+
import { registerHooks, type ModuleFormat } from 'node:module'
|
|
14
|
+
import path from 'node:path'
|
|
15
|
+
import { pathToFileURL } from 'node:url'
|
|
16
|
+
import { FROM_SOURCE, PACKAGE_DIR } from './package.ts'
|
|
17
|
+
|
|
18
|
+
const ENGINE_DIR = PACKAGE_DIR
|
|
19
|
+
const PACKAGE = '@bigmistqke/workpad'
|
|
20
|
+
|
|
21
|
+
type Exports = Record<string, string | { source?: string; default?: string }>
|
|
22
|
+
|
|
23
|
+
/** The package's `exports`, read afresh; undefined when its package.json can't be read. */
|
|
24
|
+
function ownExports(): Exports | undefined {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(fs.readFileSync(path.join(ENGINE_DIR, 'package.json'), 'utf8')).exports
|
|
27
|
+
} catch {
|
|
28
|
+
return undefined
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The file `@bigmistqke/workpad` or one of its parts is, by the package's `exports`; null for none. */
|
|
33
|
+
function ownFile(specifier: string): string | null {
|
|
34
|
+
if (specifier !== PACKAGE && !specifier.startsWith(PACKAGE + '/')) return null
|
|
35
|
+
const exports = ownExports()
|
|
36
|
+
const target = exports?.[specifier === PACKAGE ? '.' : `./${specifier.slice(PACKAGE.length + 1)}`]
|
|
37
|
+
const file = typeof target === 'string' ? target : (FROM_SOURCE && target?.source) || target?.default
|
|
38
|
+
return file ? path.join(ENGINE_DIR, file) : null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const versionOf = (url: string) => new URL(url).searchParams.get('wp-v')
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Install the hooks, once: the first time something loads a config or the package's own parts, not
|
|
45
|
+
* on import, so a command that loads nothing leaves the process's module loading alone.
|
|
46
|
+
*/
|
|
47
|
+
let installed = false
|
|
48
|
+
export function installHooks() {
|
|
49
|
+
if (installed) return
|
|
50
|
+
installed = true
|
|
51
|
+
registerHooks({
|
|
52
|
+
resolve(specifier, context, next) {
|
|
53
|
+
const own = ownFile(specifier)
|
|
54
|
+
if (own) {
|
|
55
|
+
const url = pathToFileURL(own).href
|
|
56
|
+
const v = context.parentURL && versionOf(context.parentURL)
|
|
57
|
+
return { url: v ? `${url}?wp-v=${v}` : url, shortCircuit: true, format: moduleFormat(own) ?? 'module' }
|
|
58
|
+
}
|
|
59
|
+
// a part the package doesn't have (renamed, say): say so, and which it has. Handed on to Node,
|
|
60
|
+
// it would look for the package in node_modules and, without one, say the whole package is missing
|
|
61
|
+
const exports = specifier.startsWith(PACKAGE + '/') && ownExports()
|
|
62
|
+
if (exports)
|
|
63
|
+
throw new Error(
|
|
64
|
+
`${PACKAGE} has no part "${specifier.slice(PACKAGE.length + 1)}"; its parts: ${Object.keys(exports)
|
|
65
|
+
.filter(k => k !== '.' && !k.endsWith('.json'))
|
|
66
|
+
.map(k => k.slice(2))
|
|
67
|
+
.join(', ')}`,
|
|
68
|
+
)
|
|
69
|
+
const resolved = next(specifier, context)
|
|
70
|
+
if (!resolved.url.startsWith('file:') || resolved.url.includes('/node_modules/')) return resolved
|
|
71
|
+
const v = versionOf(resolved.url) ?? (context.parentURL?.startsWith('file:') && versionOf(context.parentURL))
|
|
72
|
+
if (!v) return resolved
|
|
73
|
+
const url = new URL(resolved.url)
|
|
74
|
+
url.searchParams.set('wp-v', v)
|
|
75
|
+
// A .workpad folder has no package.json of its own to say these are ES modules, so Node goes
|
|
76
|
+
// by the nearest one above it: in a repository whose package.json is CommonJS (no "type"), it
|
|
77
|
+
// would load the config as CommonJS and fail on its imports. What is in .workpad/ is always
|
|
78
|
+
// a module, by its extension; other local files keep the format their package gives them.
|
|
79
|
+
const inProject = url.pathname.split('/').includes('.workpad')
|
|
80
|
+
return {
|
|
81
|
+
...resolved,
|
|
82
|
+
url: url.href,
|
|
83
|
+
format: inProject ? moduleFormat(url.pathname) : (resolved.format ?? moduleFormat(url.pathname)),
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function moduleFormat(file: string): ModuleFormat | undefined {
|
|
90
|
+
if (/\.[mc]?ts$/.test(file)) return file.endsWith('.cts') ? 'commonjs-typescript' : 'module-typescript'
|
|
91
|
+
if (/\.c?js$/.test(file)) return file.endsWith('.cjs') ? 'commonjs' : 'module'
|
|
92
|
+
return undefined
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let version = 0
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Import one of the package's own parts (`plugins`) afresh, with everything it imports, the way
|
|
99
|
+
* `importFresh` does a config: for what the core loads itself on a config's behalf, such as the
|
|
100
|
+
* built-in plugins, which must reload along with the config.
|
|
101
|
+
*/
|
|
102
|
+
export function importOwn(part: string): Promise<any> {
|
|
103
|
+
installHooks()
|
|
104
|
+
const file = ownFile(`${PACKAGE}/${part}`)
|
|
105
|
+
if (!file) throw new Error(`the package has no part "${part}"`)
|
|
106
|
+
return import(`${pathToFileURL(file).href}?wp-v=${++version}`)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Import `file` and everything local it imports afresh. */
|
|
110
|
+
export function importFresh(file: string): Promise<any> {
|
|
111
|
+
installHooks()
|
|
112
|
+
return import(`${pathToFileURL(file).href}?wp-v=${++version}`)
|
|
113
|
+
}
|
package/src/loader.ts
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// The project's config: its plugins, its stylesheet and whether it serves the repository's files.
|
|
2
|
+
// Node loads it (see load.ts), so it can be TypeScript and import plugins from the project's own
|
|
3
|
+
// node_modules. What the loader makes of it: the plugins (the built-in ones, then the config's), and
|
|
4
|
+
// the pages (how markdown is rendered with their blocks).
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import path from 'node:path'
|
|
7
|
+
import { fileFilter, type FileFilter } from './files.ts'
|
|
8
|
+
import { fileURLToPath } from 'node:url'
|
|
9
|
+
import { importFresh, importOwn } from './load.ts'
|
|
10
|
+
import { createPages, type Pages } from './pages/index.ts'
|
|
11
|
+
import { configCommands, pluginCommands, type PluginCommand } from './plugins.ts'
|
|
12
|
+
import { inPublic, publicDirOf } from './public.ts'
|
|
13
|
+
import type { BuildConfig, Config, Plugin } from './define.ts'
|
|
14
|
+
import type { Project } from './config.ts'
|
|
15
|
+
|
|
16
|
+
/** A config as loaded: what went wrong is in `errors`. */
|
|
17
|
+
export interface LoadedConfig {
|
|
18
|
+
/** how pages render, with the plugins' blocks; null when the config failed to load, or for the command line */
|
|
19
|
+
pages: Pages | null
|
|
20
|
+
plugins: Plugin[]
|
|
21
|
+
/** the plugins' event types declared `handOut: true`: those that reach an agent unasked */
|
|
22
|
+
handedOut: Set<string>
|
|
23
|
+
/** the plugins' event types declared `forReader: true`: from the agent, a message for the reader */
|
|
24
|
+
forReader: Set<string>
|
|
25
|
+
/** each plugin's public folder, by name: the one place its browser files are served from */
|
|
26
|
+
publicDirs: Map<string, string>
|
|
27
|
+
/** whether it serves the project's own files, and which (src/files.ts) */
|
|
28
|
+
files: boolean
|
|
29
|
+
fileFilter: FileFilter
|
|
30
|
+
/** what `workpad build` makes */
|
|
31
|
+
build: BuildConfig
|
|
32
|
+
/** the config's own commands (`commands`), checked: `workpad <name>` */
|
|
33
|
+
commands: PluginCommand[]
|
|
34
|
+
errors: unknown[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const NAME = /^[\w-]+$/
|
|
38
|
+
/** The command line's own commands: a plugin can't be named like one, since its commands are `workpad <name>`. */
|
|
39
|
+
export const BUILTIN_COMMANDS = new Set([
|
|
40
|
+
'serve',
|
|
41
|
+
'init',
|
|
42
|
+
'root',
|
|
43
|
+
'new',
|
|
44
|
+
'agent',
|
|
45
|
+
'show',
|
|
46
|
+
'check',
|
|
47
|
+
'migrate',
|
|
48
|
+
'settle',
|
|
49
|
+
'hold',
|
|
50
|
+
'release',
|
|
51
|
+
'url',
|
|
52
|
+
'anchors',
|
|
53
|
+
'docs',
|
|
54
|
+
'events',
|
|
55
|
+
'listen',
|
|
56
|
+
'append',
|
|
57
|
+
'handled',
|
|
58
|
+
'thread',
|
|
59
|
+
'status',
|
|
60
|
+
'scroll',
|
|
61
|
+
'archive',
|
|
62
|
+
'destroy',
|
|
63
|
+
'prune',
|
|
64
|
+
'build',
|
|
65
|
+
'edit',
|
|
66
|
+
'skill',
|
|
67
|
+
'paths',
|
|
68
|
+
'help',
|
|
69
|
+
])
|
|
70
|
+
// folders in .workpad/ that are the core's or meant for the project's own plugins (a plugin's own
|
|
71
|
+
// folder holds its data, ignored by git), and the built-in commands
|
|
72
|
+
const RESERVED = new Set(['pages', 'plugins', 'archive', ...BUILTIN_COMMANDS])
|
|
73
|
+
|
|
74
|
+
/** A plugin's own folder, `.workpad/<name>/`, made with a .gitignore that ignores all of it. */
|
|
75
|
+
export function pluginDir(wp: string, name: string): string {
|
|
76
|
+
const dir = path.join(wp, name)
|
|
77
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
78
|
+
const ignore = path.join(dir, '.gitignore')
|
|
79
|
+
if (!fs.existsSync(ignore)) fs.writeFileSync(ignore, `# what the ${name} plugin keeps; not for git\n*\n`)
|
|
80
|
+
return dir
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A file reference (URL, file: URL string, or absolute path) → absolute path. */
|
|
84
|
+
export function toPath(ref: unknown): string {
|
|
85
|
+
if (ref instanceof URL || (typeof ref === 'string' && ref.startsWith('file:'))) return fileURLToPath(ref)
|
|
86
|
+
return String(ref)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The built-in plugins, afresh with every load of the config, like what a config imports itself. */
|
|
90
|
+
async function builtinPlugins(): Promise<Plugin[]> {
|
|
91
|
+
const { builtins } = await importOwn('plugins')
|
|
92
|
+
return builtins()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Check a plugin, as the config gives it. */
|
|
96
|
+
function checkPlugin(e: Plugin, where: string | null) {
|
|
97
|
+
if (typeof e !== 'object' || !NAME.test(e?.name ?? ''))
|
|
98
|
+
throw new Error(`a plugin in ${where} needs a name: letters, digits, - and _`)
|
|
99
|
+
if (RESERVED.has(e.name))
|
|
100
|
+
throw new Error(`plugin name "${e.name}" in ${where} is taken by the core (a folder in .workpad/ or a command)`)
|
|
101
|
+
pluginCommands(e) // checks them
|
|
102
|
+
for (const type of Object.keys(e.events ?? {}))
|
|
103
|
+
if (!type.startsWith(`${e.name}:`) || type.length <= e.name.length + 1)
|
|
104
|
+
throw new Error(`plugin ${e.name}: event type "${type}" must be named after it: ${e.name}:<what>`)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The config, cached until invalidate() (which loads it afresh). Errors are returned, not thrown:
|
|
109
|
+
* a config that fails to load leaves the project without its plugins, so pages show as raw text,
|
|
110
|
+
* and serves no repository files.
|
|
111
|
+
*
|
|
112
|
+
* The project's plugins are the built-in ones, then the config's: one with a built-in's name
|
|
113
|
+
* replaces it. Their blocks make the pages. `pages: false` leaves the pages out, for the command
|
|
114
|
+
* line, which only needs the plugins (their commands, the session, their events).
|
|
115
|
+
*/
|
|
116
|
+
/**
|
|
117
|
+
* How long a config that failed to load is kept before it is tried again: what failed may lie
|
|
118
|
+
* outside its file (a package installed, a link made), and no change to the file would reload it.
|
|
119
|
+
*/
|
|
120
|
+
const RETRY_FAILED_MS = 5000
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* `files`: whether the project serves its own files when its config doesn't say (a machine config's
|
|
124
|
+
* entry can say false for all of its projects; see EntryConfig).
|
|
125
|
+
*/
|
|
126
|
+
export function createConfigLoader(
|
|
127
|
+
proj: Project,
|
|
128
|
+
{ pages: withPages = true, files: filesDefault = true }: { pages?: boolean; files?: boolean } = {},
|
|
129
|
+
) {
|
|
130
|
+
let cached: Promise<LoadedConfig> | null = null
|
|
131
|
+
let failedAt: number | null = null
|
|
132
|
+
|
|
133
|
+
async function load(): Promise<Omit<LoadedConfig, 'errors'>> {
|
|
134
|
+
// no config file: the defaults, the same as an empty config
|
|
135
|
+
const configFile = proj.configFile
|
|
136
|
+
const mod = configFile ? await importFresh(configFile) : { default: {} }
|
|
137
|
+
// a config can also be a function that returns one
|
|
138
|
+
const exported: Config | (() => Config | Promise<Config>) = mod.default
|
|
139
|
+
const config: Config = typeof exported === 'function' ? await exported() : exported
|
|
140
|
+
const plugins: Plugin[] = await builtinPlugins()
|
|
141
|
+
const builtin = new Set(plugins.map(p => p.name))
|
|
142
|
+
const added = new Set<string>()
|
|
143
|
+
for (const e of [config?.plugins ?? []].flat(Infinity) as Plugin[]) {
|
|
144
|
+
if (!e) continue
|
|
145
|
+
checkPlugin(e, configFile)
|
|
146
|
+
if (added.has(e.name)) throw new Error(`two plugins are named "${e.name}" in ${configFile}`)
|
|
147
|
+
added.add(e.name)
|
|
148
|
+
// one with a built-in's name replaces it, in its place
|
|
149
|
+
if (builtin.has(e.name)) plugins[plugins.findIndex(p => p.name === e.name)] = e
|
|
150
|
+
else plugins.push(e)
|
|
151
|
+
}
|
|
152
|
+
// each plugin's browser files are served from its public folder alone: its files are named in it
|
|
153
|
+
const publicDirs = new Map(plugins.map(p => [p.name, publicDirOf(p, proj.wp)]))
|
|
154
|
+
const blocks = plugins.flatMap(p =>
|
|
155
|
+
Object.entries(p.blocks ?? {}).map(([name, block]) => ({
|
|
156
|
+
name,
|
|
157
|
+
block,
|
|
158
|
+
plugin: p.name,
|
|
159
|
+
public: publicDirs.get(p.name)!,
|
|
160
|
+
})),
|
|
161
|
+
)
|
|
162
|
+
const styles = plugins
|
|
163
|
+
.filter(p => p.style)
|
|
164
|
+
.map(p => ({ plugin: p.name, file: inPublic(publicDirs.get(p.name)!, p.style!, `plugin ${p.name}: style`) }))
|
|
165
|
+
const links = plugins.flatMap(p => (p.links ? [p.links.bind(p)] : []))
|
|
166
|
+
// the plugins that compose themselves around other blocks (Plugin.compose): a block with an
|
|
167
|
+
// attribute of their name
|
|
168
|
+
const composers = new Map(
|
|
169
|
+
plugins.flatMap(p => (typeof p.compose === 'function' ? [[p.name, p.compose.bind(p)] as const] : [])),
|
|
170
|
+
)
|
|
171
|
+
const ends = plugins.flatMap(p => (p.end ? [p.end.bind(p)] : []))
|
|
172
|
+
const pages = withPages ? createPages({ blocks, style: config?.style, styles, links, composers, ends }) : null
|
|
173
|
+
const declared = (flag: 'handOut' | 'forReader') =>
|
|
174
|
+
new Set(
|
|
175
|
+
plugins.flatMap(p =>
|
|
176
|
+
Object.entries(p.events ?? {})
|
|
177
|
+
.filter(([, spec]) => spec?.[flag])
|
|
178
|
+
.map(([type]) => type),
|
|
179
|
+
),
|
|
180
|
+
)
|
|
181
|
+
return {
|
|
182
|
+
pages,
|
|
183
|
+
plugins,
|
|
184
|
+
handedOut: declared('handOut'),
|
|
185
|
+
forReader: declared('forReader'),
|
|
186
|
+
publicDirs,
|
|
187
|
+
files: config?.files == null ? filesDefault : config.files !== false,
|
|
188
|
+
fileFilter: fileFilter(config?.files),
|
|
189
|
+
build: config?.build ?? {},
|
|
190
|
+
commands: configCommands(config?.commands, { taken: RESERVED, plugins: plugins.map(p => p.name), where: configFile }),
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function config(): Promise<LoadedConfig> {
|
|
195
|
+
if (failedAt !== null && Date.now() - failedAt > RETRY_FAILED_MS) cached = null
|
|
196
|
+
cached ??= load().then(
|
|
197
|
+
value => {
|
|
198
|
+
failedAt = null
|
|
199
|
+
return { ...value, errors: [] }
|
|
200
|
+
},
|
|
201
|
+
error => (
|
|
202
|
+
(failedAt = Date.now()),
|
|
203
|
+
{
|
|
204
|
+
pages: null,
|
|
205
|
+
plugins: [],
|
|
206
|
+
handedOut: new Set<string>(),
|
|
207
|
+
forReader: new Set<string>(),
|
|
208
|
+
publicDirs: new Map<string, string>(),
|
|
209
|
+
files: false,
|
|
210
|
+
fileFilter: fileFilter(),
|
|
211
|
+
build: {},
|
|
212
|
+
commands: [],
|
|
213
|
+
errors: [error],
|
|
214
|
+
}
|
|
215
|
+
),
|
|
216
|
+
)
|
|
217
|
+
return cached
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
config,
|
|
222
|
+
invalidate: () => {
|
|
223
|
+
cached = null
|
|
224
|
+
failedAt = null
|
|
225
|
+
},
|
|
226
|
+
}
|
|
227
|
+
}
|