@jxsuite/server 9.2.1 → 9.3.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/package.json +24 -18
- package/src/collab.ts +390 -18
- package/src/jxn-response.ts +41 -0
- package/src/net-guard.ts +4 -3
- package/src/project-events.ts +622 -0
- package/src/project-server.ts +102 -3
- package/src/project-state/collections.ts +108 -0
- package/src/project-state/git-monitor.ts +302 -0
- package/src/project-state/graph.ts +651 -0
- package/src/project-state/host-deps.ts +224 -0
- package/src/project-state/host.ts +2113 -0
- package/src/project-state/overlays.ts +231 -0
- package/src/project-state/rename.ts +473 -0
- package/src/project-state/settle.ts +38 -0
- package/src/refactor/apply.ts +177 -67
- package/src/refactor/find-refs.ts +0 -0
- package/src/refactor/index.ts +2 -1
- package/src/refactor/paths.ts +3 -325
- package/src/refactor/refs.ts +5 -388
- package/src/refactor/scan.ts +17 -4
- package/src/refactor/watcher.ts +47 -2
- package/src/serve.node.ts +1 -0
- package/src/serve.ts +7 -5
- package/src/server.ts +40 -1
- package/src/sse.ts +1 -1
- package/src/studio-api.ts +76 -9
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the project state host (`./host.ts`) reaches outside itself for, each behind a function the
|
|
3
|
+
* host takes as an option, and the default each option has: the project's formats and asset mounts
|
|
4
|
+
* once its extensions are loaded (§2.2, §7), whether its dependencies are installed and how to
|
|
5
|
+
* install them (§7), and each content type's entry files (§8.1).
|
|
6
|
+
*
|
|
7
|
+
* The defaults import their implementations lazily, so a host given its own (a test, the cloud's
|
|
8
|
+
* Worker) never loads the compiler's format host or the package installer.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { stat } from "node:fs/promises";
|
|
14
|
+
import { dirname, join } from "node:path";
|
|
15
|
+
import { isJsonObject, isString } from "@jxsuite/schema/guards";
|
|
16
|
+
import type { AssetMount } from "@jxsuite/schema/asset-paths";
|
|
17
|
+
import type { FormatRegistry } from "@jxsuite/schema/format-registry";
|
|
18
|
+
import type { JsonObject, JsonValue, ProjectConfig } from "@jxsuite/schema/types";
|
|
19
|
+
|
|
20
|
+
/** The project's document formats, as its loaded extensions register them (§2.2). */
|
|
21
|
+
export interface ProjectFormats {
|
|
22
|
+
/** Every file extension a registered format parses, lowercase with its dot (`.md`). */
|
|
23
|
+
extensions: readonly string[];
|
|
24
|
+
/** Parse one document of a registered extension into the tree `extractRefs` reads. */
|
|
25
|
+
parse: (extension: string, text: string) => Promise<JsonValue>;
|
|
26
|
+
/** Project-relative asset mounts, through which a rooted reference resolves. */
|
|
27
|
+
mounts: readonly AssetMount[];
|
|
28
|
+
/**
|
|
29
|
+
* The registry behind `parse`, whose formats also write a document back; a rename rewrites a
|
|
30
|
+
* document in a registered format through it (§9). Without one such a document is named in the
|
|
31
|
+
* rename's report rather than rewritten.
|
|
32
|
+
*/
|
|
33
|
+
registry?: FormatRegistry;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The formats of a project whose extensions could not be loaded: none beyond the native ones. */
|
|
37
|
+
export const NO_FORMATS: ProjectFormats = Object.freeze({
|
|
38
|
+
extensions: [],
|
|
39
|
+
mounts: [],
|
|
40
|
+
parse: (extension: string) => Promise.reject(new Error(`No parser for "${extension}"`)),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** Load the extensions `config` names and answer the formats they register (§7). */
|
|
44
|
+
export type ExtensionLoader = (
|
|
45
|
+
root: string,
|
|
46
|
+
config: ProjectConfig | undefined,
|
|
47
|
+
) => Promise<ProjectFormats>;
|
|
48
|
+
|
|
49
|
+
/** Install the dependencies the manifest declares, writing the lockfile (§7). */
|
|
50
|
+
export type Installer = (root: string) => Promise<void>;
|
|
51
|
+
|
|
52
|
+
/** Whether every dependency `manifest` declares is installed under `root`. */
|
|
53
|
+
export type InstalledCheck = (root: string, manifest: JsonObject | null) => Promise<boolean>;
|
|
54
|
+
|
|
55
|
+
/** What a collection resolver is given: the configuration and every file in the index. */
|
|
56
|
+
export interface CollectionInput {
|
|
57
|
+
root: string;
|
|
58
|
+
config: ProjectConfig | undefined;
|
|
59
|
+
/** Every indexed file (not directory), project-relative. */
|
|
60
|
+
files: readonly string[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Each content type's entry files, project-relative, as the collection loader resolves them. */
|
|
64
|
+
export type CollectionResolver = (
|
|
65
|
+
input: CollectionInput,
|
|
66
|
+
) => Promise<Readonly<Record<string, readonly string[]>>>;
|
|
67
|
+
|
|
68
|
+
/** The manifest fields a fingerprint covers, and whose packages must be installed (§7). */
|
|
69
|
+
export const DEPENDENCY_FIELDS = [
|
|
70
|
+
"dependencies",
|
|
71
|
+
"devDependencies",
|
|
72
|
+
"optionalDependencies",
|
|
73
|
+
"peerDependencies",
|
|
74
|
+
] as const;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The default {@link ExtensionLoader}: the registry the editor already builds
|
|
78
|
+
* (`buildInstalledExtensionRegistry`), whose formats parse each document, and the project's asset
|
|
79
|
+
* mounts. An extension the project names but has not installed is skipped, as it is everywhere.
|
|
80
|
+
*/
|
|
81
|
+
export const loadInstalledFormats: ExtensionLoader = async (root, config) => {
|
|
82
|
+
const [{ buildInstalledExtensionRegistry }, { refactorMounts }] = await Promise.all([
|
|
83
|
+
import("@jxsuite/compiler/format-host"),
|
|
84
|
+
import("../refactor/mounts.ts"),
|
|
85
|
+
]);
|
|
86
|
+
const { registry } = await buildInstalledExtensionRegistry(root, config);
|
|
87
|
+
const { formats } = registry;
|
|
88
|
+
return {
|
|
89
|
+
extensions: formats.documentExtensions().filter((extension) => extension !== ".jxn"),
|
|
90
|
+
mounts: await refactorMounts(root),
|
|
91
|
+
async parse(extension, text) {
|
|
92
|
+
const entry = formats.byExtension(extension, "parse");
|
|
93
|
+
if (entry === undefined) {
|
|
94
|
+
throw new Error(`No parser for "${extension}"`);
|
|
95
|
+
}
|
|
96
|
+
/* SAFETY: a `parse` answer is a JxDocument or a ContentLoaderEntry[], both JSON trees but
|
|
97
|
+
for leaves such as a frontmatter Date; every walk `extractRefs` runs re-checks each node's
|
|
98
|
+
shape before reading it, so such a leaf is skipped rather than misread. */
|
|
99
|
+
return (await entry.call("parse", text)) as JsonValue;
|
|
100
|
+
},
|
|
101
|
+
registry: formats,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** The default {@link Installer}: the package service's install, which writes `package-lock.json`. */
|
|
106
|
+
export const installPackages: Installer = async (root) => {
|
|
107
|
+
const { installDependencies } = await import("../packages.ts");
|
|
108
|
+
const result = await installDependencies(root);
|
|
109
|
+
if (!result.ok) {
|
|
110
|
+
throw new Error(result.log ?? "The install failed");
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The default {@link InstalledCheck}: every package named in the manifest's dependency fields has a
|
|
116
|
+
* `package.json` under a `node_modules` in the root or one of its ancestors, as Node resolves a
|
|
117
|
+
* bare specifier, so a workspace member whose dependencies are hoisted to the workspace root counts
|
|
118
|
+
* as installed. Peer dependencies are the consumer's to provide, so they are not required here.
|
|
119
|
+
*/
|
|
120
|
+
export const dependenciesInstalled: InstalledCheck = async (root, manifest) => {
|
|
121
|
+
const names = new Set<string>();
|
|
122
|
+
for (const field of ["dependencies", "devDependencies", "optionalDependencies"] as const) {
|
|
123
|
+
const declared = manifest?.[field];
|
|
124
|
+
if (isJsonObject(declared)) {
|
|
125
|
+
for (const name of Object.keys(declared)) {
|
|
126
|
+
names.add(name);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
for (const name of names) {
|
|
131
|
+
if (!(await resolvesUpward(root, name))) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** Whether `node_modules/<name>/package.json` exists in `from` or any directory above it. */
|
|
139
|
+
async function resolvesUpward(from: string, name: string): Promise<boolean> {
|
|
140
|
+
for (let dir = from; ;) {
|
|
141
|
+
try {
|
|
142
|
+
await stat(join(dir, "node_modules", name, "package.json"));
|
|
143
|
+
return true;
|
|
144
|
+
} catch {
|
|
145
|
+
const parent = dirname(dir);
|
|
146
|
+
if (parent === dir) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
dir = parent;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A source glob (`content/*.md`, `docs/**`) as a regular expression over project paths. With
|
|
156
|
+
* `beneath`, a path under a matching directory matches too, as a directory source's entries do.
|
|
157
|
+
*/
|
|
158
|
+
function globPattern(glob: string, beneath = false): RegExp {
|
|
159
|
+
let out = "";
|
|
160
|
+
for (let at = 0; at < glob.length; at += 1) {
|
|
161
|
+
const char = glob[at]!;
|
|
162
|
+
if (char === "*" && glob[at + 1] === "*") {
|
|
163
|
+
out += ".*";
|
|
164
|
+
at += 1;
|
|
165
|
+
if (glob[at + 1] === "/") {
|
|
166
|
+
at += 1;
|
|
167
|
+
out += "/?";
|
|
168
|
+
}
|
|
169
|
+
} else if (char === "*") {
|
|
170
|
+
out += "[^/]*";
|
|
171
|
+
} else if (char === "?") {
|
|
172
|
+
out += "[^/]";
|
|
173
|
+
} else {
|
|
174
|
+
out += char.replaceAll(/[.+^${}()|[\]\\]/g, String.raw`\$&`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return new RegExp(beneath ? `^${out}(?:/.*)?$` : `^${out}$`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** A configuration path as a project path: no `./`, no trailing `/`. */
|
|
181
|
+
const projectPath = (value: string): string =>
|
|
182
|
+
value
|
|
183
|
+
.replace(/^\.?\//, "")
|
|
184
|
+
.replaceAll(/\/+$/g, "")
|
|
185
|
+
.replaceAll("{locale}", "*");
|
|
186
|
+
|
|
187
|
+
/** Whether `path` is a member of the source `source` names. */
|
|
188
|
+
function underSource(path: string, source: string): boolean {
|
|
189
|
+
if (/[*?[]/.test(source)) {
|
|
190
|
+
return globPattern(source, true).test(path);
|
|
191
|
+
}
|
|
192
|
+
return path === source || path.startsWith(`${source}/`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The default {@link CollectionResolver}: each content type's `source` (a directory, a file or a
|
|
197
|
+
* glob, with `{locale}` matching any one segment) over the index, less its `exclude` globs. It
|
|
198
|
+
* evaluates no `where`, so an entry a filter leaves out is still listed; a host that runs the
|
|
199
|
+
* collection loader passes its own resolver for the exact list (§8.1).
|
|
200
|
+
*/
|
|
201
|
+
export const sourceCollections: CollectionResolver = (input) => {
|
|
202
|
+
const content = input.config?.content;
|
|
203
|
+
const out: Record<string, string[]> = {};
|
|
204
|
+
if (!isJsonObject(content)) {
|
|
205
|
+
return Promise.resolve(out);
|
|
206
|
+
}
|
|
207
|
+
for (const [type, def] of Object.entries(content)) {
|
|
208
|
+
if (!isJsonObject(def) || !isString(def.source) || /^[a-z][a-z0-9+.-]*:/i.test(def.source)) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const source = projectPath(def.source);
|
|
212
|
+
const exclude = (Array.isArray(def.exclude) ? def.exclude : [])
|
|
213
|
+
.filter((item) => isString(item))
|
|
214
|
+
.map((item) => globPattern(projectPath(item)));
|
|
215
|
+
out[type] = input.files.filter(
|
|
216
|
+
(path) =>
|
|
217
|
+
underSource(path, source) &&
|
|
218
|
+
!exclude.some(
|
|
219
|
+
(pattern) => pattern.test(path.slice(source.length + 1)) || pattern.test(path),
|
|
220
|
+
),
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
return Promise.resolve(out);
|
|
224
|
+
};
|