mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-28 17:57:48 +02:00
fix(cli): fix CLI write-translation bug (#11027)
* fix write-translation bug * fix write-translation bug
This commit is contained in:
parent
8881fd1a59
commit
37d8844506
2 changed files with 32 additions and 8 deletions
|
@ -9,8 +9,22 @@ import {
|
||||||
GlobExcludeDefault,
|
GlobExcludeDefault,
|
||||||
createMatcher,
|
createMatcher,
|
||||||
createAbsoluteFilePathMatcher,
|
createAbsoluteFilePathMatcher,
|
||||||
|
isTranslatableSourceFile,
|
||||||
} from '../globUtils';
|
} 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', () => {
|
describe('createMatcher', () => {
|
||||||
it('match default exclude MD/MDX partials correctly', () => {
|
it('match default exclude MD/MDX partials correctly', () => {
|
||||||
const matcher = createMatcher(GlobExcludeDefault);
|
const matcher = createMatcher(GlobExcludeDefault);
|
||||||
|
|
|
@ -104,11 +104,8 @@ export async function safeGlobby(
|
||||||
return Globby(globPaths, options);
|
return Globby(globPaths, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// A bit weird to put this here, but it's used by core + theme-translations
|
export const isTranslatableSourceFile: (filePath: string) => boolean = (() => {
|
||||||
export async function globTranslatableSourceFiles(
|
// We only support extracting source code translations from these extensions
|
||||||
patterns: string[],
|
|
||||||
): Promise<string[]> {
|
|
||||||
// We only support extracting source code translations from these kind of files
|
|
||||||
const extensionsAllowed = new Set([
|
const extensionsAllowed = new Set([
|
||||||
'.js',
|
'.js',
|
||||||
'.jsx',
|
'.jsx',
|
||||||
|
@ -120,8 +117,21 @@ export async function globTranslatableSourceFiles(
|
||||||
// '.mdx',
|
// '.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);
|
const filePaths = await safeGlobby(patterns);
|
||||||
return filePaths.filter((filePath) =>
|
return filePaths.filter(isTranslatableSourceFile);
|
||||||
extensionsAllowed.has(path.extname(filePath)),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue