@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,119 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import type { MessageInfo } from '../compile/info.ts';
|
|
4
|
+
import type { FormatAdapter, ResolvedConfig, ResolvedNamespace } from '../config/config.ts';
|
|
5
|
+
import type { Diagnostic } from '../diagnostics.ts';
|
|
6
|
+
|
|
7
|
+
import { inherits } from './checks.ts';
|
|
8
|
+
import { compileLocale } from './locale.ts';
|
|
9
|
+
import { namespaceDeclaration, namespaceModule } from './modules.ts';
|
|
10
|
+
import { readCatalog } from './read.ts';
|
|
11
|
+
import type { CatalogText, Entry } from './read.ts';
|
|
12
|
+
|
|
13
|
+
/** What a build collects: its diagnostics, and the files to write by path under `outDir`. */
|
|
14
|
+
export interface Output {
|
|
15
|
+
readonly diagnostics: Array<Diagnostic>;
|
|
16
|
+
readonly files: Map<string, string>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Read {
|
|
20
|
+
readonly catalogs: Map<string, Map<string, Entry>>;
|
|
21
|
+
/** A locale, or `locale\0key`, already reported: what it lacks is the same problem. */
|
|
22
|
+
readonly reported: Set<string>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function readCatalogs(
|
|
26
|
+
ns: ResolvedNamespace,
|
|
27
|
+
{
|
|
28
|
+
config,
|
|
29
|
+
adapter,
|
|
30
|
+
texts,
|
|
31
|
+
diagnostics,
|
|
32
|
+
}: {
|
|
33
|
+
config: ResolvedConfig;
|
|
34
|
+
adapter: FormatAdapter;
|
|
35
|
+
texts: ReadonlyArray<CatalogText>;
|
|
36
|
+
} & Pick<Output, 'diagnostics'>,
|
|
37
|
+
): Read {
|
|
38
|
+
const catalogs = new Map<string, Map<string, Entry>>();
|
|
39
|
+
const reported = new Set<string>();
|
|
40
|
+
for (const [index, locale] of config.locales.entries()) {
|
|
41
|
+
const read = readCatalog(ns, {
|
|
42
|
+
locale,
|
|
43
|
+
adapter,
|
|
44
|
+
text: texts[index],
|
|
45
|
+
report: (d) => {
|
|
46
|
+
reported.add(d.key ? `${locale}\0${d.key}` : locale);
|
|
47
|
+
diagnostics.push({ ...d, namespace: ns.name, locale });
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
if (!read.exists && (locale === config.sourceLocale || !inherits(config, locale))) {
|
|
51
|
+
reported.add(locale);
|
|
52
|
+
diagnostics.push({
|
|
53
|
+
level: locale === config.sourceLocale ? 'error' : 'warning',
|
|
54
|
+
code: 'missing-file',
|
|
55
|
+
namespace: ns.name,
|
|
56
|
+
locale,
|
|
57
|
+
file: read.file,
|
|
58
|
+
message: 'no such file: create it',
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
catalogs.set(locale, read.entries);
|
|
62
|
+
}
|
|
63
|
+
return { catalogs, reported };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A namespace's chunk per locale, its object and its types, from its catalogs' `texts` per locale. */
|
|
67
|
+
export function buildNamespace(
|
|
68
|
+
ns: ResolvedNamespace,
|
|
69
|
+
{
|
|
70
|
+
config,
|
|
71
|
+
names: { iface, exported },
|
|
72
|
+
texts,
|
|
73
|
+
out,
|
|
74
|
+
}: {
|
|
75
|
+
config: ResolvedConfig;
|
|
76
|
+
names: { readonly iface: string; readonly exported: string };
|
|
77
|
+
texts: ReadonlyArray<CatalogText>;
|
|
78
|
+
out: Output;
|
|
79
|
+
},
|
|
80
|
+
): void {
|
|
81
|
+
const { diagnostics, files } = out;
|
|
82
|
+
if (exported !== ns.name) {
|
|
83
|
+
// two names for one namespace is what tools like codemods and x18n trip over
|
|
84
|
+
diagnostics.push({
|
|
85
|
+
level: 'warning',
|
|
86
|
+
code: 'namespace-name',
|
|
87
|
+
namespace: ns.name,
|
|
88
|
+
message: `exports as ${exported} while t keeps "${ns.name}": name it as an identifier and map the file in path`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const adapter = config.formats[ns.format];
|
|
92
|
+
if (!adapter) {
|
|
93
|
+
diagnostics.push({
|
|
94
|
+
level: 'error',
|
|
95
|
+
code: 'unknown-format',
|
|
96
|
+
namespace: ns.name,
|
|
97
|
+
message: `format "${ns.format}" is not in formats (${Object.keys(config.formats).join(', ') || 'empty'})`,
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const read = readCatalogs(ns, { config, adapter, texts, diagnostics });
|
|
102
|
+
const sourceInfo = new Map<string, { info: MessageInfo; source: string }>();
|
|
103
|
+
// The source locale first, so every translation has its source message to compare against.
|
|
104
|
+
const locales = [config.sourceLocale, ...config.locales.filter((l) => l !== config.sourceLocale)];
|
|
105
|
+
for (const locale of locales) {
|
|
106
|
+
files.set(
|
|
107
|
+
path.join('messages', locale, `${ns.name}.js`),
|
|
108
|
+
compileLocale(locale, { ...read, config, ns, sourceInfo, diagnostics }),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
files.set(
|
|
112
|
+
path.join('namespaces', `${ns.name}.js`),
|
|
113
|
+
namespaceModule(ns.name, { exported, locales: config.locales }),
|
|
114
|
+
);
|
|
115
|
+
files.set(
|
|
116
|
+
path.join('namespaces', `${ns.name}.d.ts`),
|
|
117
|
+
namespaceDeclaration(config, { namespace: ns.name, iface, exported, messages: sourceInfo }),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { FormatAdapter, ResolvedConfig } from '../config/config.ts';
|
|
2
|
+
import type { Diagnostic } from '../diagnostics.ts';
|
|
3
|
+
import { exportNames, interfaceNames } from '../types/names.ts';
|
|
4
|
+
|
|
5
|
+
import { buildNamespace } from './namespace.ts';
|
|
6
|
+
import type { Output } from './namespace.ts';
|
|
7
|
+
import { readText } from './read.ts';
|
|
8
|
+
import type { CatalogText } from './read.ts';
|
|
9
|
+
|
|
10
|
+
/** A namespace's output, and all it was built from. */
|
|
11
|
+
interface Kept {
|
|
12
|
+
readonly settings: string;
|
|
13
|
+
readonly adapter: FormatAdapter | undefined;
|
|
14
|
+
readonly texts: ReadonlyArray<CatalogText>;
|
|
15
|
+
readonly diagnostics: ReadonlyArray<Diagnostic>;
|
|
16
|
+
readonly files: ReadonlyMap<string, string>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Per config file, its last build: a watcher's rebuild compiles only the namespaces that changed.
|
|
20
|
+
const builds = new Map<string, Map<string, Kept>>();
|
|
21
|
+
|
|
22
|
+
/** Every namespace's output, the last build's where nothing the namespace reads has changed. */
|
|
23
|
+
export function buildNamespaces(config: ResolvedConfig, out: Output): void {
|
|
24
|
+
const ifaces = interfaceNames(config.namespaces.map((ns) => ns.name));
|
|
25
|
+
const exports = exportNames(ifaces);
|
|
26
|
+
// Whatever else the config holds, so an option added later is not forgotten here.
|
|
27
|
+
const shared = JSON.stringify({
|
|
28
|
+
...config,
|
|
29
|
+
formats: Object.keys(config.formats),
|
|
30
|
+
namespaces: 0,
|
|
31
|
+
watchFiles: 0,
|
|
32
|
+
watchFolders: 0,
|
|
33
|
+
});
|
|
34
|
+
const last = builds.get(config.configFile);
|
|
35
|
+
const kept = new Map<string, Kept>();
|
|
36
|
+
for (const ns of config.namespaces) {
|
|
37
|
+
const names = { iface: ifaces.get(ns.name) ?? '', exported: exports.get(ns.name) ?? '' };
|
|
38
|
+
const files = config.locales.map((locale) => ns.file(locale));
|
|
39
|
+
const settings = JSON.stringify([shared, ns.format, names, files]);
|
|
40
|
+
const adapter = config.formats[ns.format];
|
|
41
|
+
const texts = files.map((file) => readText(file));
|
|
42
|
+
let built = last?.get(ns.name);
|
|
43
|
+
if (
|
|
44
|
+
built?.settings !== settings ||
|
|
45
|
+
built.adapter !== adapter ||
|
|
46
|
+
texts.some((text, i) => text !== built?.texts[i])
|
|
47
|
+
) {
|
|
48
|
+
const fresh: Output = { diagnostics: [], files: new Map() };
|
|
49
|
+
buildNamespace(ns, { config, names, texts, out: fresh });
|
|
50
|
+
built = { settings, adapter, texts, ...fresh };
|
|
51
|
+
}
|
|
52
|
+
kept.set(ns.name, built);
|
|
53
|
+
out.diagnostics.push(...built.diagnostics);
|
|
54
|
+
for (const [file, content] of built.files) {
|
|
55
|
+
out.files.set(file, content);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
builds.set(config.configFile, kept);
|
|
59
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
|
|
3
|
+
import type { Model } from 'messageformat';
|
|
4
|
+
|
|
5
|
+
import type { FormatAdapter, ResolvedNamespace } from '../config/config.ts';
|
|
6
|
+
import type { ErrorDiagnostic, Unplaced } from '../diagnostics.ts';
|
|
7
|
+
|
|
8
|
+
/** Groups in groups past this would overflow the stack, here or in what reads the output. */
|
|
9
|
+
const MAX_DEPTH = 100;
|
|
10
|
+
|
|
11
|
+
interface Problem {
|
|
12
|
+
key: string;
|
|
13
|
+
message: string;
|
|
14
|
+
code: ErrorDiagnostic['code'];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** `value`'s messages into `out` by dotted key. */
|
|
18
|
+
function flatten(
|
|
19
|
+
value: unknown,
|
|
20
|
+
{
|
|
21
|
+
prefix,
|
|
22
|
+
out,
|
|
23
|
+
report,
|
|
24
|
+
}: { prefix: string; out: Map<string, string>; report: (problem: Problem) => void },
|
|
25
|
+
): void {
|
|
26
|
+
const invalid = (message: string): void => {
|
|
27
|
+
report({ key: prefix, message, code: 'invalid-resource' });
|
|
28
|
+
};
|
|
29
|
+
if (prefix.split('.').length > MAX_DEPTH) {
|
|
30
|
+
invalid(`more than ${MAX_DEPTH} levels of keys`);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (typeof value === 'string') {
|
|
34
|
+
if (out.has(prefix)) {
|
|
35
|
+
report({
|
|
36
|
+
key: prefix,
|
|
37
|
+
message: `"${prefix}" is written twice, with dots and in a group`,
|
|
38
|
+
code: 'key-conflict',
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
out.set(prefix, value);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
45
|
+
for (const [k, v] of Object.entries(value)) {
|
|
46
|
+
flatten(v, { prefix: `${prefix}.${k}`, out, report });
|
|
47
|
+
}
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const kind = Array.isArray(value) ? 'array' : typeof value;
|
|
51
|
+
invalid(`expected a string or an object, got ${value === null ? 'null' : kind}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A catalog's messages by dotted key; nothing, reported, unless it is an object. */
|
|
55
|
+
function flattenRoot(
|
|
56
|
+
tree: unknown,
|
|
57
|
+
report: (problem: Problem) => void,
|
|
58
|
+
): Map<string, string> | undefined {
|
|
59
|
+
if (!tree || typeof tree !== 'object' || Array.isArray(tree)) {
|
|
60
|
+
const kind = Array.isArray(tree) ? 'array' : typeof tree;
|
|
61
|
+
const message = `expected an object of messages, got ${tree === null ? 'null' : kind}`;
|
|
62
|
+
report({ key: '', message, code: 'invalid-resource' });
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const out = new Map<string, string>();
|
|
66
|
+
// From the root, as a group named "" would otherwise pass for it.
|
|
67
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
68
|
+
flatten(value, { prefix: key, out, report });
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface Entry {
|
|
74
|
+
source: string;
|
|
75
|
+
model: Model.Message;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A catalog's text: none where there is no file, an error where it cannot be read. */
|
|
79
|
+
export type CatalogText = string | Error | undefined;
|
|
80
|
+
|
|
81
|
+
export function readText(file: string): CatalogText {
|
|
82
|
+
if (!existsSync(file)) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
return readFileSync(file, 'utf8');
|
|
87
|
+
} catch (error) {
|
|
88
|
+
return error as Error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function readCatalog(
|
|
93
|
+
ns: ResolvedNamespace,
|
|
94
|
+
{
|
|
95
|
+
locale,
|
|
96
|
+
adapter,
|
|
97
|
+
text,
|
|
98
|
+
report: diag,
|
|
99
|
+
}: {
|
|
100
|
+
locale: string;
|
|
101
|
+
adapter: FormatAdapter;
|
|
102
|
+
text: CatalogText;
|
|
103
|
+
report: (d: Unplaced) => void;
|
|
104
|
+
},
|
|
105
|
+
): { file: string; entries: Map<string, Entry>; exists: boolean } {
|
|
106
|
+
const file = ns.file(locale);
|
|
107
|
+
const entries = new Map<string, Entry>();
|
|
108
|
+
if (text === undefined) {
|
|
109
|
+
return { file, entries, exists: false };
|
|
110
|
+
}
|
|
111
|
+
let tree: unknown;
|
|
112
|
+
try {
|
|
113
|
+
if (text instanceof Error) {
|
|
114
|
+
throw text;
|
|
115
|
+
}
|
|
116
|
+
tree = adapter.parseFile ? adapter.parseFile(text) : JSON.parse(text);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
diag({ level: 'error', code: 'invalid-resource', message: (error as Error).message, file });
|
|
119
|
+
return { file, entries, exists: true };
|
|
120
|
+
}
|
|
121
|
+
const flat = flattenRoot(tree, (problem) => {
|
|
122
|
+
diag({ level: 'error', ...problem, file });
|
|
123
|
+
});
|
|
124
|
+
if (!flat) {
|
|
125
|
+
return { file, entries, exists: true };
|
|
126
|
+
}
|
|
127
|
+
// Every prefix, not a sorted neighbour: `-` and others sort between `a` and `a.b`.
|
|
128
|
+
const groups = new Set<string>();
|
|
129
|
+
for (const key of flat.keys()) {
|
|
130
|
+
for (let dot = key.indexOf('.'); dot !== -1; dot = key.indexOf('.', dot + 1)) {
|
|
131
|
+
groups.add(key.slice(0, dot));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
for (const key of flat.keys()) {
|
|
135
|
+
if (groups.has(key)) {
|
|
136
|
+
diag({
|
|
137
|
+
level: 'error',
|
|
138
|
+
code: 'key-conflict',
|
|
139
|
+
key,
|
|
140
|
+
message: `"${key}" is both a message and a group`,
|
|
141
|
+
file,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
for (const [key, source] of flat) {
|
|
146
|
+
try {
|
|
147
|
+
entries.set(key, { source, model: adapter.parse(source) });
|
|
148
|
+
} catch (error) {
|
|
149
|
+
diag({
|
|
150
|
+
level: 'error',
|
|
151
|
+
code: 'invalid-syntax',
|
|
152
|
+
key,
|
|
153
|
+
message: (error as Error).message,
|
|
154
|
+
file,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return { file, entries, exists: true };
|
|
159
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { build, type BuildOptions, type BuildResult } from './build.ts';
|
|
2
|
+
export { compileMessage } from './compile/compile.ts';
|
|
3
|
+
export { CompileError } from './compile/error.ts';
|
|
4
|
+
export type {
|
|
5
|
+
CompiledMessage,
|
|
6
|
+
CustomFunctions,
|
|
7
|
+
FunctionKind,
|
|
8
|
+
MessageInfo,
|
|
9
|
+
ParamKind,
|
|
10
|
+
} from './compile/info.ts';
|
|
11
|
+
export { ModuleScope } from './compile/scope.ts';
|
|
12
|
+
export {
|
|
13
|
+
ConfigError,
|
|
14
|
+
defineConfig,
|
|
15
|
+
type FormatAdapter,
|
|
16
|
+
type NamespaceConfig,
|
|
17
|
+
type ParlaConfig,
|
|
18
|
+
type ResolvedConfig,
|
|
19
|
+
type ResolvedNamespace,
|
|
20
|
+
} from './config/config.ts';
|
|
21
|
+
export { loadConfig } from './config/load.ts';
|
|
22
|
+
export {
|
|
23
|
+
formatDiagnostic,
|
|
24
|
+
type Diagnostic,
|
|
25
|
+
type ErrorDiagnostic,
|
|
26
|
+
type WarningDiagnostic,
|
|
27
|
+
} from './diagnostics.ts';
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { readPackageJson } from '../config/load.ts';
|
|
5
|
+
|
|
6
|
+
interface Framework {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly adapter: string;
|
|
9
|
+
/** The framework's Vite plugin, as `vite.config.ts` calls it. */
|
|
10
|
+
readonly plugin: string;
|
|
11
|
+
/** How a component loads a namespace. */
|
|
12
|
+
readonly load: string;
|
|
13
|
+
readonly docs: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const DOCS = 'https://parlajs.github.io/parla';
|
|
17
|
+
|
|
18
|
+
const adapter = (name: string, plugin: string): Framework => ({
|
|
19
|
+
name,
|
|
20
|
+
adapter: `@parlajs/${name.toLowerCase()}`,
|
|
21
|
+
plugin,
|
|
22
|
+
load: 'useParla([common])',
|
|
23
|
+
docs: `${DOCS}/adapters/${name.toLowerCase()}/`,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const FRAMEWORKS: ReadonlyArray<readonly [string, Framework]> = [
|
|
27
|
+
['react', adapter('React', 'react()')],
|
|
28
|
+
['vue', adapter('Vue', 'vue()')],
|
|
29
|
+
['svelte', adapter('Svelte', 'svelte()')],
|
|
30
|
+
['solid-js', adapter('Solid', 'solid()')],
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
const CORE: Framework = {
|
|
34
|
+
name: 'no framework',
|
|
35
|
+
adapter: '@parlajs/core',
|
|
36
|
+
plugin: '',
|
|
37
|
+
load: 'await parla.load([common])',
|
|
38
|
+
docs: `${DOCS}/core/loading/`,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const ADD = {
|
|
42
|
+
pnpm: { add: 'pnpm add', dev: 'pnpm add -D' },
|
|
43
|
+
npm: { add: 'npm install', dev: 'npm install -D' },
|
|
44
|
+
yarn: { add: 'yarn add', dev: 'yarn add -D' },
|
|
45
|
+
bun: { add: 'bun add', dev: 'bun add -d' },
|
|
46
|
+
} as const;
|
|
47
|
+
|
|
48
|
+
type Manager = keyof typeof ADD;
|
|
49
|
+
|
|
50
|
+
const LOCKFILES: ReadonlyArray<readonly [string, Manager]> = [
|
|
51
|
+
['pnpm-lock.yaml', 'pnpm'],
|
|
52
|
+
['yarn.lock', 'yarn'],
|
|
53
|
+
['bun.lock', 'bun'],
|
|
54
|
+
['bun.lockb', 'bun'],
|
|
55
|
+
['package-lock.json', 'npm'],
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
export interface Project {
|
|
59
|
+
readonly dependencies: ReadonlySet<string>;
|
|
60
|
+
readonly framework: Framework;
|
|
61
|
+
readonly vite: boolean;
|
|
62
|
+
/** Whether Node reads the package's `.ts` files as ES modules. */
|
|
63
|
+
readonly esm: boolean;
|
|
64
|
+
readonly manager: Manager;
|
|
65
|
+
readonly add: (typeof ADD)[Manager];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const isManager = (name: string | undefined): name is Manager => name !== undefined && name in ADD;
|
|
69
|
+
|
|
70
|
+
/** The nearest lockfile's manager, as a workspace keeps one at its root; else the one running us. */
|
|
71
|
+
export function detectManager(cwd: string): Manager {
|
|
72
|
+
for (let dir = cwd; ; dir = path.dirname(dir)) {
|
|
73
|
+
const found = LOCKFILES.find(([file]) => existsSync(path.join(dir, file)));
|
|
74
|
+
if (found) {
|
|
75
|
+
return found[1];
|
|
76
|
+
}
|
|
77
|
+
if (path.dirname(dir) === dir) {
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const agent = process.env['npm_config_user_agent']?.split('/')[0];
|
|
82
|
+
return isManager(agent) ? agent : 'npm';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** What `parla init` reads from the folder instead of asking. */
|
|
86
|
+
export function detectProject(cwd: string): Project | undefined {
|
|
87
|
+
const pkg = readPackageJson(cwd);
|
|
88
|
+
if (!pkg) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const dependencies = new Set(
|
|
92
|
+
Object.keys({ ...pkg.peerDependencies, ...pkg.devDependencies, ...pkg.dependencies }),
|
|
93
|
+
);
|
|
94
|
+
const manager = detectManager(cwd);
|
|
95
|
+
return {
|
|
96
|
+
dependencies,
|
|
97
|
+
framework: FRAMEWORKS.find(([name]) => dependencies.has(name))?.[1] ?? CORE,
|
|
98
|
+
vite: dependencies.has('vite'),
|
|
99
|
+
esm: pkg.type === 'module',
|
|
100
|
+
manager,
|
|
101
|
+
add: ADD[manager],
|
|
102
|
+
};
|
|
103
|
+
}
|
package/src/init/init.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { checkLocales } from '../config/checks.ts';
|
|
5
|
+
import { ConfigError } from '../config/config.ts';
|
|
6
|
+
import { CONFIG_FILES } from '../config/load.ts';
|
|
7
|
+
|
|
8
|
+
import { detectProject } from './detect.ts';
|
|
9
|
+
import type { Project } from './detect.ts';
|
|
10
|
+
|
|
11
|
+
interface InitOptions {
|
|
12
|
+
/** `mf2` or `icu`. */
|
|
13
|
+
readonly format?: string | undefined;
|
|
14
|
+
/** Comma-separated, the source locale first. */
|
|
15
|
+
readonly locales?: string | undefined;
|
|
16
|
+
/** The folder of the catalogs, relative to the config. */
|
|
17
|
+
readonly catalogs?: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const FORMATS: Record<string, { readonly package: string; readonly hello: string }> = {
|
|
21
|
+
mf2: { package: '@parlajs/format-mf2', hello: 'Hello, {$name}!' },
|
|
22
|
+
icu: { package: '@parlajs/format-icu', hello: 'Hello, {name}!' },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const OUT_DIR = 'src/parla';
|
|
26
|
+
const IGNORED = /^(?:\*\*\/|\/)?src\/parla\/?$/;
|
|
27
|
+
|
|
28
|
+
interface Choices {
|
|
29
|
+
/** The config's file name. */
|
|
30
|
+
readonly config: string;
|
|
31
|
+
readonly format: string;
|
|
32
|
+
readonly locales: ReadonlyArray<string>;
|
|
33
|
+
readonly catalogs: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The options, checked before anything is written. */
|
|
37
|
+
function choose(
|
|
38
|
+
{ format = 'mf2', locales = 'en', catalogs = 'locales' }: InitOptions,
|
|
39
|
+
config: string,
|
|
40
|
+
): Choices {
|
|
41
|
+
if (!Object.hasOwn(FORMATS, format)) {
|
|
42
|
+
throw new ConfigError(`--format is mf2 or icu, not "${format}"`);
|
|
43
|
+
}
|
|
44
|
+
const list = locales.split(',').map((l) => l.trim());
|
|
45
|
+
checkLocales({ locales: list, sourceLocale: list[0] ?? '', fallbacks: {} });
|
|
46
|
+
const dir = catalogs.replace(/\/+$/, '');
|
|
47
|
+
// The path lands in a string literal of the config.
|
|
48
|
+
if (!/^[\w.-][\w./-]*$/.test(dir) || dir.split('/').includes('..')) {
|
|
49
|
+
throw new ConfigError(`--catalogs takes a relative folder of letters, digits, ".", "_" or "-"`);
|
|
50
|
+
}
|
|
51
|
+
const inside = path.posix.relative(OUT_DIR, path.posix.normalize(dir));
|
|
52
|
+
if (inside !== '..' && !inside.startsWith('../')) {
|
|
53
|
+
throw new ConfigError(`--catalogs cannot be in ${OUT_DIR}, which holds generated files only`);
|
|
54
|
+
}
|
|
55
|
+
return { config, format, locales: list, catalogs: dir };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const configModule = ({ format, locales, catalogs }: Choices): string =>
|
|
59
|
+
`import { defineConfig } from '@parlajs/compiler';
|
|
60
|
+
import { ${format} } from '${FORMATS[format]?.package}';
|
|
61
|
+
|
|
62
|
+
export default defineConfig({
|
|
63
|
+
locales: [${locales.map((l) => `'${l}'`).join(', ')}],
|
|
64
|
+
sourceLocale: '${locales[0]}',
|
|
65
|
+
outDir: '${OUT_DIR}',
|
|
66
|
+
formats: { ${format} },
|
|
67
|
+
namespaces: { common: { format: '${format}', path: '${catalogs}/{locale}/common.json' } },
|
|
68
|
+
});
|
|
69
|
+
`;
|
|
70
|
+
|
|
71
|
+
/** Adds `src/parla/` to `.gitignore`, unless it is there. */
|
|
72
|
+
function ignore(cwd: string): string {
|
|
73
|
+
const file = path.join(cwd, '.gitignore');
|
|
74
|
+
const text = existsSync(file) ? readFileSync(file, 'utf8') : undefined;
|
|
75
|
+
if (text?.split(/\r?\n/).some((line) => IGNORED.test(line.trim()))) {
|
|
76
|
+
return '';
|
|
77
|
+
}
|
|
78
|
+
const before = text === undefined || text === '' || text.endsWith('\n') ? '' : '\n';
|
|
79
|
+
writeFileSync(file, `${text ?? ''}${before}${OUT_DIR}/\n`);
|
|
80
|
+
return ` ${text === undefined ? 'created' : 'updated'} .gitignore: ${OUT_DIR}/`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Writes the config, a catalog per locale and the ignore line; never one that exists. */
|
|
84
|
+
function write(cwd: string, choices: Choices): Array<string> {
|
|
85
|
+
writeFileSync(path.join(cwd, choices.config), configModule(choices));
|
|
86
|
+
const lines = [` created ${choices.config}`];
|
|
87
|
+
const hello = `${JSON.stringify({ hello: FORMATS[choices.format]?.hello }, null, 2)}\n`;
|
|
88
|
+
for (const locale of choices.locales) {
|
|
89
|
+
const file = `${choices.catalogs}/${locale}/common.json`;
|
|
90
|
+
const target = path.join(cwd, file);
|
|
91
|
+
if (existsSync(target)) {
|
|
92
|
+
lines.push(` kept ${file}`);
|
|
93
|
+
} else {
|
|
94
|
+
mkdirSync(path.dirname(target), { recursive: true });
|
|
95
|
+
writeFileSync(target, hello);
|
|
96
|
+
lines.push(` created ${file}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return [...lines, ignore(cwd)].filter(Boolean);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** What is left to the user: installing, the Vite plugin, the first call. */
|
|
103
|
+
function next(project: Project, { format }: Choices): Array<string> {
|
|
104
|
+
const { framework, add, dependencies } = project;
|
|
105
|
+
const missing = (list: Array<string>): string =>
|
|
106
|
+
list.filter((name) => !dependencies.has(name)).join(' ');
|
|
107
|
+
const runtime = missing([...new Set([framework.adapter, '@parlajs/core'])]);
|
|
108
|
+
const dev = missing(['@parlajs/compiler', FORMATS[format]?.package ?? '']);
|
|
109
|
+
const plugins = framework.plugin ? `parla(), ${framework.plugin}` : 'parla()';
|
|
110
|
+
return [
|
|
111
|
+
'',
|
|
112
|
+
'Next',
|
|
113
|
+
...(runtime ? [` ${add.add} ${runtime}`] : []),
|
|
114
|
+
...(dev ? [` ${add.dev} ${dev}`] : []),
|
|
115
|
+
'',
|
|
116
|
+
...(project.vite
|
|
117
|
+
? [
|
|
118
|
+
' vite.config.ts',
|
|
119
|
+
" import { parla } from '@parlajs/compiler/vite';",
|
|
120
|
+
` plugins: [${plugins}]`,
|
|
121
|
+
]
|
|
122
|
+
: [
|
|
123
|
+
' parla build before the bundler',
|
|
124
|
+
' parla build --watch beside its dev server',
|
|
125
|
+
]),
|
|
126
|
+
...(framework.name === 'Svelte'
|
|
127
|
+
? [
|
|
128
|
+
' svelte.config.js, for useParla',
|
|
129
|
+
' compilerOptions: { experimental: { async: true } }',
|
|
130
|
+
]
|
|
131
|
+
: []),
|
|
132
|
+
'',
|
|
133
|
+
" import { common } from './parla/namespaces/common.js';",
|
|
134
|
+
` ${framework.load} → t.common.hello({ name: 'Ada' })`,
|
|
135
|
+
` ${framework.docs}`,
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Runs `parla init` in `cwd`: what it wrote and what is left to do. */
|
|
140
|
+
export function init(cwd: string, options: InitOptions): Array<string> {
|
|
141
|
+
const project = detectProject(cwd);
|
|
142
|
+
if (!project) {
|
|
143
|
+
throw new ConfigError(`No package.json in ${cwd}: run parla init in the app's folder`);
|
|
144
|
+
}
|
|
145
|
+
// Node reads a `.ts` as CommonJS unless the package says `"type": "module"`.
|
|
146
|
+
const choices = choose(options, CONFIG_FILES[project.esm ? 0 : 1]);
|
|
147
|
+
const existing = CONFIG_FILES.find((name) => existsSync(path.join(cwd, name)));
|
|
148
|
+
if (existing) {
|
|
149
|
+
throw new ConfigError(`${existing} exists: edit it instead`);
|
|
150
|
+
}
|
|
151
|
+
const { framework, vite, manager } = project;
|
|
152
|
+
const header = [framework.name, vite ? 'Vite' : 'no Vite', manager, choices.format];
|
|
153
|
+
return [
|
|
154
|
+
`parla init: ${[...header, choices.locales.join(', ')].join(' · ')}`,
|
|
155
|
+
...write(cwd, choices),
|
|
156
|
+
...next(project, choices),
|
|
157
|
+
];
|
|
158
|
+
}
|