|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from 'fs' |
| 3 | +import path from 'path' |
| 4 | +import walk from 'walk-sync' |
| 5 | +import program from 'commander' |
| 6 | +import languages from '../../lib/languages.js' |
| 7 | + |
| 8 | +program |
| 9 | + .description( |
| 10 | + `Removes any file in the translations directory that doesn't have a 1-1 mapping with an English file in the content directory` |
| 11 | + ) |
| 12 | + .option('-d, --dry-run', `List the files that will be deleted, but don't remove them).`) |
| 13 | + .parse(process.argv) |
| 14 | + |
| 15 | +const languageDir = Object.keys(languages) |
| 16 | + .filter((language) => !languages[language].wip && language !== 'en') |
| 17 | + .map((language) => languages[language].dir) |
| 18 | + |
| 19 | +main() |
| 20 | + |
| 21 | +async function main() { |
| 22 | + const listOfContentFiles = walk(path.join(process.cwd(), 'content'), { |
| 23 | + includeBasePath: false, |
| 24 | + directories: false, |
| 25 | + }) |
| 26 | + |
| 27 | + const translatedFilePaths = [] |
| 28 | + languageDir.forEach((directory) => { |
| 29 | + const listOfFiles = walk(path.join(directory, 'content'), { |
| 30 | + includeBasePath: true, |
| 31 | + directories: false, |
| 32 | + }).map((path) => path.replace(process.cwd(), '')) |
| 33 | + translatedFilePaths.push(...listOfFiles) |
| 34 | + }) |
| 35 | + |
| 36 | + let outOfSyncFilesCount = 0 |
| 37 | + translatedFilePaths.forEach((translatedFilePath) => { |
| 38 | + const translationRelativePath = translatedFilePath.split('/content/')[1] |
| 39 | + |
| 40 | + // If there is a 1:1 mapping of translated file to english file |
| 41 | + // we're in sync, don't log |
| 42 | + if (listOfContentFiles.includes(translationRelativePath)) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + outOfSyncFilesCount++ |
| 47 | + if (!program.opts().dryRun) { |
| 48 | + fs.unlinkSync(translatedFilePath) |
| 49 | + } else { |
| 50 | + console.log(translatedFilePath) |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + console.log(`Out of sync file size: ${outOfSyncFilesCount}`) |
| 55 | +} |
0 commit comments