mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-01 06:13:59 +00:00
feat: separate remove-unused command
This commit is contained in:
@@ -1,5 +1,14 @@
|
|||||||
const defaultLanguage = 'en-US';
|
const defaultLanguage = 'en-US';
|
||||||
|
|
||||||
|
/** @typedef {{ extensions: string[], directories: string[]}} ExtractConfig
|
||||||
|
|
||||||
|
/** @type {ExtractConfig} */
|
||||||
|
const defaultExtractConfig = {
|
||||||
|
extensions: ['.js', '.ts', '.svelte'],
|
||||||
|
directories: ['app', 'packages/web'],
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
defaultLanguage,
|
defaultLanguage,
|
||||||
|
defaultExtractConfig,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -72,8 +72,6 @@ async function extractAllTranslations(directories, extensions, options = {}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Total translations found: ${Object.keys(allTranslations).length}`);
|
|
||||||
|
|
||||||
return allTranslations;
|
return allTranslations;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error extracting translations:', error);
|
console.error('Error extracting translations:', error);
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ const { program } = require('commander');
|
|||||||
const {
|
const {
|
||||||
resolveDirs,
|
resolveDirs,
|
||||||
resolveExtensions,
|
resolveExtensions,
|
||||||
resolveFile,
|
|
||||||
ensureFileDirExists,
|
|
||||||
getTranslationChanges,
|
getTranslationChanges,
|
||||||
setLanguageTranslations,
|
setLanguageTranslations,
|
||||||
getAllNonDefaultLanguages,
|
getAllNonDefaultLanguages,
|
||||||
@@ -14,26 +12,20 @@ const {
|
|||||||
} = require('./helpers');
|
} = require('./helpers');
|
||||||
const { extractAllTranslations } = require('./extract');
|
const { extractAllTranslations } = require('./extract');
|
||||||
const { getMissingTranslations } = require('./addMissing');
|
const { getMissingTranslations } = require('./addMissing');
|
||||||
const { defaultLanguage } = require('./constants');
|
const { defaultLanguage, defaultExtractConfig } = require('./constants');
|
||||||
|
const { removeUnusedAllTranslations, removeUnusedForSignelLanguage } = require('./removeUnused');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {{ extensions: string[], directories: string[]}} ExtractConfig
|
* @typedef {import('./constants').ExtractConfig & { verbose?: boolean, removeUnused?: boolean }} ExtractOptions
|
||||||
* @typedef {ExtractConfig & { verbose?: boolean, removeUnused?: boolean }} ExtractOptions
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** @type {ExtractConfig} */
|
|
||||||
const defaultConfig = {
|
|
||||||
extensions: ['.js', '.ts', '.svelte'],
|
|
||||||
directories: ['app', 'packages/web'],
|
|
||||||
};
|
|
||||||
|
|
||||||
program.name('dbgate-translations-cli').description('CLI tool for managing translation').version('1.0.0');
|
program.name('dbgate-translations-cli').description('CLI tool for managing translation').version('1.0.0');
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('extract')
|
.command('extract')
|
||||||
.description('Extract translation keys from source files')
|
.description('Extract translation keys from source files')
|
||||||
.option('-d, --directories <directories...>', 'directories to search', defaultConfig.directories)
|
.option('-d, --directories <directories...>', 'directories to search', defaultExtractConfig.directories)
|
||||||
.option('-e, --extensions <extensions...>', 'file extensions to process', defaultConfig.extensions)
|
.option('-e, --extensions <extensions...>', 'file extensions to process', defaultExtractConfig.extensions)
|
||||||
.option('-r, --removeUnused', 'Remove unused keys from the output file')
|
.option('-r, --removeUnused', 'Remove unused keys from the output file')
|
||||||
.option('-v, --verbose', 'verbose mode')
|
.option('-v, --verbose', 'verbose mode')
|
||||||
.action(async (/** @type {ExtractOptions} */ options) => {
|
.action(async (/** @type {ExtractOptions} */ options) => {
|
||||||
@@ -134,4 +126,24 @@ program
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('remove-unused')
|
||||||
|
.description('Remove unused keys from the translation files')
|
||||||
|
.option('-t, --target <target>', 'language to add missing translations to', ALL_LANGUAGES)
|
||||||
|
.action(async options => {
|
||||||
|
try {
|
||||||
|
const { target } = options;
|
||||||
|
if (target === ALL_LANGUAGES) {
|
||||||
|
console.log('Removing unused keys from all languages\n');
|
||||||
|
await removeUnusedAllTranslations();
|
||||||
|
} else {
|
||||||
|
await removeUnusedForSignelLanguage(target);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
console.error('Error during add-missing:', error.message);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = { program };
|
module.exports = { program };
|
||||||
|
|||||||
46
common/translations-cli/removeUnused.js
Normal file
46
common/translations-cli/removeUnused.js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
// @ts-check
|
||||||
|
const { defaultExtractConfig } = require('./constants');
|
||||||
|
const { extractAllTranslations } = require('./extract');
|
||||||
|
const { getLanguageTranslations, getAllLanguages, setLanguageTranslations } = require('./helpers');
|
||||||
|
|
||||||
|
const { directories, extensions } = defaultExtractConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} language
|
||||||
|
* @param {Record<string, string>} source
|
||||||
|
*/
|
||||||
|
function getUsedTranslations(language, source) {
|
||||||
|
const languageTranslations = getLanguageTranslations(language);
|
||||||
|
|
||||||
|
for (const key in languageTranslations) {
|
||||||
|
if (!(key in source)) {
|
||||||
|
delete languageTranslations[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return languageTranslations;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeUnusedAllTranslations() {
|
||||||
|
const source = await extractAllTranslations(directories, extensions);
|
||||||
|
const languages = getAllLanguages();
|
||||||
|
|
||||||
|
for (const language of languages) {
|
||||||
|
const newTranslations = getUsedTranslations(language, source);
|
||||||
|
setLanguageTranslations(language, newTranslations);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} language
|
||||||
|
*/
|
||||||
|
async function removeUnusedForSignelLanguage(language) {
|
||||||
|
const source = await extractAllTranslations(directories, extensions);
|
||||||
|
const newTranslations = getUsedTranslations(language, source);
|
||||||
|
setLanguageTranslations(language, newTranslations);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
removeUnusedAllTranslations,
|
||||||
|
removeUnusedForSignelLanguage,
|
||||||
|
};
|
||||||
@@ -69,7 +69,8 @@
|
|||||||
"workflows": "node common/processWorkflows.js",
|
"workflows": "node common/processWorkflows.js",
|
||||||
"cy:open": "cd e2e-tests && yarn cy:open",
|
"cy:open": "cd e2e-tests && yarn cy:open",
|
||||||
"translations:extract": "node common/translations-cli/index.js extract",
|
"translations:extract": "node common/translations-cli/index.js extract",
|
||||||
"translations:add-missing": "node common/translations-cli/index.js add-missing"
|
"translations:add-missing": "node common/translations-cli/index.js add-missing",
|
||||||
|
"translations:remove-unused": "node common/translations-cli/index.js remove-unused"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"concurrently": "^5.1.0",
|
"concurrently": "^5.1.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user