fix(cli): fix CLI write-translation bug (#11027)

* fix write-translation bug

* fix write-translation bug
This commit is contained in:
Sébastien Lorber 2025-03-27 12:22:44 +01:00 committed by GitHub
parent 8881fd1a59
commit 37d8844506
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 8 deletions

View file

@ -9,8 +9,22 @@ import {
GlobExcludeDefault,
createMatcher,
createAbsoluteFilePathMatcher,
isTranslatableSourceFile,
} from '../globUtils';
describe('isTranslatableSourceFile', () => {
it('works', () => {
expect(isTranslatableSourceFile('./xyz.ts')).toBe(true);
expect(isTranslatableSourceFile('./xyz.tsx')).toBe(true);
expect(isTranslatableSourceFile('./xyz.js')).toBe(true);
expect(isTranslatableSourceFile('./xyz.jsx')).toBe(true);
expect(isTranslatableSourceFile('./xyz.md')).toBe(false);
expect(isTranslatableSourceFile('./xyz.mdx')).toBe(false);
expect(isTranslatableSourceFile('./xyz.d.ts')).toBe(false);
});
});
describe('createMatcher', () => {
it('match default exclude MD/MDX partials correctly', () => {
const matcher = createMatcher(GlobExcludeDefault);

View file

@ -104,11 +104,8 @@ export async function safeGlobby(
return Globby(globPaths, options);
}
// A bit weird to put this here, but it's used by core + theme-translations
export async function globTranslatableSourceFiles(
patterns: string[],
): Promise<string[]> {
// We only support extracting source code translations from these kind of files
export const isTranslatableSourceFile: (filePath: string) => boolean = (() => {
// We only support extracting source code translations from these extensions
const extensionsAllowed = new Set([
'.js',
'.jsx',
@ -120,8 +117,21 @@ export async function globTranslatableSourceFiles(
// '.mdx',
]);
const isBlacklistedFilePath = (filePath: string) => {
// We usually extract from ts files, unless they are .d.ts files
return filePath.endsWith('.d.ts');
};
return (filePath): boolean => {
const ext = path.extname(filePath);
return extensionsAllowed.has(ext) && !isBlacklistedFilePath(filePath);
};
})();
// A bit weird to put this here, but it's used by core + theme-translations
export async function globTranslatableSourceFiles(
patterns: string[],
): Promise<string[]> {
const filePaths = await safeGlobby(patterns);
return filePaths.filter((filePath) =>
extensionsAllowed.has(path.extname(filePath)),
);
return filePaths.filter(isTranslatableSourceFile);
}