@parlajs/compiler 0.0.0-stage → 0.0.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 +21 -2
- package/build/build.d.ts +18 -0
- package/build/build.d.ts.map +1 -0
- package/build/build.js +10 -0
- package/build/cli.d.ts +3 -0
- package/build/cli.d.ts.map +1 -0
- package/build/cli.js +8 -0
- package/build/command.d.ts +13 -0
- package/build/command.d.ts.map +1 -0
- package/build/command.js +156 -0
- package/build/compile/code.d.ts +13 -0
- package/build/compile/code.d.ts.map +1 -0
- package/build/compile/code.js +42 -0
- package/build/compile/compile.d.ts +11 -0
- package/build/compile/compile.d.ts.map +1 -0
- package/build/compile/compile.js +244 -0
- package/build/compile/error.d.ts +4 -0
- package/build/compile/error.d.ts.map +1 -0
- package/build/compile/error.js +3 -0
- package/build/compile/info.d.ts +49 -0
- package/build/compile/info.d.ts.map +1 -0
- package/build/compile/info.js +22 -0
- package/build/compile/scope.d.ts +7 -0
- package/build/compile/scope.d.ts.map +1 -0
- package/build/compile/scope.js +14 -0
- package/build/compile/single.d.ts +11 -0
- package/build/compile/single.d.ts.map +1 -0
- package/build/compile/single.js +36 -0
- package/build/compile/variants.d.ts +16 -0
- package/build/compile/variants.d.ts.map +1 -0
- package/build/compile/variants.js +48 -0
- package/build/compile.d.ts +61 -0
- package/build/compile.d.ts.map +1 -0
- package/build/compile.js +385 -0
- package/build/config/checks.d.ts +21 -0
- package/build/config/checks.d.ts.map +1 -0
- package/build/config/checks.js +97 -0
- package/build/config/config.d.ts +71 -0
- package/build/config/config.d.ts.map +1 -0
- package/build/config/config.js +7 -0
- package/build/config/fallbacks.d.ts +6 -0
- package/build/config/fallbacks.d.ts.map +1 -0
- package/build/config/fallbacks.js +13 -0
- package/build/config/import.d.ts +31 -0
- package/build/config/import.d.ts.map +1 -0
- package/build/config/import.js +115 -0
- package/build/config/load.d.ts +13 -0
- package/build/config/load.d.ts.map +1 -0
- package/build/config/load.js +59 -0
- package/build/config.d.ts +77 -0
- package/build/config.d.ts.map +1 -0
- package/build/config.js +223 -0
- package/build/diagnostics.d.ts +31 -0
- package/build/diagnostics.d.ts.map +1 -0
- package/build/diagnostics.js +41 -0
- package/build/generate/checks.d.ts +9 -0
- package/build/generate/checks.d.ts.map +1 -0
- package/build/generate/checks.js +93 -0
- package/build/generate/chunk.d.ts +11 -0
- package/build/generate/chunk.d.ts.map +1 -0
- package/build/generate/chunk.js +57 -0
- package/build/generate/digits.d.ts +8 -0
- package/build/generate/digits.d.ts.map +1 -0
- package/build/generate/digits.js +57 -0
- package/build/generate/files.d.ts +7 -0
- package/build/generate/files.d.ts.map +1 -0
- package/build/generate/files.js +59 -0
- package/build/generate/locale.d.ts +21 -0
- package/build/generate/locale.d.ts.map +1 -0
- package/build/generate/locale.js +108 -0
- package/build/generate/modules.d.ts +27 -0
- package/build/generate/modules.d.ts.map +1 -0
- package/build/generate/modules.js +61 -0
- package/build/generate/namespace.d.ts +19 -0
- package/build/generate/namespace.d.ts.map +1 -0
- package/build/generate/namespace.js +65 -0
- package/build/generate/namespaces.d.ts +5 -0
- package/build/generate/namespaces.d.ts.map +1 -0
- package/build/generate/namespaces.js +41 -0
- package/build/generate/read.d.ts +21 -0
- package/build/generate/read.d.ts.map +1 -0
- package/build/generate/read.js +115 -0
- package/build/index.d.ts +9 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7 -0
- package/build/init/detect.d.ts +43 -0
- package/build/init/detect.d.ts.map +1 -0
- package/build/init/detect.js +69 -0
- package/build/init/init.d.ts +12 -0
- package/build/init/init.d.ts.map +1 -0
- package/build/init/init.js +126 -0
- package/build/options/date.d.ts +3 -0
- package/build/options/date.d.ts.map +1 -0
- package/build/options/date.js +77 -0
- package/build/options/literal.d.ts +10 -0
- package/build/options/literal.d.ts.map +1 -0
- package/build/options/literal.js +11 -0
- package/build/options/names.d.ts +5 -0
- package/build/options/names.d.ts.map +1 -0
- package/build/options/names.js +56 -0
- package/build/options/number.d.ts +3 -0
- package/build/options/number.d.ts.map +1 -0
- package/build/options/number.js +17 -0
- package/build/options/options.d.ts +7 -0
- package/build/options/options.d.ts.map +1 -0
- package/build/options/options.js +66 -0
- package/build/options.d.ts +9 -0
- package/build/options.d.ts.map +1 -0
- package/build/options.js +214 -0
- package/build/plurals.d.ts +6 -0
- package/build/plurals.d.ts.map +1 -0
- package/build/plurals.js +50 -0
- package/build/types/names.d.ts +9 -0
- package/build/types/names.d.ts.map +1 -0
- package/build/types/names.js +109 -0
- package/build/types/namespace.d.ts +17 -0
- package/build/types/namespace.d.ts.map +1 -0
- package/build/types/namespace.js +113 -0
- package/build/types/signature.d.ts +8 -0
- package/build/types/signature.d.ts.map +1 -0
- package/build/types/signature.js +87 -0
- package/build/types.d.ts +25 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +250 -0
- package/build/vite.d.ts +9 -0
- package/build/vite.d.ts.map +1 -0
- package/build/vite.js +125 -0
- package/build/watch.d.ts +12 -0
- package/build/watch.d.ts.map +1 -0
- package/build/watch.js +93 -0
- package/package.json +84 -3
- package/src/build.ts +31 -0
- package/src/cli.ts +9 -0
- package/src/command.ts +195 -0
- package/src/compile/code.ts +51 -0
- package/src/compile/compile.ts +289 -0
- package/src/compile/error.ts +3 -0
- package/src/compile/info.ts +63 -0
- package/src/compile/scope.ts +14 -0
- package/src/compile/single.ts +46 -0
- package/src/compile/variants.ts +68 -0
- package/src/config/checks.ts +140 -0
- package/src/config/config.ts +83 -0
- package/src/config/fallbacks.ts +18 -0
- package/src/config/import.ts +146 -0
- package/src/config/load.ts +75 -0
- package/src/diagnostics.ts +98 -0
- package/src/generate/checks.ts +103 -0
- package/src/generate/chunk.ts +73 -0
- package/src/generate/digits.ts +74 -0
- package/src/generate/files.ts +83 -0
- package/src/generate/locale.ts +145 -0
- package/src/generate/modules.ts +94 -0
- package/src/generate/namespace.ts +119 -0
- package/src/generate/namespaces.ts +59 -0
- package/src/generate/read.ts +159 -0
- package/src/index.ts +27 -0
- package/src/init/detect.ts +103 -0
- package/src/init/init.ts +158 -0
- package/src/options/date.ts +87 -0
- package/src/options/literal.ts +21 -0
- package/src/options/names.ts +64 -0
- package/src/options/number.ts +20 -0
- package/src/options/options.ts +75 -0
- package/src/plurals.ts +68 -0
- package/src/types/names.ts +115 -0
- package/src/types/namespace.ts +148 -0
- package/src/types/signature.ts +111 -0
- package/src/vite.ts +143 -0
- package/src/watch.ts +115 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { MessageInfo, ParamKind } from '../compile/info.ts';
|
|
2
|
+
|
|
3
|
+
import { prop } from './names.ts';
|
|
4
|
+
|
|
5
|
+
/** Collapses every use of one parameter into a single TS type. */
|
|
6
|
+
export function paramType(kinds: ReadonlyArray<ParamKind>): string {
|
|
7
|
+
if (kinds.some((k) => k.kind === 'number')) {
|
|
8
|
+
return 'number';
|
|
9
|
+
}
|
|
10
|
+
if (kinds.some((k) => k.kind === 'date')) {
|
|
11
|
+
return 'Date | number';
|
|
12
|
+
}
|
|
13
|
+
const selects = kinds.filter((k) => k.kind === 'select');
|
|
14
|
+
if (selects.length > 0) {
|
|
15
|
+
// sorted: a translation may list the variants in another order
|
|
16
|
+
const keys = [...new Set(selects.flatMap((s) => s.keys))]
|
|
17
|
+
.toSorted()
|
|
18
|
+
.map((k) => JSON.stringify(k));
|
|
19
|
+
if (selects.some((s) => s.open)) {
|
|
20
|
+
keys.push('(string & {})');
|
|
21
|
+
}
|
|
22
|
+
return keys.join(' | ') || 'string';
|
|
23
|
+
}
|
|
24
|
+
if (kinds.some((k) => k.kind === 'string')) {
|
|
25
|
+
return 'string';
|
|
26
|
+
}
|
|
27
|
+
return 'string | number';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A select param's listed keys as a type, `undefined` unless it also takes any other string. */
|
|
31
|
+
function listedKeys(kinds: ReadonlyArray<ParamKind>): string | undefined {
|
|
32
|
+
const selects = kinds.filter((k) => k.kind === 'select');
|
|
33
|
+
if (
|
|
34
|
+
selects.every((s) => !s.open) ||
|
|
35
|
+
kinds.some((k) => k.kind === 'number' || k.kind === 'date')
|
|
36
|
+
) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const keys = new Set(selects.flatMap((s) => s.keys));
|
|
40
|
+
return keys.size > 0
|
|
41
|
+
? [...keys.add('other')]
|
|
42
|
+
.toSorted()
|
|
43
|
+
.map((k) => JSON.stringify(k))
|
|
44
|
+
.join(' | ')
|
|
45
|
+
: undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** One params object: `names` typed by their uses, `selected` overriding a selector's type. */
|
|
49
|
+
function fields(
|
|
50
|
+
info: MessageInfo,
|
|
51
|
+
{
|
|
52
|
+
names,
|
|
53
|
+
defaults,
|
|
54
|
+
selected,
|
|
55
|
+
}: {
|
|
56
|
+
names: Iterable<string>;
|
|
57
|
+
defaults: ReadonlySet<string>;
|
|
58
|
+
selected?: [param: string, type: string];
|
|
59
|
+
},
|
|
60
|
+
): { fields: Array<string>; required: number } {
|
|
61
|
+
const out = [...names]
|
|
62
|
+
.toSorted((a, b) => a.localeCompare(b))
|
|
63
|
+
.map((name) => {
|
|
64
|
+
const kinds = info.params.get(name) ?? [];
|
|
65
|
+
const keys = listedKeys(kinds);
|
|
66
|
+
const open = keys === undefined ? paramType(kinds) : `${keys} | Other<G>`;
|
|
67
|
+
const type = name === selected?.[0] ? selected[1] : open;
|
|
68
|
+
return `${prop(name)}: ${type}`;
|
|
69
|
+
});
|
|
70
|
+
let required = out.length;
|
|
71
|
+
for (const tag of [...info.markup].toSorted()) {
|
|
72
|
+
if (!info.params.has(tag)) {
|
|
73
|
+
const optional = defaults.has(tag);
|
|
74
|
+
out.push(`${prop(tag)}${optional ? '?' : ''}: Markup<Node>`);
|
|
75
|
+
required += optional ? 0 : 1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return { fields: out, required };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Any `string` but a literal, which only a generic tells apart: a literal makes `Record` a required key. */
|
|
82
|
+
export const OTHER =
|
|
83
|
+
'type Other<G extends string> = G extends string ? {} extends Record<G, 1> ? G : never : never;';
|
|
84
|
+
|
|
85
|
+
/** `defaults`: markup the runtime renders when a call leaves it out (the config's `markup`). */
|
|
86
|
+
export function signature(info: MessageInfo, defaults: ReadonlySet<string> = new Set()): string {
|
|
87
|
+
const ret = info.rich ? 'Node' : 'string';
|
|
88
|
+
const { branches } = info;
|
|
89
|
+
if (branches) {
|
|
90
|
+
// One member per key, so a key's own params are required only with it; `string` last.
|
|
91
|
+
const every = new Set([...branches.needs.values()].flatMap((names) => [...names]));
|
|
92
|
+
const members = [...branches.needs]
|
|
93
|
+
.toSorted(
|
|
94
|
+
([a], [b]) => Number(a === null) - Number(b === null) || String(a).localeCompare(String(b)),
|
|
95
|
+
)
|
|
96
|
+
.map(([key, names]) => {
|
|
97
|
+
// a plain `string` may hold any key, so it needs every branch's params; a literal must be listed
|
|
98
|
+
const [type, needed] =
|
|
99
|
+
key === null ? ['"other" | Other<G>', every] : [JSON.stringify(key), names];
|
|
100
|
+
return `{ ${fields(info, { names: needed, defaults, selected: [branches.param, type] }).fields.join('; ')} }`;
|
|
101
|
+
});
|
|
102
|
+
return `<G extends string>(params: ${members.join(' | ')}): ${ret}`;
|
|
103
|
+
}
|
|
104
|
+
const own = fields(info, { names: info.params.keys(), defaults });
|
|
105
|
+
if (own.fields.length === 0) {
|
|
106
|
+
return info.rich ? '(): Node' : ': Text';
|
|
107
|
+
}
|
|
108
|
+
// One `G` for every select, as a union of methods with different type params is not callable.
|
|
109
|
+
const generic = own.fields.some((f) => f.includes('Other<G>')) ? '<G extends string>' : '';
|
|
110
|
+
return `${generic}(params${own.required === 0 ? '?' : ''}: { ${own.fields.join('; ')} }): ${ret}`;
|
|
111
|
+
}
|
package/src/vite.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import type { ErrorPayload, Logger, Plugin, ViteDevServer } from 'vite';
|
|
4
|
+
|
|
5
|
+
import { build } from './build.ts';
|
|
6
|
+
import type { BuildOptions, BuildResult } from './build.ts';
|
|
7
|
+
import { formatDiagnostics, unreported } from './diagnostics.ts';
|
|
8
|
+
|
|
9
|
+
export interface ParlaViteOptions extends Pick<BuildOptions, 'config'> {
|
|
10
|
+
/** Folder holding the config. Defaults to Vite's root. */
|
|
11
|
+
root?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const overlay = (message: string): ErrorPayload => ({
|
|
15
|
+
type: 'error',
|
|
16
|
+
err: { message, stack: '', plugin: 'parla' },
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
/** The catalog errors, as Vite's overlay shows them; undefined without any. */
|
|
20
|
+
function catalogErrors({ diagnostics, config }: BuildResult): ErrorPayload | undefined {
|
|
21
|
+
const lines = formatDiagnostics(diagnostics, config.root)
|
|
22
|
+
.filter(({ level }) => level === 'error')
|
|
23
|
+
.map(({ line }) => line);
|
|
24
|
+
return lines.length > 0 ? overlay(lines.join('\n')) : undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Runs `parla build` on start and on every catalog change. */
|
|
28
|
+
export function parla(options: ParlaViteOptions = {}): Plugin {
|
|
29
|
+
let root = process.cwd();
|
|
30
|
+
let config: BuildResult['config'] | undefined;
|
|
31
|
+
let isBuild = false;
|
|
32
|
+
let logger: Logger | undefined;
|
|
33
|
+
// One plugin serves every environment and rebuild: each would repeat the same warnings.
|
|
34
|
+
const fresh = unreported();
|
|
35
|
+
// From a change until the build it starts has ended, in dev.
|
|
36
|
+
let pending: PromiseWithResolvers<void> | undefined;
|
|
37
|
+
// What the overlay shows in dev: the build leaves a broken message out, so the page would fail.
|
|
38
|
+
let shown: ErrorPayload | undefined;
|
|
39
|
+
|
|
40
|
+
const run = async (): Promise<BuildResult> => {
|
|
41
|
+
const result = await build({ cwd: root, config: options.config });
|
|
42
|
+
({ config } = result);
|
|
43
|
+
for (const { level, line } of formatDiagnostics(
|
|
44
|
+
fresh(result.diagnostics),
|
|
45
|
+
result.config.root,
|
|
46
|
+
)) {
|
|
47
|
+
logger?.[level === 'error' ? 'error' : 'warn'](`[parla] ${line}`);
|
|
48
|
+
}
|
|
49
|
+
shown = catalogErrors(result);
|
|
50
|
+
return result;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const settle = (): void => {
|
|
54
|
+
pending?.resolve();
|
|
55
|
+
pending = undefined;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const watched = (): Array<string> =>
|
|
59
|
+
config ? [...config.watchFiles, ...config.watchFolders] : [];
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
name: 'parla',
|
|
63
|
+
enforce: 'pre',
|
|
64
|
+
configResolved(vite) {
|
|
65
|
+
root = options.root ? path.resolve(vite.root, options.root) : vite.root;
|
|
66
|
+
isBuild = vite.command === 'build';
|
|
67
|
+
({ logger } = vite);
|
|
68
|
+
},
|
|
69
|
+
async buildStart() {
|
|
70
|
+
const result = await run();
|
|
71
|
+
for (const f of watched()) {
|
|
72
|
+
this.addWatchFile(f);
|
|
73
|
+
}
|
|
74
|
+
if (isBuild && result.diagnostics.some((d) => d.level === 'error')) {
|
|
75
|
+
this.error('parla: catalog errors, see above');
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
resolveId: {
|
|
79
|
+
filter: { id: /\b(?:namespaces\/|catalog\b)/ },
|
|
80
|
+
// On a branch switch, a page may import a namespace the build is about to generate.
|
|
81
|
+
async handler() {
|
|
82
|
+
await pending?.promise;
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
configureServer(server: ViteDevServer) {
|
|
86
|
+
// a save that leaves the config as broken as before repeats nothing
|
|
87
|
+
let failure: string | undefined;
|
|
88
|
+
const rebuild = async (): Promise<void> => {
|
|
89
|
+
const wasShown = shown !== undefined;
|
|
90
|
+
try {
|
|
91
|
+
const result = await run();
|
|
92
|
+
failure = undefined;
|
|
93
|
+
server.watcher.add(watched());
|
|
94
|
+
if (shown) {
|
|
95
|
+
server.ws.send(shown);
|
|
96
|
+
} else if (wasShown || result.written.length > 0) {
|
|
97
|
+
server.ws.send({ type: 'full-reload' });
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
const line = `parla: ${(error as Error).message}`;
|
|
101
|
+
if (line !== failure) {
|
|
102
|
+
server.config.logger.error(line);
|
|
103
|
+
}
|
|
104
|
+
failure = line;
|
|
105
|
+
shown = overlay(line);
|
|
106
|
+
server.ws.send(shown);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
// One build for files that change together, as on a branch switch.
|
|
110
|
+
let timer: NodeJS.Timeout | undefined;
|
|
111
|
+
let changes = 0;
|
|
112
|
+
const rebuildAt = async (change: number): Promise<void> => {
|
|
113
|
+
await rebuild();
|
|
114
|
+
if (change === changes) {
|
|
115
|
+
settle();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const onChange = (file: string): void => {
|
|
119
|
+
if (config?.watches(path.resolve(file))) {
|
|
120
|
+
pending ??= Promise.withResolvers();
|
|
121
|
+
changes += 1;
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
timer = setTimeout(() => {
|
|
124
|
+
void rebuildAt(changes);
|
|
125
|
+
}, 50);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
server.httpServer?.on('close', () => {
|
|
129
|
+
clearTimeout(timer);
|
|
130
|
+
settle();
|
|
131
|
+
});
|
|
132
|
+
// A page that loads, or reloads as Vite sees the new chunk, has missed it.
|
|
133
|
+
server.ws.on('connection', () => {
|
|
134
|
+
if (shown) {
|
|
135
|
+
server.ws.send(shown);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
for (const event of ['change', 'add', 'unlink'] as const) {
|
|
139
|
+
server.watcher.on(event, onChange);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
package/src/watch.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { existsSync, watch } from 'node:fs';
|
|
2
|
+
import type { FSWatcher } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import type { ResolvedConfig } from './config/config.ts';
|
|
6
|
+
|
|
7
|
+
/** What to watch in each folder: the files, or for a folder not there yet, its name in its parent. */
|
|
8
|
+
function watchedNames(files: ReadonlyArray<string>): Map<string, Set<string>> {
|
|
9
|
+
const out = new Map<string, Set<string>>();
|
|
10
|
+
for (const file of files) {
|
|
11
|
+
let name = file;
|
|
12
|
+
let dir = path.dirname(file);
|
|
13
|
+
while (!existsSync(dir)) {
|
|
14
|
+
name = dir;
|
|
15
|
+
dir = path.dirname(dir);
|
|
16
|
+
}
|
|
17
|
+
out.set(dir, (out.get(dir) ?? new Set()).add(path.basename(name)));
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface Folders {
|
|
23
|
+
/** Watches this config's files from now on; called after each build, as it may name new ones. */
|
|
24
|
+
readonly follow: (config: ResolvedConfig) => void;
|
|
25
|
+
readonly close: () => void;
|
|
26
|
+
readonly size: () => number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function watchFolders(changes: EventTarget): Folders {
|
|
30
|
+
const watchers = new Map<string, FSWatcher>();
|
|
31
|
+
let names = new Map<string, Set<string>>();
|
|
32
|
+
let watched: ResolvedConfig | undefined;
|
|
33
|
+
let closed = false;
|
|
34
|
+
const start = (dir: string): FSWatcher =>
|
|
35
|
+
watch(dir, (_, name) => {
|
|
36
|
+
// a folder holds more than catalogs; `name` is null where the platform does not tell
|
|
37
|
+
if (name === null || names.get(dir)?.has(name) || watched?.watches(path.join(dir, name))) {
|
|
38
|
+
changes.dispatchEvent(new Event('change'));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
follow(config) {
|
|
43
|
+
// a build may end after `close`
|
|
44
|
+
if (closed) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
names = watchedNames(config.watchFiles);
|
|
48
|
+
watched = config;
|
|
49
|
+
for (const [dir, watcher] of watchers) {
|
|
50
|
+
if (!names.has(dir)) {
|
|
51
|
+
watcher.close();
|
|
52
|
+
watchers.delete(dir);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const dir of names.keys()) {
|
|
56
|
+
if (!watchers.has(dir)) {
|
|
57
|
+
watchers.set(dir, start(dir));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
close() {
|
|
62
|
+
closed = true;
|
|
63
|
+
for (const watcher of watchers.values()) {
|
|
64
|
+
watcher.close();
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
size: () => watchers.size,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Rebuilds after each change to what the last build's config watches, until `close`. */
|
|
72
|
+
export function rebuildOnChange(
|
|
73
|
+
first: ResolvedConfig,
|
|
74
|
+
{
|
|
75
|
+
run,
|
|
76
|
+
error,
|
|
77
|
+
}: {
|
|
78
|
+
run: () => Promise<{ readonly config: ResolvedConfig }>;
|
|
79
|
+
error: (line: string) => void;
|
|
80
|
+
},
|
|
81
|
+
): { readonly folders: number; readonly close: () => void } {
|
|
82
|
+
const changes = new EventTarget();
|
|
83
|
+
const folders = watchFolders(changes);
|
|
84
|
+
let timer: NodeJS.Timeout | undefined;
|
|
85
|
+
// a save that leaves the config as broken as before repeats nothing
|
|
86
|
+
let failure: string | undefined;
|
|
87
|
+
const rebuild = async (): Promise<void> => {
|
|
88
|
+
try {
|
|
89
|
+
const { config } = await run();
|
|
90
|
+
folders.follow(config);
|
|
91
|
+
failure = undefined;
|
|
92
|
+
} catch (error_) {
|
|
93
|
+
const line = `parla: ${(error_ as Error).message}`;
|
|
94
|
+
if (line !== failure) {
|
|
95
|
+
error(line);
|
|
96
|
+
}
|
|
97
|
+
failure = line;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const schedule = (): void => {
|
|
101
|
+
clearTimeout(timer);
|
|
102
|
+
timer = setTimeout(() => {
|
|
103
|
+
void rebuild();
|
|
104
|
+
}, 50);
|
|
105
|
+
};
|
|
106
|
+
changes.addEventListener('change', schedule);
|
|
107
|
+
folders.follow(first);
|
|
108
|
+
return {
|
|
109
|
+
folders: folders.size(),
|
|
110
|
+
close: () => {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
folders.close();
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|