UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

140 lines (138 loc) 6.77 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const buffer_1 = require("buffer"); const path_1 = tslib_1.__importDefault(require("path")); const plugin_error_1 = tslib_1.__importDefault(require("plugin-error")); const through2_1 = tslib_1.__importDefault(require("through2")); const vinyl_1 = tslib_1.__importDefault(require("vinyl")); const util = tslib_1.__importStar(require("../utilities")); const PLUGIN_NAME = 'gulp-ps-resjson'; function gulpPsResjson(options) { // first file is not loc file but original strings.resjson // if there is any missing property, add the property to output psd1 file. let masterEnglish = true; let masterData = {}; // override options settings if not specified. options = options || {}; if (typeof options.localeOffset !== 'number') { options.localeOffset = 1; } return through2_1.default.obj( /** * Transform */ function (file, encoding, callback) { let error = null; try { if (file.isNull()) { // nothing to do return callback(null, file); } if (file.isStream()) { // file.contents is a Stream - https://nodejs.org/api/stream.html this.emit('error', new plugin_error_1.default(PLUGIN_NAME, 'Streams not supported!')); return callback(null, file); } else if (file.isBuffer()) { let nestedObject = null; const lines = []; const prefix = options.resourceName + '_PowerShell_'; let content = file.contents.toString('utf8'); // Remove comments, /* multiline comment */ and // one line comment and "//": "JSON element comment" content = content.replace(/(\/\*([^*]|[\n]|(\*+([^*/]|[\n])))*\*\/+)|( +\/\/.*)|( +\"\/\/\".*)/g, ''); const dataObject = JSON.parse(content); if (dataObject[options.resourceName] && dataObject[options.resourceName]['PowerShell']) { // if nested format, use nestedObject. nestedObject = dataObject[options.resourceName]['PowerShell']; } const path = path_1.default.parse(file.path); const segments = path.dir.split('\\'); if (masterEnglish) { // collect master data, no output from the first file. if (nestedObject) { masterData = nestedObject; } else { const keys = Object.keys(dataObject); for (const key of keys) { if (key.startsWith(prefix)) { const name = key.substring(prefix.length); masterData[name] = dataObject[key]; } } } masterEnglish = false; segments.push('en-US'); } const fullDir = segments.join('\\'); segments.length--; const baseDir = segments.join('\\'); const copyData = { ...{}, ...masterData }; if (fullDir.endsWith('en-US')) { // override en-US string by current strings.resjson const names = Object.keys(masterData); for (const name of names) { lines.push(name + '=' + masterData[name]); } } else { if (nestedObject) { const names2 = Object.keys(nestedObject); for (const name of names2) { lines.push(name + '=' + nestedObject[name]); if (copyData[name]) { delete copyData[name]; } } } else { const keys = Object.keys(dataObject); for (const key of keys) { if (key.startsWith(prefix)) { const name = key.substring(prefix.length); lines.push(name + '=' + dataObject[key]); if (copyData[name]) { delete copyData[name]; } } } } // if there is any missing data, add into the lines. // when changed a string but not changed the key, it needs to wait for localization // build to update locale language strings. const names = Object.keys(copyData); for (const name of names) { lines.push(name + '=' + copyData[name]); } } if (lines.length > 0) { // if there is no data, don't create locale file. // validate the folder name once again. It could happen when different folder structure is used in future. const checkSegments = fullDir.split('\\'); const locale = checkSegments[checkSegments.length - 1]; // eslint-disable-next-line max-len const localeList = ['en-US', 'cs-CZ', 'de-DE', 'es-ES', 'fr-FR', 'hu-HU', 'id-ID', 'it-IT', 'ja-JP', 'ko-KR', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sv-SE', 'tr-TR', 'zh-CN', 'zh-TW']; if (localeList.indexOf(locale) < 0 || fullDir !== `${baseDir}\\${locale}`) { throw new Error(`Output folder includes a wrong locale name. ${locale}`); } content = 'ConvertFrom-StringData @\'\r\n' + lines.join('\r\n') + '\r\n\'@\r\n'; const jsonFile = new vinyl_1.default({ cwd: '/', base: baseDir, path: fullDir + '\\' + path.name + '.psd1', contents: buffer_1.Buffer.from(content, 'utf8') }); this.push(jsonFile); } } } catch (e) { error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ? util.extendError(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: e.message }), e) : e; } callback(error); }); } module.exports = gulpPsResjson; //# sourceMappingURL=index.js.map