@rexezuge/tooling 1.3.0 → 1.5.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/dist/eslint-boundaries.d.ts +12 -4
- package/dist/eslint-boundaries.d.ts.map +1 -1
- package/dist/eslint-boundaries.js +1 -224
- package/dist/eslint-boundaries.js.map +7 -1
- package/dist/eslint-rules.js +1 -273
- package/dist/eslint-rules.js.map +7 -1
- package/dist/eslint.js +1 -194
- package/dist/eslint.js.map +7 -1
- package/dist/functions/pages-proxy.d.ts.map +1 -1
- package/dist/functions/pages-proxy.js +1 -131
- package/dist/functions/pages-proxy.js.map +7 -1
- package/dist/index.js +1 -31
- package/dist/index.js.map +7 -1
- package/dist/scripts/backup/d1-target.d.ts +15 -5
- package/dist/scripts/backup/d1-target.d.ts.map +1 -1
- package/dist/scripts/backup/d1-target.js +1 -31
- package/dist/scripts/backup/d1-target.js.map +7 -1
- package/dist/scripts/backup/destination-config.d.ts +24 -8
- package/dist/scripts/backup/destination-config.d.ts.map +1 -1
- package/dist/scripts/backup/destination-config.js +1 -57
- package/dist/scripts/backup/destination-config.js.map +7 -1
- package/dist/scripts/backup/encrypt-backup.js +2 -97
- package/dist/scripts/backup/encrypt-backup.js.map +7 -1
- package/dist/scripts/backup/naming.d.ts +3 -1
- package/dist/scripts/backup/naming.d.ts.map +1 -1
- package/dist/scripts/backup/naming.js +1 -58
- package/dist/scripts/backup/naming.js.map +7 -1
- package/dist/scripts/check-god-files.d.ts +40 -14
- package/dist/scripts/check-god-files.d.ts.map +1 -1
- package/dist/scripts/check-god-files.js +2 -271
- package/dist/scripts/check-god-files.js.map +7 -1
- package/dist/scripts/ensure-spa-shell-stub.js +1 -29
- package/dist/scripts/ensure-spa-shell-stub.js.map +7 -1
- package/dist/scripts/init-secrets.d.ts +0 -16
- package/dist/scripts/init-secrets.d.ts.map +1 -1
- package/dist/scripts/init-secrets.js +2 -239
- package/dist/scripts/init-secrets.js.map +7 -1
- package/dist/scripts/lib/cli-args.d.ts.map +1 -1
- package/dist/scripts/lib/cli-args.js +1 -116
- package/dist/scripts/lib/cli-args.js.map +7 -1
- package/dist/scripts/lib/github-actions.js +3 -38
- package/dist/scripts/lib/github-actions.js.map +7 -1
- package/dist/scripts/lib/wrangler-table.d.ts.map +1 -1
- package/dist/scripts/lib/wrangler-table.js +2 -99
- package/dist/scripts/lib/wrangler-table.js.map +7 -1
- package/dist/scripts/migrations-lock.js +1 -44
- package/dist/scripts/migrations-lock.js.map +7 -1
- package/dist/scripts/prepare-wrangler-config.js +1 -49
- package/dist/scripts/prepare-wrangler-config.js.map +7 -1
- package/dist/scripts/spa-shell.d.ts +12 -4
- package/dist/scripts/spa-shell.d.ts.map +1 -1
- package/dist/scripts/spa-shell.js +3 -160
- package/dist/scripts/spa-shell.js.map +7 -1
- package/dist/scripts/validate-locales.d.ts +13 -5
- package/dist/scripts/validate-locales.d.ts.map +1 -1
- package/dist/scripts/validate-locales.js +4 -350
- package/dist/scripts/validate-locales.js.map +7 -1
- package/dist/scripts/verify-migrations.d.ts +27 -9
- package/dist/scripts/verify-migrations.d.ts.map +1 -1
- package/dist/scripts/verify-migrations.js +2 -312
- package/dist/scripts/verify-migrations.js.map +7 -1
- package/dist/scripts/verify-spa-shell.js +1 -52
- package/dist/scripts/verify-spa-shell.js.map +7 -1
- package/dist/scripts/wrangler-config/cli.js +3 -51
- package/dist/scripts/wrangler-config/cli.js.map +7 -1
- package/dist/scripts/wrangler-config/patches.d.ts +9 -3
- package/dist/scripts/wrangler-config/patches.d.ts.map +1 -1
- package/dist/scripts/wrangler-config/patches.js +4 -141
- package/dist/scripts/wrangler-config/patches.js.map +7 -1
- package/dist/scripts/wrangler-config/resources.d.ts +18 -6
- package/dist/scripts/wrangler-config/resources.d.ts.map +1 -1
- package/dist/scripts/wrangler-config/resources.js +1 -290
- package/dist/scripts/wrangler-config/resources.js.map +7 -1
- package/dist/scripts/wrangler-config/types.d.ts +24 -8
- package/dist/scripts/wrangler-config/types.d.ts.map +1 -1
- package/dist/scripts/wrangler-config/types.js +1 -49
- package/dist/scripts/wrangler-config/types.js.map +7 -1
- package/dist/test/integration-migrations.d.ts +29 -12
- package/dist/test/integration-migrations.d.ts.map +1 -1
- package/dist/test/integration-migrations.js +2 -171
- package/dist/test/integration-migrations.js.map +7 -1
- package/dist/test/mocks/cloudflare-workers.js +1 -90
- package/dist/test/mocks/cloudflare-workers.js.map +7 -1
- package/dist/vite.d.ts +5 -3
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +3 -125
- package/dist/vite.js.map +7 -1
- package/dist/vitest-aliases.d.ts.map +1 -1
- package/dist/vitest-aliases.js +1 -81
- package/dist/vitest-aliases.js.map +7 -1
- package/dist/vitest-integration.js +1 -64
- package/dist/vitest-integration.js.map +7 -1
- package/dist/vitest-repo.js +1 -146
- package/dist/vitest-repo.js.map +7 -1
- package/dist/vitest-web.js +1 -109
- package/dist/vitest-web.js.map +7 -1
- package/dist/vitest.js +1 -133
- package/dist/vitest.js.map +7 -1
- package/package.json +3 -3
- package/src/eslint-boundaries.ts +13 -5
- package/src/eslint.test.ts +18 -8
- package/src/functions/pages-proxy.test.ts +1 -1
- package/src/functions/pages-proxy.ts +4 -2
- package/src/scripts/backup/backup-rules.test.ts +1 -1
- package/src/scripts/backup/d1-target.ts +15 -5
- package/src/scripts/backup/destination-config.ts +27 -9
- package/src/scripts/backup/encrypt-backup.ts +10 -8
- package/src/scripts/backup/naming.ts +3 -1
- package/src/scripts/check-god-files.test.ts +18 -18
- package/src/scripts/check-god-files.ts +60 -31
- package/src/scripts/init-secrets.ts +17 -7
- package/src/scripts/lib/cli-args.ts +6 -3
- package/src/scripts/lib/wrangler-table.ts +5 -3
- package/src/scripts/migrations-lock.ts +1 -1
- package/src/scripts/spa-shell.test.ts +17 -17
- package/src/scripts/spa-shell.ts +26 -14
- package/src/scripts/validate-locales.test.ts +17 -17
- package/src/scripts/validate-locales.ts +43 -25
- package/src/scripts/verify-migrations.test.ts +7 -7
- package/src/scripts/verify-migrations.ts +47 -24
- package/src/scripts/wrangler-config/patches.ts +12 -4
- package/src/scripts/wrangler-config/resources.ts +30 -14
- package/src/scripts/wrangler-config/types.ts +24 -8
- package/src/test/integration-migrations.test.ts +6 -6
- package/src/test/integration-migrations.ts +33 -14
- package/src/vite.test.ts +24 -13
- package/src/vite.ts +5 -5
- package/src/vitest-aliases.ts +1 -3
|
@@ -59,7 +59,7 @@ export function parseFlags(argv: readonly string[], spec: FlagSpec): ParsedFlags
|
|
|
59
59
|
const flags: ParsedFlags = new Map();
|
|
60
60
|
|
|
61
61
|
for (let index = 0; index < argv.length; index += 1) {
|
|
62
|
-
const arg = argv[index]
|
|
62
|
+
const arg = argv[index];
|
|
63
63
|
if (!arg.startsWith('-')) {
|
|
64
64
|
throw new Error(`Unexpected argument: ${arg}`);
|
|
65
65
|
}
|
|
@@ -81,7 +81,10 @@ export function parseFlags(argv: readonly string[], spec: FlagSpec): ParsedFlags
|
|
|
81
81
|
throw new Error(`--${name} was given more than once`);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
// `argv[index + 1]` is statically a string, but a value flag in last
|
|
85
|
+
// position has no next element at runtime — the length check is what makes
|
|
86
|
+
// the `undefined` below reachable rather than dead.
|
|
87
|
+
const value = index + 1 < argv.length ? argv[index + 1] : undefined;
|
|
85
88
|
// A value that starts with `-` is treated as a missing value so that
|
|
86
89
|
// `--to --dry-run` reports the real problem instead of capturing the flag.
|
|
87
90
|
if (value === undefined || value.startsWith('-')) {
|
|
@@ -132,7 +135,7 @@ export function splitPositional(
|
|
|
132
135
|
const rest: string[] = [];
|
|
133
136
|
|
|
134
137
|
for (let index = 0; index < argv.length; index += 1) {
|
|
135
|
-
const arg = argv[index]
|
|
138
|
+
const arg = argv[index];
|
|
136
139
|
if (arg.startsWith('-')) {
|
|
137
140
|
rest.push(arg);
|
|
138
141
|
continue;
|
|
@@ -56,10 +56,10 @@ export function parseWranglerTableRows(output: string): string[][] {
|
|
|
56
56
|
.split(COLUMN_SEPARATOR)
|
|
57
57
|
.map((cell) => cell.trim())
|
|
58
58
|
.filter(Boolean);
|
|
59
|
-
|
|
60
|
-
if (firstCell === undefined || cells.length < 2) {
|
|
59
|
+
if (cells.length < 2) {
|
|
61
60
|
continue;
|
|
62
61
|
}
|
|
62
|
+
const firstCell = cells[0];
|
|
63
63
|
// The header row and the horizontal rules are frame, not data.
|
|
64
64
|
if (firstCell === HEADER_FIRST_CELL || firstCell.includes(RULE_CHARACTER)) {
|
|
65
65
|
continue;
|
|
@@ -78,7 +78,9 @@ export function parseWranglerTableRows(output: string): string[][] {
|
|
|
78
78
|
export function parseWranglerNames(output: string): Set<string> {
|
|
79
79
|
const names = new Set<string>();
|
|
80
80
|
for (const row of parseWranglerTableRows(output)) {
|
|
81
|
-
|
|
81
|
+
// `.at()` rather than `[0]`: the absence case is real for a hand-built row
|
|
82
|
+
// in a test double, and the guard below is what keeps it out of the set.
|
|
83
|
+
const name = row.at(0);
|
|
82
84
|
if (name !== undefined) names.add(name);
|
|
83
85
|
}
|
|
84
86
|
return names;
|
|
@@ -41,7 +41,7 @@ for (const problem of report.problems) {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
console.log(
|
|
44
|
-
`${report.lock === null ? 'left' : 'wrote'} ${path.join(migrationsDir, LOCK_NAME)}
|
|
44
|
+
`${report.lock === null ? 'left' : 'wrote'} ${path.join(migrationsDir, LOCK_NAME)} (${report.files.length} migration(s) recorded)`,
|
|
45
45
|
);
|
|
46
46
|
|
|
47
47
|
// The lock is re-checked by the caller: `verify-migrations.ts` re-reads from disk,
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { tmpdir } from 'node:os';
|
|
3
|
-
import
|
|
3
|
+
import path from 'node:path';
|
|
4
4
|
import { describe, expect, it } from 'vitest';
|
|
5
5
|
import { ensureSpaShellStub, spaShellCli, verifySpaShell } from './spa-shell.ts';
|
|
6
6
|
|
|
7
7
|
function tempTree(): string {
|
|
8
|
-
return mkdtempSync(join(tmpdir(), 'spa-shell-'));
|
|
8
|
+
return mkdtempSync(path.join(tmpdir(), 'spa-shell-'));
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
function writeBuild(root: string, html: string): void {
|
|
12
|
-
const shell = join(root, 'apps/api/src/generated');
|
|
12
|
+
const shell = path.join(root, 'apps/api/src/generated');
|
|
13
13
|
mkdirSync(shell, { recursive: true });
|
|
14
|
-
writeFileSync(join(shell, 'spa-shell.ts'), `export const SPA_HTML: string = ${JSON.stringify(html)};\n`, 'utf8');
|
|
15
|
-
const distAssets = join(root, 'apps/web/dist/assets');
|
|
14
|
+
writeFileSync(path.join(shell, 'spa-shell.ts'), `export const SPA_HTML: string = ${JSON.stringify(html)};\n`, 'utf8');
|
|
15
|
+
const distAssets = path.join(root, 'apps/web/dist/assets');
|
|
16
16
|
mkdirSync(distAssets, { recursive: true });
|
|
17
|
-
writeFileSync(join(root, 'apps/web/dist/index.html'), html, 'utf8');
|
|
17
|
+
writeFileSync(path.join(root, 'apps/web/dist/index.html'), html, 'utf8');
|
|
18
18
|
for (const asset of html.matchAll(/\/assets\/([^"]+)/g)) {
|
|
19
|
-
writeFileSync(join(distAssets, asset[1] as string), 'console.log(1)', 'utf8');
|
|
19
|
+
writeFileSync(path.join(distAssets, asset[1] as string), 'console.log(1)', 'utf8');
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -27,18 +27,18 @@ describe('ensureSpaShellStub', () => {
|
|
|
27
27
|
const root = tempTree();
|
|
28
28
|
const result = ensureSpaShellStub({ root });
|
|
29
29
|
expect(result.created).toBe(true);
|
|
30
|
-
expect(existsSync(join(root, 'apps/api/src/generated/spa-shell.ts'))).toBe(true);
|
|
30
|
+
expect(existsSync(path.join(root, 'apps/api/src/generated/spa-shell.ts'))).toBe(true);
|
|
31
31
|
expect(readFileSync(result.path, 'utf8')).toContain("SPA_HTML: string = ''");
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
it('never overwrites an existing file', () => {
|
|
35
35
|
const root = tempTree();
|
|
36
|
-
const
|
|
37
|
-
mkdirSync(dirname(
|
|
38
|
-
writeFileSync(
|
|
36
|
+
const file = path.join(root, 'apps/api/src/generated', 'spa-shell.ts');
|
|
37
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
38
|
+
writeFileSync(file, 'real content', 'utf8');
|
|
39
39
|
const result = ensureSpaShellStub({ root });
|
|
40
40
|
expect(result.created).toBe(false);
|
|
41
|
-
expect(readFileSync(
|
|
41
|
+
expect(readFileSync(file, 'utf8')).toBe('real content');
|
|
42
42
|
});
|
|
43
43
|
});
|
|
44
44
|
|
|
@@ -57,23 +57,23 @@ describe('verifySpaShell', () => {
|
|
|
57
57
|
|
|
58
58
|
it('reports the postinstall stub', () => {
|
|
59
59
|
const root = tempTree();
|
|
60
|
-
const
|
|
61
|
-
mkdirSync(dirname(
|
|
62
|
-
writeFileSync(
|
|
60
|
+
const file = path.join(root, 'apps/api/src/generated', 'spa-shell.ts');
|
|
61
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
62
|
+
writeFileSync(file, "export const SPA_HTML: string = '';\n", 'utf8');
|
|
63
63
|
expect(verifySpaShell({ root }).problems.join('\n')).toContain('postinstall stub');
|
|
64
64
|
});
|
|
65
65
|
|
|
66
66
|
it('reports referenced-but-missing assets', () => {
|
|
67
67
|
const root = tempTree();
|
|
68
68
|
writeBuild(root, REAL_HTML);
|
|
69
|
-
rmSync(join(root, 'apps/web/dist/assets'), { recursive: true, force: true });
|
|
69
|
+
rmSync(path.join(root, 'apps/web/dist/assets'), { recursive: true, force: true });
|
|
70
70
|
expect(verifySpaShell({ root }).problems.join('\n')).toContain('/assets/index-abc123.js');
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
it('reports a shell that disagrees with dist/index.html', () => {
|
|
74
74
|
const root = tempTree();
|
|
75
75
|
writeBuild(root, REAL_HTML);
|
|
76
|
-
writeFileSync(join(root, 'apps/web/dist/index.html'), '<!doctype html><html>different</html>', 'utf8');
|
|
76
|
+
writeFileSync(path.join(root, 'apps/web/dist/index.html'), '<!doctype html><html>different</html>', 'utf8');
|
|
77
77
|
expect(verifySpaShell({ root }).problems.join('\n')).toContain('does not match');
|
|
78
78
|
});
|
|
79
79
|
});
|
package/src/scripts/spa-shell.ts
CHANGED
|
@@ -15,14 +15,20 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
17
17
|
import { pathToFileURL } from 'node:url';
|
|
18
|
-
import
|
|
18
|
+
import path from 'node:path';
|
|
19
19
|
|
|
20
20
|
interface SpaShellPaths {
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
Repo root.
|
|
23
|
+
*/
|
|
22
24
|
root: string;
|
|
23
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
Generated TS module path, relative to root.
|
|
27
|
+
*/
|
|
24
28
|
shellPath?: string;
|
|
25
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
Built SPA index.html path, relative to root.
|
|
31
|
+
*/
|
|
26
32
|
distIndexPath?: string;
|
|
27
33
|
}
|
|
28
34
|
|
|
@@ -35,14 +41,16 @@ const DEFAULT_DIST_INDEX = 'apps/web/dist/index.html';
|
|
|
35
41
|
const STUB_CONTENT =
|
|
36
42
|
"// Auto-generated stub - real content is produced by the build step via the Vite plugin.\nexport const SPA_HTML: string = '';\n";
|
|
37
43
|
|
|
38
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
A built shell is a full Vite index.html; any small threshold rejects the stub.
|
|
46
|
+
*/
|
|
39
47
|
const MIN_HTML_CHARS = 200;
|
|
40
48
|
|
|
41
49
|
function resolvePaths(options: SpaShellPaths): { shell: string; distIndex: string; distAssets: string } {
|
|
42
|
-
const root = resolve(options.root);
|
|
43
|
-
const shell = join(root, options.shellPath ?? DEFAULT_SHELL);
|
|
44
|
-
const distIndex = join(root, options.distIndexPath ?? DEFAULT_DIST_INDEX);
|
|
45
|
-
return { shell, distIndex, distAssets: join(dirname(distIndex), 'assets') };
|
|
50
|
+
const root = path.resolve(options.root);
|
|
51
|
+
const shell = path.join(root, options.shellPath ?? DEFAULT_SHELL);
|
|
52
|
+
const distIndex = path.join(root, options.distIndexPath ?? DEFAULT_DIST_INDEX);
|
|
53
|
+
return { shell, distIndex, distAssets: path.join(path.dirname(distIndex), 'assets') };
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
/**
|
|
@@ -53,7 +61,7 @@ function resolvePaths(options: SpaShellPaths): { shell: string; distIndex: strin
|
|
|
53
61
|
*/
|
|
54
62
|
function ensureSpaShellStub(options: SpaShellPaths): { created: boolean; path: string } {
|
|
55
63
|
const { shell } = resolvePaths(options);
|
|
56
|
-
mkdirSync(dirname(shell), { recursive: true });
|
|
64
|
+
mkdirSync(path.dirname(shell), { recursive: true });
|
|
57
65
|
try {
|
|
58
66
|
writeFileSync(shell, STUB_CONTENT, { encoding: 'utf8', flag: 'wx' });
|
|
59
67
|
} catch (error) {
|
|
@@ -65,7 +73,9 @@ function ensureSpaShellStub(options: SpaShellPaths): { created: boolean; path: s
|
|
|
65
73
|
return { created: true, path: shell };
|
|
66
74
|
}
|
|
67
75
|
|
|
68
|
-
/**
|
|
76
|
+
/**
|
|
77
|
+
The `SPA_HTML` string literal, decoded from the generated TS module.
|
|
78
|
+
*/
|
|
69
79
|
function readEmbeddedHtml(shell: string, problems: string[]): string | null {
|
|
70
80
|
if (!existsSync(shell)) {
|
|
71
81
|
problems.push(`${shell} is missing — run the build (only apps/web has a build step).`);
|
|
@@ -120,12 +130,12 @@ function verifySpaShell(options: SpaShellPaths): VerifyResult {
|
|
|
120
130
|
if (html === null) {
|
|
121
131
|
return { problems };
|
|
122
132
|
}
|
|
123
|
-
const referenced = [...html.matchAll(/(?:src|href)="\/assets\/([^"]+)"/g)].map((match) => match[1]
|
|
133
|
+
const referenced = [...html.matchAll(/(?:src|href)="\/assets\/([^"]+)"/g)].map((match) => match[1]);
|
|
124
134
|
if (referenced.length === 0) {
|
|
125
135
|
problems.push('The embedded shell references no /assets/ bundles — it is not a real build output.');
|
|
126
136
|
}
|
|
127
137
|
for (const asset of new Set(referenced)) {
|
|
128
|
-
if (!existsSync(join(distAssets, asset))) {
|
|
138
|
+
if (!existsSync(path.join(distAssets, asset))) {
|
|
129
139
|
problems.push(`The embedded shell references /assets/${asset}, which is not in ${distAssets} — run the build.`);
|
|
130
140
|
}
|
|
131
141
|
}
|
|
@@ -139,7 +149,9 @@ function verifySpaShell(options: SpaShellPaths): VerifyResult {
|
|
|
139
149
|
|
|
140
150
|
const CLI_SHAPES = 'verify|ensure';
|
|
141
151
|
|
|
142
|
-
/**
|
|
152
|
+
/**
|
|
153
|
+
CLI entry: `spaShellCli(['verify'|'ensure', 'repo-root?'])`.
|
|
154
|
+
*/
|
|
143
155
|
function spaShellCli(argv: readonly string[] = process.argv.slice(2)): number {
|
|
144
156
|
const mode = argv[0] ?? 'verify';
|
|
145
157
|
if (!CLI_SHAPES.split('|').includes(mode)) {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { tmpdir } from 'node:os';
|
|
3
|
-
import
|
|
3
|
+
import path from 'node:path';
|
|
4
4
|
import { describe, expect, it } from 'vitest';
|
|
5
5
|
import { parseObjectLiteral, validateLocales, validateLocalesCli } from './validate-locales.ts';
|
|
6
6
|
|
|
7
7
|
function writeBundles(root: string, locale: string, bundle: Record<string, unknown>): string {
|
|
8
|
-
const dir = join(root, locale);
|
|
8
|
+
const dir = path.join(root, locale);
|
|
9
9
|
mkdirSync(dir, { recursive: true });
|
|
10
|
-
writeFileSync(join(dir, 'translation.json'), JSON.stringify(bundle), 'utf8');
|
|
10
|
+
writeFileSync(path.join(dir, 'translation.json'), JSON.stringify(bundle), 'utf8');
|
|
11
11
|
return root;
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -15,7 +15,7 @@ const EN = { common: { unauthorized: 'Unauthorized', forbidden: 'Forbidden' } };
|
|
|
15
15
|
|
|
16
16
|
describe('validateLocales', () => {
|
|
17
17
|
it('passes when every locale mirrors the base keys', () => {
|
|
18
|
-
const root = mkdtempSync(join(tmpdir(), 'locales-'));
|
|
18
|
+
const root = mkdtempSync(path.join(tmpdir(), 'locales-'));
|
|
19
19
|
writeBundles(root, 'en', EN);
|
|
20
20
|
writeBundles(root, 'de', { common: { unauthorized: 'Nicht autorisiert', forbidden: 'Verboten' } });
|
|
21
21
|
const report = validateLocales(root);
|
|
@@ -25,7 +25,7 @@ describe('validateLocales', () => {
|
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
it('reports both missing and extra keys', () => {
|
|
28
|
-
const root = mkdtempSync(join(tmpdir(), 'locales-'));
|
|
28
|
+
const root = mkdtempSync(path.join(tmpdir(), 'locales-'));
|
|
29
29
|
writeBundles(root, 'en', EN);
|
|
30
30
|
writeBundles(root, 'de', { common: { unauthorized: 'x', oops: 'y' } });
|
|
31
31
|
const problems = validateLocales(root).problems;
|
|
@@ -37,38 +37,38 @@ describe('validateLocales', () => {
|
|
|
37
37
|
|
|
38
38
|
it('reports a missing directory and a missing base locale', () => {
|
|
39
39
|
expect(validateLocales('/nonexistent-xyz').problems.length).toBeGreaterThan(0);
|
|
40
|
-
const root = mkdtempSync(join(tmpdir(), 'locales-'));
|
|
40
|
+
const root = mkdtempSync(path.join(tmpdir(), 'locales-'));
|
|
41
41
|
writeBundles(root, 'de', EN);
|
|
42
42
|
expect(validateLocales(root).problems[0]?.problem).toContain('base locale');
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
it('supports flat <locale>.json bundles', () => {
|
|
46
|
-
const root = mkdtempSync(join(tmpdir(), 'locales-'));
|
|
47
|
-
writeFileSync(join(root, 'en.json'), JSON.stringify(EN), 'utf8');
|
|
48
|
-
writeFileSync(join(root, 'de.json'), JSON.stringify({ common: { unauthorized: 'x', forbidden: 'y' } }), 'utf8');
|
|
46
|
+
const root = mkdtempSync(path.join(tmpdir(), 'locales-'));
|
|
47
|
+
writeFileSync(path.join(root, 'en.json'), JSON.stringify(EN), 'utf8');
|
|
48
|
+
writeFileSync(path.join(root, 'de.json'), JSON.stringify({ common: { unauthorized: 'x', forbidden: 'y' } }), 'utf8');
|
|
49
49
|
expect(validateLocales(root).problems).toStrictEqual([]);
|
|
50
50
|
});
|
|
51
51
|
|
|
52
52
|
it('CLI returns 1 with problems and 0 without', () => {
|
|
53
|
-
const root = mkdtempSync(join(tmpdir(), 'locales-'));
|
|
53
|
+
const root = mkdtempSync(path.join(tmpdir(), 'locales-'));
|
|
54
54
|
writeBundles(root, 'en', EN);
|
|
55
55
|
writeBundles(root, 'de', { common: { unauthorized: 'x' } });
|
|
56
56
|
expect(validateLocalesCli([root])).toBe(1);
|
|
57
|
-
writeFileSync(join(root, 'de', 'translation.json'), JSON.stringify(EN), 'utf8');
|
|
57
|
+
writeFileSync(path.join(root, 'de', 'translation.json'), JSON.stringify(EN), 'utf8');
|
|
58
58
|
expect(validateLocalesCli([root])).toBe(0);
|
|
59
59
|
});
|
|
60
60
|
|
|
61
61
|
it('reads the module shape the kit i18n bundles use', () => {
|
|
62
62
|
// `@rexezuge/i18n` ships `const enStrings: BackendStrings = { … }` per file, so
|
|
63
63
|
// the same gate covers the backend catalogs and the SPA JSON bundles.
|
|
64
|
-
const root = mkdtempSync(join(tmpdir(), 'locales-'));
|
|
64
|
+
const root = mkdtempSync(path.join(tmpdir(), 'locales-'));
|
|
65
65
|
writeFileSync(
|
|
66
|
-
join(root, 'en.ts'),
|
|
66
|
+
path.join(root, 'en.ts'),
|
|
67
67
|
"import type { BackendStrings } from '../BackendStrings';\n\nconst enStrings: BackendStrings = {\n common: {\n unauthorized: 'Authentication Required.',\n forbidden: 'Access Denied.',\n },\n};\n\nexport { enStrings };\n",
|
|
68
68
|
'utf8',
|
|
69
69
|
);
|
|
70
70
|
writeFileSync(
|
|
71
|
-
join(root, 'de.ts'),
|
|
71
|
+
path.join(root, 'de.ts'),
|
|
72
72
|
"const deStrings = {\n common: {\n unauthorized: 'Authentifizierung Erforderlich.',\n forbidden: 'Zugriff Verweigert.',\n },\n};\n",
|
|
73
73
|
'utf8',
|
|
74
74
|
);
|
|
@@ -80,7 +80,7 @@ describe('validateLocales', () => {
|
|
|
80
80
|
"import type { BackendStrings } from '../BackendStrings';",
|
|
81
81
|
'',
|
|
82
82
|
'const enStrings: BackendStrings = {',
|
|
83
|
-
|
|
83
|
+
String.raw` common: { unauthorized: 'It\'s fine', },`,
|
|
84
84
|
'};',
|
|
85
85
|
'',
|
|
86
86
|
'export { enStrings };',
|
|
@@ -89,8 +89,8 @@ describe('validateLocales', () => {
|
|
|
89
89
|
});
|
|
90
90
|
|
|
91
91
|
it('reports an unparseable module rather than passing it silently', () => {
|
|
92
|
-
const root = mkdtempSync(join(tmpdir(), 'locales-'));
|
|
93
|
-
writeFileSync(join(root, 'en.ts'), 'const enStrings = {};\n', 'utf8');
|
|
92
|
+
const root = mkdtempSync(path.join(tmpdir(), 'locales-'));
|
|
93
|
+
writeFileSync(path.join(root, 'en.ts'), 'const enStrings = {};\n', 'utf8');
|
|
94
94
|
const problems = validateLocales(root).problems;
|
|
95
95
|
expect(problems[0]?.problem).toMatch(/no keys|object literal/);
|
|
96
96
|
});
|
|
@@ -37,25 +37,33 @@
|
|
|
37
37
|
*/
|
|
38
38
|
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
39
39
|
import { pathToFileURL } from 'node:url';
|
|
40
|
-
import
|
|
40
|
+
import path from 'node:path';
|
|
41
41
|
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
The bundle layouts the checker understands.
|
|
44
|
+
*/
|
|
43
45
|
type LocaleShape = 'auto' | 'dir' | 'file' | 'module';
|
|
44
46
|
|
|
45
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
One defect, kept as a locale plus a message so a caller can group them.
|
|
49
|
+
*/
|
|
46
50
|
interface LocaleProblem {
|
|
47
51
|
locale: string;
|
|
48
52
|
problem: string;
|
|
49
53
|
}
|
|
50
54
|
|
|
51
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
The outcome of one run.
|
|
57
|
+
*/
|
|
52
58
|
interface LocaleReport {
|
|
53
59
|
locales: string[];
|
|
54
60
|
keys: number;
|
|
55
61
|
problems: LocaleProblem[];
|
|
56
62
|
}
|
|
57
63
|
|
|
58
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
Every dotted key path in a bundle, depth first.
|
|
66
|
+
*/
|
|
59
67
|
function flatten(value: unknown, prefix = ''): string[] {
|
|
60
68
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
61
69
|
return prefix ? [prefix] : [];
|
|
@@ -73,12 +81,12 @@ function flatten(value: unknown, prefix = ''): string[] {
|
|
|
73
81
|
* the module is the point — a validator that imported the bundle would typecheck it
|
|
74
82
|
* against the kit's own types, and therefore agree with whatever it was given.
|
|
75
83
|
*/
|
|
76
|
-
function readBundle(
|
|
77
|
-
const source = readFileSync(
|
|
84
|
+
function readBundle(file: string): unknown {
|
|
85
|
+
const source = readFileSync(file, 'utf8');
|
|
78
86
|
// Decided by the file's extension, not by the caller's `shape`: under `auto` a
|
|
79
87
|
// `.ts` bundle is found, and reading it as JSON would report a parse error for a
|
|
80
88
|
// file that is perfectly well-formed.
|
|
81
|
-
return
|
|
89
|
+
return file.endsWith('.ts') ? parseObjectLiteral(source, file) : (JSON.parse(source) as unknown);
|
|
82
90
|
}
|
|
83
91
|
|
|
84
92
|
/**
|
|
@@ -106,13 +114,13 @@ function objectLiteralStart(source: string): number {
|
|
|
106
114
|
let inLineComment = false;
|
|
107
115
|
let inBlockComment = false;
|
|
108
116
|
for (let index = 0; index < source.length; index += 1) {
|
|
109
|
-
const char = source[index]
|
|
110
|
-
const next = source[index + 1];
|
|
117
|
+
const char = source[index];
|
|
111
118
|
|
|
112
119
|
if (inLineComment) {
|
|
113
120
|
if (char === '\n') inLineComment = false;
|
|
114
121
|
continue;
|
|
115
122
|
}
|
|
123
|
+
const next = source[index + 1];
|
|
116
124
|
if (inBlockComment) {
|
|
117
125
|
if (char === '*' && next === '/') {
|
|
118
126
|
inBlockComment = false;
|
|
@@ -151,10 +159,10 @@ function objectLiteralStart(source: string): number {
|
|
|
151
159
|
return -1;
|
|
152
160
|
}
|
|
153
161
|
|
|
154
|
-
function parseObjectLiteral(source: string,
|
|
162
|
+
function parseObjectLiteral(source: string, file: string): unknown {
|
|
155
163
|
const start = objectLiteralStart(source);
|
|
156
164
|
if (start === -1) {
|
|
157
|
-
throw new Error(`${
|
|
165
|
+
throw new Error(`${file} has no object literal`);
|
|
158
166
|
}
|
|
159
167
|
|
|
160
168
|
let depth = 0;
|
|
@@ -163,13 +171,13 @@ function parseObjectLiteral(source: string, path: string): unknown {
|
|
|
163
171
|
let inLineComment = false;
|
|
164
172
|
let inBlockComment = false;
|
|
165
173
|
for (let index = start; index < source.length; index += 1) {
|
|
166
|
-
const char = source[index]
|
|
167
|
-
const next = source[index + 1];
|
|
174
|
+
const char = source[index];
|
|
168
175
|
|
|
169
176
|
if (inLineComment) {
|
|
170
177
|
if (char === '\n') inLineComment = false;
|
|
171
178
|
continue;
|
|
172
179
|
}
|
|
180
|
+
const next = source[index + 1];
|
|
173
181
|
if (inBlockComment) {
|
|
174
182
|
if (char === '*' && next === '/') {
|
|
175
183
|
inBlockComment = false;
|
|
@@ -209,7 +217,7 @@ function parseObjectLiteral(source: string, path: string): unknown {
|
|
|
209
217
|
}
|
|
210
218
|
}
|
|
211
219
|
if (end === -1) {
|
|
212
|
-
throw new Error(`${
|
|
220
|
+
throw new Error(`${file} has an unterminated object literal`);
|
|
213
221
|
}
|
|
214
222
|
|
|
215
223
|
const literal = source.slice(start, end + 1);
|
|
@@ -219,17 +227,19 @@ function parseObjectLiteral(source: string, path: string): unknown {
|
|
|
219
227
|
// commas are dropped. A translator file that needs more than that is not a
|
|
220
228
|
// translator file.
|
|
221
229
|
const doubleQuoted = literal.replaceAll(/'((?:[^'\\]|\\.)*)'/g, (_match, body: string) => JSON.stringify(unescapeLiteral(body)));
|
|
222
|
-
const quotedKeys = doubleQuoted.replaceAll(/([{,]\s*)([A-
|
|
230
|
+
const quotedKeys = doubleQuoted.replaceAll(/([{,]\s*)([A-Z_$][\w$]*)(\s*:)/gi, '$1"$2"$3');
|
|
223
231
|
const withoutTrailingCommas = quotedKeys.replaceAll(/,(\s*[}\]])/g, '$1');
|
|
224
232
|
|
|
225
233
|
try {
|
|
226
234
|
return JSON.parse(withoutTrailingCommas) as unknown;
|
|
227
235
|
} catch (error: unknown) {
|
|
228
|
-
throw new Error(`${
|
|
236
|
+
throw new Error(`${file} object literal could not be parsed: ${error instanceof Error ? error.message : String(error)}`);
|
|
229
237
|
}
|
|
230
238
|
}
|
|
231
239
|
|
|
232
|
-
/**
|
|
240
|
+
/**
|
|
241
|
+
The escapes a translator file actually uses; anything else is passed through.
|
|
242
|
+
*/
|
|
233
243
|
function unescapeLiteral(body: string): string {
|
|
234
244
|
return body
|
|
235
245
|
.replaceAll(String.raw`\'`, "'")
|
|
@@ -239,15 +249,21 @@ function unescapeLiteral(body: string): string {
|
|
|
239
249
|
.replaceAll(String.raw`\\`, '\\');
|
|
240
250
|
}
|
|
241
251
|
|
|
242
|
-
/**
|
|
252
|
+
/**
|
|
253
|
+
The extensions a flat layout uses, keyed by shape.
|
|
254
|
+
*/
|
|
243
255
|
const FLAT_EXTENSION: Record<'file' | 'module', string> = { file: 'json', module: 'ts' };
|
|
244
256
|
|
|
245
|
-
/**
|
|
257
|
+
/**
|
|
258
|
+
The layouts `shape` covers. `auto` covers all three, in the order they are tried.
|
|
259
|
+
*/
|
|
246
260
|
function layouts(shape: LocaleShape): Array<'dir' | 'file' | 'module'> {
|
|
247
261
|
return shape === 'auto' ? ['dir', 'file', 'module'] : [shape];
|
|
248
262
|
}
|
|
249
263
|
|
|
250
|
-
/**
|
|
264
|
+
/**
|
|
265
|
+
The tags on disk, in whichever layouts `shape` covers.
|
|
266
|
+
*/
|
|
251
267
|
function tagsOnDisk(localesDir: string, shape: LocaleShape): string[] {
|
|
252
268
|
const tags: string[] = [];
|
|
253
269
|
if (layouts(shape).includes('dir')) {
|
|
@@ -280,11 +296,11 @@ function tagsOnDisk(localesDir: string, shape: LocaleShape): string[] {
|
|
|
280
296
|
function bundleFor(localesDir: string, locale: string, shape: LocaleShape): string | undefined {
|
|
281
297
|
const candidates: string[] = [];
|
|
282
298
|
if (layouts(shape).includes('dir')) {
|
|
283
|
-
candidates.push(join(localesDir, locale, 'translation.json'));
|
|
299
|
+
candidates.push(path.join(localesDir, locale, 'translation.json'));
|
|
284
300
|
}
|
|
285
301
|
for (const layout of layouts(shape)) {
|
|
286
302
|
if (layout === 'dir') continue;
|
|
287
|
-
candidates.push(join(localesDir, `${locale}.${FLAT_EXTENSION[layout]}`));
|
|
303
|
+
candidates.push(path.join(localesDir, `${locale}.${FLAT_EXTENSION[layout]}`));
|
|
288
304
|
}
|
|
289
305
|
return candidates.find((candidate) => existsSync(candidate));
|
|
290
306
|
}
|
|
@@ -324,7 +340,7 @@ function validateLocales(localesDir: string, baseLocale = 'en', shape: LocaleSha
|
|
|
324
340
|
return { locales: [baseLocale], keys: 0, problems };
|
|
325
341
|
}
|
|
326
342
|
|
|
327
|
-
const locales = [...new Set([...tagsOnDisk(localesDir, shape), baseLocale])].sort();
|
|
343
|
+
const locales = [...new Set([...tagsOnDisk(localesDir, shape), baseLocale])].sort((a, b) => a.localeCompare(b));
|
|
328
344
|
for (const locale of locales) {
|
|
329
345
|
const file = bundleFor(localesDir, locale, shape);
|
|
330
346
|
if (file === undefined) {
|
|
@@ -349,7 +365,9 @@ function validateLocales(localesDir: string, baseLocale = 'en', shape: LocaleSha
|
|
|
349
365
|
return { locales, keys: baseKeys.size, problems };
|
|
350
366
|
}
|
|
351
367
|
|
|
352
|
-
/**
|
|
368
|
+
/**
|
|
369
|
+
CLI entry: `validateLocalesCli(['[locales-dir]?', '--shape=auto|dir|file|module'?, '--base=<tag>'?])`.
|
|
370
|
+
*/
|
|
353
371
|
function validateLocalesCli(argv: readonly string[] = process.argv.slice(2)): number {
|
|
354
372
|
const dir = argv.find((arg) => !arg.startsWith('--')) ?? 'apps/web/src/locales';
|
|
355
373
|
const shape = (argv.find((arg) => arg.startsWith('--shape='))?.slice('--shape='.length) ?? 'auto') as LocaleShape;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { tmpdir } from 'node:os';
|
|
3
|
-
import
|
|
3
|
+
import path from 'node:path';
|
|
4
4
|
import { describe, expect, it } from 'vitest';
|
|
5
5
|
import { verifyMigrations, verifyMigrationsCli, LOCK_NAME } from './verify-migrations.ts';
|
|
6
6
|
|
|
7
7
|
function tempMigrations(files: Record<string, string>): string {
|
|
8
|
-
const root = mkdtempSync(join(tmpdir(), 'migrations-'));
|
|
8
|
+
const root = mkdtempSync(path.join(tmpdir(), 'migrations-'));
|
|
9
9
|
for (const [name, content] of Object.entries(files)) {
|
|
10
|
-
writeFileSync(join(root, name), content, 'utf8');
|
|
10
|
+
writeFileSync(path.join(root, name), content, 'utf8');
|
|
11
11
|
}
|
|
12
12
|
return root;
|
|
13
13
|
}
|
|
@@ -33,7 +33,7 @@ describe('verifyMigrations', () => {
|
|
|
33
33
|
it('fails when a migration changed after locking', () => {
|
|
34
34
|
const root = tempMigrations({ '0001_init.sql': ONE });
|
|
35
35
|
verifyMigrations(root, { write: true });
|
|
36
|
-
writeFileSync(join(root, '0001_init.sql'), 'CREATE TABLE changed (id TEXT);', 'utf8');
|
|
36
|
+
writeFileSync(path.join(root, '0001_init.sql'), 'CREATE TABLE changed (id TEXT);', 'utf8');
|
|
37
37
|
const report = verifyMigrations(root);
|
|
38
38
|
expect(report.problems.map((problem) => problem.problem).join('\n')).toContain('changed after locking');
|
|
39
39
|
});
|
|
@@ -58,7 +58,7 @@ describe('verifyMigrations', () => {
|
|
|
58
58
|
const root = tempMigrations({ '0001_init.sql': ONE, '0002_more.sql': TWO });
|
|
59
59
|
verifyMigrations(root, { write: true });
|
|
60
60
|
const { rmSync } = await import('node:fs');
|
|
61
|
-
rmSync(join(root, '0002_more.sql'));
|
|
61
|
+
rmSync(path.join(root, '0002_more.sql'));
|
|
62
62
|
expect(
|
|
63
63
|
verifyMigrations(root)
|
|
64
64
|
.problems.map((problem) => problem.problem)
|
|
@@ -70,7 +70,7 @@ describe('verifyMigrations', () => {
|
|
|
70
70
|
const root = tempMigrations({ '0001_init.sql': ONE });
|
|
71
71
|
const report = verifyMigrations(root, { write: true });
|
|
72
72
|
expect(report.problems).toStrictEqual([]);
|
|
73
|
-
const lock = JSON.parse(readFileSync(join(root, LOCK_NAME), 'utf8')) as { migrations: { sha256: string }[] };
|
|
73
|
+
const lock = JSON.parse(readFileSync(path.join(root, LOCK_NAME), 'utf8')) as { migrations: { sha256: string }[] };
|
|
74
74
|
expect(lock.migrations[0]?.sha256).toMatch(/^[a-f0-9]{64}$/);
|
|
75
75
|
});
|
|
76
76
|
|