UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

117 lines (115 loc) 4.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonMerge = void 0; const fs = require('fs'); const vinyl = require('vinyl'); class JsonMerge { /** * Recursively merges JSON files with the same name and same subPath from the sourceFolders into the JSON files * in the targetFolderPath. * * @example * src/assets/resources/strings/ <- targetFolderPath * strings.json * es/strings.json * pt/strings.json * [ * 'node_modules/@msft-sme/core/dist/assets/resources/strings', * 'node_modules/@msft-sme/ng2/dist/assets/resources/strings' * ] <- sourceFoldersPath * * src/assets/resources/strings/strings.json contents are merged with the contents of * node_modules/@msft-sme/core/dist/assets/resources/strings/strings.json and * node_modules/@msft-ng2/core/dist/assets/resources/strings/strings.json * and the source file is overwritten by the merged content * * @param targetFolderPathRoot The path of the base folder where the destination files are placed. * @param sourceFoldersPathRoot The array of paths to the source folders from where to read the JSON files to merge */ mergeJsonInFolders(targetFolderPathRoot, sourceFoldersPathRoot) { const outputFiles = []; const targetFilesContentMap = {}; const targetFiles = this.getFilePaths(targetFolderPathRoot); targetFiles.forEach((targetFile) => { const relativePath = targetFile.substring(targetFolderPathRoot.length + 1, targetFile.length); targetFilesContentMap[relativePath] = this.readJSON(targetFile); }); sourceFoldersPathRoot.forEach((sourceFolderPathRoot) => { const sourceFiles = this.getFilePaths(sourceFolderPathRoot); sourceFiles.forEach((sourceFile) => { const relativePath = sourceFile.substring(sourceFolderPathRoot.length + 1, sourceFile.length); const sourceJson = this.readJSON(sourceFile); this.mergeJsons(relativePath, sourceJson, targetFilesContentMap); }); }); Object.keys(targetFilesContentMap).forEach(path => { const jsonFile = new vinyl({ cwd: './', path: path, contents: Buffer.from(JSON.stringify(targetFilesContentMap[path]), 'utf8') }); outputFiles.push(jsonFile); }); return outputFiles; } getFilePaths(dir, paths = []) { if (!dir.endsWith('/')) { dir += '/'; } const files = fs.readdirSync(dir); files.forEach(file => { const filePath = dir + file; if (fs.statSync(filePath).isDirectory()) { paths.concat(this.getFilePaths(filePath, paths)); } else { paths.push(dir + file); } }); return paths; } mergeJsons(relativePath, sourceJson, targetFilesContentMap) { if (targetFilesContentMap[relativePath]) { this.extend(targetFilesContentMap[relativePath], [sourceJson]); } else { targetFilesContentMap[relativePath] = sourceJson; } } readJSON(path) { return JSON.parse(fs.readFileSync(path, 'utf8')); } isObject(value) { return value !== null && typeof value === 'object'; } isFunction(value) { return typeof value === 'function'; } extend(dest, sources) { if (!sources || sources.length === 0) { return dest; } for (let i = 0; i < sources.length; i++) { const src = sources[i]; // Cant extend primitives or null/undefined values. so skip them if (!this.isObject(src) && !this.isFunction(src)) { continue; } const keys = Object.keys(src); let ki = keys.length; while (ki--) { const srcField = keys[ki]; const srcValue = src[srcField]; let destValue = srcValue; if (this.isObject(srcValue) && !Array.isArray(srcValue)) { destValue = {}; this.extend(destValue, [dest[srcField], srcValue]); } dest[srcField] = destValue; } } return dest; } } exports.JsonMerge = JsonMerge; //# sourceMappingURL=json-merge.js.map