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'], - }, - }, -})