Skip to content

Commit

Permalink
fix: exit code if local foun (#53)
Browse files Browse the repository at this point in the history
* 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: 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] <github-actions[bot]@users.noreply.github.com>

* 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] <github-actions[bot]@users.noreply.github.com>

* fix: some bugs

* fix: exit code if local found

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored May 31, 2024
1 parent c6fc867 commit 26dafa7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
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

0 comments on commit 26dafa7

Please sign in to comment.