From ea4d481cff047ae09a9a9f2c517f8012aa0108a9 Mon Sep 17 00:00:00 2001 From: songjiang Date: Tue, 7 Mar 2023 09:15:47 +0800 Subject: [PATCH] Improve CLI Script on adding encoding source string feature. --- cli/collect-source-bundle.js | 35 ++++++++++++++++++++--- cli/translation-service.js | 54 ------------------------------------ package.client.json | 11 +++++++- package.json | 11 +++++++- 4 files changed, 51 insertions(+), 60 deletions(-) delete mode 100644 cli/translation-service.js diff --git a/cli/collect-source-bundle.js b/cli/collect-source-bundle.js index 11a5c329b..a1ea68016 100755 --- a/cli/collect-source-bundle.js +++ b/cli/collect-source-bundle.js @@ -1,7 +1,7 @@ #!/usr/bin/env node /* - * Copyright 2019-2022 VMware, Inc. + * Copyright 2019-2023 VMware, Inc. * SPDX-License-Identifier: EPL-2.0 */ @@ -13,7 +13,7 @@ const ts = require('typescript'); const ArgumentParser = require('argparse').ArgumentParser; class CollectSourceBundle { - constructor(logger, vipService, vipConfig) { + constructor(logger, vipService, vipConfig, cliOptions) { this.logger = logger; this.vipService = vipService; this.vipConfig = vipConfig; @@ -21,6 +21,7 @@ class CollectSourceBundle { total: 0, current: [] }; + this.cliOptions = cliOptions; } isSourceBundle(file) { return file.match(/l10n\.(ts|js)$/); @@ -30,16 +31,25 @@ class CollectSourceBundle { return this.vipService.collectSources(sourceSet); } convertDataForVIP(data) { + const isEncode = this.cliOptions.encode ? true : false; + const sourceFormat = isEncode ? this.cliOptions.encode + ",STRING" : "STRING"; var sourceSet = []; for (let i in data) { + const source = isEncode ? this.encodeString(data[i]) : data[i]; sourceSet.push({ commentForSource: '', key: i, - source: data[i] + source, + sourceFormat, }); } return sourceSet; } + encodeString(str) { + var buff = Buffer.from(str); + var res = buff.toString(this.cliOptions.encode); + return res; + } throttleCollectSources(dataObject, stepSize, file) { var data = this.convertDataForVIP(dataObject); if (data.length === 0) { @@ -141,6 +151,14 @@ function run() { } ); + parser.addArgument( + ['--encode'], + { + help: 'Encode the resource.', + required: false, + } + ); + parser.addArgument( ['--moduletype'], { @@ -163,7 +181,16 @@ function run() { try { let vipConfig = new VIPConfig(args.host, args.product, args.version, args.component); let vipService = new VIPService(vipConfig, logger, null); - let collectSourceBundle = new CollectSourceBundle(logger, vipService, vipConfig); + let encode = args.encode && args.encode.toUpperCase(); + if (encode && encode !== "BASE64") { + logger.error("The encoding type '" + encode + "' is not supported."); + return; + } + const cliOptions = { + encode: encode, + }; + + let collectSourceBundle = new CollectSourceBundle(logger, vipService, vipConfig, cliOptions); walkDirectory(args.source_dir, function (files) { logger.debug('walkDirectory resolve files', files); files.forEach(function (file) { diff --git a/cli/translation-service.js b/cli/translation-service.js deleted file mode 100644 index 2c9ee6da8..000000000 --- a/cli/translation-service.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2019-2021 VMware, Inc. - * SPDX-License-Identifier: EPL-2.0 - */ -const axios = require('axios'); -const debug = require('debug')('translate-service'); - -class TranslationService { - constructor(host, token, product, version, component) { - this.product = product; - this.version = version; - this.component = component; - this.host = host; - this.token = token; - } - - collectSource(key, source, pid = 'main thread') { - var locale = 'en_US'; - - debug(`[pid: ${pid}] calling translate service for the locale: ${locale} and the key ${key}`); - - return this.send(this.token, key, locale, source) - .then((res) => { - debug(`[pid: ${pid} locale: ${locale}, key: ${key}] Translate API successfully returned a translation.`); - return res.data; - }).catch((e) => { - console.error('Got an error when send with token: ', e && e.message || e); - return Promise.reject(e && e.message || e); - }); - } - - send(token, key, locale, source) { - let headers = { - 'Content-Type': 'application/json', - } - - if (token) { - headers['csp-auth-token'] = token; - } - - return axios.post(`${this.host}/i18n/api/v2/translation/products/${this.product}/versions/${this.version}/locales/${locale}/components/${this.component}/keys/${key}`, - source, - { - headers, - params: { - collectSource: true, - pseudo: false, - } - } - ); - } -} - -module.exports = TranslationService; diff --git a/package.client.json b/package.client.json index 8400e1687..c5a65b79a 100644 --- a/package.client.json +++ b/package.client.json @@ -1,6 +1,6 @@ { "name": "@vip/vip-core-sdk", - "version": "0.5.5", + "version": "0.5.6", "publishConfig": { "registry": "" }, @@ -54,6 +54,15 @@ "webpack-cli": "^3.3.0" }, "changelogHistory": [ + { + "date": "02/22/23", + "version": "0.5.6", + "notes": [ + { + "description": "Improve CLI Script on adding encoding source string feature." + } + ] + }, { "date": "08/30/22", "version": "0.5.5", diff --git a/package.json b/package.json index 6e7877598..a3ff04fa8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vip/vip-core-sdk-server", - "version": "0.5.5", + "version": "0.5.6", "publishConfig": { "registry": "" }, @@ -54,6 +54,15 @@ "webpack-cli": "^3.3.0" }, "changelogHistory": [ + { + "date": "02/22/23", + "version": "0.5.6", + "notes": [ + { + "description": "Improve CLI Script on adding encoding source string feature." + } + ] + }, { "date": "08/30/22", "version": "0.5.5",