chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,166 @@
|
||||
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { gzipSync } from 'node:zlib'
|
||||
import {
|
||||
collect,
|
||||
compare,
|
||||
parseAnalysis,
|
||||
parseSnapshot,
|
||||
shouldComment,
|
||||
staticClosure,
|
||||
} from '../bundle-analysis'
|
||||
|
||||
const dirs: string[] = []
|
||||
const sha = 'a'.repeat(40)
|
||||
const graph = () => ({
|
||||
meta: { bundler: 'rolldown', version: '1.2.9' },
|
||||
chunks: [
|
||||
{
|
||||
name: 'entry.js',
|
||||
size: 5,
|
||||
type: 'static-entry',
|
||||
entryModule: 0,
|
||||
imports: [
|
||||
{ type: 'static', targetChunkIndex: 1 },
|
||||
{ type: 'dynamic', targetChunkIndex: 2 },
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'shared.js',
|
||||
size: 6,
|
||||
type: 'common',
|
||||
imports: [{ type: 'static', targetChunkIndex: 0 }],
|
||||
},
|
||||
{
|
||||
name: 'lazy.js',
|
||||
size: 4,
|
||||
type: 'dynamic-entry',
|
||||
entryModule: 1,
|
||||
imports: [{ type: 'static', targetChunkIndex: 1 }],
|
||||
},
|
||||
],
|
||||
modules: [
|
||||
{ path: 'app/view.tsx', size: 5 },
|
||||
{ path: 'app/lazy.tsx', size: 4 },
|
||||
{ path: '../node_modules/.pnpm/@scope+pkg@1/node_modules/@scope/pkg/index.js', size: 6 },
|
||||
],
|
||||
})
|
||||
async function fixture(analysis = graph()) {
|
||||
const root = await mkdtemp(join(tmpdir(), 'vinext-analysis-test-'))
|
||||
dirs.push(root)
|
||||
const client = join(root, 'dist/client')
|
||||
await mkdir(join(client, '_next/static/css'), { recursive: true })
|
||||
await mkdir(join(root, 'node_modules/vinext'), { recursive: true })
|
||||
await writeFile(
|
||||
join(root, 'node_modules/vinext/package.json'),
|
||||
JSON.stringify({ version: '1.0.0' }),
|
||||
)
|
||||
await writeFile(join(client, 'bundle-analysis.json'), JSON.stringify(analysis))
|
||||
for (const [name, data] of Object.entries({
|
||||
'entry.js': 'entry',
|
||||
'shared.js': 'shared',
|
||||
'lazy.js': 'lazy',
|
||||
'_next/static/css/app.css': 'css',
|
||||
}))
|
||||
await writeFile(join(client, name), data)
|
||||
return root
|
||||
}
|
||||
afterEach(async () => {
|
||||
await Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })))
|
||||
})
|
||||
|
||||
it('deduplicates cycles and shared dependencies without following dynamic edges', () => {
|
||||
expect([...staticClosure(parseAnalysis(JSON.stringify(graph())), 0)]).toEqual([0, 1])
|
||||
expect([...staticClosure(parseAnalysis(JSON.stringify(graph())), 2)]).toEqual([2, 1, 0])
|
||||
})
|
||||
it('measures emitted files independently of attribution and groups scoped packages', async () => {
|
||||
const result = await collect(await fixture(), sha)
|
||||
expect(result.totals.js).toEqual({
|
||||
raw: 15,
|
||||
gzip: gzipSync('entry').length + gzipSync('shared').length + gzipSync('lazy').length,
|
||||
})
|
||||
expect(result.entries['static-entry: app/view.tsx']?.raw).toBe(11)
|
||||
expect(result.totals.css.raw).toBe(3)
|
||||
expect(result.packages).toEqual({ '@scope/pkg': 6 })
|
||||
expect(parseSnapshot(JSON.stringify(result))).toEqual(result)
|
||||
})
|
||||
it('fails on missing emitted files instead of reporting false savings', async () => {
|
||||
const root = await fixture()
|
||||
await rm(join(root, 'dist/client/lazy.js'))
|
||||
await expect(collect(root, sha)).rejects.toThrow('ENOENT')
|
||||
})
|
||||
it('rejects incompatible graph data and dangling references', () => {
|
||||
const invalid = graph()
|
||||
invalid.chunks[0]!.imports![0]!.targetChunkIndex = 99
|
||||
expect(() => parseAnalysis(JSON.stringify(invalid))).toThrow('Invalid')
|
||||
expect(() => parseAnalysis('{}')).toThrow('Invalid')
|
||||
})
|
||||
it('rejects out-of-output chunk paths', async () => {
|
||||
const invalid = graph()
|
||||
invalid.chunks[0]!.name = '../outside.js'
|
||||
await expect(collect(await fixture(invalid), sha)).rejects.toThrow('outside')
|
||||
})
|
||||
it('reports additions, removals, gzip differences and package attribution without calling entries pages', async () => {
|
||||
const base = await collect(await fixture(), sha)
|
||||
const merged = {
|
||||
...base,
|
||||
commit: 'b'.repeat(40),
|
||||
entries: { 'dynamic-entry: app/new|view.tsx': { raw: 4096, gzip: 2048 } },
|
||||
packages: { '@scope/pkg': 1030 },
|
||||
}
|
||||
const report = compare(base, merged)
|
||||
expect(report).toContain('New')
|
||||
expect(report).toContain('Removed')
|
||||
expect(report).toContain('new|view.tsx')
|
||||
expect(report).toContain('New entry')
|
||||
expect(report).not.toContain('+2.00 KiB')
|
||||
expect(report).toContain('+1.00 KiB')
|
||||
expect(report).toContain('not page first-load metrics')
|
||||
expect(() => parseSnapshot(JSON.stringify({ ...base, totals: {} }))).toThrow('Invalid')
|
||||
})
|
||||
|
||||
const snapshot = () => ({
|
||||
version: 1 as const,
|
||||
commit: sha,
|
||||
vinext: '1.0.0',
|
||||
rolldown: '1.2.9',
|
||||
totals: { js: { raw: 100000, gzip: 20000 }, css: { raw: 50000, gzip: 10000 } },
|
||||
entries: { shared: { raw: 50000, gzip: 10000 } },
|
||||
packages: { example: 10000 },
|
||||
})
|
||||
|
||||
it.each([
|
||||
['js', 5 * 1024],
|
||||
['css', 1024],
|
||||
['entry', 2 * 1024],
|
||||
] as const)('comments at the %s gzip threshold in either direction', (metric, threshold) => {
|
||||
for (const direction of [-1, 1]) {
|
||||
const base = snapshot()
|
||||
const merged = snapshot()
|
||||
const target = metric === 'entry' ? merged.entries.shared : merged.totals[metric]
|
||||
target.gzip += direction * (threshold - 1)
|
||||
expect(shouldComment(base, merged)).toBe(false)
|
||||
target.gzip += direction
|
||||
expect(shouldComment(base, merged)).toBe(true)
|
||||
target.gzip += direction
|
||||
expect(shouldComment(base, merged)).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
it('keeps tiny gzip changes and attribution-only or entry-boundary changes in the full report', () => {
|
||||
const base = snapshot()
|
||||
expect(shouldComment(base, base)).toBe(false)
|
||||
const merged = {
|
||||
...snapshot(),
|
||||
totals: { js: { raw: 200000, gzip: 20500 }, css: { raw: 100000, gzip: 10000 } },
|
||||
entries: { added: { raw: 100000, gzip: 20000 } },
|
||||
packages: { example: 100000 },
|
||||
}
|
||||
expect(shouldComment(base, merged)).toBe(false)
|
||||
const report = compare(base, merged)
|
||||
expect(report).toContain('+500 B (+2.50%)')
|
||||
expect(report).toContain('New entry')
|
||||
expect(report).toContain('Removed entry')
|
||||
expect(report).toContain('example')
|
||||
})
|
||||
@@ -0,0 +1,95 @@
|
||||
// @vitest-environment node
|
||||
|
||||
import { execFileSync } from 'node:child_process'
|
||||
import { copyFile, mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { getTranslationSourceKey } from '@dify/tooling/i18n-plurals'
|
||||
import { describe, expect, it } from 'vite-plus/test'
|
||||
|
||||
describe('locale-specific plural keys', () => {
|
||||
const sourceKeys = new Set(['skill.references_one', 'skill.references_other', 'skill.title'])
|
||||
|
||||
it.each([
|
||||
['ru-RU', 'few'],
|
||||
['ru-RU', 'many'],
|
||||
['ar-TN', 'zero'],
|
||||
['ar-TN', 'two'],
|
||||
['ar-TN', 'few'],
|
||||
['ar-TN', 'many'],
|
||||
['sl-SI', 'two'],
|
||||
['fr-FR', 'many'],
|
||||
['en-US', 'zero'],
|
||||
])('accepts %s %s without treating it as an extra key for auto-removal', (locale, category) => {
|
||||
expect(getTranslationSourceKey(sourceKeys, `skill.references_${category}`, locale)).toBe(
|
||||
'skill.references_other',
|
||||
)
|
||||
})
|
||||
|
||||
it.each([
|
||||
['ru-RU', 'skill.references_two'],
|
||||
['en-US', 'skill.references_few'],
|
||||
['ru-RU', 'skill.unknown_few'],
|
||||
['ru-RU', 'otherNamespace.references_few'],
|
||||
['ru-RU', 'skill.title_many'],
|
||||
['ru-RU', 'skill.references_typo'],
|
||||
])('still rejects unrelated or invalid keys in %s: %s', (locale, key) => {
|
||||
expect(getTranslationSourceKey(sourceKeys, key, locale)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('uses ordinal categories for ordinal families', () => {
|
||||
const ordinalKeys = new Set(['position_ordinal_other'])
|
||||
expect(getTranslationSourceKey(ordinalKeys, 'position_ordinal_two', 'en-US')).toBe(
|
||||
'position_ordinal_other',
|
||||
)
|
||||
expect(getTranslationSourceKey(ordinalKeys, 'position_ordinal_zero', 'en-US')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('preserves Russian plural variants when the CLI removes unrelated extra keys', async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), 'dify-i18n-plurals-'))
|
||||
try {
|
||||
await mkdir(path.join(directory, 'scripts'))
|
||||
await symlink(
|
||||
new URL('../../../node_modules', import.meta.url),
|
||||
path.join(directory, 'node_modules'),
|
||||
'junction',
|
||||
)
|
||||
for (const file of ['check-i18n.js', 'check-i18n-values.ts']) {
|
||||
await copyFile(
|
||||
new URL(`../${file}`, import.meta.url),
|
||||
path.join(directory, 'scripts', file),
|
||||
)
|
||||
}
|
||||
for (const locale of ['en-US', 'ru-RU'])
|
||||
await mkdir(path.join(directory, 'i18n', 'locales', locale), { recursive: true })
|
||||
await writeFile(path.join(directory, 'package.json'), '{"type":"module"}')
|
||||
await writeFile(
|
||||
path.join(directory, 'i18n', 'languages.ts'),
|
||||
`export default { languages: [{ value: 'en-US', supported: true }, { value: 'ru-RU', supported: true }] }`,
|
||||
)
|
||||
const source = { references_one: '{{count}} agent', references_other: '{{count}} agents' }
|
||||
const translation = {
|
||||
references_one: '{{count}} агент',
|
||||
references_other: '{{count}} агента',
|
||||
references_few: '{{count}} агента',
|
||||
references_many: '{{count}} агентов',
|
||||
}
|
||||
await writeFile(path.join(directory, 'i18n/locales/en-US/skill.json'), JSON.stringify(source))
|
||||
const target = path.join(directory, 'i18n/locales/ru-RU/skill.json')
|
||||
await writeFile(target, JSON.stringify({ ...translation, obsolete: 'Remove me' }))
|
||||
|
||||
execFileSync(process.execPath, [
|
||||
path.join(directory, 'scripts/check-i18n.js'),
|
||||
'--file',
|
||||
'skill',
|
||||
'--lang',
|
||||
'ru-RU',
|
||||
'--auto-remove',
|
||||
])
|
||||
|
||||
expect(JSON.parse(await readFile(target, 'utf8'))).toEqual(translation)
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,202 @@
|
||||
import { describe, expect, it } from 'vite-plus/test'
|
||||
import { findValueIssues, formatValueIssue } from '../check-i18n-values'
|
||||
|
||||
describe('check-i18n-values', () => {
|
||||
describe('findValueIssues', () => {
|
||||
it('checks placeholders and tags in locale-specific plural variants', () => {
|
||||
const source = { references_other: '<agents>{{count}} agents</agents>' }
|
||||
const translation = { references_few: '{{num}} агента' }
|
||||
expect(findValueIssues(source, translation, 'ru-RU')).toEqual([
|
||||
{
|
||||
key: 'references_few',
|
||||
kind: 'placeholder',
|
||||
expected: ['{{count}}'],
|
||||
actual: ['{{num}}'],
|
||||
},
|
||||
{ key: 'references_few', kind: 'tag', expected: ['</agents>', '<agents>'], actual: [] },
|
||||
])
|
||||
})
|
||||
|
||||
it('accepts valid Arabic variants with the same placeholders', () => {
|
||||
const source = { references_other: '{{count}} agents' }
|
||||
const translation = { references_two: 'وكيلان ({{count}})' }
|
||||
expect(findValueIssues(source, translation, 'ar-TN')).toEqual([])
|
||||
})
|
||||
|
||||
it('should accept a translation that keeps every placeholder and tag', () => {
|
||||
// Arrange
|
||||
const source = {
|
||||
'invite.sent': 'Invitation sent to {{email}}',
|
||||
'plan.docs': '{{count,number}} documents',
|
||||
'encrypted.front': 'Your API KEY will be encrypted using <techLink>PKCS1_OAEP</techLink>',
|
||||
}
|
||||
const translation = {
|
||||
'invite.sent': '{{email}} adresine davet gönderildi',
|
||||
'plan.docs': '{{count,number}} belge',
|
||||
'encrypted.front':
|
||||
'API anahtarınız <techLink>PKCS1_OAEP</techLink> kullanılarak şifrelenecek',
|
||||
}
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([])
|
||||
})
|
||||
|
||||
it('should report a placeholder the translation dropped', () => {
|
||||
// Arrange
|
||||
const source = { 'invite.sent': 'Invitation sent to {{email}}' }
|
||||
const translation = { 'invite.sent': 'Davet gönderildi' }
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([
|
||||
{ key: 'invite.sent', kind: 'placeholder', expected: ['{{email}}'], actual: [] },
|
||||
])
|
||||
})
|
||||
|
||||
it('should report a placeholder the translation renamed', () => {
|
||||
// Arrange
|
||||
const source = { 'workspace.limit': 'Upgrade from {{free}} to Pro' }
|
||||
const translation = { 'workspace.limit': "{{hobby}} planından Pro'ya yükseltin" }
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([
|
||||
{
|
||||
key: 'workspace.limit',
|
||||
kind: 'placeholder',
|
||||
expected: ['{{free}}'],
|
||||
actual: ['{{hobby}}'],
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('should report a placeholder the translation invented', () => {
|
||||
// Arrange
|
||||
const source = { 'goal.editCriterion': 'Edit criterion' }
|
||||
const translation = { 'goal.editCriterion': 'Kriteri düzenle C{{seq}}' }
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([
|
||||
{ key: 'goal.editCriterion', kind: 'placeholder', expected: [], actual: ['{{seq}}'] },
|
||||
])
|
||||
})
|
||||
|
||||
it('should report a dropped tag separately from placeholders', () => {
|
||||
// Arrange
|
||||
const source = { 'payment.switched': 'Switched from <bold>{{from}}</bold> to {{to}}' }
|
||||
const translation = { 'payment.switched': '{{from}} planından {{to}} planına geçildi' }
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([
|
||||
{
|
||||
key: 'payment.switched',
|
||||
kind: 'tag',
|
||||
expected: ['</bold>', '<bold>'],
|
||||
actual: [],
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('should accept placeholders reordered by the target language word order', () => {
|
||||
// Arrange
|
||||
const source = { 'usage.window': '{{used}} of {{total}} used' }
|
||||
const translation = { 'usage.window': '{{total}} içinden {{used}} kullanıldı' }
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([])
|
||||
})
|
||||
|
||||
it('should report a repeated placeholder that appears a different number of times', () => {
|
||||
// Arrange
|
||||
const source = { 'quota.hint': '{{name}} is full. Free up space in {{name}}.' }
|
||||
const translation = { 'quota.hint': '{{name}} dolu. Yer açın.' }
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([
|
||||
{
|
||||
key: 'quota.hint',
|
||||
kind: 'placeholder',
|
||||
expected: ['{{name}}', '{{name}}'],
|
||||
actual: ['{{name}}'],
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it('should ignore keys that are missing from the translation', () => {
|
||||
// Arrange
|
||||
const source = { present: 'Sent to {{email}}', absent: 'Retry in {{minutes}}' }
|
||||
const translation = { present: '{{email}} adresine gönderildi' }
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([])
|
||||
})
|
||||
|
||||
it('should report a key whose translation is present but not a string', () => {
|
||||
// Arrange
|
||||
// The key check is satisfied by the key alone, so nothing else would
|
||||
// notice that the sentence and its placeholder are gone.
|
||||
const source = { 'invite.sent': 'Sent to {{email}}' }
|
||||
const translation = { 'invite.sent': null } as Record<string, unknown>
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([
|
||||
{ key: 'invite.sent', kind: 'placeholder', expected: ['{{email}}'], actual: [] },
|
||||
])
|
||||
})
|
||||
|
||||
it('should accept a non-string translation when the source carries no marker', () => {
|
||||
// Arrange
|
||||
const source = { 'plan.name': 'Sandbox' }
|
||||
const translation = { 'plan.name': 42 } as Record<string, unknown>
|
||||
|
||||
// Act
|
||||
const issues = findValueIssues(source, translation)
|
||||
|
||||
// Assert
|
||||
expect(issues).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatValueIssue', () => {
|
||||
it('should name the file, key and both sides of a placeholder mismatch', () => {
|
||||
// Arrange
|
||||
const issue = {
|
||||
key: 'invite.sent',
|
||||
kind: 'placeholder' as const,
|
||||
expected: ['{{email}}'],
|
||||
actual: [],
|
||||
}
|
||||
|
||||
// Act
|
||||
const line = formatValueIssue('common', issue)
|
||||
|
||||
// Assert
|
||||
expect(line).toBe('common.invite.sent: expected placeholders {{email}}, found (none)')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,91 @@
|
||||
// @vitest-environment node
|
||||
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import ts from 'typescript'
|
||||
import { describe, expect, it } from 'vite-plus/test'
|
||||
import { generateResourceTypes } from '../generate-i18n-types'
|
||||
|
||||
describe('generated i18n resource types', () => {
|
||||
it('checks interpolation and plural options through the official selector types', async () => {
|
||||
const generated = await generateResourceTypes({
|
||||
app: {
|
||||
title: 'Delete {{ name }}?',
|
||||
'files.count_one': '{{count}} file for {{name}}',
|
||||
'files.count_other': '{{count}} files for {{name}}',
|
||||
size: '{{size, number}} bytes',
|
||||
features: ['First feature', 'Second feature'],
|
||||
},
|
||||
common: { save: 'Save' },
|
||||
})
|
||||
const source = `${generated}
|
||||
import type { TFunction } from 'i18next';
|
||||
declare module 'i18next' {
|
||||
interface CustomTypeOptions {
|
||||
defaultNS: 'app';
|
||||
enableSelector: 'optimize';
|
||||
keySeparator: false;
|
||||
resources: Resources;
|
||||
}
|
||||
}
|
||||
declare const t: TFunction<['app', 'common']>;
|
||||
t($ => $.title, { name: 'Alice' });
|
||||
t($ => $['files.count'], { count: 2, name: 'Alice' });
|
||||
// @ts-expect-error Optimized resources expose plural base keys only.
|
||||
t($ => $['files.count_other'], { count: 2, name: 'Alice' });
|
||||
t($ => $.size, { size: 42 });
|
||||
t($ => $.save, { ns: 'common' });
|
||||
const features: string[] = t($ => $.features, { returnObjects: true });
|
||||
// @ts-expect-error Unknown key.
|
||||
t($ => $.missing);
|
||||
// @ts-expect-error Namespace does not contain this key.
|
||||
t($ => $.save, { ns: 'app' });
|
||||
// @ts-expect-error Misspelled interpolation parameter.
|
||||
t($ => $.title, { typo: 'Alice' });
|
||||
// @ts-expect-error Empty options cannot satisfy interpolation.
|
||||
t($ => $.title, {});
|
||||
// @ts-expect-error Plural options need count.
|
||||
t($ => $['files.count'], { name: 'Alice' });
|
||||
// @ts-expect-error Count is numeric.
|
||||
t($ => $['files.count'], { count: '2', name: 'Alice' });
|
||||
// @ts-expect-error Built-in number formatter requires a number.
|
||||
t($ => $.size, { size: '42' });
|
||||
`
|
||||
const file = fileURLToPath(new URL('./i18n-type-contract.ts', import.meta.url))
|
||||
const options: ts.CompilerOptions = {
|
||||
strict: true,
|
||||
noEmit: true,
|
||||
skipLibCheck: true,
|
||||
types: [],
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
module: ts.ModuleKind.Preserve,
|
||||
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
||||
}
|
||||
const host = ts.createCompilerHost(options)
|
||||
const getSourceFile = host.getSourceFile.bind(host)
|
||||
host.getSourceFile = (name, ...args) =>
|
||||
name === file
|
||||
? ts.createSourceFile(file, source, ts.ScriptTarget.ES2022, true)
|
||||
: getSourceFile(name, ...args)
|
||||
const program = ts.createProgram([file], options, host)
|
||||
expect(
|
||||
ts
|
||||
.getPreEmitDiagnostics(program)
|
||||
.map((diagnostic) => ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')),
|
||||
).toEqual([])
|
||||
})
|
||||
|
||||
it('retains all plural variants and an existing base translation without emitting runtime code', async () => {
|
||||
const source = await generateResourceTypes({
|
||||
app: {
|
||||
item: 'Item',
|
||||
item_one: '{{count}} item',
|
||||
item_other: '{{count}} items',
|
||||
quote: 'A "quote"\nNew line',
|
||||
},
|
||||
})
|
||||
expect(source).toContain("item: 'Item' | '{{count}} item' | '{{count}} items'")
|
||||
expect(
|
||||
ts.transpile(source, { module: ts.ModuleKind.ESNext, removeComments: true }).trim(),
|
||||
).toBe('export {};')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,445 @@
|
||||
import ts from 'typescript'
|
||||
import { describe, expect, it } from 'vite-plus/test'
|
||||
import { transformSource } from '../migrate-i18n-selectors'
|
||||
|
||||
describe('migrate-i18n-selectors', () => {
|
||||
describe('Source Transformation', () => {
|
||||
it('should migrate flat t and Trans keys without reformatting surrounding code', () => {
|
||||
const source = `import { Trans, useTranslation } from 'react-i18next'
|
||||
|
||||
export function Example() {
|
||||
const { t } = useTranslation('app')
|
||||
return <Trans i18nKey="account.changeEmail.description" ns="app">{t('account.changeEmail.title')}</Trans>
|
||||
}
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.tsx')
|
||||
|
||||
expect(result.output).toBe(`import { Trans, useTranslation } from 'react-i18next'
|
||||
|
||||
export function Example() {
|
||||
const { t } = useTranslation('app')
|
||||
return <Trans i18nKey={$ => $['account.changeEmail.description']} ns="app">{t($ => $['account.changeEmail.title'])}</Trans>
|
||||
}
|
||||
`)
|
||||
expect(result.changes).toBe(2)
|
||||
})
|
||||
|
||||
it('should use property access for identifier keys and normalize existing selectors', () => {
|
||||
const source = `import { useTranslation } from 'react-i18next'
|
||||
const { t } = useTranslation('app')
|
||||
const loadingSelector = $ => $["loading"]
|
||||
const dottedSelector = $ => $["account.changeEmail.title"]
|
||||
t('loading')
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.ts')
|
||||
|
||||
expect(result.output).toBe(`import { useTranslation } from 'react-i18next'
|
||||
const { t } = useTranslation('app')
|
||||
const loadingSelector = $ => $.loading
|
||||
const dottedSelector = $ => $['account.changeEmail.title']
|
||||
t($ => $.loading)
|
||||
`)
|
||||
expect(result.changes).toBe(3)
|
||||
})
|
||||
|
||||
it('should migrate global, instance, dynamic, and fallback translation calls', () => {
|
||||
const source = `import { t as globalT } from 'i18next'
|
||||
import { useTranslation } from '#i18n'
|
||||
|
||||
const { t } = useTranslation('app')
|
||||
globalT('global.key', { ns: 'app' })
|
||||
i18n.t('instance.key', { ns: 'app' })
|
||||
t(\`dynamic.\${kind}\`)
|
||||
t('withFallback', 'Fallback')
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.ts')
|
||||
|
||||
expect(result.output).toBe(`import { t as globalT } from 'i18next'
|
||||
import { useTranslation } from '#i18n'
|
||||
|
||||
const { t } = useTranslation('app')
|
||||
globalT($ => $['global.key'], { ns: 'app' })
|
||||
i18n.t($ => $['instance.key'], { ns: 'app' })
|
||||
t($ => $[\`dynamic.\${kind}\`])
|
||||
t($ => $.withFallback, { defaultValue: 'Fallback' })
|
||||
`)
|
||||
expect(result.changes).toBe(5)
|
||||
})
|
||||
|
||||
it('should leave an unrelated t binding unchanged in another lexical scope', () => {
|
||||
const source = `import { useTranslation } from 'react-i18next'
|
||||
|
||||
function renderUnrelated() {
|
||||
const t = (key: string, fallback: string) => \`\${key}:\${fallback}\`
|
||||
return t('ordinary.key', 'Ordinary fallback')
|
||||
}
|
||||
|
||||
export function Example() {
|
||||
const { t } = useTranslation('app')
|
||||
return t('account.changeEmail.title', 'Change email')
|
||||
}
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.tsx')
|
||||
|
||||
expect(result.output).toBe(`import { useTranslation } from 'react-i18next'
|
||||
|
||||
function renderUnrelated() {
|
||||
const t = (key: string, fallback: string) => \`\${key}:\${fallback}\`
|
||||
return t('ordinary.key', 'Ordinary fallback')
|
||||
}
|
||||
|
||||
export function Example() {
|
||||
const { t } = useTranslation('app')
|
||||
return t($ => $['account.changeEmail.title'], { defaultValue: 'Change email' })
|
||||
}
|
||||
`)
|
||||
expect(result.changes).toBe(2)
|
||||
})
|
||||
|
||||
it('should migrate legacy translation callback aliases and explicitly untyped callbacks', () => {
|
||||
const source = `type Translate = (key: string, options: { ns: string }) => string
|
||||
|
||||
export function translateAlias(t: Translate) {
|
||||
return t('alias.key', { ns: 'app' })
|
||||
}
|
||||
|
||||
export function translateLegacy(t: any) {
|
||||
return t('legacy.key', { ns: 'app' })
|
||||
}
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.ts')
|
||||
|
||||
expect(result.output).toBe(`type Translate = (key: string, options: { ns: string }) => string
|
||||
|
||||
export function translateAlias(t: Translate) {
|
||||
return t($ => $['alias.key'], { ns: 'app' })
|
||||
}
|
||||
|
||||
export function translateLegacy(t: any) {
|
||||
return t($ => $['legacy.key'], { ns: 'app' })
|
||||
}
|
||||
`)
|
||||
expect(result.changes).toBe(2)
|
||||
})
|
||||
|
||||
it('should follow aliased translation factory bindings without trusting shadowed names', () => {
|
||||
const source = `import { useTranslation as useI18n } from 'react-i18next'
|
||||
import { getTranslation as loadTranslation } from '@/i18n/server'
|
||||
|
||||
function renderUnrelated(useI18n: () => { t: (key: string) => string }) {
|
||||
const { t } = useI18n()
|
||||
return t('ordinary.key')
|
||||
}
|
||||
|
||||
export async function Example() {
|
||||
const { t: clientT } = useI18n('app')
|
||||
const { t: serverT } = await loadTranslation('en-US', 'app')
|
||||
return [clientT('client.key'), serverT('server.key')]
|
||||
}
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.tsx')
|
||||
|
||||
expect(result.output).toBe(`import { useTranslation as useI18n } from 'react-i18next'
|
||||
import { getTranslation as loadTranslation } from '@/i18n/server'
|
||||
|
||||
function renderUnrelated(useI18n: () => { t: (key: string) => string }) {
|
||||
const { t } = useI18n()
|
||||
return t('ordinary.key')
|
||||
}
|
||||
|
||||
export async function Example() {
|
||||
const { t: clientT } = useI18n('app')
|
||||
const { t: serverT } = await loadTranslation('en-US', 'app')
|
||||
return [clientT($ => $['client.key']), serverT($ => $['server.key'])]
|
||||
}
|
||||
`)
|
||||
expect(result.changes).toBe(2)
|
||||
})
|
||||
|
||||
it('should not rewrite fallback arguments for a shadowed imported t binding', () => {
|
||||
const source = `import { t } from 'i18next'
|
||||
|
||||
function renderUnrelated(t: (selector: ($: Record<string, string>) => string, fallback: string) => string) {
|
||||
return t($ => $['ordinary.key'], 'Ordinary fallback')
|
||||
}
|
||||
|
||||
export const translated = t($ => $['global.key'], 'Global fallback')
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.ts')
|
||||
|
||||
expect(result.output).toBe(`import { t } from 'i18next'
|
||||
|
||||
function renderUnrelated(t: (selector: ($: Record<string, string>) => string, fallback: string) => string) {
|
||||
return t($ => $['ordinary.key'], 'Ordinary fallback')
|
||||
}
|
||||
|
||||
export const translated = t($ => $['global.key'], { defaultValue: 'Global fallback' })
|
||||
`)
|
||||
expect(result.changes).toBe(1)
|
||||
})
|
||||
|
||||
it('should leave a shadowed Trans component key unchanged', () => {
|
||||
const source = `import { Trans } from 'react-i18next'
|
||||
|
||||
function Local({ Trans }: { Trans: (props: { i18nKey: string }) => null }) {
|
||||
return <Trans i18nKey="ordinary.key" />
|
||||
}
|
||||
|
||||
export const translated = <Trans i18nKey="global.key" />
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.tsx')
|
||||
|
||||
expect(result.output).toBe(`import { Trans } from 'react-i18next'
|
||||
|
||||
function Local({ Trans }: { Trans: (props: { i18nKey: string }) => null }) {
|
||||
return <Trans i18nKey="ordinary.key" />
|
||||
}
|
||||
|
||||
export const translated = <Trans i18nKey={$ => $['global.key']} />
|
||||
`)
|
||||
expect(result.changes).toBe(1)
|
||||
})
|
||||
|
||||
it('should be idempotent after selectors are migrated', () => {
|
||||
const source = `import { useTranslation } from 'react-i18next'
|
||||
const { t } = useTranslation('app')
|
||||
t($ => $['account.changeEmail.title'])
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.ts')
|
||||
|
||||
expect(result).toEqual({ changes: 0, output: source })
|
||||
})
|
||||
|
||||
it('should preserve selector-compatible casts, parameters, identifiers, and map entries', () => {
|
||||
const source = `import type { SelectorKey, SelectorParam } from 'i18next'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
const selectorMap: Record<'description' | 'title', SelectorParam<'app'>> = {
|
||||
description: $ => $['account.changeEmail.description'],
|
||||
title: $ => $['account.changeEmail.title'],
|
||||
}
|
||||
const titleSelector: SelectorParam<'app'> = $ => $['account.changeEmail.title']
|
||||
|
||||
export function translate(selector: SelectorParam<'app'>, key: string, kind: keyof typeof selectorMap) {
|
||||
const { t } = useTranslation('app')
|
||||
return [
|
||||
t(key as SelectorKey),
|
||||
t(selector),
|
||||
t(titleSelector),
|
||||
t(selectorMap[kind]),
|
||||
]
|
||||
}
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.ts')
|
||||
|
||||
expect(result).toEqual({ changes: 0, output: source })
|
||||
})
|
||||
|
||||
it('should adapt local react-i18next translation mocks', () => {
|
||||
const source = `import { vi } from 'vite-plus/test'
|
||||
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => key,
|
||||
}),
|
||||
Trans: ({ i18nKey }: { i18nKey: string }) => i18nKey,
|
||||
}))
|
||||
|
||||
const businessProps = {
|
||||
t: ((key: string) => key) as never,
|
||||
}
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.spec.ts')
|
||||
|
||||
expect(result.output).toBe(`import { vi } from 'vite-plus/test'
|
||||
|
||||
vi.mock('react-i18next', async () => {
|
||||
const { withSelectorKey, withSelectorKeyProps } = await import('@/test/i18n-mock')
|
||||
return ({
|
||||
useTranslation: () => ({
|
||||
t: withSelectorKey((key: string) => key),
|
||||
}),
|
||||
Trans: withSelectorKeyProps(({ i18nKey }: { i18nKey: string }) => i18nKey),
|
||||
})
|
||||
})
|
||||
|
||||
const businessProps = {
|
||||
t: ((key: string) => key) as never,
|
||||
}
|
||||
`)
|
||||
expect(result.changes).toBe(4)
|
||||
expect(transformSource(result.output, 'example.spec.ts')).toEqual({
|
||||
changes: 0,
|
||||
output: result.output,
|
||||
})
|
||||
})
|
||||
|
||||
it('should adapt selector mocks from the local i18n facade', () => {
|
||||
const source = `import { vi } from 'vite-plus/test'
|
||||
|
||||
vi.mock('#i18n', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => key,
|
||||
}),
|
||||
}))
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.spec.ts')
|
||||
|
||||
expect(result.output).toContain(`vi.mock('#i18n', async () => {`)
|
||||
expect(result.output).toContain(
|
||||
`const { withSelectorKey } = await import('@/test/i18n-mock')`,
|
||||
)
|
||||
expect(result.output).toContain('t: withSelectorKey((key: string) => key)')
|
||||
expect(transformSource(result.output, 'example.spec.ts')).toEqual({
|
||||
changes: 0,
|
||||
output: result.output,
|
||||
})
|
||||
})
|
||||
|
||||
it('should defer mock callbacks declared outside a hoisted factory', () => {
|
||||
const source = `import { vi } from 'vite-plus/test'
|
||||
|
||||
const mockTranslation = (key: string) => key
|
||||
|
||||
vi.mock('#i18n', () => ({
|
||||
useTranslation: () => ({
|
||||
t: mockTranslation,
|
||||
}),
|
||||
}))
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.spec.ts')
|
||||
|
||||
expect(result.output).toContain(
|
||||
't: withSelectorKey((...args: Parameters<typeof mockTranslation>) => mockTranslation(...args))',
|
||||
)
|
||||
expect(transformSource(result.output, 'example.spec.ts')).toEqual({
|
||||
changes: 0,
|
||||
output: result.output,
|
||||
})
|
||||
})
|
||||
|
||||
it('should preserve selector-aware i18next mock adapters', () => {
|
||||
const source = `import type { Namespace, SelectorParam } from 'i18next'
|
||||
import { vi } from 'vite-plus/test'
|
||||
|
||||
vi.mock('i18next', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('i18next')>()
|
||||
return {
|
||||
...actual,
|
||||
t: <Ns extends Namespace>(selector: SelectorParam<Ns>) => actual.keyFromSelector(selector),
|
||||
}
|
||||
})
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.spec.ts')
|
||||
|
||||
expect(result).toEqual({ changes: 0, output: source })
|
||||
})
|
||||
|
||||
it('should transform a multiline JSX Trans mock without overlapping edits', () => {
|
||||
const source = `import type { ReactNode } from 'react'
|
||||
import { vi } from 'vite-plus/test'
|
||||
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => {
|
||||
const translated = key
|
||||
|
||||
return translated
|
||||
},
|
||||
}),
|
||||
Trans: ({
|
||||
i18nKey,
|
||||
components,
|
||||
}: {
|
||||
i18nKey: string
|
||||
components?: Record<string, ReactNode>
|
||||
}) => (
|
||||
<div>
|
||||
<div>{i18nKey}</div>
|
||||
{components?.CtrlKey}
|
||||
{components?.Key}
|
||||
</div>
|
||||
),
|
||||
}))
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.spec.tsx')
|
||||
const diagnostics =
|
||||
ts.transpileModule(result.output, {
|
||||
compilerOptions: { jsx: ts.JsxEmit.ReactJSX },
|
||||
fileName: 'example.spec.tsx',
|
||||
reportDiagnostics: true,
|
||||
}).diagnostics ?? []
|
||||
|
||||
expect(
|
||||
diagnostics.filter((diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error),
|
||||
).toEqual([])
|
||||
expect(result.output).toContain('{components?.Key}')
|
||||
expect(result.output).not.toMatch(/[ \t]+$/m)
|
||||
expect(transformSource(result.output, 'example.spec.tsx')).toEqual({
|
||||
changes: 0,
|
||||
output: result.output,
|
||||
})
|
||||
})
|
||||
|
||||
it('should adapt only translation values configured through a referenced hoisted mock', () => {
|
||||
const source = `import { vi } from 'vite-plus/test'
|
||||
|
||||
const mockUseTranslation = vi.hoisted(() => vi.fn())
|
||||
const reactI18nextMock = {
|
||||
useTranslation: () => mockUseTranslation(),
|
||||
}
|
||||
|
||||
vi.mock('react-i18next', () => reactI18nextMock)
|
||||
|
||||
mockUseTranslation.mockReturnValue({
|
||||
t: (key: string) => key,
|
||||
})
|
||||
|
||||
const businessProps = {
|
||||
t: ((key: string) => key) as never,
|
||||
}
|
||||
`
|
||||
|
||||
const result = transformSource(source, 'example.spec.ts')
|
||||
|
||||
expect(result.output).toBe(`import { vi } from 'vite-plus/test'
|
||||
import { withSelectorKey } from '@/test/i18n-mock'
|
||||
|
||||
const mockUseTranslation = vi.hoisted(() => vi.fn())
|
||||
const reactI18nextMock = {
|
||||
useTranslation: () => mockUseTranslation(),
|
||||
}
|
||||
|
||||
vi.mock('react-i18next', () => reactI18nextMock)
|
||||
|
||||
mockUseTranslation.mockReturnValue({
|
||||
t: withSelectorKey((key: string) => key),
|
||||
})
|
||||
|
||||
const businessProps = {
|
||||
t: ((key: string) => key) as never,
|
||||
}
|
||||
`)
|
||||
expect(result.changes).toBe(2)
|
||||
expect(transformSource(result.output, 'example.spec.ts')).toEqual({
|
||||
changes: 0,
|
||||
output: result.output,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,409 @@
|
||||
/**
|
||||
* This script compares i18n keys between current branch (flat JSON) and main branch (nested TS).
|
||||
*
|
||||
* It checks:
|
||||
* 1. All namespaces from main branch have corresponding JSON files
|
||||
* 2. No TS files exist in current branch (all should be converted to JSON)
|
||||
* 3. All keys from main branch exist in current branch
|
||||
* 4. Values for existing keys haven't changed
|
||||
* 5. Lists newly added keys and values
|
||||
*
|
||||
* Usage: node scripts/analyze-i18n-diff.ts
|
||||
*/
|
||||
|
||||
import { execSync } from 'node:child_process'
|
||||
import * as fs from 'node:fs'
|
||||
import * as path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
|
||||
const I18N_DIR = path.join(__dirname, '../i18n/locales/en-US')
|
||||
const LOCALE = 'en-US'
|
||||
|
||||
type TranslationValue = string | string[]
|
||||
|
||||
type FlatTranslation = {
|
||||
[key: string]: TranslationValue
|
||||
}
|
||||
|
||||
type NestedTranslation = {
|
||||
[key: string]: string | string[] | NestedTranslation
|
||||
}
|
||||
|
||||
type AnalysisResult = {
|
||||
file: string
|
||||
missingKeys: string[]
|
||||
changedValues: { key: string; oldValue: TranslationValue; newValue: TranslationValue }[]
|
||||
newKeys: { key: string; value: TranslationValue }[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten nested object to dot-separated keys
|
||||
* Arrays are preserved as-is (not split into .0, .1, etc.)
|
||||
*/
|
||||
function flattenObject(obj: NestedTranslation, prefix = ''): FlatTranslation {
|
||||
const result: FlatTranslation = {}
|
||||
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
const newKey = prefix ? `${prefix}.${key}` : key
|
||||
|
||||
if (typeof value === 'string') {
|
||||
result[newKey] = value
|
||||
} else if (Array.isArray(value)) {
|
||||
// Preserve arrays as-is
|
||||
result[newKey] = value
|
||||
} else if (typeof value === 'object' && value !== null) {
|
||||
Object.assign(result, flattenObject(value, newKey))
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two translation values (string or array)
|
||||
*/
|
||||
function valuesEqual(a: TranslationValue, b: TranslationValue): boolean {
|
||||
if (typeof a === 'string' && typeof b === 'string') {
|
||||
return a === b
|
||||
}
|
||||
if (Array.isArray(a) && Array.isArray(b)) {
|
||||
if (a.length !== b.length) return false
|
||||
return a.every((item, index) => item === b[index])
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Format value for display
|
||||
*/
|
||||
function formatValue(value: TranslationValue): string {
|
||||
if (Array.isArray(value)) {
|
||||
return `[${value.map((v) => `"${v}"`).join(', ')}]`
|
||||
}
|
||||
return `"${value}"`
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse TS file content to extract the translation object
|
||||
*/
|
||||
function parseTsContent(content: string): NestedTranslation {
|
||||
// Remove 'const translation = ' and 'export default translation'
|
||||
let cleaned = content
|
||||
.replace(/const\s+translation\s*=\s*/, '')
|
||||
.replace(/export\s+default\s+translation\s*(?:;\s*)?$/, '')
|
||||
.trim()
|
||||
|
||||
// Remove trailing semicolon if present
|
||||
if (cleaned.endsWith(';')) cleaned = cleaned.slice(0, -1)
|
||||
|
||||
// Use Function constructor to safely evaluate the object literal
|
||||
// This handles JS object syntax like unquoted keys, template literals, etc.
|
||||
try {
|
||||
// oxlint-disable-next-line no-new-func
|
||||
const fn = new Function(`return (${cleaned})`)
|
||||
return fn() as NestedTranslation
|
||||
} catch (e) {
|
||||
console.error('Failed to parse TS content:', e)
|
||||
console.error('Content preview:', cleaned.slice(0, 200))
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file content from main branch
|
||||
*/
|
||||
function getMainBranchFile(filePath: string): string | null {
|
||||
try {
|
||||
const relativePath = `./i18n/locales/${LOCALE}/${filePath}`
|
||||
|
||||
return execSync(`git show main:${relativePath}`, {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
})
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of translation files
|
||||
*/
|
||||
function getTranslationFiles(): string[] {
|
||||
const files = fs.readdirSync(I18N_DIR)
|
||||
return files.filter((f) => f.endsWith('.json')).map((f) => f.replace('.json', ''))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of namespaces from main branch (ts files)
|
||||
*/
|
||||
function getMainBranchNamespaces(): string[] {
|
||||
try {
|
||||
const relativePath = `./i18n/locales/${LOCALE}`
|
||||
|
||||
const output = execSync(`git ls-tree --name-only main ${relativePath}/`, {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
})
|
||||
|
||||
return output
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter((f) => f.endsWith('.ts'))
|
||||
.map((f) => path.basename(f, '.ts'))
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
type NamespaceCheckResult = {
|
||||
mainNamespaces: string[]
|
||||
currentJsonFiles: string[]
|
||||
currentTsFiles: string[]
|
||||
missingJsonFiles: string[]
|
||||
unexpectedTsFiles: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Check namespace file consistency between main and current branch
|
||||
*/
|
||||
function checkNamespaceFiles(): NamespaceCheckResult {
|
||||
const mainNamespaces = getMainBranchNamespaces()
|
||||
const currentFiles = fs.readdirSync(I18N_DIR)
|
||||
|
||||
const currentJsonFiles = currentFiles
|
||||
.filter((f) => f.endsWith('.json'))
|
||||
.map((f) => f.replace('.json', ''))
|
||||
|
||||
const currentTsFiles = currentFiles
|
||||
.filter((f) => f.endsWith('.ts'))
|
||||
.map((f) => f.replace('.ts', ''))
|
||||
|
||||
// Check which namespaces from main are missing json files
|
||||
const missingJsonFiles = mainNamespaces.filter((ns) => !currentJsonFiles.includes(ns))
|
||||
|
||||
// ts files should not exist in current branch
|
||||
const unexpectedTsFiles = currentTsFiles
|
||||
|
||||
return {
|
||||
mainNamespaces,
|
||||
currentJsonFiles,
|
||||
currentTsFiles,
|
||||
missingJsonFiles,
|
||||
unexpectedTsFiles,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyze a single translation file
|
||||
*/
|
||||
function analyzeFile(baseName: string): AnalysisResult {
|
||||
const result: AnalysisResult = {
|
||||
file: baseName,
|
||||
missingKeys: [],
|
||||
changedValues: [],
|
||||
newKeys: [],
|
||||
}
|
||||
|
||||
// Read current branch JSON file
|
||||
const jsonPath = path.join(I18N_DIR, `${baseName}.json`)
|
||||
const currentContent = JSON.parse(fs.readFileSync(jsonPath, 'utf-8')) as Record<
|
||||
string,
|
||||
TranslationValue
|
||||
>
|
||||
|
||||
// Read main branch TS file
|
||||
const tsContent = getMainBranchFile(`${baseName}.ts`)
|
||||
if (!tsContent) {
|
||||
// New file, all keys are new
|
||||
for (const [key, value] of Object.entries(currentContent)) {
|
||||
result.newKeys.push({ key, value })
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Parse and flatten the TS content
|
||||
const nestedObj = parseTsContent(tsContent)
|
||||
const mainFlat = flattenObject(nestedObj)
|
||||
|
||||
// Check for missing keys (in main but not in current)
|
||||
for (const key of Object.keys(mainFlat)) {
|
||||
if (!(key in currentContent)) {
|
||||
result.missingKeys.push(key)
|
||||
}
|
||||
}
|
||||
|
||||
// Check for changed values
|
||||
for (const [key, oldValue] of Object.entries(mainFlat)) {
|
||||
if (key in currentContent && !valuesEqual(currentContent[key]!, oldValue)) {
|
||||
result.changedValues.push({
|
||||
key,
|
||||
oldValue,
|
||||
newValue: currentContent[key]!,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Find new keys (in current but not in main)
|
||||
for (const [key, value] of Object.entries(currentContent)) {
|
||||
if (!(key in mainFlat)) {
|
||||
result.newKeys.push({ key, value })
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Main analysis function
|
||||
*/
|
||||
function main() {
|
||||
console.log(
|
||||
'🔍 Analyzing i18n differences between current branch (flat JSON) and main branch (nested TS)...\n',
|
||||
)
|
||||
|
||||
// Check namespace file consistency first
|
||||
console.log('📂 Checking namespace files...')
|
||||
console.log('='.repeat(60))
|
||||
const nsCheck = checkNamespaceFiles()
|
||||
|
||||
console.log(`Namespaces in main branch (ts files): ${nsCheck.mainNamespaces.length}`)
|
||||
console.log(`JSON files in current branch: ${nsCheck.currentJsonFiles.length}`)
|
||||
console.log(`TS files in current branch: ${nsCheck.currentTsFiles.length}`)
|
||||
|
||||
let hasNamespaceError = false
|
||||
|
||||
if (nsCheck.missingJsonFiles.length > 0) {
|
||||
console.log('\n❌ Missing JSON files (namespace exists in main but no corresponding JSON):')
|
||||
for (const ns of nsCheck.missingJsonFiles) {
|
||||
console.log(` - ${ns}.json (was ${ns}.ts in main)`)
|
||||
}
|
||||
hasNamespaceError = true
|
||||
} else {
|
||||
console.log('\n✅ All namespaces from main branch have corresponding JSON files')
|
||||
}
|
||||
|
||||
if (nsCheck.unexpectedTsFiles.length > 0) {
|
||||
console.log('\n❌ Unexpected TS files (should be deleted):')
|
||||
for (const ns of nsCheck.unexpectedTsFiles) {
|
||||
console.log(` - ${ns}.ts`)
|
||||
}
|
||||
hasNamespaceError = true
|
||||
} else {
|
||||
console.log('✅ No TS files in current branch (all converted to JSON)')
|
||||
}
|
||||
|
||||
console.log()
|
||||
|
||||
const files = getTranslationFiles()
|
||||
const allResults: AnalysisResult[] = []
|
||||
|
||||
let totalMissing = 0
|
||||
let totalChanged = 0
|
||||
let totalNew = 0
|
||||
|
||||
for (const file of files) {
|
||||
const result = analyzeFile(file)
|
||||
allResults.push(result)
|
||||
|
||||
totalMissing += result.missingKeys.length
|
||||
totalChanged += result.changedValues.length
|
||||
totalNew += result.newKeys.length
|
||||
}
|
||||
|
||||
// Summary
|
||||
console.log('📊 Key Analysis Summary')
|
||||
console.log('='.repeat(60))
|
||||
console.log(`Total files analyzed: ${files.length}`)
|
||||
console.log(`Missing keys (in main but not in current): ${totalMissing}`)
|
||||
console.log(`Changed values: ${totalChanged}`)
|
||||
console.log(`New keys: ${totalNew}`)
|
||||
console.log()
|
||||
|
||||
// Detailed report
|
||||
if (totalMissing > 0) {
|
||||
console.log('\n❌ MISSING KEYS (exist in main but not in current branch)')
|
||||
console.log('='.repeat(60))
|
||||
for (const result of allResults) {
|
||||
if (result.missingKeys.length > 0) {
|
||||
console.log(`\n📁 ${result.file}:`)
|
||||
for (const key of result.missingKeys) {
|
||||
console.log(` - ${key}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (totalChanged > 0) {
|
||||
console.log('\n⚠️ CHANGED VALUES (same key, different value)')
|
||||
console.log('='.repeat(60))
|
||||
for (const result of allResults) {
|
||||
if (result.changedValues.length > 0) {
|
||||
console.log(`\n📁 ${result.file}:`)
|
||||
for (const { key, oldValue, newValue } of result.changedValues) {
|
||||
console.log(` Key: ${key}`)
|
||||
console.log(` Old: ${formatValue(oldValue)}`)
|
||||
console.log(` New: ${formatValue(newValue)}`)
|
||||
console.log()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (totalNew > 0) {
|
||||
console.log('\n✨ NEW KEYS (exist in current branch but not in main)')
|
||||
console.log('='.repeat(60))
|
||||
for (const result of allResults) {
|
||||
if (result.newKeys.length > 0) {
|
||||
console.log(`\n📁 ${result.file}:`)
|
||||
for (const { key, value } of result.newKeys) {
|
||||
console.log(` + ${key}: ${formatValue(value)}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write detailed report to JSON file
|
||||
const reportPath = path.join(__dirname, '../i18n-analysis-report.json')
|
||||
fs.writeFileSync(
|
||||
reportPath,
|
||||
JSON.stringify(
|
||||
{
|
||||
summary: {
|
||||
totalFiles: files.length,
|
||||
missingKeys: totalMissing,
|
||||
changedValues: totalChanged,
|
||||
newKeys: totalNew,
|
||||
},
|
||||
namespaceCheck: {
|
||||
mainNamespaces: nsCheck.mainNamespaces,
|
||||
currentJsonFiles: nsCheck.currentJsonFiles,
|
||||
missingJsonFiles: nsCheck.missingJsonFiles,
|
||||
unexpectedTsFiles: nsCheck.unexpectedTsFiles,
|
||||
},
|
||||
details: allResults,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
)
|
||||
|
||||
console.log(`\n📄 Detailed report written to: i18n-analysis-report.json`)
|
||||
|
||||
// Exit with error code if there are issues
|
||||
if (hasNamespaceError) {
|
||||
console.log('\n⚠️ Warning: Namespace file issues detected!')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
if (totalMissing > 0) {
|
||||
console.log('\n⚠️ Warning: Some keys are missing in the current branch!')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
console.log('\n✅ All namespace files and keys from main branch exist in current branch.')
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,369 @@
|
||||
import { appendFile, mkdir, readdir, readFile, rename, writeFile } from 'node:fs/promises'
|
||||
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
|
||||
import { gzipSync } from 'node:zlib'
|
||||
|
||||
type Size = { raw: number; gzip: number }
|
||||
type Analysis = {
|
||||
meta: { bundler: string; version: string }
|
||||
chunks: {
|
||||
name: string
|
||||
size: number
|
||||
type: string
|
||||
entryModule?: number
|
||||
imports?: { targetChunkIndex: number; type: string }[]
|
||||
}[]
|
||||
modules: { path: string; size: number }[]
|
||||
}
|
||||
type Snapshot = {
|
||||
version: 1
|
||||
commit: string
|
||||
vinext: string
|
||||
rolldown: string
|
||||
totals: { js: Size; css: Size }
|
||||
entries: Record<string, Size>
|
||||
packages: Record<string, number>
|
||||
}
|
||||
const record = (v: unknown): v is Record<string, unknown> =>
|
||||
typeof v === 'object' && v !== null && !Array.isArray(v)
|
||||
const bytes = (v: unknown): v is number => Number.isSafeInteger(v) && Number(v) >= 0
|
||||
const size = (v: unknown): v is Size => record(v) && bytes(v.raw) && bytes(v.gzip)
|
||||
const empty = (): Size => ({ raw: 0, gzip: 0 })
|
||||
const add = (a: Size, b: Size): Size => ({ raw: a.raw + b.raw, gzip: a.gzip + b.gzip })
|
||||
|
||||
export function parseAnalysis(text: string): Analysis {
|
||||
const v: unknown = JSON.parse(text)
|
||||
if (
|
||||
!record(v) ||
|
||||
!record(v.meta) ||
|
||||
v.meta.bundler !== 'rolldown' ||
|
||||
typeof v.meta.version !== 'string' ||
|
||||
!Array.isArray(v.modules) ||
|
||||
!v.modules.every((m) => record(m) && typeof m.path === 'string' && bytes(m.size)) ||
|
||||
!Array.isArray(v.chunks) ||
|
||||
!v.chunks.length
|
||||
)
|
||||
throw new Error('Invalid Rolldown analysis.')
|
||||
const moduleCount = v.modules.length
|
||||
const chunkCount = v.chunks.length
|
||||
for (const c of v.chunks as unknown[]) {
|
||||
if (
|
||||
!record(c) ||
|
||||
typeof c.name !== 'string' ||
|
||||
!bytes(c.size) ||
|
||||
!['static-entry', 'dynamic-entry', 'common'].includes(String(c.type)) ||
|
||||
(c.entryModule !== undefined && (!bytes(c.entryModule) || c.entryModule >= moduleCount)) ||
|
||||
(c.imports !== undefined &&
|
||||
(!Array.isArray(c.imports) ||
|
||||
!c.imports.every(
|
||||
(i) =>
|
||||
record(i) &&
|
||||
bytes(i.targetChunkIndex) &&
|
||||
i.targetChunkIndex < chunkCount &&
|
||||
['static', 'dynamic'].includes(String(i.type)),
|
||||
)))
|
||||
)
|
||||
throw new Error('Invalid Rolldown chunk graph.')
|
||||
}
|
||||
return v as Analysis
|
||||
}
|
||||
|
||||
export function staticClosure(analysis: Analysis, index: number): Set<number> {
|
||||
const seen = new Set<number>()
|
||||
const visit = (i: number) => {
|
||||
if (seen.has(i)) return
|
||||
seen.add(i)
|
||||
const chunk = analysis.chunks[i]
|
||||
if (!chunk) throw new Error(`Missing chunk ${i}`)
|
||||
for (const edge of chunk.imports ?? []) if (edge.type === 'static') visit(edge.targetChunkIndex)
|
||||
}
|
||||
visit(index)
|
||||
return seen
|
||||
}
|
||||
|
||||
export async function collect(appDirectory: string, commit: string): Promise<Snapshot> {
|
||||
if (!/^[\da-f]{40}$/.test(commit)) throw new Error('Expected a commit SHA.')
|
||||
const app = resolve(appDirectory)
|
||||
const client = join(app, 'dist/client')
|
||||
const analysis = parseAnalysis(await readFile(join(client, 'bundle-analysis.json'), 'utf8'))
|
||||
const cache = new Map<string, Size>()
|
||||
const measure = async (file: string) => {
|
||||
const absolute = resolve(client, file)
|
||||
const local = relative(client, absolute)
|
||||
if (local.startsWith('..') || isAbsolute(local)) throw new Error('Asset outside client output.')
|
||||
const cached = cache.get(absolute)
|
||||
if (cached) return cached
|
||||
const data = await readFile(absolute)
|
||||
const result = { raw: data.length, gzip: gzipSync(data).length }
|
||||
cache.set(absolute, result)
|
||||
return result
|
||||
}
|
||||
const sumFiles = async (files: Iterable<string>) => {
|
||||
let result = empty()
|
||||
for (const file of new Set(files)) result = add(result, await measure(file))
|
||||
return result
|
||||
}
|
||||
const css: string[] = []
|
||||
const visit = async (directory: string): Promise<void> => {
|
||||
for (const item of await readdir(directory, { withFileTypes: true })) {
|
||||
const path = join(directory, item.name)
|
||||
if (item.isDirectory()) await visit(path)
|
||||
else if (item.isFile() && item.name.endsWith('.css')) css.push(path)
|
||||
}
|
||||
}
|
||||
await visit(join(client, '_next/static'))
|
||||
const entries: Record<string, Size> = Object.create(null)
|
||||
for (const [index, chunk] of analysis.chunks.entries()) {
|
||||
if (chunk.entryModule === undefined || chunk.type === 'common') continue
|
||||
const source = analysis.modules[chunk.entryModule]!.path
|
||||
// Application and virtual browser entries are stable across chunk hash changes.
|
||||
if (!source.startsWith('app/') && !source.includes('virtual:vinext-app-browser-entry')) continue
|
||||
const key = `${chunk.type}: ${source}`
|
||||
if (Object.hasOwn(entries, key)) throw new Error(`Duplicate entry: ${key}`)
|
||||
entries[key] = await sumFiles(
|
||||
[...staticClosure(analysis, index)].map((i) => analysis.chunks[i]!.name),
|
||||
)
|
||||
}
|
||||
if (!Object.keys(entries).length) throw new Error('No application entries in Rolldown analysis.')
|
||||
const packages: Record<string, number> = Object.create(null)
|
||||
for (const module of analysis.modules) {
|
||||
const tail = module.path.split('node_modules/').at(-1)!
|
||||
if (tail === module.path) continue
|
||||
const parts = tail.split('/')
|
||||
const name = tail.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]!
|
||||
packages[name] = (packages[name] ?? 0) + module.size
|
||||
}
|
||||
const pkg: unknown = JSON.parse(
|
||||
await readFile(join(app, 'node_modules/vinext/package.json'), 'utf8'),
|
||||
)
|
||||
if (!record(pkg) || typeof pkg.version !== 'string') throw new Error('Missing Vinext version.')
|
||||
return {
|
||||
version: 1,
|
||||
commit,
|
||||
vinext: pkg.version,
|
||||
rolldown: analysis.meta.version,
|
||||
totals: { js: await sumFiles(analysis.chunks.map((c) => c.name)), css: await sumFiles(css) },
|
||||
entries,
|
||||
packages,
|
||||
}
|
||||
}
|
||||
|
||||
export function parseSnapshot(text: string): Snapshot {
|
||||
const v: unknown = JSON.parse(text)
|
||||
if (
|
||||
!record(v) ||
|
||||
v.version !== 1 ||
|
||||
typeof v.commit !== 'string' ||
|
||||
!/^[\da-f]{40}$/.test(v.commit) ||
|
||||
typeof v.vinext !== 'string' ||
|
||||
typeof v.rolldown !== 'string' ||
|
||||
!record(v.totals) ||
|
||||
!size(v.totals.js) ||
|
||||
!size(v.totals.css) ||
|
||||
!record(v.entries) ||
|
||||
!Object.keys(v.entries).length ||
|
||||
!Object.values(v.entries).every(size) ||
|
||||
!record(v.packages) ||
|
||||
!Object.values(v.packages).every(bytes)
|
||||
)
|
||||
throw new Error('Invalid bundle snapshot.')
|
||||
return v as Snapshot
|
||||
}
|
||||
const kib = (n: number) => `${(n / 1024).toFixed(2)} KiB`
|
||||
export function shouldComment(a: Snapshot, b: Snapshot): boolean {
|
||||
return (
|
||||
Math.abs(b.totals.js.gzip - a.totals.js.gzip) >= 5 * 1024 ||
|
||||
Math.abs(b.totals.css.gzip - a.totals.css.gzip) >= 1024 ||
|
||||
Object.keys(a.entries).some(
|
||||
(key) => b.entries[key] && Math.abs(b.entries[key].gzip - a.entries[key]!.gzip) >= 2 * 1024,
|
||||
)
|
||||
)
|
||||
}
|
||||
const delta = (a: number, b: number) => {
|
||||
if (a === b) return '⚪ No change'
|
||||
const change = b - a
|
||||
const sign = change > 0 ? '+' : '−'
|
||||
const magnitude = Math.abs(change)
|
||||
const amount = magnitude < 1024 ? `${magnitude} B` : kib(magnitude)
|
||||
const ratio = a === 0 ? null : (magnitude / a) * 100
|
||||
const percent =
|
||||
ratio === null ? 'from zero' : `${sign}${ratio < 0.01 ? '<0.01' : ratio.toFixed(2)}%`
|
||||
return `${change > 0 ? '🟠' : '🟢'} **${sign}${amount} (${percent})**`
|
||||
}
|
||||
const details = (summary: string, rows: string[]) => [
|
||||
'<details>',
|
||||
`<summary>${summary}</summary>`,
|
||||
'',
|
||||
...rows,
|
||||
'',
|
||||
'</details>',
|
||||
]
|
||||
const label = (s: string) =>
|
||||
`<code>${s.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('|', '|').replaceAll('\n', ' ')}</code>`
|
||||
export function compare(a: Snapshot, b: Snapshot): string {
|
||||
const lines = [
|
||||
'<!-- dify-vinext-bundle-size -->',
|
||||
'## Vinext bundle analysis',
|
||||
'',
|
||||
`**JS gzip:** ${delta(a.totals.js.gzip, b.totals.js.gzip)} · **CSS gzip:** ${delta(a.totals.css.gzip, b.totals.css.gzip)}`,
|
||||
'',
|
||||
'| All client output | Base gzip → Merge gzip | Gzip change |',
|
||||
'| --- | ---: | ---: |',
|
||||
]
|
||||
for (const kind of ['js', 'css'] as const)
|
||||
lines.push(
|
||||
`| ${kind.toUpperCase()} | ${kib(a.totals[kind].gzip)} → ${kib(b.totals[kind].gzip)} | ${delta(a.totals[kind].gzip, b.totals[kind].gzip)} |`,
|
||||
)
|
||||
const changed = [...new Set([...Object.keys(a.entries), ...Object.keys(b.entries)])]
|
||||
.filter(
|
||||
(k) =>
|
||||
a.entries[k] &&
|
||||
b.entries[k] &&
|
||||
(a.entries[k].gzip !== b.entries[k].gzip || a.entries[k].raw !== b.entries[k].raw),
|
||||
)
|
||||
.sort(
|
||||
(x, y) =>
|
||||
Math.abs((b.entries[y]?.gzip ?? 0) - (a.entries[y]?.gzip ?? 0)) -
|
||||
Math.abs((b.entries[x]?.gzip ?? 0) - (a.entries[x]?.gzip ?? 0)) || x.localeCompare(y),
|
||||
)
|
||||
const increased = changed.filter((k) => b.entries[k]!.gzip > a.entries[k]!.gzip).length
|
||||
const decreased = changed.filter((k) => b.entries[k]!.gzip < a.entries[k]!.gzip).length
|
||||
const rawOnly = changed.length - increased - decreased
|
||||
lines.push(
|
||||
'',
|
||||
`**Entry changes:** 🟠 ${increased} increased · 🟢 ${decreased} decreased${rawOnly ? ` · ⚪ ${rawOnly} raw-only changes` : ''} (gzip).`,
|
||||
'',
|
||||
'### Largest entry changes',
|
||||
'',
|
||||
`Top ${Math.min(5, changed.length)} by absolute gzip delta. Static dependencies overlap; do not sum entries.`,
|
||||
'',
|
||||
)
|
||||
const entryTable = (keys: string[], full = false) => [
|
||||
`| Source entry | Base gzip → Merge gzip | Gzip change |${full ? ' Raw change |' : ''}`,
|
||||
`| --- | ---: | ---: |${full ? ' ---: |' : ''}`,
|
||||
...keys.map(
|
||||
(k) =>
|
||||
`| ${label(full ? k : k.replace(/^(?:static|dynamic)-entry: /, ''))} | ${kib(a.entries[k]!.gzip)} → ${kib(b.entries[k]!.gzip)} | ${delta(a.entries[k]!.gzip, b.entries[k]!.gzip)} |${full ? ` ${delta(a.entries[k]!.raw, b.entries[k]!.raw)} |` : ''}`,
|
||||
),
|
||||
]
|
||||
if (changed.length) {
|
||||
lines.push(...entryTable(changed.slice(0, 5)), '')
|
||||
lines.push(
|
||||
...details(
|
||||
`Top ${Math.min(20, changed.length)} of ${changed.length} changed entries (full paths and raw sizes)`,
|
||||
entryTable(changed.slice(0, 20), true),
|
||||
),
|
||||
)
|
||||
} else {
|
||||
lines.push('No entry size changes.')
|
||||
}
|
||||
const topology = [...new Set([...Object.keys(a.entries), ...Object.keys(b.entries)])]
|
||||
.filter((k) => !a.entries[k] || !b.entries[k])
|
||||
.sort()
|
||||
if (topology.length) {
|
||||
lines.push(
|
||||
'',
|
||||
'<details>',
|
||||
`<summary>🆕 / ➖ ${topology.length} added or removed entries</summary>`,
|
||||
'',
|
||||
`Showing ${Math.min(20, topology.length)} of ${topology.length}. Entry-boundary changes are not size deltas from zero.`,
|
||||
'',
|
||||
'| Source entry | Change | Static dependencies (gzip) |',
|
||||
'| --- | --- | ---: |',
|
||||
)
|
||||
for (const k of topology.slice(0, 20))
|
||||
lines.push(
|
||||
`| ${label(k)} | ${b.entries[k] ? '🆕 New entry' : '➖ Removed entry'} | ${kib((b.entries[k] ?? a.entries[k])!.gzip)} |`,
|
||||
)
|
||||
lines.push('', '</details>')
|
||||
}
|
||||
const packages = [...new Set([...Object.keys(a.packages), ...Object.keys(b.packages)])]
|
||||
.filter((k) => a.packages[k] !== b.packages[k])
|
||||
.sort(
|
||||
(x, y) =>
|
||||
Math.abs((b.packages[y] ?? 0) - (a.packages[y] ?? 0)) -
|
||||
Math.abs((b.packages[x] ?? 0) - (a.packages[x] ?? 0)),
|
||||
)
|
||||
lines.push(
|
||||
'',
|
||||
'<details>',
|
||||
`<summary>📦 ${packages.length} package attribution changes (uncompressed modules)</summary>`,
|
||||
'',
|
||||
`Showing ${Math.min(15, packages.length)} of ${packages.length}, ordered by absolute module-size delta.`,
|
||||
'',
|
||||
'| Package | Base | Merge | Δ |',
|
||||
'| --- | ---: | ---: | ---: |',
|
||||
)
|
||||
if (!packages.length) lines.push('| No package attribution changes | — | — | — |')
|
||||
for (const k of packages.slice(0, 15))
|
||||
lines.push(
|
||||
`| ${label(k)} | ${kib(a.packages[k] ?? 0)} | ${kib(b.packages[k] ?? 0)} | ${delta(a.packages[k] ?? 0, b.packages[k] ?? 0)} |`,
|
||||
)
|
||||
lines.push(
|
||||
'',
|
||||
'</details>',
|
||||
'',
|
||||
'<details>',
|
||||
'<summary>Measurement details and build revisions</summary>',
|
||||
'',
|
||||
`Base: \`${a.commit}\` · Merge: \`${b.commit}\``,
|
||||
`Vinext: ${a.vinext} → ${b.vinext}; Rolldown: ${a.rolldown} → ${b.rolldown}`,
|
||||
'',
|
||||
`Raw JS change: ${delta(a.totals.js.raw, b.totals.js.raw)} · Raw CSS change: ${delta(a.totals.css.raw, b.totals.css.raw)}`,
|
||||
'',
|
||||
'🟠 Increase · 🟢 Decrease · ⚪ Unchanged. Colors indicate direction, not a performance budget verdict. Percentages use the base size.',
|
||||
'',
|
||||
'Entries follow only static edges in the official Rolldown chunk graph, deduplicating shared files. Dynamic entries are measured when loaded; they are not page first-load metrics. Entry totals overlap and must not be summed.',
|
||||
'Package attribution sums module sizes reported by Rolldown, not compressed emitted bytes. Gzip totals are measured per emitted file. This is Vinext output, not Next.js/Turbopack output.',
|
||||
'Report only: increases do not fail this check. Missing or incompatible analysis data does.',
|
||||
)
|
||||
if (a.vinext !== b.vinext || a.rolldown !== b.rolldown)
|
||||
lines.push('', 'Compiler versions differ; changes may include compiler effects.')
|
||||
lines.push('', '</details>')
|
||||
return `${lines.join('\n')}\n`
|
||||
}
|
||||
|
||||
async function instrument(app: string) {
|
||||
await rename(join(app, 'vite.config.ts'), join(app, 'vite.config.bundle-original.ts'))
|
||||
await writeFile(
|
||||
join(app, 'vite.config.ts'),
|
||||
`import { defineConfig, mergeConfig } from 'vite-plus'
|
||||
import { bundleAnalyzerPlugin } from 'vite/rolldown/experimental'
|
||||
import original from './vite.config.bundle-original.ts'
|
||||
export default defineConfig(async env => mergeConfig(await (typeof original === 'function' ? original(env) : original), {
|
||||
plugins: [{ name: 'dify:bundle-analysis', applyToEnvironment(environment) {
|
||||
return environment.name === 'client' ? [
|
||||
bundleAnalyzerPlugin({ fileName: 'bundle-analysis.json' }),
|
||||
bundleAnalyzerPlugin({ fileName: 'bundle-analysis.md', format: 'md' }),
|
||||
] : false
|
||||
} }],
|
||||
}))
|
||||
`,
|
||||
)
|
||||
}
|
||||
async function main() {
|
||||
const [command, first, second, output] = process.argv.slice(2)
|
||||
if (command === 'instrument' && first) return instrument(first)
|
||||
if (!first || !second || !output)
|
||||
throw new Error(
|
||||
'Usage: instrument <web> | collect <web> <sha> <json> | compare <base.json> <merge.json> <report.md>',
|
||||
)
|
||||
let result: string
|
||||
if (command === 'collect') {
|
||||
result = `${JSON.stringify(await collect(first, second), null, 2)}\n`
|
||||
} else if (command === 'compare') {
|
||||
const base = parseSnapshot(await readFile(first, 'utf8'))
|
||||
const merged = parseSnapshot(await readFile(second, 'utf8'))
|
||||
result = compare(base, merged)
|
||||
if (process.env.GITHUB_OUTPUT)
|
||||
await appendFile(process.env.GITHUB_OUTPUT, `should-comment=${shouldComment(base, merged)}\n`)
|
||||
} else {
|
||||
throw new Error('Unknown command.')
|
||||
}
|
||||
await mkdir(dirname(resolve(output)), { recursive: true })
|
||||
await writeFile(output, result)
|
||||
}
|
||||
if (import.meta.main)
|
||||
main().catch((error: unknown) => {
|
||||
console.error(error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
@@ -0,0 +1,103 @@
|
||||
import { getTranslationSourceKey } from '@dify/tooling/i18n-plurals'
|
||||
|
||||
/**
|
||||
* Value-level checks for locale files.
|
||||
*
|
||||
* `check-i18n.js` compares key sets, so a translation can drop, invent or rename
|
||||
* an interpolation variable and still pass. Those breakages reach the user as a
|
||||
* raw `{{name}}` in the UI, or as a sentence missing the value it was meant to
|
||||
* show. The translation sync workflow asks its agent to preserve placeholders
|
||||
* exactly, and these functions are what verifies that it did.
|
||||
*/
|
||||
|
||||
export type ValueIssueKind = 'placeholder' | 'tag'
|
||||
|
||||
export type ValueIssue = {
|
||||
key: string
|
||||
kind: ValueIssueKind
|
||||
expected: string[]
|
||||
actual: string[]
|
||||
}
|
||||
|
||||
/** i18next interpolation, including formatters such as `{{count,number}}`. */
|
||||
const PLACEHOLDER_PATTERN = /\{\{[^{}]*\}\}|\$\{[^{}]*\}/g
|
||||
|
||||
/** Markup handed to `<Trans>`, plus plain HTML tags used inside values. */
|
||||
const TAG_PATTERN = /<\/?[a-z][^<>]*>/gi
|
||||
|
||||
function collect(value: string, pattern: RegExp): string[] {
|
||||
return (value.match(pattern) ?? []).sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpolation variables in `value`, sorted so that reordering a sentence is
|
||||
* not reported as a difference.
|
||||
*/
|
||||
export function extractPlaceholders(value: string): string[] {
|
||||
return collect(value, PLACEHOLDER_PATTERN)
|
||||
}
|
||||
|
||||
/** Markup tags in `value`, sorted for the same reason as placeholders. */
|
||||
export function extractTags(value: string): string[] {
|
||||
return collect(value, TAG_PATTERN)
|
||||
}
|
||||
|
||||
function sameMembers(expected: string[], actual: string[]): boolean {
|
||||
return (
|
||||
expected.length === actual.length && expected.every((item, index) => item === actual[index])
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare one locale file against its en-US source.
|
||||
*
|
||||
* Locale-specific plural variants use their English family's `other` entry.
|
||||
* Missing and unrelated extra keys are reported by the key-level check.
|
||||
*
|
||||
* A key that is present but holds something other than a string is a different
|
||||
* case: the key check sees it and is satisfied, so nothing else would notice
|
||||
* that the sentence — and every placeholder in it — is gone. Such a value is
|
||||
* compared as the empty string, which makes a marker-bearing source fail here.
|
||||
*/
|
||||
export function findValueIssues(
|
||||
source: Record<string, unknown>,
|
||||
translation: Record<string, unknown>,
|
||||
language = 'en-US',
|
||||
): ValueIssue[] {
|
||||
const issues: ValueIssue[] = []
|
||||
const sourceKeys = new Set(Object.keys(source))
|
||||
|
||||
for (const [key, rawValue] of Object.entries(translation)) {
|
||||
const sourceKey = getTranslationSourceKey(sourceKeys, key, language)
|
||||
if (!sourceKey) continue
|
||||
const sourceValue = source[sourceKey]
|
||||
if (typeof sourceValue !== 'string') continue
|
||||
|
||||
const translatedValue = typeof rawValue === 'string' ? rawValue : ''
|
||||
|
||||
const expectedPlaceholders = extractPlaceholders(sourceValue)
|
||||
const actualPlaceholders = extractPlaceholders(translatedValue)
|
||||
if (!sameMembers(expectedPlaceholders, actualPlaceholders)) {
|
||||
issues.push({
|
||||
key,
|
||||
kind: 'placeholder',
|
||||
expected: expectedPlaceholders,
|
||||
actual: actualPlaceholders,
|
||||
})
|
||||
}
|
||||
|
||||
const expectedTags = extractTags(sourceValue)
|
||||
const actualTags = extractTags(translatedValue)
|
||||
if (!sameMembers(expectedTags, actualTags))
|
||||
issues.push({ key, kind: 'tag', expected: expectedTags, actual: actualTags })
|
||||
}
|
||||
|
||||
return issues
|
||||
}
|
||||
|
||||
export function formatValueIssue(fileName: string, issue: ValueIssue): string {
|
||||
const label = issue.kind === 'placeholder' ? 'placeholders' : 'tags'
|
||||
const expected = issue.expected.length ? issue.expected.join(' ') : '(none)'
|
||||
const actual = issue.actual.length ? issue.actual.join(' ') : '(none)'
|
||||
return `${fileName}.${issue.key}: expected ${label} ${expected}, found ${actual}`
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { getTranslationSourceKey } from '@dify/tooling/i18n-plurals'
|
||||
import data from '../i18n/languages.ts'
|
||||
import { findValueIssues, formatValueIssue } from './check-i18n-values.ts'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
|
||||
const targetLanguage = 'en-US'
|
||||
const localizedFileLocales = {
|
||||
contacts: new Set(['en-US', 'zh-Hans']),
|
||||
}
|
||||
|
||||
const languages = data.languages
|
||||
.filter((language) => language.supported)
|
||||
.map((language) => language.value)
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {
|
||||
files: [],
|
||||
languages: [],
|
||||
autoRemove: false,
|
||||
help: false,
|
||||
errors: [],
|
||||
}
|
||||
|
||||
const collectValues = (startIndex) => {
|
||||
const values = []
|
||||
let cursor = startIndex + 1
|
||||
while (cursor < argv.length && !argv[cursor].startsWith('--')) {
|
||||
const value = argv[cursor].trim()
|
||||
if (value) values.push(value)
|
||||
cursor++
|
||||
}
|
||||
return { values, nextIndex: cursor - 1 }
|
||||
}
|
||||
|
||||
const validateList = (values, flag) => {
|
||||
if (!values.length) {
|
||||
args.errors.push(`${flag} requires at least one value. Example: ${flag} app billing`)
|
||||
return false
|
||||
}
|
||||
|
||||
const invalid = values.find((value) => value.includes(','))
|
||||
if (invalid) {
|
||||
args.errors.push(`${flag} expects space-separated values. Example: ${flag} app billing`)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
for (let index = 2; index < argv.length; index++) {
|
||||
const arg = argv[index]
|
||||
|
||||
if (arg === '--auto-remove') {
|
||||
args.autoRemove = true
|
||||
continue
|
||||
}
|
||||
|
||||
if (arg === '--help' || arg === '-h') {
|
||||
args.help = true
|
||||
break
|
||||
}
|
||||
|
||||
if (arg.startsWith('--file=')) {
|
||||
args.errors.push('--file expects space-separated values. Example: --file app billing')
|
||||
continue
|
||||
}
|
||||
|
||||
if (arg === '--file') {
|
||||
const { values, nextIndex } = collectValues(index)
|
||||
if (validateList(values, '--file')) args.files.push(...values)
|
||||
index = nextIndex
|
||||
continue
|
||||
}
|
||||
|
||||
if (arg.startsWith('--lang=')) {
|
||||
args.errors.push('--lang expects space-separated values. Example: --lang zh-Hans ja-JP')
|
||||
continue
|
||||
}
|
||||
|
||||
if (arg === '--lang') {
|
||||
const { values, nextIndex } = collectValues(index)
|
||||
if (validateList(values, '--lang')) args.languages.push(...values)
|
||||
index = nextIndex
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Usage: pnpm run i18n:check [options]
|
||||
|
||||
Options:
|
||||
--file <name...> Check only specific files; provide space-separated names and repeat --file if needed
|
||||
--lang <locale> Check only specific locales; provide space-separated locales and repeat --lang if needed
|
||||
--auto-remove Remove extra keys automatically
|
||||
-h, --help Show help
|
||||
|
||||
Examples:
|
||||
pnpm run i18n:check --file app billing --lang zh-Hans ja-JP
|
||||
pnpm run i18n:check --auto-remove
|
||||
`)
|
||||
}
|
||||
|
||||
async function getKeysFromLanguage(language) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const folderPath = path.resolve(__dirname, '../i18n/locales', language)
|
||||
const allKeys = []
|
||||
fs.readdir(folderPath, (err, files) => {
|
||||
if (err) {
|
||||
console.error('Error reading folder:', err)
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Filter only .json files
|
||||
const translationFiles = files.filter((file) => file.endsWith('.json'))
|
||||
|
||||
translationFiles.forEach((file) => {
|
||||
const filePath = path.join(folderPath, file)
|
||||
const fileName = file.replace(/\.json$/, '') // Remove file extension
|
||||
const camelCaseFileName = fileName.replace(/[-_](.)/g, (_, c) => c.toUpperCase()) // Convert to camel case
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, 'utf8')
|
||||
const translationObj = JSON.parse(content)
|
||||
|
||||
if (!translationObj || typeof translationObj !== 'object') {
|
||||
console.error(`Error parsing file: ${filePath}`)
|
||||
reject(new Error(`Error parsing file: ${filePath}`))
|
||||
return
|
||||
}
|
||||
|
||||
// Flat structure: just get all keys directly
|
||||
const fileKeys = Object.keys(translationObj).map((key) => `${camelCaseFileName}.${key}`)
|
||||
allKeys.push(...fileKeys)
|
||||
} catch (error) {
|
||||
console.error(`Error processing file ${filePath}:`, error.message)
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
resolve(allKeys)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function removeExtraKeysFromFile(language, fileName, extraKeys) {
|
||||
const filePath = path.resolve(__dirname, '../i18n/locales', language, `${fileName}.json`)
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.log(`⚠️ File not found: ${filePath}`)
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
// Filter keys that belong to this file
|
||||
const camelCaseFileName = fileName.replace(/[-_](.)/g, (_, c) => c.toUpperCase())
|
||||
const fileSpecificKeys = extraKeys
|
||||
.filter((key) => key.startsWith(`${camelCaseFileName}.`))
|
||||
.map((key) => key.substring(camelCaseFileName.length + 1)) // Remove file prefix
|
||||
|
||||
if (fileSpecificKeys.length === 0) return false
|
||||
|
||||
console.log(`🔄 Processing file: ${filePath}`)
|
||||
|
||||
// Read and parse JSON
|
||||
const content = fs.readFileSync(filePath, 'utf8')
|
||||
const translationObj = JSON.parse(content)
|
||||
|
||||
let modified = false
|
||||
|
||||
// Remove each extra key (flat structure - direct property deletion)
|
||||
for (const keyToRemove of fileSpecificKeys) {
|
||||
if (keyToRemove in translationObj) {
|
||||
delete translationObj[keyToRemove]
|
||||
console.log(`🗑️ Removed key: ${keyToRemove}`)
|
||||
modified = true
|
||||
} else {
|
||||
console.log(`⚠️ Could not find key: ${keyToRemove}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
// Write back to file
|
||||
const newContent = `${JSON.stringify(translationObj, null, 2)}\n`
|
||||
fs.writeFileSync(filePath, newContent)
|
||||
console.log(`💾 Updated file: ${filePath}`)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
} catch (error) {
|
||||
console.error(`Error processing file ${filePath}:`, error.message)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function readLocaleFile(language, fileName) {
|
||||
const filePath = path.resolve(__dirname, '../i18n/locales', language, `${fileName}.json`)
|
||||
|
||||
if (!fs.existsSync(filePath)) return null
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'))
|
||||
return parsed && typeof parsed === 'object' ? parsed : null
|
||||
} catch (error) {
|
||||
console.error(`Error processing file ${filePath}:`, error.message)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function listSourceFileNames() {
|
||||
const folderPath = path.resolve(__dirname, '../i18n/locales', targetLanguage)
|
||||
return fs
|
||||
.readdirSync(folderPath)
|
||||
.filter((file) => file.endsWith('.json'))
|
||||
.map((file) => file.replace(/\.json$/, ''))
|
||||
}
|
||||
|
||||
// Add command line argument support
|
||||
const args = parseArgs(process.argv)
|
||||
const targetFiles = Array.from(new Set(args.files))
|
||||
const targetLangs = Array.from(new Set(args.languages))
|
||||
const autoRemove = args.autoRemove
|
||||
|
||||
async function main() {
|
||||
const compareKeysCount = async () => {
|
||||
let hasDiff = false
|
||||
const allTargetKeys = await getKeysFromLanguage(targetLanguage)
|
||||
|
||||
// Filter target keys by file if specified
|
||||
const camelTargetFiles = targetFiles.map((file) =>
|
||||
file.replace(/[-_](.)/g, (_, c) => c.toUpperCase()),
|
||||
)
|
||||
const targetKeys = targetFiles.length
|
||||
? allTargetKeys.filter((key) => camelTargetFiles.some((file) => key.startsWith(`${file}.`)))
|
||||
: allTargetKeys
|
||||
|
||||
// Filter languages by target language if specified
|
||||
const languagesToProcess = targetLangs.length ? targetLangs : languages
|
||||
|
||||
const allLanguagesKeys = await Promise.all(
|
||||
languagesToProcess.map((language) => getKeysFromLanguage(language)),
|
||||
)
|
||||
|
||||
// Filter language keys by file if specified
|
||||
const languagesKeys = targetFiles.length
|
||||
? allLanguagesKeys.map((keys) =>
|
||||
keys.filter((key) => camelTargetFiles.some((file) => key.startsWith(`${file}.`))),
|
||||
)
|
||||
: allLanguagesKeys
|
||||
|
||||
const getRequiredTargetKeys = (language) =>
|
||||
targetKeys.filter((key) => {
|
||||
const fileName = key.split('.')[0]
|
||||
const localizedLocales = localizedFileLocales[fileName]
|
||||
return !localizedLocales || localizedLocales.has(language)
|
||||
})
|
||||
|
||||
const sourceKeys = new Set(targetKeys)
|
||||
const comparison = languagesToProcess.reduce((result, language, index) => {
|
||||
const keys = languagesKeys[index]
|
||||
const missing = getRequiredTargetKeys(language).filter((key) => !keys.includes(key))
|
||||
const extra = keys.filter((key) => !getTranslationSourceKey(sourceKeys, key, language))
|
||||
result[language] = missing.length - extra.length
|
||||
return result
|
||||
}, {})
|
||||
|
||||
console.log(comparison)
|
||||
|
||||
// Print missing keys and extra keys
|
||||
for (let index = 0; index < languagesToProcess.length; index++) {
|
||||
const language = languagesToProcess[index]
|
||||
const languageKeys = languagesKeys[index]
|
||||
const missingKeys = getRequiredTargetKeys(language).filter(
|
||||
(key) => !languageKeys.includes(key),
|
||||
)
|
||||
const extraKeys = languageKeys.filter(
|
||||
(key) => !getTranslationSourceKey(sourceKeys, key, language),
|
||||
)
|
||||
|
||||
console.log(`Missing keys in ${language}:`, missingKeys)
|
||||
if (missingKeys.length > 0) hasDiff = true
|
||||
|
||||
// Show extra keys only when there are extra keys (negative difference)
|
||||
if (extraKeys.length > 0) {
|
||||
console.log(`Extra keys in ${language} (not in ${targetLanguage}):`, extraKeys)
|
||||
|
||||
// Auto-remove extra keys if flag is set
|
||||
if (autoRemove) {
|
||||
console.log(`\n🤖 Auto-removing extra keys from ${language}...`)
|
||||
|
||||
// Get all translation files
|
||||
const i18nFolder = path.resolve(__dirname, '../i18n/locales', language)
|
||||
const files = fs
|
||||
.readdirSync(i18nFolder)
|
||||
.filter((file) => file.endsWith('.json'))
|
||||
.map((file) => file.replace(/\.json$/, ''))
|
||||
.filter((f) => targetFiles.length === 0 || targetFiles.includes(f))
|
||||
|
||||
let totalRemoved = 0
|
||||
for (const fileName of files) {
|
||||
const removed = await removeExtraKeysFromFile(language, fileName, extraKeys)
|
||||
if (removed) totalRemoved++
|
||||
}
|
||||
|
||||
console.log(`✅ Auto-removal completed for ${language}. Modified ${totalRemoved} files.`)
|
||||
} else {
|
||||
hasDiff = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return hasDiff
|
||||
}
|
||||
|
||||
const compareValues = () => {
|
||||
let hasDiff = false
|
||||
const fileNames = targetFiles.length ? targetFiles : listSourceFileNames()
|
||||
const languagesToProcess = (targetLangs.length ? targetLangs : languages).filter(
|
||||
(language) => language !== targetLanguage,
|
||||
)
|
||||
|
||||
for (const language of languagesToProcess) {
|
||||
const issueLines = []
|
||||
|
||||
for (const fileName of fileNames) {
|
||||
const source = readLocaleFile(targetLanguage, fileName)
|
||||
const translation = readLocaleFile(language, fileName)
|
||||
if (!source || !translation) continue
|
||||
|
||||
for (const issue of findValueIssues(source, translation, language))
|
||||
issueLines.push(formatValueIssue(fileName, issue))
|
||||
}
|
||||
|
||||
if (issueLines.length) {
|
||||
hasDiff = true
|
||||
console.log(`Placeholder and tag mismatches in ${language}:`)
|
||||
issueLines.forEach((line) => console.log(` ${line}`))
|
||||
}
|
||||
}
|
||||
|
||||
return hasDiff
|
||||
}
|
||||
|
||||
console.log('🚀 Starting i18n:check script...')
|
||||
if (targetFiles.length) console.log(`📁 Checking files: ${targetFiles.join(', ')}`)
|
||||
|
||||
if (targetLangs.length) console.log(`🌍 Checking languages: ${targetLangs.join(', ')}`)
|
||||
|
||||
if (autoRemove) console.log('🤖 Auto-remove mode: ENABLED')
|
||||
|
||||
const hasKeyDiff = await compareKeysCount()
|
||||
const hasValueDiff = compareValues()
|
||||
if (hasKeyDiff || hasValueDiff) {
|
||||
console.error('\n❌ i18n files are not aligned. Fix issues above.')
|
||||
process.exitCode = 1
|
||||
} else {
|
||||
console.log('\n✅ All i18n files are in sync')
|
||||
}
|
||||
}
|
||||
|
||||
async function bootstrap() {
|
||||
if (args.help) {
|
||||
printHelp()
|
||||
return
|
||||
}
|
||||
|
||||
if (args.errors.length) {
|
||||
args.errors.forEach((message) => console.error(`❌ ${message}`))
|
||||
printHelp()
|
||||
process.exit(1)
|
||||
return
|
||||
}
|
||||
|
||||
const unknownLangs = targetLangs.filter((lang) => !languages.includes(lang))
|
||||
if (unknownLangs.length) {
|
||||
console.error(`❌ Unsupported languages: ${unknownLangs.join(', ')}`)
|
||||
process.exit(1)
|
||||
return
|
||||
}
|
||||
|
||||
await main()
|
||||
}
|
||||
|
||||
bootstrap().catch((error) => {
|
||||
console.error('❌ Unexpected error:', error.message)
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* This script copies static files to the target directory and starts the server.
|
||||
* It is intended to be used as a replacement for `next start`.
|
||||
*/
|
||||
|
||||
import { spawn } from 'node:child_process'
|
||||
import { cp, mkdir, stat } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
|
||||
// Function to check if a path exists
|
||||
const pathExists = async (path) => {
|
||||
try {
|
||||
console.debug(`Checking if path exists: ${path}`)
|
||||
await stat(path)
|
||||
console.debug(`Path exists: ${path}`)
|
||||
return true
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
console.warn(`Path does not exist: ${path}`)
|
||||
return false
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
const STANDALONE_ROOT_CANDIDATES = [
|
||||
path.join('.next', 'standalone', 'web'),
|
||||
path.join('.next', 'standalone'),
|
||||
]
|
||||
|
||||
const getStandaloneRoot = async () => {
|
||||
for (const standaloneRoot of STANDALONE_ROOT_CANDIDATES) {
|
||||
const serverScriptPath = path.join(standaloneRoot, 'server.js')
|
||||
if (await pathExists(serverScriptPath)) return standaloneRoot
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Unable to find Next standalone server entry. Checked: ${STANDALONE_ROOT_CANDIDATES.join(', ')}`,
|
||||
)
|
||||
}
|
||||
|
||||
// Function to recursively copy directories
|
||||
const copyDir = async (src, dest) => {
|
||||
console.debug(`Copying directory from ${src} to ${dest}`)
|
||||
await cp(src, dest, { recursive: true })
|
||||
console.info(`Successfully copied ${src} to ${dest}`)
|
||||
}
|
||||
|
||||
// Process each directory copy operation
|
||||
const copyAllDirs = async (standaloneRoot) => {
|
||||
const dirsToCopy = [
|
||||
{
|
||||
src: path.join('.next', 'static'),
|
||||
dest: path.join(standaloneRoot, '.next', 'static'),
|
||||
},
|
||||
{
|
||||
src: 'public',
|
||||
dest: path.join(standaloneRoot, 'public'),
|
||||
},
|
||||
]
|
||||
|
||||
console.debug('Starting directory copy operations')
|
||||
for (const { src, dest } of dirsToCopy) {
|
||||
try {
|
||||
// Instead of pre-creating destination directory, we ensure parent directory exists
|
||||
const destParent = path.dirname(dest)
|
||||
console.debug(`Ensuring destination parent directory exists: ${destParent}`)
|
||||
await mkdir(destParent, { recursive: true })
|
||||
if (await pathExists(src)) {
|
||||
await copyDir(src, dest)
|
||||
} else {
|
||||
console.error(`Error: ${src} directory does not exist. This is a required build artifact.`)
|
||||
process.exit(1)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error processing ${src}:`, err.message)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
console.debug('Finished directory copy operations')
|
||||
}
|
||||
|
||||
// Run copy operations and start server
|
||||
const main = async () => {
|
||||
console.debug('Starting copy-and-start script')
|
||||
const standaloneRoot = await getStandaloneRoot()
|
||||
const serverScriptPath = path.join(standaloneRoot, 'server.js')
|
||||
|
||||
await copyAllDirs(standaloneRoot)
|
||||
|
||||
// Start server
|
||||
const port = process.env.pnpm_config_port || process.env.PORT || '3000'
|
||||
const host = process.env.pnpm_config_host || process.env.HOSTNAME || '0.0.0.0'
|
||||
|
||||
console.info(`Starting server on ${host}:${port}`)
|
||||
console.debug(`Server script path: ${serverScriptPath}`)
|
||||
console.debug(`Environment variables - PORT: ${port}, HOSTNAME: ${host}`)
|
||||
|
||||
const server = spawn(process.execPath, [serverScriptPath], {
|
||||
env: {
|
||||
...process.env,
|
||||
PORT: port,
|
||||
HOSTNAME: host,
|
||||
},
|
||||
stdio: 'inherit',
|
||||
})
|
||||
|
||||
server.on('error', (err) => {
|
||||
console.error('Failed to start server:', err)
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
server.on('exit', (code) => {
|
||||
console.debug(`Server exited with code: ${code}`)
|
||||
process.exit(code || 0)
|
||||
})
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('Unexpected error:', err)
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -0,0 +1,455 @@
|
||||
// GENERATE BY script
|
||||
// DON NOT EDIT IT MANUALLY
|
||||
//
|
||||
// This script fetches the docs.json from dify-docs repository
|
||||
// and generates TypeScript types for documentation paths.
|
||||
//
|
||||
// Usage: pnpm gen-doc-paths
|
||||
|
||||
import { writeFile } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
const DEFAULT_DOCS_JSON_URL =
|
||||
'https://raw.githubusercontent.com/langgenius/dify-docs/refs/heads/main/docs.json'
|
||||
const DOCS_JSON_URL = process.env.DOCS_JSON_URL || DEFAULT_DOCS_JSON_URL
|
||||
const OUTPUT_PATH = path.resolve(__dirname, '../types/doc-paths.ts')
|
||||
|
||||
type NavItem = string | NavObject | NavItem[]
|
||||
|
||||
type NavObject = {
|
||||
pages?: NavItem[]
|
||||
groups?: NavItem[]
|
||||
dropdowns?: NavItem[]
|
||||
languages?: NavItem[]
|
||||
products?: NavItem[]
|
||||
tabs?: NavItem[]
|
||||
menu?: NavItem[]
|
||||
versions?: NavItem[]
|
||||
root?: string
|
||||
openapi?: string
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
type OpenAPIOperation = {
|
||||
summary?: string
|
||||
operationId?: string
|
||||
tags?: string[]
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
type OpenAPIPathItem = {
|
||||
get?: OpenAPIOperation
|
||||
post?: OpenAPIOperation
|
||||
put?: OpenAPIOperation
|
||||
patch?: OpenAPIOperation
|
||||
delete?: OpenAPIOperation
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
type OpenAPISpec = {
|
||||
paths?: Record<string, OpenAPIPathItem>
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
type DocsJson = {
|
||||
navigation?: NavItem
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
const OPENAPI_BASE_URL = (
|
||||
process.env.DOCS_OPENAPI_BASE_URL || new URL('.', DOCS_JSON_URL).toString()
|
||||
).replace(/\/?$/, '/')
|
||||
const DOCS_PRODUCTS = ['cloud', 'self-host'] as const
|
||||
|
||||
type DocsProduct = (typeof DOCS_PRODUCTS)[number]
|
||||
type ProductAvailability = Record<string, Set<DocsProduct>>
|
||||
|
||||
function isDocsProduct(segment: string): segment is DocsProduct {
|
||||
return DOCS_PRODUCTS.includes(segment as DocsProduct)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert summary to URL slug
|
||||
* e.g., "Get Knowledge Base List" -> "get-knowledge-base-list"
|
||||
* Non-Latin summaries without spaces are preserved.
|
||||
*/
|
||||
function summaryToSlug(summary: string): string {
|
||||
return summary.toLowerCase().replace(/\s+/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '')
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first path segment from an API path
|
||||
* e.g., "/datasets/{dataset_id}/documents" -> "datasets"
|
||||
*/
|
||||
function getFirstPathSegment(apiPath: string): string {
|
||||
const segments = apiPath.split('/').filter(Boolean)
|
||||
return segments[0] || ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively extract OpenAPI file paths from navigation structure
|
||||
*/
|
||||
function extractOpenAPIPaths(
|
||||
item: NavItem | undefined,
|
||||
paths: Set<string> = new Set(),
|
||||
): Set<string> {
|
||||
if (!item) return paths
|
||||
|
||||
if (Array.isArray(item)) {
|
||||
for (const el of item) extractOpenAPIPaths(el, paths)
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
if (typeof item === 'object') {
|
||||
if (item.openapi && typeof item.openapi === 'string') paths.add(item.openapi)
|
||||
|
||||
if (item.pages) extractOpenAPIPaths(item.pages, paths)
|
||||
|
||||
if (item.groups) extractOpenAPIPaths(item.groups, paths)
|
||||
|
||||
if (item.dropdowns) extractOpenAPIPaths(item.dropdowns, paths)
|
||||
|
||||
if (item.languages) extractOpenAPIPaths(item.languages, paths)
|
||||
|
||||
if (item.products) extractOpenAPIPaths(item.products, paths)
|
||||
|
||||
if (item.tabs) extractOpenAPIPaths(item.tabs, paths)
|
||||
|
||||
if (item.menu) extractOpenAPIPaths(item.menu, paths)
|
||||
|
||||
if (item.versions) extractOpenAPIPaths(item.versions, paths)
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
type EndpointPathMap = Map<string, string> // key: `${apiPath}_${method}`, value: generated doc path
|
||||
|
||||
/**
|
||||
* Fetch and parse OpenAPI spec, extract API reference paths with endpoint keys
|
||||
*/
|
||||
async function fetchOpenAPIAndExtractPaths(openapiPath: string): Promise<EndpointPathMap> {
|
||||
const url = `${OPENAPI_BASE_URL}${openapiPath}`
|
||||
const response = await fetch(url)
|
||||
if (!response.ok) {
|
||||
console.warn(`Failed to fetch ${url}: ${response.status}`)
|
||||
return new Map()
|
||||
}
|
||||
|
||||
const spec = (await response.json()) as OpenAPISpec
|
||||
const pathMap: EndpointPathMap = new Map()
|
||||
|
||||
if (!spec.paths) return pathMap
|
||||
|
||||
const httpMethods = ['get', 'post', 'put', 'patch', 'delete'] as const
|
||||
|
||||
for (const [apiPath, pathItem] of Object.entries(spec.paths)) {
|
||||
for (const method of httpMethods) {
|
||||
const operation = pathItem[method]
|
||||
if (operation?.summary) {
|
||||
// Try to get tag from operation, fallback to path segment
|
||||
const tag = operation.tags?.[0]
|
||||
const segment = tag ? summaryToSlug(tag) : getFirstPathSegment(apiPath)
|
||||
if (!segment) continue
|
||||
|
||||
const slug = summaryToSlug(operation.summary)
|
||||
// Skip empty slugs
|
||||
if (slug) {
|
||||
const endpointKey = `${apiPath}_${method}`
|
||||
pathMap.set(endpointKey, `/api-reference/${segment}/${slug}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pathMap
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively extract all page paths from navigation structure
|
||||
*/
|
||||
function extractPaths(item: NavItem | undefined, paths: Set<string> = new Set()): Set<string> {
|
||||
if (!item) return paths
|
||||
|
||||
if (Array.isArray(item)) {
|
||||
for (const el of item) extractPaths(el, paths)
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
if (typeof item === 'string') {
|
||||
paths.add(item)
|
||||
return paths
|
||||
}
|
||||
|
||||
if (typeof item === 'object') {
|
||||
if (item.root) paths.add(item.root)
|
||||
|
||||
// Handle pages array
|
||||
if (item.pages) extractPaths(item.pages, paths)
|
||||
|
||||
// Handle groups array
|
||||
if (item.groups) extractPaths(item.groups, paths)
|
||||
|
||||
// Handle dropdowns
|
||||
if (item.dropdowns) extractPaths(item.dropdowns, paths)
|
||||
|
||||
// Handle languages
|
||||
if (item.languages) extractPaths(item.languages, paths)
|
||||
|
||||
if (item.products) extractPaths(item.products, paths)
|
||||
|
||||
if (item.tabs) extractPaths(item.tabs, paths)
|
||||
|
||||
if (item.menu) extractPaths(item.menu, paths)
|
||||
|
||||
// Handle versions in navigation
|
||||
if (item.versions) extractPaths(item.versions, paths)
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
function addPathToGroup(groups: Record<string, Set<string>>, pathWithoutLang: string): void {
|
||||
const parts = pathWithoutLang.split('/')
|
||||
const section = parts[0]
|
||||
if (!section) return
|
||||
|
||||
if (!groups[section]) groups[section] = new Set()
|
||||
|
||||
groups[section]!.add(pathWithoutLang)
|
||||
}
|
||||
|
||||
function getProductPathInfo(
|
||||
pathWithoutLang: string,
|
||||
): { product: DocsProduct; pathWithoutProduct: string } | undefined {
|
||||
const parts = pathWithoutLang.split('/')
|
||||
const [product, ...rest] = parts
|
||||
|
||||
if (!product || !isDocsProduct(product) || rest.length === 0) return undefined
|
||||
|
||||
return {
|
||||
product,
|
||||
pathWithoutProduct: rest.join('/'),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Group paths by their prefix structure
|
||||
*/
|
||||
function groupPathsBySection(paths: Set<string>): {
|
||||
groups: Record<string, Set<string>>
|
||||
productAvailability: ProductAvailability
|
||||
} {
|
||||
const groups: Record<string, Set<string>> = {}
|
||||
const productAvailability: ProductAvailability = {}
|
||||
|
||||
for (const fullPath of paths) {
|
||||
// Remove language prefix (en/, zh/, ja/)
|
||||
const withoutLang = fullPath.replace(/^(en|zh|ja)\//, '')
|
||||
if (!withoutLang || withoutLang === fullPath) continue
|
||||
|
||||
// Skip non-doc paths (like .json files for OpenAPI)
|
||||
if (withoutLang.endsWith('.json') || withoutLang === 'None') continue
|
||||
|
||||
addPathToGroup(groups, withoutLang)
|
||||
|
||||
const productPathInfo = getProductPathInfo(withoutLang)
|
||||
if (productPathInfo) {
|
||||
const productlessPath = productPathInfo.pathWithoutProduct
|
||||
const normalizedPath = `/${productlessPath}`
|
||||
|
||||
addPathToGroup(groups, productlessPath)
|
||||
|
||||
if (!productAvailability[normalizedPath]) productAvailability[normalizedPath] = new Set()
|
||||
|
||||
productAvailability[normalizedPath]!.add(productPathInfo.product)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
groups,
|
||||
productAvailability,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert section name to TypeScript type name
|
||||
*/
|
||||
function sectionToTypeName(section: string): string {
|
||||
return section
|
||||
.split('-')
|
||||
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
||||
.join('')
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate TypeScript type definitions
|
||||
*/
|
||||
function generateTypeDefinitions(
|
||||
groups: Record<string, Set<string>>,
|
||||
productAvailability: ProductAvailability,
|
||||
apiReferencePaths: string[],
|
||||
): string {
|
||||
const lines: string[] = [
|
||||
'// GENERATE BY script',
|
||||
'// DON NOT EDIT IT MANUALLY',
|
||||
'//',
|
||||
`// Generated from: ${DOCS_JSON_URL}`,
|
||||
`// Generated at: ${new Date().toISOString()}`,
|
||||
'',
|
||||
'// Language prefixes',
|
||||
"export type DocLanguage = 'en' | 'zh' | 'ja'",
|
||||
"export type DocsProduct = 'cloud' | 'self-host'",
|
||||
'',
|
||||
]
|
||||
|
||||
const typeNames: string[] = []
|
||||
|
||||
// Generate type for each section
|
||||
for (const [section, pathsSet] of Object.entries(groups)) {
|
||||
const paths = Array.from(pathsSet).sort()
|
||||
const typeName = `${sectionToTypeName(section)}Path`
|
||||
typeNames.push(typeName)
|
||||
|
||||
lines.push(`// ${sectionToTypeName(section)} paths`)
|
||||
lines.push(`type ${typeName} =`)
|
||||
|
||||
for (const p of paths) {
|
||||
lines.push(` | '/${p}'`)
|
||||
}
|
||||
|
||||
lines.push('')
|
||||
|
||||
// Add UseDifyNodesPath helper type after UseDifyPath
|
||||
if (section === 'use-dify') {
|
||||
lines.push('// UseDify node paths (without prefix)')
|
||||
/* oxlint-disable no-template-curly-in-string */
|
||||
lines.push(
|
||||
'type ExtractNodesPath<T> = T extends `/use-dify/nodes/${infer Path}` ? Path : never',
|
||||
)
|
||||
/* oxlint-enable no-template-curly-in-string */
|
||||
lines.push('export type UseDifyNodesPath = ExtractNodesPath<UseDifyPath>')
|
||||
lines.push('')
|
||||
}
|
||||
}
|
||||
|
||||
// Generate API reference type (English paths only)
|
||||
if (apiReferencePaths.length > 0) {
|
||||
const sortedPaths = [...apiReferencePaths].sort()
|
||||
lines.push('// API Reference endpoint paths')
|
||||
lines.push('type ApiEndpointReferencePath =')
|
||||
for (const p of sortedPaths) {
|
||||
lines.push(` | '${p}'`)
|
||||
}
|
||||
lines.push('')
|
||||
typeNames.push('ApiEndpointReferencePath')
|
||||
}
|
||||
|
||||
// Generate base combined type
|
||||
lines.push('// Base path without language prefix')
|
||||
lines.push('type DocPathWithoutLangBase =')
|
||||
for (const typeName of typeNames) {
|
||||
lines.push(` | ${typeName}`)
|
||||
}
|
||||
lines.push('')
|
||||
|
||||
// Generate combined type with optional anchor support
|
||||
lines.push('// Combined path without language prefix (supports optional #anchor)')
|
||||
lines.push('export type DocPathWithoutLang =')
|
||||
lines.push(' | DocPathWithoutLangBase')
|
||||
// oxlint-disable-next-line no-template-curly-in-string
|
||||
lines.push(' | `${DocPathWithoutLangBase}#${string}`')
|
||||
lines.push('')
|
||||
|
||||
// Generate product availability map for productless runtime links.
|
||||
lines.push('// Product availability for productless docs paths')
|
||||
lines.push('export const docPathProductAvailability = {')
|
||||
for (const path of Object.keys(productAvailability).sort()) {
|
||||
const products = [...productAvailability[path]!].sort(
|
||||
(a, b) => DOCS_PRODUCTS.indexOf(a) - DOCS_PRODUCTS.indexOf(b),
|
||||
)
|
||||
lines.push(` '${path}': [${products.map((product) => `'${product}'`).join(', ')}],`)
|
||||
}
|
||||
lines.push('} as const satisfies Record<string, readonly DocsProduct[]>')
|
||||
lines.push('')
|
||||
lines.push('export type ProductlessDocPath = keyof typeof docPathProductAvailability')
|
||||
lines.push('export type ProductlessDocPathWithAnchor =')
|
||||
lines.push(' | ProductlessDocPath')
|
||||
// oxlint-disable-next-line no-template-curly-in-string
|
||||
lines.push(' | `${ProductlessDocPath}#${string}`')
|
||||
lines.push('')
|
||||
lines.push('export const isProductlessDocPath = (path: string): path is ProductlessDocPath =>')
|
||||
lines.push(' Object.hasOwn(docPathProductAvailability, path)')
|
||||
lines.push('')
|
||||
lines.push(
|
||||
'export const isProductlessDocPathWithAnchor = (path: string): path is ProductlessDocPathWithAnchor => {',
|
||||
)
|
||||
lines.push(" const pathname = path.split('#', 1)[0]")
|
||||
lines.push(' return Boolean(pathname && isProductlessDocPath(pathname))')
|
||||
lines.push('}')
|
||||
lines.push('')
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
console.log('Fetching docs.json from GitHub...')
|
||||
|
||||
const response = await fetch(DOCS_JSON_URL)
|
||||
if (!response.ok)
|
||||
throw new Error(`Failed to fetch docs.json: ${response.status} ${response.statusText}`)
|
||||
|
||||
const docsJson = (await response.json()) as DocsJson
|
||||
console.log('Successfully fetched docs.json')
|
||||
|
||||
// Extract paths from navigation
|
||||
const allPaths = extractPaths(docsJson.navigation)
|
||||
|
||||
console.log(`Found ${allPaths.size} total paths`)
|
||||
|
||||
// Extract OpenAPI file paths from navigation for all languages
|
||||
const openApiPaths = extractOpenAPIPaths(docsJson.navigation)
|
||||
|
||||
console.log(`Found ${openApiPaths.size} OpenAPI specs to process`)
|
||||
|
||||
// Fetch English OpenAPI specs and extract API reference paths.
|
||||
// API reference URLs are language-prefixed by useDocLink at runtime.
|
||||
const enApiPaths: string[] = []
|
||||
|
||||
for (const openapiPath of openApiPaths) {
|
||||
const langMatch = /^(en|zh|ja)\//.exec(openapiPath)
|
||||
if (langMatch?.[1] !== 'en') continue
|
||||
|
||||
console.log(`Fetching OpenAPI spec: ${openapiPath}`)
|
||||
const pathMap = await fetchOpenAPIAndExtractPaths(openapiPath)
|
||||
for (const enPath of pathMap.values()) enApiPaths.push(enPath)
|
||||
}
|
||||
|
||||
// Deduplicate English API paths
|
||||
const uniqueEnApiPaths = [...new Set(enApiPaths)]
|
||||
|
||||
console.log(`Extracted ${uniqueEnApiPaths.length} unique English API reference paths`)
|
||||
|
||||
// Group by section
|
||||
const { groups, productAvailability } = groupPathsBySection(allPaths)
|
||||
|
||||
console.log(`Grouped into ${Object.keys(groups).length} sections:`, Object.keys(groups))
|
||||
console.log(`Found ${Object.keys(productAvailability).length} product-aware paths`)
|
||||
|
||||
// Generate TypeScript
|
||||
const tsContent = generateTypeDefinitions(groups, productAvailability, uniqueEnApiPaths)
|
||||
|
||||
// Write to file
|
||||
await writeFile(OUTPUT_PATH, tsContent, 'utf-8')
|
||||
|
||||
console.log(`Generated TypeScript types at: ${OUTPUT_PATH}`)
|
||||
}
|
||||
|
||||
main().catch((err: Error) => {
|
||||
console.error('Error:', err.message)
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
import { readFile, writeFile } from 'node:fs/promises'
|
||||
import { mergeResourcesAsInterface } from 'i18next-resources-for-ts'
|
||||
import { kebabCase } from 'string-ts'
|
||||
import { format } from 'vite-plus/fmt'
|
||||
import { namespaces } from '../i18n/resources.ts'
|
||||
|
||||
type Translation = string | string[]
|
||||
|
||||
const output = new URL('../i18n/resources.generated.d.ts', import.meta.url)
|
||||
|
||||
export async function generateResourceTypes(
|
||||
resources: Record<string, Record<string, Translation>>,
|
||||
) {
|
||||
const declaration = mergeResourcesAsInterface(
|
||||
Object.entries(resources).map(([name, resources]) => ({ name, resources })),
|
||||
{ optimize: true },
|
||||
)
|
||||
const source = `// Generated from i18n/locales/en-US/*.json by pnpm i18n:generate-types. Do not edit.\n\n${declaration}`
|
||||
const result = await format('resources.generated.d.ts', source, {
|
||||
singleQuote: true,
|
||||
semi: false,
|
||||
})
|
||||
if (result.errors.length) throw new Error('Could not format generated i18n types')
|
||||
return result.code
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const resources: Record<string, Record<string, Translation>> = {}
|
||||
for (const namespace of namespaces) {
|
||||
const file = new URL(`../i18n/locales/en-US/${kebabCase(namespace)}.json`, import.meta.url)
|
||||
const entries: unknown = JSON.parse(await readFile(file, 'utf8'))
|
||||
if (
|
||||
!entries ||
|
||||
typeof entries !== 'object' ||
|
||||
Array.isArray(entries) ||
|
||||
Object.values(entries).some(
|
||||
(value) =>
|
||||
typeof value !== 'string' &&
|
||||
!(Array.isArray(value) && value.every((item) => typeof item === 'string')),
|
||||
)
|
||||
) {
|
||||
throw new Error(`${file.pathname} must contain flat string translations or string arrays`)
|
||||
}
|
||||
resources[namespace] = entries as Record<string, Translation>
|
||||
}
|
||||
const generated = await generateResourceTypes(resources)
|
||||
if (process.argv.includes('--check')) {
|
||||
const current = await readFile(output, 'utf8').catch(() => '')
|
||||
if (current !== generated) {
|
||||
throw new Error(
|
||||
'i18n types are stale. Run pnpm --dir web i18n:generate-types and commit the result.',
|
||||
)
|
||||
}
|
||||
} else {
|
||||
await writeFile(output, generated)
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.main) await main()
|
||||
@@ -0,0 +1,45 @@
|
||||
import { readFile, writeFile } from 'node:fs/promises'
|
||||
import { pinyin } from 'pinyin-pro'
|
||||
import { format } from 'vite-plus/fmt'
|
||||
|
||||
const output = new URL(
|
||||
'../app/components/workflow/block-selector/provider-initials.generated.ts',
|
||||
import.meta.url,
|
||||
)
|
||||
const metadata = await readFile(
|
||||
new URL('../node_modules/pinyin-pro/package.json', import.meta.url),
|
||||
'utf8',
|
||||
)
|
||||
const { version } = JSON.parse(metadata) as { version: string }
|
||||
const license = await readFile(
|
||||
new URL('../node_modules/pinyin-pro/LICENSE', import.meta.url),
|
||||
'utf8',
|
||||
)
|
||||
|
||||
// Preserve the existing single-character grouping range and default readings.
|
||||
let initials = ''
|
||||
for (let code = 0x4e00; code <= 0x9fa5; code++) {
|
||||
const character = String.fromCharCode(code)
|
||||
const initial = (
|
||||
pinyin(character, { pattern: 'first', toneType: 'none' })[0] || character
|
||||
).toUpperCase()
|
||||
initials += /^[A-Z]$/.test(initial) ? initial : '#'
|
||||
}
|
||||
|
||||
const source = `// Generated by pnpm --dir web gen-provider-initials. Do not edit.
|
||||
// Single-character initials derived from pinyin-pro ${version} (https://github.com/zh-lx/pinyin-pro).
|
||||
/*
|
||||
${license.trim()}
|
||||
*/
|
||||
|
||||
// Indexed by UTF-16 code unit minus U+4E00, through U+9FA5 inclusive.
|
||||
export const providerInitials = ${JSON.stringify(initials)}
|
||||
`
|
||||
const result = await format(output.pathname, source, { singleQuote: true, semi: false })
|
||||
if (result.errors.length) throw new Error('Could not format provider initials')
|
||||
if (process.argv.includes('--check')) {
|
||||
if ((await readFile(output, 'utf8')) !== result.code)
|
||||
throw new Error('Provider initials are stale. Run pnpm --dir web gen-provider-initials.')
|
||||
} else {
|
||||
await writeFile(output, result.code)
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "@dify/tsconfig/node.json",
|
||||
"compilerOptions": {
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"allowImportingTsExtensions": true
|
||||
},
|
||||
"include": ["*.ts", "*.js", "*.mjs"]
|
||||
}
|
||||
Reference in new issue
Block a user