Skip to content

Commit 37d8844

Browse files
authored
fix(cli): fix CLI write-translation bug (#11027)
* fix write-translation bug * fix write-translation bug
1 parent 8881fd1 commit 37d8844

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

packages/docusaurus-utils/src/__tests__/globUtils.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,22 @@ import {
99
GlobExcludeDefault,
1010
createMatcher,
1111
createAbsoluteFilePathMatcher,
12+
isTranslatableSourceFile,
1213
} from '../globUtils';
1314

15+
describe('isTranslatableSourceFile', () => {
16+
it('works', () => {
17+
expect(isTranslatableSourceFile('./xyz.ts')).toBe(true);
18+
expect(isTranslatableSourceFile('./xyz.tsx')).toBe(true);
19+
expect(isTranslatableSourceFile('./xyz.js')).toBe(true);
20+
expect(isTranslatableSourceFile('./xyz.jsx')).toBe(true);
21+
22+
expect(isTranslatableSourceFile('./xyz.md')).toBe(false);
23+
expect(isTranslatableSourceFile('./xyz.mdx')).toBe(false);
24+
expect(isTranslatableSourceFile('./xyz.d.ts')).toBe(false);
25+
});
26+
});
27+
1428
describe('createMatcher', () => {
1529
it('match default exclude MD/MDX partials correctly', () => {
1630
const matcher = createMatcher(GlobExcludeDefault);

packages/docusaurus-utils/src/globUtils.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,8 @@ export async function safeGlobby(
104104
return Globby(globPaths, options);
105105
}
106106

107-
// A bit weird to put this here, but it's used by core + theme-translations
108-
export async function globTranslatableSourceFiles(
109-
patterns: string[],
110-
): Promise<string[]> {
111-
// We only support extracting source code translations from these kind of files
107+
export const isTranslatableSourceFile: (filePath: string) => boolean = (() => {
108+
// We only support extracting source code translations from these extensions
112109
const extensionsAllowed = new Set([
113110
'.js',
114111
'.jsx',
@@ -120,8 +117,21 @@ export async function globTranslatableSourceFiles(
120117
// '.mdx',
121118
]);
122119

120+
const isBlacklistedFilePath = (filePath: string) => {
121+
// We usually extract from ts files, unless they are .d.ts files
122+
return filePath.endsWith('.d.ts');
123+
};
124+
125+
return (filePath): boolean => {
126+
const ext = path.extname(filePath);
127+
return extensionsAllowed.has(ext) && !isBlacklistedFilePath(filePath);
128+
};
129+
})();
130+
131+
// A bit weird to put this here, but it's used by core + theme-translations
132+
export async function globTranslatableSourceFiles(
133+
patterns: string[],
134+
): Promise<string[]> {
123135
const filePaths = await safeGlobby(patterns);
124-
return filePaths.filter((filePath) =>
125-
extensionsAllowed.has(path.extname(filePath)),
126-
);
136+
return filePaths.filter(isTranslatableSourceFile);
127137
}

0 commit comments

Comments
 (0)