From e014393a24282a57c11d8f0ac4cc38d6fdf91649 Mon Sep 17 00:00:00 2001 From: Kirill Omelianenko Date: Tue, 5 Dec 2023 15:28:32 +0300 Subject: [PATCH] fix: build only used langs --- src/models.ts | 1 + src/steps/processServiceFiles.ts | 3 +- src/utils/filterByLang.ts | 13 ++++++++ tests/units/utils/filterByLang.test.ts | 46 ++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/utils/filterByLang.ts create mode 100644 tests/units/utils/filterByLang.test.ts diff --git a/src/models.ts b/src/models.ts index 4f8877da..94230ddc 100644 --- a/src/models.ts +++ b/src/models.ts @@ -62,6 +62,7 @@ export interface YfmArgv extends YfmConfig { addMapFile: boolean; allowCustomResources: boolean; staticContent: boolean; + langs: string[]; } export interface DocPreset { diff --git a/src/steps/processServiceFiles.ts b/src/steps/processServiceFiles.ts index d5bc321a..d6c6146a 100644 --- a/src/steps/processServiceFiles.ts +++ b/src/steps/processServiceFiles.ts @@ -6,6 +6,7 @@ import log from '@diplodoc/transform/lib/log'; import {ArgvService, PresetService, TocService} from '../services'; import {logger} from '../utils'; +import {filterByLang} from '../utils/filterByLang'; import {DocPreset} from '../models'; import shell from 'shelljs'; @@ -83,7 +84,7 @@ async function preparingTocFiles( getFilePathsByGlobals: GetFilePathsByGlobalsFunction, ): Promise { try { - const tocFilePaths = getFilePathsByGlobals(['**/toc.yaml']); + const tocFilePaths = filterByLang(getFilePathsByGlobals(['**/toc.yaml'])); for (const path of tocFilePaths) { logger.proc(path); diff --git a/src/utils/filterByLang.ts b/src/utils/filterByLang.ts new file mode 100644 index 00000000..7d447728 --- /dev/null +++ b/src/utils/filterByLang.ts @@ -0,0 +1,13 @@ +import {ArgvService} from '../services'; + +const filterByLang = (tocFilePaths: string[]): string[] => { + const {langs} = ArgvService.getConfig(); + if (langs && langs.length) { + return tocFilePaths.filter((path) => langs.includes(path.split('/')[0])); + } + return tocFilePaths; +}; + +export {filterByLang}; + +export default {filterByLang}; diff --git a/tests/units/utils/filterByLang.test.ts b/tests/units/utils/filterByLang.test.ts new file mode 100644 index 00000000..ab78c81f --- /dev/null +++ b/tests/units/utils/filterByLang.test.ts @@ -0,0 +1,46 @@ +import {filterByLang} from 'utils/filterByLang'; +import {ArgvService} from 'services/index'; +import {YfmArgv} from 'models'; + +describe('test filtering paths of toc files by lang', () => { + afterEach(() => { + ArgvService.set(undefined as YfmArgv); + }) + + it('should remove toc from not included lang', () => { + ArgvService.set({ + langs: ['ru'] + } as YfmArgv); + + const tocFilePaths = [ + 'ru/toc.yaml', + 'en/toc.yaml' + ]; + const expected = [ + 'ru/toc.yaml', + ]; + expect(filterByLang(tocFilePaths)).toEqual(expected); + }); + + it('should remove tocs from not included lang and work with nested paths', () => { + ArgvService.set({ + langs: ['ru', 'fr'] + } as YfmArgv); + + const tocFilePaths = [ + 'ru/toc.yaml', + 'ru/xxx/yyy/toc.yaml', + 'en/toc.yaml', + 'en/aaa/bbb/toc.yaml', + 'fr/toc.yaml', + 'fr/111/222/toc.yaml' + ]; + const expected = [ + 'ru/toc.yaml', + 'ru/xxx/yyy/toc.yaml', + 'fr/toc.yaml', + 'fr/111/222/toc.yaml' + ]; + expect(filterByLang(tocFilePaths)).toEqual(expected); + }); +}); \ No newline at end of file