@parlajs/compiler 0.0.0-stage → 0.1.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/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/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/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/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
package/build/vite.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { build } from './build.js';
|
|
3
|
+
import { formatDiagnostics, unreported } from './diagnostics.js';
|
|
4
|
+
const overlay = (message) => ({
|
|
5
|
+
type: 'error',
|
|
6
|
+
err: { message, stack: '', plugin: 'parla' },
|
|
7
|
+
});
|
|
8
|
+
/** The catalog errors, as Vite's overlay shows them; undefined without any. */
|
|
9
|
+
function catalogErrors({ diagnostics, config }) {
|
|
10
|
+
const lines = formatDiagnostics(diagnostics, config.root)
|
|
11
|
+
.filter(({ level }) => level === 'error')
|
|
12
|
+
.map(({ line }) => line);
|
|
13
|
+
return lines.length > 0 ? overlay(lines.join('\n')) : undefined;
|
|
14
|
+
}
|
|
15
|
+
/** Runs `parla build` on start and on every catalog change. */
|
|
16
|
+
export function parla(options = {}) {
|
|
17
|
+
let root = process.cwd();
|
|
18
|
+
let config;
|
|
19
|
+
let isBuild = false;
|
|
20
|
+
let logger;
|
|
21
|
+
// One plugin serves every environment and rebuild: each would repeat the same warnings.
|
|
22
|
+
const fresh = unreported();
|
|
23
|
+
// From a change until the build it starts has ended, in dev.
|
|
24
|
+
let pending;
|
|
25
|
+
// What the overlay shows in dev: the build leaves a broken message out, so the page would fail.
|
|
26
|
+
let shown;
|
|
27
|
+
const run = async () => {
|
|
28
|
+
const result = await build({ cwd: root, config: options.config });
|
|
29
|
+
({ config } = result);
|
|
30
|
+
for (const { level, line } of formatDiagnostics(fresh(result.diagnostics), result.config.root)) {
|
|
31
|
+
logger?.[level === 'error' ? 'error' : 'warn'](`[parla] ${line}`);
|
|
32
|
+
}
|
|
33
|
+
shown = catalogErrors(result);
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
const settle = () => {
|
|
37
|
+
pending?.resolve();
|
|
38
|
+
pending = undefined;
|
|
39
|
+
};
|
|
40
|
+
const watched = () => config ? [...config.watchFiles, ...config.watchFolders] : [];
|
|
41
|
+
return {
|
|
42
|
+
name: 'parla',
|
|
43
|
+
enforce: 'pre',
|
|
44
|
+
configResolved(vite) {
|
|
45
|
+
root = options.root ? path.resolve(vite.root, options.root) : vite.root;
|
|
46
|
+
isBuild = vite.command === 'build';
|
|
47
|
+
({ logger } = vite);
|
|
48
|
+
},
|
|
49
|
+
async buildStart() {
|
|
50
|
+
const result = await run();
|
|
51
|
+
for (const f of watched()) {
|
|
52
|
+
this.addWatchFile(f);
|
|
53
|
+
}
|
|
54
|
+
if (isBuild && result.diagnostics.some((d) => d.level === 'error')) {
|
|
55
|
+
this.error('parla: catalog errors, see above');
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
resolveId: {
|
|
59
|
+
filter: { id: /\b(?:namespaces\/|catalog\b)/ },
|
|
60
|
+
// On a branch switch, a page may import a namespace the build is about to generate.
|
|
61
|
+
async handler() {
|
|
62
|
+
await pending?.promise;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
configureServer(server) {
|
|
66
|
+
// a save that leaves the config as broken as before repeats nothing
|
|
67
|
+
let failure;
|
|
68
|
+
const rebuild = async () => {
|
|
69
|
+
const wasShown = shown !== undefined;
|
|
70
|
+
try {
|
|
71
|
+
const result = await run();
|
|
72
|
+
failure = undefined;
|
|
73
|
+
server.watcher.add(watched());
|
|
74
|
+
if (shown) {
|
|
75
|
+
server.ws.send(shown);
|
|
76
|
+
}
|
|
77
|
+
else if (wasShown || result.written.length > 0) {
|
|
78
|
+
server.ws.send({ type: 'full-reload' });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
const line = `parla: ${error.message}`;
|
|
83
|
+
if (line !== failure) {
|
|
84
|
+
server.config.logger.error(line);
|
|
85
|
+
}
|
|
86
|
+
failure = line;
|
|
87
|
+
shown = overlay(line);
|
|
88
|
+
server.ws.send(shown);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
// One build for files that change together, as on a branch switch.
|
|
92
|
+
let timer;
|
|
93
|
+
let changes = 0;
|
|
94
|
+
const rebuildAt = async (change) => {
|
|
95
|
+
await rebuild();
|
|
96
|
+
if (change === changes) {
|
|
97
|
+
settle();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const onChange = (file) => {
|
|
101
|
+
if (config?.watches(path.resolve(file))) {
|
|
102
|
+
pending ??= Promise.withResolvers();
|
|
103
|
+
changes += 1;
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
timer = setTimeout(() => {
|
|
106
|
+
void rebuildAt(changes);
|
|
107
|
+
}, 50);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
server.httpServer?.on('close', () => {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
settle();
|
|
113
|
+
});
|
|
114
|
+
// A page that loads, or reloads as Vite sees the new chunk, has missed it.
|
|
115
|
+
server.ws.on('connection', () => {
|
|
116
|
+
if (shown) {
|
|
117
|
+
server.ws.send(shown);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
for (const event of ['change', 'add', 'unlink']) {
|
|
121
|
+
server.watcher.on(event, onChange);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
package/build/watch.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ResolvedConfig } from './config/config.ts';
|
|
2
|
+
/** Rebuilds after each change to what the last build's config watches, until `close`. */
|
|
3
|
+
export declare function rebuildOnChange(first: ResolvedConfig, { run, error, }: {
|
|
4
|
+
run: () => Promise<{
|
|
5
|
+
readonly config: ResolvedConfig;
|
|
6
|
+
}>;
|
|
7
|
+
error: (line: string) => void;
|
|
8
|
+
}): {
|
|
9
|
+
readonly folders: number;
|
|
10
|
+
readonly close: () => void;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=watch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch.d.ts","sourceRoot":"","sources":["../src/watch.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAkEzD,yFAAyF;AACzF,wBAAgB,eAAe,CAC7B,KAAK,EAAE,cAAc,EACrB,EACE,GAAG,EACH,KAAK,GACN,EAAE;IACD,GAAG,EAAE,MAAM,OAAO,CAAC;QAAE,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;KAAE,CAAC,CAAC;IACxD,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC/B,GACA;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAA;CAAE,CAkC1D"}
|
package/build/watch.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { existsSync, watch } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
/** What to watch in each folder: the files, or for a folder not there yet, its name in its parent. */
|
|
4
|
+
function watchedNames(files) {
|
|
5
|
+
const out = new Map();
|
|
6
|
+
for (const file of files) {
|
|
7
|
+
let name = file;
|
|
8
|
+
let dir = path.dirname(file);
|
|
9
|
+
while (!existsSync(dir)) {
|
|
10
|
+
name = dir;
|
|
11
|
+
dir = path.dirname(dir);
|
|
12
|
+
}
|
|
13
|
+
out.set(dir, (out.get(dir) ?? new Set()).add(path.basename(name)));
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
function watchFolders(changes) {
|
|
18
|
+
const watchers = new Map();
|
|
19
|
+
let names = new Map();
|
|
20
|
+
let watched;
|
|
21
|
+
let closed = false;
|
|
22
|
+
const start = (dir) => watch(dir, (_, name) => {
|
|
23
|
+
// a folder holds more than catalogs; `name` is null where the platform does not tell
|
|
24
|
+
if (name === null || names.get(dir)?.has(name) || watched?.watches(path.join(dir, name))) {
|
|
25
|
+
changes.dispatchEvent(new Event('change'));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
follow(config) {
|
|
30
|
+
// a build may end after `close`
|
|
31
|
+
if (closed) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
names = watchedNames(config.watchFiles);
|
|
35
|
+
watched = config;
|
|
36
|
+
for (const [dir, watcher] of watchers) {
|
|
37
|
+
if (!names.has(dir)) {
|
|
38
|
+
watcher.close();
|
|
39
|
+
watchers.delete(dir);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
for (const dir of names.keys()) {
|
|
43
|
+
if (!watchers.has(dir)) {
|
|
44
|
+
watchers.set(dir, start(dir));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
close() {
|
|
49
|
+
closed = true;
|
|
50
|
+
for (const watcher of watchers.values()) {
|
|
51
|
+
watcher.close();
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
size: () => watchers.size,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** Rebuilds after each change to what the last build's config watches, until `close`. */
|
|
58
|
+
export function rebuildOnChange(first, { run, error, }) {
|
|
59
|
+
const changes = new EventTarget();
|
|
60
|
+
const folders = watchFolders(changes);
|
|
61
|
+
let timer;
|
|
62
|
+
// a save that leaves the config as broken as before repeats nothing
|
|
63
|
+
let failure;
|
|
64
|
+
const rebuild = async () => {
|
|
65
|
+
try {
|
|
66
|
+
const { config } = await run();
|
|
67
|
+
folders.follow(config);
|
|
68
|
+
failure = undefined;
|
|
69
|
+
}
|
|
70
|
+
catch (error_) {
|
|
71
|
+
const line = `parla: ${error_.message}`;
|
|
72
|
+
if (line !== failure) {
|
|
73
|
+
error(line);
|
|
74
|
+
}
|
|
75
|
+
failure = line;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const schedule = () => {
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
timer = setTimeout(() => {
|
|
81
|
+
void rebuild();
|
|
82
|
+
}, 50);
|
|
83
|
+
};
|
|
84
|
+
changes.addEventListener('change', schedule);
|
|
85
|
+
folders.follow(first);
|
|
86
|
+
return {
|
|
87
|
+
folders: folders.size(),
|
|
88
|
+
close: () => {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
folders.close();
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,87 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parlajs/compiler",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Compiles Parla catalogs into per-locale chunks and typed accessors.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"codegen",
|
|
7
|
+
"compiler",
|
|
8
|
+
"i18n",
|
|
9
|
+
"icu",
|
|
10
|
+
"internationalization",
|
|
11
|
+
"l10n",
|
|
12
|
+
"localization",
|
|
13
|
+
"messageformat",
|
|
14
|
+
"messageformat2",
|
|
15
|
+
"mf2",
|
|
16
|
+
"parla",
|
|
17
|
+
"type-safe",
|
|
18
|
+
"typescript",
|
|
19
|
+
"vite",
|
|
20
|
+
"vite-plugin"
|
|
21
|
+
],
|
|
22
|
+
"homepage": "https://github.com/parlajs/parla",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/parlajs/parla/issues"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "Marco Pasqualetti",
|
|
29
|
+
"url": "https://github.com/marcalexiei"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/parlajs/parla.git",
|
|
34
|
+
"directory": "packages/compiler"
|
|
35
|
+
},
|
|
36
|
+
"bin": {
|
|
37
|
+
"parla": "./build/cli.js"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"build",
|
|
41
|
+
"src",
|
|
42
|
+
"README.md"
|
|
43
|
+
],
|
|
44
|
+
"type": "module",
|
|
45
|
+
"exports": {
|
|
46
|
+
".": {
|
|
47
|
+
"@parlajs/source": "./src/index.ts",
|
|
48
|
+
"default": "./build/index.js"
|
|
49
|
+
},
|
|
50
|
+
"./vite": {
|
|
51
|
+
"@parlajs/source": "./src/vite.ts",
|
|
52
|
+
"default": "./build/vite.js"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"messageformat": "^4.1.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@parlajs/core": "^0.1.0",
|
|
63
|
+
"@parlajs/format-icu": "^0.1.0",
|
|
64
|
+
"@parlajs/format-mf2": "^0.1.0",
|
|
65
|
+
"@types/node": "26.6.6",
|
|
66
|
+
"intl-messageformat": "12.2.0",
|
|
67
|
+
"vite": "8.3.8",
|
|
68
|
+
"vitest": "5.1.1"
|
|
69
|
+
},
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"vite": ">=6"
|
|
72
|
+
},
|
|
73
|
+
"peerDependenciesMeta": {
|
|
74
|
+
"vite": {
|
|
75
|
+
"optional": true
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"engines": {
|
|
79
|
+
"node": "^22.18 || ^24 || >=26"
|
|
80
|
+
},
|
|
81
|
+
"scripts": {
|
|
82
|
+
"build": "tsc -p tsconfig.build.json",
|
|
83
|
+
"lint:publish": "attw --pack . --profile esm-only && publint --strict",
|
|
84
|
+
"test": "vitest",
|
|
85
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
86
|
+
}
|
|
6
87
|
}
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ResolvedConfig } from './config/config.ts';
|
|
2
|
+
import { loadConfig } from './config/load.ts';
|
|
3
|
+
import type { Diagnostic } from './diagnostics.ts';
|
|
4
|
+
import { writeOutput } from './generate/files.ts';
|
|
5
|
+
import type { Output } from './generate/namespace.ts';
|
|
6
|
+
import { buildNamespaces } from './generate/namespaces.ts';
|
|
7
|
+
|
|
8
|
+
export interface BuildOptions {
|
|
9
|
+
cwd?: string;
|
|
10
|
+
/** Path to the config file, relative to `cwd`. */
|
|
11
|
+
config?: string | undefined;
|
|
12
|
+
/** `false` writes nothing: a check. */
|
|
13
|
+
write?: boolean | undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface BuildResult {
|
|
17
|
+
config: ResolvedConfig;
|
|
18
|
+
diagnostics: Array<Diagnostic>;
|
|
19
|
+
written: Array<string>;
|
|
20
|
+
/** Files an earlier build generated that this one no longer does. */
|
|
21
|
+
removed: Array<string>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function build(options: BuildOptions = {}): Promise<BuildResult> {
|
|
25
|
+
const config = await loadConfig(options.cwd ?? process.cwd(), options.config);
|
|
26
|
+
const out: Output = { diagnostics: [], files: new Map() };
|
|
27
|
+
buildNamespaces(config, out);
|
|
28
|
+
const { written, removed } =
|
|
29
|
+
(options.write ?? true) ? writeOutput(config, out.files) : { written: [], removed: [] };
|
|
30
|
+
return { config, diagnostics: out.diagnostics, written, removed };
|
|
31
|
+
}
|
package/src/cli.ts
ADDED
package/src/command.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util';
|
|
2
|
+
import type { ParseArgsConfig } from 'node:util';
|
|
3
|
+
|
|
4
|
+
import { build } from './build.ts';
|
|
5
|
+
import type { BuildResult } from './build.ts';
|
|
6
|
+
import { ConfigError } from './config/config.ts';
|
|
7
|
+
import type { ResolvedConfig } from './config/config.ts';
|
|
8
|
+
import { formatDiagnostics, unreported } from './diagnostics.ts';
|
|
9
|
+
import type { Diagnostic } from './diagnostics.ts';
|
|
10
|
+
import { init } from './init/init.ts';
|
|
11
|
+
import { rebuildOnChange } from './watch.ts';
|
|
12
|
+
|
|
13
|
+
const HELP = `Usage: parla <command> [options]
|
|
14
|
+
|
|
15
|
+
Commands
|
|
16
|
+
build Compile catalogs into per-locale chunks and typed namespace objects
|
|
17
|
+
check Validate catalogs without writing anything
|
|
18
|
+
init Write parla.config.ts, a catalog per locale and the .gitignore line
|
|
19
|
+
|
|
20
|
+
Options
|
|
21
|
+
-c, --config <file> Config file (default: parla.config.ts, then .mts)
|
|
22
|
+
-w, --watch Rebuild on change (build)
|
|
23
|
+
--strict Treat warnings as errors
|
|
24
|
+
--json Print diagnostics as JSON (check)
|
|
25
|
+
--format <name> mf2 or icu (init; default mf2)
|
|
26
|
+
--locales <list> Comma-separated, source first (init; default en)
|
|
27
|
+
--catalogs <dir> Catalog folder (init; default locales)
|
|
28
|
+
-h, --help`;
|
|
29
|
+
|
|
30
|
+
export interface CommandIO {
|
|
31
|
+
readonly cwd: string;
|
|
32
|
+
readonly log: (line: string) => void;
|
|
33
|
+
readonly error: (line: string) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface CommandRun {
|
|
37
|
+
readonly code: number;
|
|
38
|
+
/** Stops watching; a no-op unless `--watch`. */
|
|
39
|
+
readonly close: () => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const done = (code: number): CommandRun => ({
|
|
43
|
+
code,
|
|
44
|
+
close: () => {
|
|
45
|
+
// nothing to stop
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const ARGS = {
|
|
50
|
+
allowPositionals: true,
|
|
51
|
+
options: {
|
|
52
|
+
config: { type: 'string', short: 'c' },
|
|
53
|
+
watch: { type: 'boolean', short: 'w' },
|
|
54
|
+
strict: { type: 'boolean' },
|
|
55
|
+
json: { type: 'boolean' },
|
|
56
|
+
format: { type: 'string' },
|
|
57
|
+
locales: { type: 'string' },
|
|
58
|
+
catalogs: { type: 'string' },
|
|
59
|
+
help: { type: 'boolean', short: 'h' },
|
|
60
|
+
},
|
|
61
|
+
} as const satisfies ParseArgsConfig;
|
|
62
|
+
|
|
63
|
+
type Parsed = ReturnType<typeof parseArgs<typeof ARGS>>;
|
|
64
|
+
|
|
65
|
+
/** The options of `parla`, or why they are wrong. */
|
|
66
|
+
function parse(args: ReadonlyArray<string>): Parsed | string {
|
|
67
|
+
try {
|
|
68
|
+
return parseArgs({ ...ARGS, args: [...args] });
|
|
69
|
+
} catch (error) {
|
|
70
|
+
return (error as Error).message;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const INIT_ONLY = ['format', 'locales', 'catalogs'] as const;
|
|
75
|
+
const NOT_INIT = ['config', 'strict', 'json'] as const;
|
|
76
|
+
|
|
77
|
+
/** Why a known command cannot run as given, or nothing. */
|
|
78
|
+
function misuse(
|
|
79
|
+
command: string,
|
|
80
|
+
{ extra, values }: { extra: string | undefined; values: Parsed['values'] },
|
|
81
|
+
): string {
|
|
82
|
+
if (extra !== undefined) {
|
|
83
|
+
return `unexpected argument "${extra}"`;
|
|
84
|
+
}
|
|
85
|
+
if (values.watch && command !== 'build') {
|
|
86
|
+
return '--watch works only with build';
|
|
87
|
+
}
|
|
88
|
+
if (command === 'init') {
|
|
89
|
+
const option = NOT_INIT.find((name) => values[name] !== undefined);
|
|
90
|
+
return option ? `--${option} does not work with init` : '';
|
|
91
|
+
}
|
|
92
|
+
const option = INIT_ONLY.find((name) => values[name] !== undefined);
|
|
93
|
+
return option ? `--${option} works only with init` : '';
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function runInit({ cwd, log, error }: CommandIO, values: Parsed['values']): CommandRun {
|
|
97
|
+
try {
|
|
98
|
+
for (const line of init(cwd, values)) {
|
|
99
|
+
log(line);
|
|
100
|
+
}
|
|
101
|
+
return done(0);
|
|
102
|
+
} catch (error_) {
|
|
103
|
+
error(`parla: ${(error_ as Error).message}`);
|
|
104
|
+
return done(1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function reportFailure(
|
|
109
|
+
thrown: unknown,
|
|
110
|
+
{ json, io }: { json: boolean | undefined; io: CommandIO },
|
|
111
|
+
): void {
|
|
112
|
+
const { message } = thrown as Error;
|
|
113
|
+
if (json && thrown instanceof ConfigError) {
|
|
114
|
+
// A consumer parsing stdout gets a document either way.
|
|
115
|
+
const diagnostic: Diagnostic = { level: 'error', code: 'invalid-config', message };
|
|
116
|
+
io.log(JSON.stringify([diagnostic], null, 2));
|
|
117
|
+
} else {
|
|
118
|
+
io.error(`parla: ${message}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function filesOf({ written, removed }: BuildResult): string {
|
|
123
|
+
const gone = removed.length > 0 ? ` · ${removed.length} removed` : '';
|
|
124
|
+
return ` · ${written.length} files written${gone}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Runs `parla <args>`: the exit code, and in watch mode the watchers that keep it running. */
|
|
128
|
+
export async function runCommand(
|
|
129
|
+
args: ReadonlyArray<string>,
|
|
130
|
+
{ cwd, log, error }: CommandIO,
|
|
131
|
+
): Promise<CommandRun> {
|
|
132
|
+
const parsed = parse(args);
|
|
133
|
+
if (typeof parsed === 'string') {
|
|
134
|
+
error(`parla: ${parsed}`);
|
|
135
|
+
return done(1);
|
|
136
|
+
}
|
|
137
|
+
const { values, positionals } = parsed;
|
|
138
|
+
const [command, extra] = positionals;
|
|
139
|
+
if (values.help || !command || !['build', 'check', 'init'].includes(command)) {
|
|
140
|
+
log(HELP);
|
|
141
|
+
return done(values.help ? 0 : 1);
|
|
142
|
+
}
|
|
143
|
+
const problem = misuse(command, { extra, values });
|
|
144
|
+
if (problem) {
|
|
145
|
+
error(`parla: ${problem}`);
|
|
146
|
+
return done(1);
|
|
147
|
+
}
|
|
148
|
+
if (command === 'init') {
|
|
149
|
+
return runInit({ cwd, log, error }, values);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const write = command === 'build';
|
|
153
|
+
const fresh = unreported();
|
|
154
|
+
|
|
155
|
+
const run = async (): Promise<{ failed: boolean; config: ResolvedConfig }> => {
|
|
156
|
+
const started = performance.now();
|
|
157
|
+
const result = await build({ cwd, config: values.config, write });
|
|
158
|
+
const { diagnostics, config } = result;
|
|
159
|
+
const reports = formatDiagnostics(diagnostics);
|
|
160
|
+
const errors = reports.filter((r) => r.level === 'error').length;
|
|
161
|
+
const warnings = reports.length - errors;
|
|
162
|
+
if (values.json) {
|
|
163
|
+
log(JSON.stringify(diagnostics, null, 2));
|
|
164
|
+
} else {
|
|
165
|
+
for (const { line } of formatDiagnostics(fresh(diagnostics), config.root)) {
|
|
166
|
+
log(line);
|
|
167
|
+
}
|
|
168
|
+
const ms = Math.round(performance.now() - started);
|
|
169
|
+
const files = write ? filesOf(result) : '';
|
|
170
|
+
log(
|
|
171
|
+
`parla ${command}: ${config.namespaces.length} namespaces × ${config.locales.length} locales · ${errors} errors · ${warnings} warnings · ${ms}ms${files}`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
failed: errors > 0 || (Boolean(values.strict) && warnings > 0),
|
|
176
|
+
config,
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
let first;
|
|
181
|
+
try {
|
|
182
|
+
first = await run();
|
|
183
|
+
} catch (error_) {
|
|
184
|
+
reportFailure(error_, { json: values.json, io: { cwd, log, error } });
|
|
185
|
+
return done(1);
|
|
186
|
+
}
|
|
187
|
+
const code = first.failed ? 1 : 0;
|
|
188
|
+
if (!values.watch) {
|
|
189
|
+
return done(code);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const watcher = rebuildOnChange(first.config, { run, error });
|
|
193
|
+
log(`parla: watching ${watcher.folders} folders`);
|
|
194
|
+
return { code, close: watcher.close };
|
|
195
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Model } from 'messageformat';
|
|
2
|
+
|
|
3
|
+
import { CompileError } from './error.ts';
|
|
4
|
+
|
|
5
|
+
export function literalValue(value: string): string {
|
|
6
|
+
if (/^-?\d+(?:\.\d+)?$/.test(value)) {
|
|
7
|
+
// `02` would be a legacy octal literal: a syntax error in the chunk, which is a module
|
|
8
|
+
return String(Number(value));
|
|
9
|
+
}
|
|
10
|
+
if (value === 'true' || value === 'false') {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
return JSON.stringify(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Where markup finds its callback, without what objects inherit: a check at run time costs 30%. */
|
|
17
|
+
export const params = (markup: string): string =>
|
|
18
|
+
markup in Object.prototype ? '{ __proto__: null, ...p }' : 'p';
|
|
19
|
+
|
|
20
|
+
/** A call's `{ __proto__: value }` sets the object's prototype, so the message would never get it. */
|
|
21
|
+
export function checkName(name: string): void {
|
|
22
|
+
if (name === '__proto__') {
|
|
23
|
+
throw new CompileError('a param or markup cannot be named "__proto__": rename it');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A parser that assigns a `__proto__` option sets the prototype instead, dropping the option. */
|
|
28
|
+
export function checkOptions(options: object | undefined): void {
|
|
29
|
+
if (options && Object.getPrototypeOf(options) !== Object.prototype) {
|
|
30
|
+
throw new CompileError('an option cannot be named "__proto__": rename it');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `:offset`'s `add` or `subtract`, as an operation. */
|
|
35
|
+
export function offsetBy({ options = {} }: Model.FunctionRef): string {
|
|
36
|
+
const [[name, value] = [], ...rest] = Object.entries(options);
|
|
37
|
+
const isDigits = value?.type === 'literal' && /^\d+$/.test(value.value);
|
|
38
|
+
if (!isDigits || rest.length > 0 || (name !== 'add' && name !== 'subtract')) {
|
|
39
|
+
throw new CompileError(':offset needs one literal integer, add or subtract');
|
|
40
|
+
}
|
|
41
|
+
return `${name === 'add' ? '+' : '-'} ${Number(value.value)}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The number a selector picks by: the one its function writes. */
|
|
45
|
+
export function selected(name: string, raw: string): string {
|
|
46
|
+
if (name === 'integer') {
|
|
47
|
+
return `Math.round(Number(${raw}))`;
|
|
48
|
+
}
|
|
49
|
+
// Rounded, as 0.29 * 100 is 28.999999999999996.
|
|
50
|
+
return name === 'percent' ? `Math.round(Number(${raw}) * 1e4) / 100` : raw;
|
|
51
|
+
}
|