diff --git a/README.md b/README.md index f13cb1e..7913292 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ will extract translations to .pot file --numberedExpressions boolean overrides babel-plugin-ttag setting - https://ttag.js.org/docs/plugin-api.html#confignumberedexpressions. Refer to the doc for the details. --extractLocation string - 'full' | 'file' | 'never' - https://ttag.js.org/docs/plugin-api.html#configextractlocation. Is used to format location comments in the .po file. --sortByMsgid boolean. Will sort output in alphabetically by msgid. https://ttag.js.org/docs/plugin-api.html#configsortbymsgid - + --exclude -e exclude files or directories matching the given glob pattern(s) from extraction ### `check [lang] ` will check if all translations are present in .po file diff --git a/src/commands/extract.ts b/src/commands/extract.ts index d98dc8b..4cc6260 100644 --- a/src/commands/extract.ts +++ b/src/commands/extract.ts @@ -8,18 +8,23 @@ async function extract( paths: string[], lang: string = "en", ttagOverrideOpts?: c3poTypes.TtagOpts, - ttagRcOpts?: c3poTypes.TtagRc + ttagRcOpts?: c3poTypes.TtagRc, + exclude?: string[] ) { const progress: c3poTypes.Progress = ora( `[ttag] extracting translations to ${output} ...` ); progress.start(); + + const excludeRegexp = exclude ? new RegExp(exclude.join("|")) : undefined; + const result = await extractAll( paths, lang, progress, ttagOverrideOpts, - ttagRcOpts + ttagRcOpts, + excludeRegexp ); fs.writeFileSync(output, result); progress.succeed(`[ttag] translations extracted to ${output}`); diff --git a/src/index.ts b/src/index.ts index b86f01b..5f8631f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -84,6 +84,11 @@ yargs default: "en", description: "sets default lang (ISO format)" }, + exclude: { + alias: "e", + type: "array", + description: "exclude files or directories from extraction" + }, ...getTtagOptsForYargs() }, argv => { @@ -92,7 +97,8 @@ yargs argv.src, argv.lang, parseTtagPluginOpts(argv), - parseTtagRcOpts() + parseTtagRcOpts(), + argv.exclude // pass the exclude option value to the extract function ); } ) diff --git a/src/lib/extract.ts b/src/lib/extract.ts index 3c72676..2881a3d 100644 --- a/src/lib/extract.ts +++ b/src/lib/extract.ts @@ -18,7 +18,8 @@ export async function extractAll( lang: string, progress: ttagTypes.Progress, overrideOpts?: ttagTypes.TtagOpts, - rcOpts?: ttagTypes.TtagRc + rcOpts?: ttagTypes.TtagRc, + excludeRegexp?: RegExp ): Promise { const tmpFile = tmp.fileSync(); let ttagOpts: ttagTypes.TtagOpts = { @@ -34,6 +35,11 @@ export async function extractAll( } const babelOptions = makeBabelConf(ttagOpts); const transformFn: TransformFn = filepath => { + // exclude paths + if (excludeRegexp && excludeRegexp.test(filepath)) { + return; + } + try { switch (extname(filepath)) { case ".vue": { @@ -116,9 +122,9 @@ export async function extractAll( } progress.fail("Failed to extract translations"); process.exit(1); - return; } }; + await pathsWalk( getWalkingPaths(paths, rcOpts), progress, diff --git a/tests/commands/__snapshots__/test_extract.ts.snap b/tests/commands/__snapshots__/test_extract.ts.snap index 02c1cba..f2c28e8 100644 --- a/tests/commands/__snapshots__/test_extract.ts.snap +++ b/tests/commands/__snapshots__/test_extract.ts.snap @@ -179,6 +179,49 @@ msgstr \\"\\" " `; +exports[`extract with e alias 1`] = ` +"msgid \\"\\" +msgstr \\"\\" +\\"Content-Type: text/plain; charset=utf-8\\\\n\\" +\\"Plural-Forms: nplurals=2; plural=(n!=1);\\\\n\\" + +#: tests/fixtures/extractExcludeTest/test1.js:4 +#, javascript-format +msgid \\"test translation \${ name }\\" +msgstr \\"\\" +" +`; + +exports[`extract with exclude by regexp 1`] = ` +"msgid \\"\\" +msgstr \\"\\" +\\"Content-Type: text/plain; charset=utf-8\\\\n\\" +\\"Plural-Forms: nplurals=2; plural=(n!=1);\\\\n\\" + +#: tests/fixtures/extractExcludeTest/test1.js:4 +#, javascript-format +msgid \\"test translation \${ name }\\" +msgstr \\"\\" +" +`; + +exports[`extract with excludePaths 1`] = ` +"msgid \\"\\" +msgstr \\"\\" +\\"Content-Type: text/plain; charset=utf-8\\\\n\\" +\\"Plural-Forms: nplurals=2; plural=(n!=1);\\\\n\\" + +#: tests/fixtures/extractExcludeTest/test1.js:4 +#, javascript-format +msgid \\"test translation \${ name }\\" +msgstr \\"\\" + +#: tests/fixtures/extractExcludeTest/excluded/test2.js:4 +msgid \\"test translation 2 \${ name }\\" +msgstr \\"\\" +" +`; + exports[`should extract in the alphabetical order (sortByMsgid) 1`] = ` "msgid \\"\\" msgstr \\"\\" diff --git a/tests/commands/test_extract.ts b/tests/commands/test_extract.ts index 2376050..c3edeb3 100644 --- a/tests/commands/test_extract.ts +++ b/tests/commands/test_extract.ts @@ -17,6 +17,10 @@ const sveltePath = path.resolve( __dirname, "../fixtures/testSvelteParse.svelte" ); +const extractExcludePath = path.resolve( + __dirname, + "../fixtures/extractExcludeTest" +); const globalFn = path.resolve(__dirname, "../fixtures/globalFunc.js"); const tsPath = path.resolve(__dirname, "../fixtures/tSParse.ts"); const tsChaning = path.resolve(__dirname, "../fixtures/tsOptionalChaning.ts"); @@ -132,3 +136,27 @@ test("extract from ts with const enum", () => { const result = fs.readFileSync(potPath).toString(); expect(result).toMatchSnapshot(); }); + +test("extract with excludePaths", () => { + execSync( + `ts-node src/index.ts extract --exclude="nested" -o ${potPath} ${extractExcludePath}` + ); + const result = fs.readFileSync(potPath).toString(); + expect(result).toMatchSnapshot(); +}); + +test("extract with exclude by regexp", () => { + execSync( + `ts-node src/index.ts extract --exclude="nested|excluded" -o ${potPath} ${extractExcludePath}` + ); + const result = fs.readFileSync(potPath).toString(); + expect(result).toMatchSnapshot(); +}); + +test("extract with e alias", () => { + execSync( + `ts-node src/index.ts extract -e "nested" "excluded" -o ${potPath} ${extractExcludePath}` + ); + const result = fs.readFileSync(potPath).toString(); + expect(result).toMatchSnapshot(); +}); diff --git a/tests/fixtures/extractExcludeTest/excluded/nested/test2.js b/tests/fixtures/extractExcludeTest/excluded/nested/test2.js new file mode 100644 index 0000000..9d2e291 --- /dev/null +++ b/tests/fixtures/extractExcludeTest/excluded/nested/test2.js @@ -0,0 +1,4 @@ +import { t, c } from 'ttag'; + +const name = 'Mike'; +t`test translation 2 ${name}`; \ No newline at end of file diff --git a/tests/fixtures/extractExcludeTest/excluded/test2.js b/tests/fixtures/extractExcludeTest/excluded/test2.js new file mode 100644 index 0000000..9d2e291 --- /dev/null +++ b/tests/fixtures/extractExcludeTest/excluded/test2.js @@ -0,0 +1,4 @@ +import { t, c } from 'ttag'; + +const name = 'Mike'; +t`test translation 2 ${name}`; \ No newline at end of file diff --git a/tests/fixtures/extractExcludeTest/nested/test2.js b/tests/fixtures/extractExcludeTest/nested/test2.js new file mode 100644 index 0000000..9d2e291 --- /dev/null +++ b/tests/fixtures/extractExcludeTest/nested/test2.js @@ -0,0 +1,4 @@ +import { t, c } from 'ttag'; + +const name = 'Mike'; +t`test translation 2 ${name}`; \ No newline at end of file diff --git a/tests/fixtures/extractExcludeTest/test.xml b/tests/fixtures/extractExcludeTest/test.xml new file mode 100644 index 0000000..f141244 --- /dev/null +++ b/tests/fixtures/extractExcludeTest/test.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/tests/fixtures/extractExcludeTest/test1.js b/tests/fixtures/extractExcludeTest/test1.js new file mode 100644 index 0000000..327b6e0 --- /dev/null +++ b/tests/fixtures/extractExcludeTest/test1.js @@ -0,0 +1,4 @@ +import { t } from 'ttag'; + +const name = 'Mike'; +t`test translation ${name}`;