From 71af6549498eadf6845c9ebe57e8fff0d41338e6 Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Thu, 19 Sep 2024 07:51:18 +0200 Subject: [PATCH] download/sync: introduce `--languages lng1,lng2` argument (#96) --- CHANGELOG.md | 4 ++++ bin/locize | 6 ++++++ download.js | 20 +++++++++++++++++--- sync.js | 9 +++++++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f256a7..4e54fd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. Project versioning adheres to [Semantic Versioning](http://semver.org/). Change log format is based on [Keep a Changelog](http://keepachangelog.com/). +## [8.1.0](https://github.com/locize/locize-cli/compare/v8.0.2...v8.1.0) - 2024-09-19 + +- introduce `--languages lng1,lng2` argument (#96) + ## [8.0.2](https://github.com/locize/locize-cli/compare/v8.0.1...v8.0.2) - 2024-08-14 - update some dependencies to address #95 and #94 diff --git a/bin/locize b/bin/locize index 2877e95..09a7083 100755 --- a/bin/locize +++ b/bin/locize @@ -227,6 +227,7 @@ program .option('-i, --project-id ', 'The project-id that should be used') .option('-v, --ver ', 'The version that should be targeted (default: latest)') .option('-l, --language ', 'The language that should be targeted') + .option('-ls, --languages ', 'The languages that should be targeted') .option('-n, --namespace ', 'The namespace that should be targeted') .option('-p, --path ', `Specify the path that should be used (default: ${process.cwd()})`, process.cwd()) .option('-g, --get-path ', `Specify the get-path url that should be used (default: ${getPathUrl})`) @@ -253,6 +254,7 @@ program const apiKey = options.apiKey || config.apiKey || process.env.LOCIZE_API_KEY || process.env.LOCIZE_KEY; const language = options.language || config.language || config.lng || process.env.LOCIZE_LANGUAGE || process.env.LOCIZE_LANG || process.env.LOCIZE_LNG; + const languages = options.languages || config.languages || config.lngs || process.env.LOCIZE_LANGUAGES || process.env.LOCIZE_LANGS || process.env.LOCIZE_LNGS; const version = options.ver || config.ver || config.version || process.env.LOCIZE_VERSION || process.env.LOCIZE_VER || 'latest'; @@ -277,6 +279,7 @@ program projectId: projectId, apiPath: url.parse(getPath).protocol + '//' + url.parse(getPath).host, language: language, + languages: languages && languages.split(','), version: version, namespace: namespace, path: options.path, @@ -370,6 +373,7 @@ program .option('-R, --reference-language-only ', 'Check for changes in reference language only. (default: true)', 'true') .option('-t, --compare-modification-time ', 'while comparing the namespace content between local and remote, take the modification time of the local file and the last modified time of the remote namespace into account. (default: false)', 'false') .option('-l, --language ', 'The language that should be targeted') + .option('-ls, --languages ', 'The languages that should be targeted') .option('-n, --namespace ', 'The namespace that should be targeted') .option('-g, --get-path ', `Specify the get-path url that should be used (default: ${getPathUrl})`) .option('-up, --unpublished ', 'Downloads the current (unpublished) translations. This will generate private download costs (default: false)', 'false') @@ -398,6 +402,7 @@ program const version = options.ver || config.ver || config.version || process.env.LOCIZE_VERSION || process.env.LOCIZE_VER || 'latest'; const language = options.language || config.language || config.lng || process.env.LOCIZE_LANGUAGE || process.env.LOCIZE_LANG || process.env.LOCIZE_LNG; + const languages = options.languages || config.languages || config.lngs || process.env.LOCIZE_LANGUAGES || process.env.LOCIZE_LANGS || process.env.LOCIZE_LNGS; const namespace = options.namespace; @@ -436,6 +441,7 @@ program referenceLanguageOnly: referenceLanguageOnly, compareModificationTime: compareModificationTime, language: language, + languages: languages && languages.split(','), namespace: namespace, dry: dry, pathMask: pathMask, diff --git a/download.js b/download.js index f84297d..2b18e06 100644 --- a/download.js +++ b/download.js @@ -151,8 +151,22 @@ const handleError = (err, cb) => { if (cb) cb(err); }; +const filterDownloadsLanguages = (opt, downloads) => { + if (opt.languages) { + downloads = downloads.filter((d) => { + const splitted = d.key.split('/'); + // const p = splitted[d.isPrivate ? 1 : 0]; + // const v = splitted[d.isPrivate ? 2 : 1]; + const l = splitted[d.isPrivate ? 3 : 2]; + const n = splitted[d.isPrivate ? 4 : 3]; + return opt.languages.indexOf(l) > -1 && (!opt.namespace || opt.namespace === n); + }); + } + return downloads; +}; + const filterDownloads = (opt, downloads) => { - if (opt.skipEmpty) return downloads.filter((d) => d.size > 2); + if (opt.skipEmpty) return filterDownloadsLanguages(opt, downloads.filter((d) => d.size > 2)); if (downloads.length < 1) return downloads; const allNamespaces = []; @@ -185,7 +199,7 @@ const filterDownloads = (opt, downloads) => { }); }); }); - return downloads; + return filterDownloadsLanguages(opt, downloads); }; const download = (opt, cb) => { @@ -216,7 +230,7 @@ const download = (opt, cb) => { if (opt.version) { url += '/' + opt.version; - if (opt.language) { + if (!opt.languages && opt.language) { url += '/' + opt.language; if (opt.namespace) { url += '/' + opt.namespace; diff --git a/sync.js b/sync.js index 927c8c4..4a09d38 100644 --- a/sync.js +++ b/sync.js @@ -48,7 +48,7 @@ const getDownloads = (opt, cb) => { if (!res || !res[opt.version]) return handleError(new Error('Nothing found!'), cb); const toDownload = []; - const lngsToCheck = opt.language ? [opt.language] : Object.keys(res[opt.version]); + const lngsToCheck = opt.language ? [opt.language] : (opt.languages && opt.languages.length > 0) ? opt.languages : Object.keys(res[opt.version]); lngsToCheck.forEach((l) => { if (opt.namespaces) { opt.namespaces.forEach((n) => { @@ -212,6 +212,7 @@ const downloadAll = (opt, remoteLanguages, omitRef, manipulate, cb) => { opt.isPrivate = download.isPrivate; if (opt.language && opt.language !== lng && lng !== opt.referenceLanguage) return clb(null); + if (opt.languages && opt.languages.length > 0 && opt.languages.indexOf(lng) < 0 && lng !== opt.referenceLanguage) return clb(null); if (opt.namespace && opt.namespace !== namespace) return clb(null); if (opt.namespaces && opt.namespaces.length > 0 && opt.namespaces.indexOf(namespace) < 0) return clb(null); @@ -317,7 +318,7 @@ const update = (opt, lng, ns, shouldOmit, cb) => { const cleanupLanguages = (opt, remoteLanguages) => { if (opt.pathMask.lastIndexOf(path.sep) < 0) return; const dirs = getDirectories(opt.path).filter((dir) => dir.indexOf('.') !== 0); - if (!opt.language && !opt.namespace && !opt.namespaces) { + if (!opt.language && (!opt.languages || opt.languages.length === 0) && !opt.namespace && !opt.namespaces) { dirs .filter((lng) => { const lMask = `${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}`; @@ -339,6 +340,7 @@ const cleanupLanguages = (opt, remoteLanguages) => { } remoteLanguages.forEach((lng) => { if (opt.language && opt.language !== lng) return; + if (opt.languages && opt.languages.length > 0 && opt.languages.indexOf(lng) < 0) return; const filledLngMask = opt.pathMask.replace(`${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}`, lng); var lngPath; if (filledLngMask.lastIndexOf(path.sep) > 0) { @@ -583,6 +585,9 @@ const sync = (opt, cb) => { if (opt.referenceLanguageOnly && opt.language && opt.referenceLanguage !== opt.language) { opt.referenceLanguage = opt.language; } + if (opt.referenceLanguageOnly && !opt.language && opt.languages && opt.languages.length > 0 && opt.languages.indexOf(opt.referenceLanguage) < 0) { + opt.referenceLanguage = opt.languages[0]; + } if (opt.referenceLanguageOnly) { console.log(colors.grey(`checking local${opt.path !== process.cwd() ? ` (${opt.path})` : ''} only reference language...`));