Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exit code if local foun #53

Merged
merged 13 commits into from
May 31, 2024
5 changes: 5 additions & 0 deletions .changeset/giant-emus-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"unused-i18n": patch
---

fix some bug
34 changes: 19 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export const processTranslations = async ({
let pathUnusedLocalesCount = 0

srcPath.forEach((pathEntry) => {
const ignorePathExists = config.ignorePaths?.some(
(ignorePath) => ignorePath === pathEntry
const ignorePathExists = config.ignorePaths?.some((ignorePath) =>
pathEntry.includes(ignorePath)
)
if (ignorePathExists) return
const files = searchFilesRecursively({
Expand Down Expand Up @@ -61,7 +61,10 @@ export const processTranslations = async ({
allExtractedTranslations = [...new Set(allExtractedTranslations)].sort()

const localeFilePath = `${localPath}/${localesNames}.${localesExtensions}`
if (fs.existsSync(localeFilePath)) {
const ignorePathExists = config.ignorePaths?.some((ignorePath) =>
localeFilePath.includes(ignorePath)
)
if (fs.existsSync(localeFilePath) && !ignorePathExists) {
console.log(`${localeFilePath}...`)

const localLines = fs
Expand All @@ -87,12 +90,14 @@ export const processTranslations = async ({

const message = missingTranslations.length
? `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`
: undefined

unusedLocalesCountByPath.push({
path: localPath,
messages: message,
})
if (message) {
unusedLocalesCountByPath.push({
path: localPath,
messages: message,
})
}

// Remove the unused locale keys
action === 'remove'
Expand All @@ -101,20 +106,19 @@ export const processTranslations = async ({
missingTranslations: missingTranslations,
})
: null
} else {
const warningMessage = `\x1b[31mLocale file not found: ${localeFilePath}\x1b[0m`

unusedLocalesCountByPath.push({
path: localPath,
warning: warningMessage,
})
}
})
const endTime = performance.now()

summary({ unusedLocalesCountByPath, totalUnusedLocales })
console.log(
`\x1b[38;2;128;128;128mDuration : ${(endTime - startTime).toFixed(
0
)}ms\x1b[0m`
)

// Check if totalUnusedLocales is greater than 0
if (totalUnusedLocales > 0) {
process.exit(1)
}
}
4 changes: 0 additions & 4 deletions src/utils/summary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SummaryArgs } from '../types'
import { exit } from 'process'

export const summary = ({
unusedLocalesCountByPath,
Expand All @@ -13,7 +12,4 @@ export const summary = ({
})

console.log(`Total unused locales: \x1b[33m${totalUnusedLocales}\x1b[0m`)
if (unusedLocalesCountByPath.length === 1) {
exit(1)
}
}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const external = (id: string) => {
id.endsWith('fs') ||
id.endsWith('path') ||
id.endsWith('perf_hooks') ||
id.endsWith('process') ||
id.endsWith('process') ||
id.endsWith('commander')
) {
return true
Expand Down
Loading