@celljs/vite 4.3.0 → 4.4.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 +2 -1
- package/lib/builder/cell-builder.spec.js +27 -21
- package/lib/builder/cell-builder.spec.js.map +1 -1
- package/lib/builder/pages.spec.d.ts +2 -0
- package/lib/builder/pages.spec.d.ts.map +1 -0
- package/lib/builder/pages.spec.js +96 -0
- package/lib/builder/pages.spec.js.map +1 -0
- package/lib/constants.d.ts +0 -2
- package/lib/constants.d.ts.map +1 -1
- package/lib/constants.js +0 -2
- package/lib/constants.js.map +1 -1
- package/lib/dev/cell-dev-server.d.ts +4 -2
- package/lib/dev/cell-dev-server.d.ts.map +1 -1
- package/lib/dev/cell-dev-server.js +32 -2
- package/lib/dev/cell-dev-server.js.map +1 -1
- package/lib/dev/cell-dev-server.spec.js +23 -2
- package/lib/dev/cell-dev-server.spec.js.map +1 -1
- package/lib/expression/expression-evaluator.d.ts +11 -2
- package/lib/expression/expression-evaluator.d.ts.map +1 -1
- package/lib/expression/expression-evaluator.js +6 -2
- package/lib/expression/expression-evaluator.js.map +1 -1
- package/lib/expression/expression-evaluator.spec.js +3 -3
- package/lib/expression/expression-evaluator.spec.js.map +1 -1
- package/lib/generator/config-generator.d.ts +4 -3
- package/lib/generator/config-generator.d.ts.map +1 -1
- package/lib/generator/config-generator.js +7 -5
- package/lib/generator/config-generator.js.map +1 -1
- package/lib/generator/generator.spec.js +20 -5
- package/lib/generator/generator.spec.js.map +1 -1
- package/lib/generator/html-generator.js +3 -3
- package/lib/generator/html-generator.js.map +1 -1
- package/lib/generator/route-generator.d.ts +13 -0
- package/lib/generator/route-generator.d.ts.map +1 -0
- package/lib/generator/route-generator.js +34 -0
- package/lib/generator/route-generator.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/output/output-writer.d.ts +4 -0
- package/lib/output/output-writer.d.ts.map +1 -1
- package/lib/output/output-writer.js +10 -0
- package/lib/output/output-writer.js.map +1 -1
- package/lib/plugin/cell-plugin.d.ts +17 -2
- package/lib/plugin/cell-plugin.d.ts.map +1 -1
- package/lib/plugin/cell-plugin.js +63 -5
- package/lib/plugin/cell-plugin.js.map +1 -1
- package/lib/plugin/cell-plugin.spec.js +15 -0
- package/lib/plugin/cell-plugin.spec.js.map +1 -1
- package/lib/plugin/class-name-util.d.ts +14 -0
- package/lib/plugin/class-name-util.d.ts.map +1 -0
- package/lib/plugin/class-name-util.js +82 -0
- package/lib/plugin/class-name-util.js.map +1 -0
- package/lib/plugin/class-name-util.spec.d.ts +2 -0
- package/lib/plugin/class-name-util.spec.d.ts.map +1 -0
- package/lib/plugin/class-name-util.spec.js +37 -0
- package/lib/plugin/class-name-util.spec.js.map +1 -0
- package/lib/project/browser-dependency-scanner.d.ts +29 -0
- package/lib/project/browser-dependency-scanner.d.ts.map +1 -0
- package/lib/project/browser-dependency-scanner.js +96 -0
- package/lib/project/browser-dependency-scanner.js.map +1 -0
- package/lib/project/browser-dependency-scanner.spec.d.ts +2 -0
- package/lib/project/browser-dependency-scanner.spec.d.ts.map +1 -0
- package/lib/project/browser-dependency-scanner.spec.js +41 -0
- package/lib/project/browser-dependency-scanner.spec.js.map +1 -0
- package/lib/project/config-loader.d.ts +6 -6
- package/lib/project/config-loader.d.ts.map +1 -1
- package/lib/project/config-loader.js +16 -22
- package/lib/project/config-loader.js.map +1 -1
- package/lib/project/config-schema.d.ts +107 -0
- package/lib/project/config-schema.d.ts.map +1 -0
- package/lib/project/config-schema.js +522 -0
- package/lib/project/config-schema.js.map +1 -0
- package/lib/project/config-schema.spec.d.ts +2 -0
- package/lib/project/config-schema.spec.d.ts.map +1 -0
- package/lib/project/config-schema.spec.js +248 -0
- package/lib/project/config-schema.spec.js.map +1 -0
- package/lib/project/env-overrides.d.ts +14 -10
- package/lib/project/env-overrides.d.ts.map +1 -1
- package/lib/project/env-overrides.js +28 -19
- package/lib/project/env-overrides.js.map +1 -1
- package/lib/project/env-overrides.spec.js +23 -13
- package/lib/project/env-overrides.spec.js.map +1 -1
- package/lib/project/page-scanner.d.ts +26 -0
- package/lib/project/page-scanner.d.ts.map +1 -0
- package/lib/project/page-scanner.js +102 -0
- package/lib/project/page-scanner.js.map +1 -0
- package/lib/project/page-scanner.spec.d.ts +2 -0
- package/lib/project/page-scanner.spec.d.ts.map +1 -0
- package/lib/project/page-scanner.spec.js +68 -0
- package/lib/project/page-scanner.spec.js.map +1 -0
- package/lib/project/project-loader.d.ts +32 -3
- package/lib/project/project-loader.d.ts.map +1 -1
- package/lib/project/project-loader.js +70 -15
- package/lib/project/project-loader.js.map +1 -1
- package/lib/project/project-loader.spec.js +78 -56
- package/lib/project/project-loader.spec.js.map +1 -1
- package/lib/testing/test-app.spec.js +3 -3
- package/lib/testing/test-app.spec.js.map +1 -1
- package/package.json +7 -7
- package/src/constants.ts +0 -3
- package/src/dev/cell-dev-server.ts +39 -5
- package/src/expression/expression-evaluator.ts +16 -3
- package/src/generator/config-generator.ts +7 -5
- package/src/generator/html-generator.ts +3 -3
- package/src/generator/route-generator.ts +34 -0
- package/src/index.ts +1 -0
- package/src/output/output-writer.ts +12 -0
- package/src/plugin/cell-plugin.ts +68 -6
- package/src/plugin/class-name-util.ts +85 -0
- package/src/project/browser-dependency-scanner.ts +102 -0
- package/src/project/config-loader.ts +18 -23
- package/src/project/config-schema.ts +596 -0
- package/src/project/env-overrides.ts +33 -22
- package/src/project/page-scanner.ts +105 -0
- package/src/project/project-loader.ts +82 -17
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { CellError } from '@celljs/kit';
|
|
4
|
+
import { ConfigUtil } from '../utils/config-util.js';
|
|
5
|
+
import type { Component } from './component-collector.js';
|
|
6
|
+
|
|
7
|
+
/** A configuration file as ConfigLoader read it. */
|
|
8
|
+
export interface ConfigLayerFile {
|
|
9
|
+
component: string;
|
|
10
|
+
file: string;
|
|
11
|
+
text: string;
|
|
12
|
+
doc: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The subset of JSON Schema a component's `cell.schema.json` may use (docs/design/config.md §3.2). */
|
|
16
|
+
export interface SchemaNode {
|
|
17
|
+
type?: SchemaType | SchemaType[];
|
|
18
|
+
description?: string;
|
|
19
|
+
default?: unknown;
|
|
20
|
+
properties?: Record<string, SchemaNode>;
|
|
21
|
+
/** Objects with `properties` are closed unless this is given. */
|
|
22
|
+
additionalProperties?: boolean | SchemaNode;
|
|
23
|
+
items?: SchemaNode;
|
|
24
|
+
enum?: unknown[];
|
|
25
|
+
anyOf?: SchemaNode[];
|
|
26
|
+
/** The value (the whole subtree) is part of the frontend configuration. */
|
|
27
|
+
'x-cell-public'?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type SchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null';
|
|
31
|
+
|
|
32
|
+
/** A key of a configuration file that does not fit the schema. */
|
|
33
|
+
export interface ConfigProblem {
|
|
34
|
+
file: string;
|
|
35
|
+
/** Dotted key, e.g. `server.prot`. */
|
|
36
|
+
path: string;
|
|
37
|
+
line?: number;
|
|
38
|
+
message: string;
|
|
39
|
+
hint: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const KEYWORDS = new Set([
|
|
43
|
+
'type',
|
|
44
|
+
'description',
|
|
45
|
+
'default',
|
|
46
|
+
'properties',
|
|
47
|
+
'additionalProperties',
|
|
48
|
+
'items',
|
|
49
|
+
'enum',
|
|
50
|
+
'anyOf',
|
|
51
|
+
'x-cell-public',
|
|
52
|
+
'$schema',
|
|
53
|
+
'title',
|
|
54
|
+
'examples',
|
|
55
|
+
]);
|
|
56
|
+
const TYPES = new Set<SchemaType>(['string', 'number', 'integer', 'boolean', 'object', 'array', 'null']);
|
|
57
|
+
/** Keys of the configuration format before docs/design/config.md, with what replaced them. */
|
|
58
|
+
const RETIRED: Record<string, string> = {
|
|
59
|
+
cell: 'cell.yml has no `cell:` level any more: move its keys to the top level (cell.server.port → server.port).',
|
|
60
|
+
frontend:
|
|
61
|
+
'cell.yml has no frontend section any more: write the keys at the top level; the components decide which keys reach the browser, and your own frontend keys go under app.public.',
|
|
62
|
+
backend: 'cell.yml has no backend section any more: write the keys at the top level; configuration is server-only unless its schema marks it public.',
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The configuration schema of a project: the `cell.schema.json` of every component merged into one
|
|
67
|
+
* tree. It says which component owns a key, validates configuration files, picks the keys that
|
|
68
|
+
* reach the browser (`x-cell-public`) and the types that expressions convert to.
|
|
69
|
+
*/
|
|
70
|
+
export class ConfigSchema {
|
|
71
|
+
static readonly FILE = 'cell.schema.json';
|
|
72
|
+
/** Where the merged schema is written for editors (yaml-language-server) and agents. */
|
|
73
|
+
static readonly OUTPUT = '.cell/schema/cell.yml.json';
|
|
74
|
+
/** Kept for applications: no component may declare it (docs/design/config.md §3.2). */
|
|
75
|
+
static readonly APP_KEY = 'app';
|
|
76
|
+
|
|
77
|
+
/** Owner of the keys the toolchain itself defines: `app`, `mode` and `paths`. */
|
|
78
|
+
static readonly TOOLCHAIN = 'cell';
|
|
79
|
+
|
|
80
|
+
/** Keys every project has, whatever its components (docs/design/config.md §3.2). */
|
|
81
|
+
static builtin(): Record<string, SchemaNode> {
|
|
82
|
+
return {
|
|
83
|
+
app: {
|
|
84
|
+
type: 'object',
|
|
85
|
+
description: 'Your own configuration. No component declares keys here; the browser only receives app.public.',
|
|
86
|
+
additionalProperties: true,
|
|
87
|
+
properties: {
|
|
88
|
+
public: {
|
|
89
|
+
type: 'object',
|
|
90
|
+
description: 'Your own configuration that the frontend needs as well.',
|
|
91
|
+
additionalProperties: true,
|
|
92
|
+
'x-cell-public': true,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
mode: {
|
|
97
|
+
type: 'array',
|
|
98
|
+
items: { type: 'string' },
|
|
99
|
+
description: 'Active modes, set by the toolchain (cell dev / cell build / -m); not written in cell.yml.',
|
|
100
|
+
'x-cell-public': true,
|
|
101
|
+
},
|
|
102
|
+
paths: {
|
|
103
|
+
type: 'object',
|
|
104
|
+
description: 'Paths the toolchain gives the server.',
|
|
105
|
+
properties: {
|
|
106
|
+
public: {
|
|
107
|
+
type: 'string',
|
|
108
|
+
description: 'Directory of the frontend files the server serves; relative to the output directory unless absolute.',
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
additionalProperties: { type: 'string' },
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
readonly root: SchemaNode & { properties: Record<string, SchemaNode> } = { type: 'object', properties: {}, additionalProperties: false };
|
|
117
|
+
protected readonly owners = new Map<SchemaNode, string[]>();
|
|
118
|
+
|
|
119
|
+
constructor() {
|
|
120
|
+
for (const [key, node] of Object.entries(ConfigSchema.builtin())) {
|
|
121
|
+
this.root.properties[key] = this.adopt(node, ConfigSchema.TOOLCHAIN);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Merges the schemas of `components` in order. The project (the root component) may describe
|
|
127
|
+
* keys under `app` only; components without a schema own the top-level keys of their cell.yml
|
|
128
|
+
* that no schema claims, without checks below them.
|
|
129
|
+
*/
|
|
130
|
+
static load(components: Component[], layers: ConfigLayerFile[]): ConfigSchema {
|
|
131
|
+
const schema = new ConfigSchema();
|
|
132
|
+
const implicit: Component[] = [];
|
|
133
|
+
for (const component of components) {
|
|
134
|
+
const file = join(component.dir, ConfigSchema.FILE);
|
|
135
|
+
if (existsSync(file)) {
|
|
136
|
+
schema.add(ConfigSchema.read(file), component, file);
|
|
137
|
+
} else if (!component.root) {
|
|
138
|
+
implicit.push(component);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
for (const component of implicit) {
|
|
142
|
+
for (const layer of layers.filter(l => l.component === component.name)) {
|
|
143
|
+
for (const key of Object.keys(layer.doc)) {
|
|
144
|
+
if (!schema.root.properties[key]) {
|
|
145
|
+
const node: SchemaNode = {};
|
|
146
|
+
schema.root.properties[key] = node;
|
|
147
|
+
schema.owners.set(node, [component.name]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return schema;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static read(file: string): SchemaNode {
|
|
156
|
+
let value: unknown;
|
|
157
|
+
try {
|
|
158
|
+
value = JSON.parse(readFileSync(file, 'utf8'));
|
|
159
|
+
} catch (cause) {
|
|
160
|
+
throw new CellError({
|
|
161
|
+
code: 'CELL_CONFIG_SCHEMA_INVALID',
|
|
162
|
+
message: `Invalid JSON in ${file}: ${(cause as Error).message}`,
|
|
163
|
+
file,
|
|
164
|
+
hint: 'Fix the JSON syntax.',
|
|
165
|
+
cause,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
const problem = ConfigSchema.checkNode(value, '');
|
|
169
|
+
if (problem) {
|
|
170
|
+
throw new CellError({
|
|
171
|
+
code: 'CELL_CONFIG_SCHEMA_INVALID',
|
|
172
|
+
message: `${file}: ${problem}`,
|
|
173
|
+
file,
|
|
174
|
+
hint: 'Use the JSON Schema keywords of docs/design/config.md §3.2.',
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
if (!ConfigUtil.isPlainObject((value as SchemaNode).properties)) {
|
|
178
|
+
throw new CellError({
|
|
179
|
+
code: 'CELL_CONFIG_SCHEMA_INVALID',
|
|
180
|
+
message: `${file} has no top-level properties.`,
|
|
181
|
+
file,
|
|
182
|
+
hint: 'Declare the keys the component owns under "properties".',
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return value as SchemaNode;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The first unsupported keyword or malformed value in `node`, if any. */
|
|
189
|
+
protected static checkNode(node: unknown, path: string): string | undefined {
|
|
190
|
+
if (!ConfigUtil.isPlainObject(node)) {
|
|
191
|
+
return `${path || 'the schema'} must be an object.`;
|
|
192
|
+
}
|
|
193
|
+
for (const key of Object.keys(node)) {
|
|
194
|
+
if (!KEYWORDS.has(key)) {
|
|
195
|
+
return `unsupported keyword '${key}' at ${path || 'the top level'}.`;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const types = node.type === undefined ? [] : Array.isArray(node.type) ? node.type : [node.type];
|
|
199
|
+
const bad = types.find((type: unknown) => !TYPES.has(type as SchemaType));
|
|
200
|
+
if (bad !== undefined) {
|
|
201
|
+
return `unknown type '${bad}' at ${path || 'the top level'}.`;
|
|
202
|
+
}
|
|
203
|
+
const children: [string, unknown][] = [
|
|
204
|
+
...Object.entries((node.properties ?? {}) as Record<string, unknown>).map(([k, v]) => [path ? `${path}.${k}` : k, v] as [string, unknown]),
|
|
205
|
+
...(ConfigUtil.isPlainObject(node.additionalProperties) ? [[`${path}.*`, node.additionalProperties] as [string, unknown]] : []),
|
|
206
|
+
...(node.items !== undefined ? [[`${path}[]`, node.items] as [string, unknown]] : []),
|
|
207
|
+
...((node.anyOf ?? []) as unknown[]).map((n, i) => [`${path}.anyOf[${i}]`, n] as [string, unknown]),
|
|
208
|
+
];
|
|
209
|
+
for (const [childPath, child] of children) {
|
|
210
|
+
const problem = ConfigSchema.checkNode(child, childPath);
|
|
211
|
+
if (problem) {
|
|
212
|
+
return problem;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
protected add(source: SchemaNode, component: Component, file: string): void {
|
|
219
|
+
for (const [key, node] of Object.entries(source.properties ?? {})) {
|
|
220
|
+
if (!component.root && key in ConfigSchema.builtin()) {
|
|
221
|
+
throw new CellError({
|
|
222
|
+
code: 'CELL_CONFIG_KEY_CONFLICT',
|
|
223
|
+
message: `${component.name} declares '${key}', which the toolchain keeps${key === ConfigSchema.APP_KEY ? ' for applications' : ''}.`,
|
|
224
|
+
file,
|
|
225
|
+
hint: `Rename the key in ${ConfigSchema.FILE} and cell.yml of ${component.name}, e.g. after the package.`,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
if (component.root && key !== ConfigSchema.APP_KEY) {
|
|
229
|
+
throw new CellError({
|
|
230
|
+
code: 'CELL_CONFIG_SCHEMA_INVALID',
|
|
231
|
+
message: `The project's ${ConfigSchema.FILE} declares '${key}'; an application describes its own keys under app.`,
|
|
232
|
+
file,
|
|
233
|
+
hint: `Move '${key}' under properties.app.properties in ${ConfigSchema.FILE}, and the value under app: in cell.yml.`,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
this.merge(this.root, key, node, component.name, file, key);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
protected merge(parent: SchemaNode, key: string, node: SchemaNode, owner: string, file: string, path: string): void {
|
|
241
|
+
parent.properties ??= {};
|
|
242
|
+
const properties = parent.properties;
|
|
243
|
+
const existing = properties[key];
|
|
244
|
+
if (!existing) {
|
|
245
|
+
properties[key] = this.adopt(structuredClone(node), owner);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (!ConfigSchema.isObject(existing) || !ConfigSchema.isObject(node)) {
|
|
249
|
+
throw new CellError({
|
|
250
|
+
code: 'CELL_CONFIG_KEY_CONFLICT',
|
|
251
|
+
message: `'${path}' is declared by both ${this.ownersOf(existing).join(', ')} and ${owner}.`,
|
|
252
|
+
file,
|
|
253
|
+
hint: 'Each key has one owner: rename the key in one of the components, or declare it as an object both add properties to.',
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
if (node.additionalProperties !== undefined) {
|
|
257
|
+
if (existing.additionalProperties !== undefined && JSON.stringify(existing.additionalProperties) !== JSON.stringify(node.additionalProperties)) {
|
|
258
|
+
throw new CellError({
|
|
259
|
+
code: 'CELL_CONFIG_KEY_CONFLICT',
|
|
260
|
+
message: `'${path}' has different additionalProperties in ${this.ownersOf(existing).join(', ')} and ${owner}.`,
|
|
261
|
+
file,
|
|
262
|
+
hint: 'Declare additionalProperties of a shared object in one component only.',
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
existing.additionalProperties = structuredClone(node.additionalProperties);
|
|
266
|
+
}
|
|
267
|
+
existing.description ??= node.description;
|
|
268
|
+
if (node['x-cell-public']) {
|
|
269
|
+
existing['x-cell-public'] = true;
|
|
270
|
+
}
|
|
271
|
+
this.owners.set(existing, [...this.ownersOf(existing), owner]);
|
|
272
|
+
for (const [childKey, child] of Object.entries(node.properties ?? {})) {
|
|
273
|
+
this.merge(existing, childKey, child, owner, file, `${path}.${childKey}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
protected adopt(node: SchemaNode, owner: string): SchemaNode {
|
|
278
|
+
this.owners.set(node, [owner]);
|
|
279
|
+
for (const child of Object.values(node.properties ?? {})) {
|
|
280
|
+
this.adopt(child, owner);
|
|
281
|
+
}
|
|
282
|
+
return node;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
protected ownersOf(node: SchemaNode): string[] {
|
|
286
|
+
return this.owners.get(node) ?? [];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** An object schema that only describes properties, which other components may extend. */
|
|
290
|
+
protected static isObject(node: SchemaNode): boolean {
|
|
291
|
+
return node.type === 'object' && node.items === undefined && node.enum === undefined && node.anyOf === undefined;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** The schema of a child key, or `undefined` when `node` does not allow it; `null` when anything goes. */
|
|
295
|
+
protected static child(node: SchemaNode, key: string): SchemaNode | null | undefined {
|
|
296
|
+
const declared = node.properties?.[key];
|
|
297
|
+
if (declared) {
|
|
298
|
+
return declared;
|
|
299
|
+
}
|
|
300
|
+
const extra = node.additionalProperties;
|
|
301
|
+
if (ConfigUtil.isPlainObject(extra)) {
|
|
302
|
+
return extra as SchemaNode;
|
|
303
|
+
}
|
|
304
|
+
if (extra === false || (extra === undefined && node.properties)) {
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** The schema of a dotted key (array items by index), with the components that own it. */
|
|
311
|
+
describe(path: string[]): { node?: SchemaNode; owners: string[]; public: boolean } {
|
|
312
|
+
let node: SchemaNode | null | undefined = this.root;
|
|
313
|
+
let owners: string[] = [];
|
|
314
|
+
let isPublic = false;
|
|
315
|
+
for (const key of path) {
|
|
316
|
+
if (!node) {
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
node = /^\d+$/.test(key) && node.items ? node.items : ConfigSchema.child(node, key);
|
|
320
|
+
if (node) {
|
|
321
|
+
owners = this.owners.get(node) ?? owners;
|
|
322
|
+
isPublic ||= node['x-cell-public'] === true;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return { node: node ?? undefined, owners, public: isPublic };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Problems of one configuration file (a partial configuration). */
|
|
329
|
+
validate(doc: Record<string, unknown>, file: string, text: string): ConfigProblem[] {
|
|
330
|
+
const problems: ConfigProblem[] = [];
|
|
331
|
+
const report = (path: string[], message: string, hint: string) =>
|
|
332
|
+
problems.push({ file, path: path.join('.'), line: ConfigSchema.lineOf(text, path), message, hint });
|
|
333
|
+
for (const [key, value] of Object.entries(doc)) {
|
|
334
|
+
const node = this.root.properties[key];
|
|
335
|
+
if (!node) {
|
|
336
|
+
const known = Object.keys(this.root.properties);
|
|
337
|
+
const close = ConfigSchema.closest(key, known);
|
|
338
|
+
report(
|
|
339
|
+
[key],
|
|
340
|
+
`'${key}' is not a configuration key of any component.`,
|
|
341
|
+
RETIRED[key] ??
|
|
342
|
+
(close
|
|
343
|
+
? `Did you mean '${close}'? Your own configuration goes under app.`
|
|
344
|
+
: `Put your own configuration under app (app.${key}); the keys of the components are in ${ConfigSchema.OUTPUT}.`),
|
|
345
|
+
);
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
this.check(value, node, [key], report);
|
|
349
|
+
}
|
|
350
|
+
return problems;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
protected check(value: unknown, node: SchemaNode, path: string[], report: (path: string[], message: string, hint: string) => void): void {
|
|
354
|
+
if (value === null || (typeof value === 'string' && value.includes('${'))) {
|
|
355
|
+
// null unsets a key; expressions are evaluated (and converted) later.
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
const key = path.join('.');
|
|
359
|
+
if (node.anyOf) {
|
|
360
|
+
const fits = node.anyOf.some(option => {
|
|
361
|
+
let ok = true;
|
|
362
|
+
this.check(value, option, path, () => {
|
|
363
|
+
ok = false;
|
|
364
|
+
});
|
|
365
|
+
return ok;
|
|
366
|
+
});
|
|
367
|
+
if (!fits) {
|
|
368
|
+
report(path, `${key} does not fit any of the allowed forms.`, ConfigSchema.describeHint(node));
|
|
369
|
+
}
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const types = node.type === undefined ? [] : Array.isArray(node.type) ? node.type : [node.type];
|
|
373
|
+
if (types.length > 0 && !types.some(type => ConfigSchema.isType(value, type))) {
|
|
374
|
+
report(path, `${key} must be ${types.join(' or ')}, got ${ConfigSchema.typeOf(value)} ${JSON.stringify(value)}.`, ConfigSchema.describeHint(node));
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (node.enum && !node.enum.some(option => option === value)) {
|
|
378
|
+
report(
|
|
379
|
+
path,
|
|
380
|
+
`${key} must be one of ${node.enum.map(v => JSON.stringify(v)).join(', ')}, got ${JSON.stringify(value)}.`,
|
|
381
|
+
ConfigSchema.describeHint(node),
|
|
382
|
+
);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (Array.isArray(value)) {
|
|
386
|
+
const items = node.items;
|
|
387
|
+
if (items) {
|
|
388
|
+
value.forEach((item, i) => {
|
|
389
|
+
this.check(item, items, [...path, String(i)], report);
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (ConfigUtil.isPlainObject(value)) {
|
|
395
|
+
for (const [childKey, child] of Object.entries(value)) {
|
|
396
|
+
const childNode = ConfigSchema.child(node, childKey);
|
|
397
|
+
if (childNode === undefined) {
|
|
398
|
+
const known = Object.keys(node.properties ?? {});
|
|
399
|
+
const close = ConfigSchema.closest(childKey, known);
|
|
400
|
+
const owners = this.owners.get(node);
|
|
401
|
+
report(
|
|
402
|
+
[...path, childKey],
|
|
403
|
+
`'${childKey}' is not an option of ${key}${owners ? ` (${owners.join(', ')})` : ''}.`,
|
|
404
|
+
close
|
|
405
|
+
? `Did you mean '${key}.${close}'?`
|
|
406
|
+
: `Options of ${key}: ${known.join(', ') || '(none)'}. Your own configuration goes under app.`,
|
|
407
|
+
);
|
|
408
|
+
} else if (childNode) {
|
|
409
|
+
this.check(child, childNode, [...path, childKey], report);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
protected static describeHint(node: SchemaNode): string {
|
|
416
|
+
return node.description ? `${node.description}` : 'Fix the value to match the schema.';
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
protected static typeOf(value: unknown): string {
|
|
420
|
+
return Array.isArray(value) ? 'array' : value === null ? 'null' : typeof value;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
protected static isType(value: unknown, type: SchemaType): boolean {
|
|
424
|
+
switch (type) {
|
|
425
|
+
case 'integer':
|
|
426
|
+
return Number.isInteger(value);
|
|
427
|
+
case 'number':
|
|
428
|
+
return typeof value === 'number';
|
|
429
|
+
case 'array':
|
|
430
|
+
return Array.isArray(value);
|
|
431
|
+
case 'object':
|
|
432
|
+
return ConfigUtil.isPlainObject(value);
|
|
433
|
+
case 'null':
|
|
434
|
+
return value === null;
|
|
435
|
+
default:
|
|
436
|
+
return typeof value === type;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* The frontend configuration: the values of keys marked `x-cell-public` (whole subtrees), and
|
|
442
|
+
* nothing else.
|
|
443
|
+
*/
|
|
444
|
+
project(config: Record<string, unknown>): Record<string, unknown> {
|
|
445
|
+
return (ConfigSchema.pick(config, this.root) as Record<string, unknown> | undefined) ?? {};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
protected static pick(value: unknown, node: SchemaNode): unknown {
|
|
449
|
+
if (node['x-cell-public']) {
|
|
450
|
+
return structuredClone(value);
|
|
451
|
+
}
|
|
452
|
+
if (!ConfigUtil.isPlainObject(value)) {
|
|
453
|
+
return undefined;
|
|
454
|
+
}
|
|
455
|
+
const result: Record<string, unknown> = {};
|
|
456
|
+
for (const [key, child] of Object.entries(value)) {
|
|
457
|
+
const childNode = ConfigSchema.child(node, key);
|
|
458
|
+
const picked = childNode ? ConfigSchema.pick(child, childNode) : undefined;
|
|
459
|
+
if (picked !== undefined) {
|
|
460
|
+
result[key] = picked;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return Object.keys(result).length > 0 ? result : undefined;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** Whether the frontend sees the key `path` (dotted). */
|
|
467
|
+
isPublic(path: string): boolean {
|
|
468
|
+
return this.describe(path.split('.')).public;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Schema types of the keys whose value is a `${...}` expression and whose schema type is a
|
|
473
|
+
* number, an integer or a boolean: what evaluation converts the expression's text to.
|
|
474
|
+
*/
|
|
475
|
+
types(config: Record<string, unknown>): Record<string, 'number' | 'integer' | 'boolean'> {
|
|
476
|
+
const types: Record<string, 'number' | 'integer' | 'boolean'> = {};
|
|
477
|
+
const walk = (value: unknown, node: SchemaNode | null | undefined, path: string[]) => {
|
|
478
|
+
if (!node) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
if (typeof value === 'string') {
|
|
482
|
+
const scalar = ConfigSchema.scalarType(node);
|
|
483
|
+
if (scalar && value.includes('${')) {
|
|
484
|
+
types[path.join('.')] = scalar;
|
|
485
|
+
}
|
|
486
|
+
} else if (Array.isArray(value)) {
|
|
487
|
+
value.forEach((item, i) => {
|
|
488
|
+
walk(item, node.items, [...path, String(i)]);
|
|
489
|
+
});
|
|
490
|
+
} else if (ConfigUtil.isPlainObject(value)) {
|
|
491
|
+
for (const [key, child] of Object.entries(value)) {
|
|
492
|
+
walk(child, ConfigSchema.child(node, key), [...path, key]);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
walk(config, this.root, []);
|
|
497
|
+
return types;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
protected static scalarType(node: SchemaNode): 'number' | 'integer' | 'boolean' | undefined {
|
|
501
|
+
const types = (node.type === undefined ? [] : Array.isArray(node.type) ? node.type : [node.type]).filter(type => type !== 'null');
|
|
502
|
+
return types.length === 1 && (types[0] === 'number' || types[0] === 'integer' || types[0] === 'boolean') ? types[0] : undefined;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The merged schema as standard JSON Schema for editors: closed objects say so, and keys of
|
|
507
|
+
* other types also take a `${...}` expression.
|
|
508
|
+
*/
|
|
509
|
+
toJSON(): Record<string, unknown> {
|
|
510
|
+
const convert = (node: SchemaNode): Record<string, unknown> => {
|
|
511
|
+
const result: Record<string, unknown> = { ...node };
|
|
512
|
+
if (node.properties) {
|
|
513
|
+
result.properties = Object.fromEntries(Object.entries(node.properties).map(([k, v]) => [k, convert(v)]));
|
|
514
|
+
if (node.additionalProperties === undefined) {
|
|
515
|
+
result.additionalProperties = false;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (ConfigUtil.isPlainObject(node.additionalProperties)) {
|
|
519
|
+
result.additionalProperties = convert(node.additionalProperties as SchemaNode);
|
|
520
|
+
}
|
|
521
|
+
if (node.items) {
|
|
522
|
+
result.items = convert(node.items);
|
|
523
|
+
}
|
|
524
|
+
if (node.anyOf) {
|
|
525
|
+
result.anyOf = node.anyOf.map(convert);
|
|
526
|
+
}
|
|
527
|
+
const types = node.type === undefined ? [] : Array.isArray(node.type) ? node.type : [node.type];
|
|
528
|
+
const scalar = types.length > 0 && types.every(type => type !== 'object' && type !== 'array');
|
|
529
|
+
if (scalar || (node.enum && types.length === 0)) {
|
|
530
|
+
const { description, default: defaultValue, 'x-cell-public': isPublic, ...shape } = result;
|
|
531
|
+
const meta = {
|
|
532
|
+
...(description ? { description } : {}),
|
|
533
|
+
...(defaultValue !== undefined ? { default: defaultValue } : {}),
|
|
534
|
+
...(isPublic ? { 'x-cell-public': true } : {}),
|
|
535
|
+
};
|
|
536
|
+
return { ...meta, anyOf: [shape, { type: 'string', pattern: '\\$\\{' }, { type: 'null' }] };
|
|
537
|
+
}
|
|
538
|
+
return result;
|
|
539
|
+
};
|
|
540
|
+
return { $schema: 'http://json-schema.org/draft-07/schema#', title: 'cell.yml', ...convert(this.root) };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/** Best-effort line of a dotted key in a YAML text (block mappings), for error messages. */
|
|
544
|
+
static lineOf(text: string, path: string[]): number | undefined {
|
|
545
|
+
const keys = path.filter(part => !/^\d+$/.test(part));
|
|
546
|
+
const stack: { indent: number; key: string }[] = [];
|
|
547
|
+
const lines = text.split(/\r?\n/);
|
|
548
|
+
for (let i = 0; i < lines.length; i++) {
|
|
549
|
+
const match = /^(\s*)(?:-\s+)?(["']?)([^"'#:\s][^"'#:]*?)\2\s*:(?:\s|$)/.exec(lines[i]);
|
|
550
|
+
if (!match) {
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
const indent = match[1].length + (lines[i].slice(match[1].length).startsWith('-') ? 2 : 0);
|
|
554
|
+
while (stack.length > 0 && stack[stack.length - 1].indent >= indent) {
|
|
555
|
+
stack.pop();
|
|
556
|
+
}
|
|
557
|
+
stack.push({ indent, key: match[3] });
|
|
558
|
+
if (stack.length === keys.length && stack.every((entry, j) => entry.key === keys[j])) {
|
|
559
|
+
return i + 1;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return undefined;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/** The candidate within edit distance 2 (or a case-insensitive match), if one is clearly meant. */
|
|
566
|
+
static closest(word: string, candidates: string[]): string | undefined {
|
|
567
|
+
const lower = word.toLowerCase();
|
|
568
|
+
let best: string | undefined;
|
|
569
|
+
let bestDistance = 3;
|
|
570
|
+
for (const candidate of candidates) {
|
|
571
|
+
if (candidate.toLowerCase() === lower) {
|
|
572
|
+
return candidate;
|
|
573
|
+
}
|
|
574
|
+
const distance = ConfigSchema.distance(lower, candidate.toLowerCase());
|
|
575
|
+
if (distance < bestDistance) {
|
|
576
|
+
best = candidate;
|
|
577
|
+
bestDistance = distance;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return best;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
protected static distance(a: string, b: string): number {
|
|
584
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
585
|
+
for (let i = 1; i <= a.length; i++) {
|
|
586
|
+
let previous = row[0];
|
|
587
|
+
row[0] = i;
|
|
588
|
+
for (let j = 1; j <= b.length; j++) {
|
|
589
|
+
const current = row[j];
|
|
590
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, previous + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
591
|
+
previous = current;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return row[b.length];
|
|
595
|
+
}
|
|
596
|
+
}
|
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
|
-
import { CellError, type EnvOverrideReport
|
|
3
|
+
import { CellError, type EnvOverrideReport } from '@celljs/kit';
|
|
4
4
|
|
|
5
5
|
interface EnvConfigUtil {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
PREFIX: string;
|
|
7
|
+
RESERVED: readonly string[];
|
|
8
|
+
apply(config: Record<string, unknown>, env: Record<string, string | undefined>, types?: Record<string, string>): EnvOverrideReport;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
|
-
* Applies `
|
|
12
|
-
*
|
|
12
|
+
* Applies `CELL_*` environment variables with the `EnvConfigUtil` of the project's own
|
|
13
|
+
* @celljs/core (`@celljs/core/el`), the same code the server runs at startup, so build and runtime agree.
|
|
13
14
|
*/
|
|
14
15
|
export class EnvOverrides {
|
|
15
|
-
static readonly
|
|
16
|
+
static readonly PREFIX = 'CELL_';
|
|
17
|
+
/** Toolchain variables with the prefix (`CELL_PRESET`...); the same list as `EnvConfigUtil.RESERVED` of @celljs/core, which a test compares. */
|
|
18
|
+
static readonly RESERVED: readonly string[] = ['CELL_PRESET', 'CELL_MODE', 'CELL_EVENT', 'CELL_OUT_DIR', 'CELL_DEBUG'];
|
|
16
19
|
|
|
17
20
|
protected util?: EnvConfigUtil;
|
|
18
21
|
|
|
@@ -21,35 +24,43 @@ export class EnvOverrides {
|
|
|
21
24
|
protected readonly env: Record<string, string | undefined>,
|
|
22
25
|
) {}
|
|
23
26
|
|
|
24
|
-
/** Applies the variables
|
|
25
|
-
apply(
|
|
26
|
-
|
|
27
|
-
if (!Object.keys(this.env).some(name => name.startsWith(prefix))) {
|
|
27
|
+
/** Applies the variables to `config` in place. */
|
|
28
|
+
apply(config: Record<string, unknown>, types: Record<string, string> = {}): EnvOverrideReport {
|
|
29
|
+
if (!Object.keys(this.env).some(name => name.startsWith(EnvOverrides.PREFIX) && !EnvOverrides.RESERVED.includes(name))) {
|
|
28
30
|
return { applied: [], problems: [] };
|
|
29
31
|
}
|
|
30
|
-
return this.load().apply(config, this.env,
|
|
32
|
+
return this.load().apply(config, this.env, types);
|
|
31
33
|
}
|
|
32
34
|
|
|
33
|
-
/** What the variables
|
|
34
|
-
preview(
|
|
35
|
-
return this.apply(
|
|
35
|
+
/** What the variables would change, leaving `config` alone. */
|
|
36
|
+
preview(config: Record<string, unknown>, types: Record<string, string> = {}): EnvOverrideReport {
|
|
37
|
+
return this.apply(structuredClone(config), types);
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
protected load(): EnvConfigUtil {
|
|
39
41
|
if (!this.util) {
|
|
40
42
|
const require = createRequire(join(this.root, 'package.json'));
|
|
43
|
+
let util: EnvConfigUtil | undefined;
|
|
41
44
|
try {
|
|
42
|
-
|
|
45
|
+
util = require('@celljs/core/el').EnvConfigUtil as EnvConfigUtil | undefined;
|
|
43
46
|
} catch (cause) {
|
|
44
|
-
throw
|
|
45
|
-
code: 'CELL_CORE_NOT_FOUND',
|
|
46
|
-
message: 'The @celljs/core of this project cannot apply CELL_FRONTEND__ / CELL_BACKEND__ variables.',
|
|
47
|
-
file: join(this.root, 'package.json'),
|
|
48
|
-
hint: 'Depend on @celljs/core 4 or later.',
|
|
49
|
-
cause,
|
|
50
|
-
});
|
|
47
|
+
throw EnvOverrides.missing(this.root, cause);
|
|
51
48
|
}
|
|
49
|
+
if (!util) {
|
|
50
|
+
throw EnvOverrides.missing(this.root);
|
|
51
|
+
}
|
|
52
|
+
this.util = util;
|
|
52
53
|
}
|
|
53
54
|
return this.util;
|
|
54
55
|
}
|
|
56
|
+
|
|
57
|
+
protected static missing(root: string, cause?: unknown): CellError {
|
|
58
|
+
return new CellError({
|
|
59
|
+
code: 'CELL_CORE_NOT_FOUND',
|
|
60
|
+
message: 'The @celljs/core of this project cannot apply CELL_ variables.',
|
|
61
|
+
file: join(root, 'package.json'),
|
|
62
|
+
hint: 'Upgrade @celljs/core and the other @celljs/* packages to the version of @celljs/cli.',
|
|
63
|
+
cause,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
55
66
|
}
|