{"version":3,"sources":["../../../packages/tools/gulp-ps-resjson/index.ts"],"names":[],"mappings":"","file":"index.d.ts","sourcesContent":["'use strict';\r\n\r\nimport { Buffer } from 'buffer';\r\nimport Path from 'path';\r\nimport pluginError from 'plugin-error';\r\nimport through2 from 'through2';\r\nimport Vinyl from 'vinyl';\r\nimport * as util from '../utilities';\r\n\r\nconst PLUGIN_NAME = 'gulp-ps-resjson';\r\n\r\ninterface Options {\r\n    /**\r\n     * specify the resource name.\r\n     */\r\n    resourceName: string;\r\n\r\n    /**\r\n     * specify the conversion is Localized file with folder offset.\r\n     */\r\n    localeOffset?: number;\r\n}\r\n\r\nfunction gulpPsResjson(options: Options) {\r\n    // first file is not loc file but original strings.resjson\r\n    // if there is any missing property, add the property to output psd1 file.\r\n    let masterEnglish = true;\r\n    let masterData = {};\r\n\r\n    // override options settings if not specified.\r\n    options = options || <Options>{};\r\n    if (typeof options.localeOffset !== 'number') {\r\n        options.localeOffset = 1;\r\n    }\r\n\r\n    return through2.obj(\r\n        /**\r\n         * Transform\r\n         */\r\n        function (file, encoding, callback) {\r\n            let error = null;\r\n            try {\r\n                if (file.isNull()) {\r\n                    // nothing to do\r\n                    return callback(null, file);\r\n                }\r\n\r\n                if (file.isStream()) {\r\n                    // file.contents is a Stream - https://nodejs.org/api/stream.html\r\n                    this.emit('error', new pluginError(PLUGIN_NAME, 'Streams not supported!'));\r\n                    return callback(null, file);\r\n                } else if (file.isBuffer()) {\r\n                    let nestedObject = null;\r\n                    const lines = [];\r\n                    const prefix = options.resourceName + '_PowerShell_';\r\n                    let content = file.contents.toString('utf8');\r\n                    // Remove comments, /* multiline comment */ and // one line comment and \"//\": \"JSON element comment\"\r\n                    content = content.replace(/(\\/\\*([^*]|[\\n]|(\\*+([^*/]|[\\n])))*\\*\\/+)|( +\\/\\/.*)|(  +\\\"\\/\\/\\\".*)/g, '');\r\n                    const dataObject = JSON.parse(content);\r\n                    if (dataObject[options.resourceName] && dataObject[options.resourceName]['PowerShell']) {\r\n                        // if nested format, use nestedObject.\r\n                        nestedObject = dataObject[options.resourceName]['PowerShell'];\r\n                    }\r\n\r\n                    const path = Path.parse(file.path);\r\n                    const segments = path.dir.split('\\\\');\r\n                    if (masterEnglish) {\r\n                        // collect master data, no output from the first file.\r\n                        if (nestedObject) {\r\n                            masterData = nestedObject;\r\n                        } else {\r\n                            const keys = Object.keys(dataObject);\r\n                            for (const key of keys) {\r\n                                if (key.startsWith(prefix)) {\r\n                                    const name = key.substring(prefix.length);\r\n                                    masterData[name] = dataObject[key];\r\n                                }\r\n                            }\r\n                        }\r\n\r\n                        masterEnglish = false;\r\n                        segments.push('en-US');\r\n                    }\r\n\r\n                    const fullDir = segments.join('\\\\');\r\n                    segments.length--;\r\n                    const baseDir = segments.join('\\\\');\r\n\r\n                    const copyData = { ...{}, ...masterData };\r\n                    if (fullDir.endsWith('en-US')) {\r\n                        // override en-US string by current strings.resjson\r\n                        const names = Object.keys(masterData);\r\n                        for (const name of names) {\r\n                            lines.push(name + '=' + masterData[name]);\r\n                        }\r\n                    } else {\r\n                        if (nestedObject) {\r\n                            const names2 = Object.keys(nestedObject);\r\n                            for (const name of names2) {\r\n                                lines.push(name + '=' + nestedObject[name]);\r\n                                if (copyData[name]) {\r\n                                    delete copyData[name];\r\n                                }\r\n                            }\r\n                        } else {\r\n                            const keys = Object.keys(dataObject);\r\n                            for (const key of keys) {\r\n                                if (key.startsWith(prefix)) {\r\n                                    const name = key.substring(prefix.length);\r\n                                    lines.push(name + '=' + dataObject[key]);\r\n                                    if (copyData[name]) {\r\n                                        delete copyData[name];\r\n                                    }\r\n                                }\r\n                            }\r\n                        }\r\n\r\n                        // if there is any missing data, add into the lines.\r\n                        // when changed a string but not changed the key, it needs to wait for localization\r\n                        // build to update locale language strings.\r\n                        const names = Object.keys(copyData);\r\n                        for (const name of names) {\r\n                            lines.push(name + '=' + copyData[name]);\r\n                        }\r\n                    }\r\n\r\n                    if (lines.length > 0) {\r\n                        // if there is no data, don't create locale file.\r\n\r\n                        // validate the folder name once again. It could happen when different folder structure is used in future.\r\n                        const checkSegments = fullDir.split('\\\\');\r\n                        const locale = checkSegments[checkSegments.length - 1];\r\n                        // eslint-disable-next-line max-len\r\n                        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'];\r\n                        if (localeList.indexOf(locale) < 0 || fullDir !== `${baseDir}\\\\${locale}`) {\r\n                            throw new Error(`Output folder includes a wrong locale name. ${locale}`);\r\n                        }\r\n\r\n                        content = 'ConvertFrom-StringData @\\'\\r\\n' + lines.join('\\r\\n') + '\\r\\n\\'@\\r\\n';\r\n                        const jsonFile = new Vinyl({\r\n                            cwd: '/',\r\n                            base: baseDir,\r\n                            path: fullDir + '\\\\' + path.name + '.psd1',\r\n                            contents: Buffer.from(content, 'utf8')\r\n                        });\r\n                        this.push(jsonFile);\r\n                    }\r\n                }\r\n            } catch (e) {\r\n                error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?\r\n                    util.extendError(new pluginError({ plugin: PLUGIN_NAME, message: e.message }), e) : e;\r\n            }\r\n\r\n            callback(error);\r\n        });\r\n}\r\n\r\nmodule.exports = gulpPsResjson;\r\n"]}