From 6f1f435d4ba7f6b8ce6b4b46e232f087aa46f817 Mon Sep 17 00:00:00 2001 From: Levende Date: Fri, 31 May 2024 11:26:11 +0200 Subject: [PATCH] fix: some bugs on ignore path (#51) * fit: fix github workflow * fit: fix github workflow * fit: fix github workflow * fix: replace color library by native color to console log * fix: readme file * fix: build setup with vite * fix: build setup with vite * fix: build setup with vite * fix: some bugs * fix: add vite (#47) * fit: fix github workflow * fit: fix github workflow * fit: fix github workflow * fix: replace color library by native color to console log * fix: readme file * fix: build setup with vite * chore: release (#48) Co-authored-by: github-actions[bot] * fix: build setup with vite (#49) * fit: fix github workflow * fit: fix github workflow * fit: fix github workflow * fix: replace color library by native color to console log * fix: readme file * fix: build setup with vite * fix: build setup with vite * fix: build setup with vite * chore: release (#50) Co-authored-by: github-actions[bot] --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] --- .changeset/early-years-divide.md | 5 +++++ README.md | 15 ++++++--------- src/index.ts | 9 ++++++--- src/utils/summary.ts | 4 ++++ vite.config.ts | 1 + vitest.config.ts | 12 ------------ 6 files changed, 22 insertions(+), 24 deletions(-) create mode 100644 .changeset/early-years-divide.md delete mode 100644 vitest.config.ts diff --git a/.changeset/early-years-divide.md b/.changeset/early-years-divide.md new file mode 100644 index 0000000..8146fdc --- /dev/null +++ b/.changeset/early-years-divide.md @@ -0,0 +1,5 @@ +--- +"unused-i18n": patch +--- + +Fix some bugs diff --git a/README.md b/README.md index 66325b5..df162de 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Simplifies managing and cleaning up unused translation keys in localization file - Analyzes source files to identify used and unused translation keys. - Supports multiple scoped translation functions. - Can display or remove unused translation keys. -- Configurable through a JSON, JS, or TS config file. +- Configurable through a JSON, CJS, or JS config file. ## Installation @@ -25,10 +25,8 @@ npm install -D unused-i18n Create a unused-i18n.config.json or unused-i18n.config.js file in the root of your project. Here's an example configuration: -```ts -import type { Config } from 'unused-i18n' - -const config = { +```cjs +module.exports = { paths: [ { srcPath: ['src/pages/products'], @@ -40,9 +38,7 @@ const config = { scopedNames: ['scopedT', 'scopedTOne'], ignorePaths: ['src/pages/products/ignoreThisFolder'], excludeKey: ['someKey'], -} satisfies Config - -export default config +} ``` | Option | Type | Default | Required | Description | @@ -118,6 +114,7 @@ Contributions are welcome! Please open an issue or submit a pull request if you Acknowledgements -- [Rollup](https://rollupjs.org/) - Module bundler used in this project. +- [Vite](https://vitejs.dev/) - Next Generation Frontend Tooling. - [TypeScript](https://www.typescriptlang.org/) - Typed JavaScript used in this project. - [Vitest](https://vitest.dev/guide/cli) - Testing framework used in this project. +- [Commander](https://github.com/tj/commander.js#readme) - Node.js command-line interfaces. diff --git a/src/index.ts b/src/index.ts index 6f2e3d3..f6f3a75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,10 @@ export const processTranslations = async ({ let pathUnusedLocalesCount = 0 srcPath.forEach((pathEntry) => { - if (config.ignorePaths && config.ignorePaths.includes(pathEntry)) return + const ignorePathExists = config.ignorePaths?.some( + (ignorePath) => ignorePath === pathEntry + ) + if (ignorePathExists) return const files = searchFilesRecursively({ excludePatterns, regex: /use-i18n/, @@ -83,8 +86,8 @@ export const processTranslations = async ({ .join('\n') const message = missingTranslations.length - ? `Missing translations for \x1b[33m${localeFilePath}\x1b[0m : \x1b[31m${pathUnusedLocalesCount} \n${formattedMissingTranslations}\x1b[0m` - : `\x1b[32mNo missing translations for \x1b[33m${localeFilePath}\x1b[0m\x1b[0m` + ? `Unused translations in \x1b[33m${localeFilePath}\x1b[0m : \x1b[31m${pathUnusedLocalesCount} \n${formattedMissingTranslations}\x1b[0m` + : `\x1b[32mNo unused translations in \x1b[33m${localeFilePath}\x1b[0m\x1b[0m` unusedLocalesCountByPath.push({ path: localPath, diff --git a/src/utils/summary.ts b/src/utils/summary.ts index 8132e34..53af59b 100644 --- a/src/utils/summary.ts +++ b/src/utils/summary.ts @@ -1,4 +1,5 @@ import { SummaryArgs } from '../types' +import { exit } from 'process' export const summary = ({ unusedLocalesCountByPath, @@ -12,4 +13,7 @@ export const summary = ({ }) console.log(`Total unused locales: \x1b[33m${totalUnusedLocales}\x1b[0m`) + if (unusedLocalesCountByPath.length === 1) { + exit(1) + } } diff --git a/vite.config.ts b/vite.config.ts index a3bcca0..0044cd1 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,6 +6,7 @@ const external = (id: string) => { id.endsWith('fs') || id.endsWith('path') || id.endsWith('perf_hooks') || + id.endsWith('process') || id.endsWith('commander') ) { return true diff --git a/vitest.config.ts b/vitest.config.ts deleted file mode 100644 index 0ecfcab..0000000 --- a/vitest.config.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { defineConfig } from 'vitest/config' - -export default defineConfig({ - test: { - globals: true, - environment: 'node', - coverage: { - provider: 'istanbul', - reporter: ['text', 'json', 'html'], - }, - }, -})