|
| 1 | +let fs = require('fs'), |
| 2 | + deepmerge = require('deepmerge'), |
| 3 | + File = require('./file'); |
| 4 | + |
| 5 | +module.exports = class Processor { |
| 6 | + constructor(config, cwd) { |
| 7 | + this.config = config; |
| 8 | + this.cwd = cwd; |
| 9 | + } |
| 10 | + |
| 11 | + process() { |
| 12 | + this.files = []; |
| 13 | + |
| 14 | + this.config.forEach(config => { |
| 15 | + let file = this.processFile(config); |
| 16 | + |
| 17 | + this.files.push(file); |
| 18 | + }) |
| 19 | + } |
| 20 | + |
| 21 | + write() { |
| 22 | + this.files.forEach(file => fs.writeFile(file.output, JSON.stringify(file.content, null, 2), 'UTF-8')); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @param {{envMap: {}, output: string, source: string}} config |
| 27 | + * |
| 28 | + * @returns {*} |
| 29 | + */ |
| 30 | + processFile(config) { |
| 31 | + let file = new File(); |
| 32 | + |
| 33 | + let pathSource = this.resolvePath(config.source), |
| 34 | + pathOutput = this.resolvePath(config.output); |
| 35 | + |
| 36 | + let packageJsonPath = this.resolvePath(pathSource), |
| 37 | + packageJsonContent = fs.readFileSync(packageJsonPath); |
| 38 | + |
| 39 | + /** @param {{extra: {}}} content */ |
| 40 | + let packageJson = JSON.parse(packageJsonContent), |
| 41 | + solvedJson = this.resolveOverwritten(config.envMap), |
| 42 | + completedJson = this.constructor.getMergedData(packageJson, solvedJson); |
| 43 | + |
| 44 | + file.source = pathSource; |
| 45 | + file.output = pathOutput; |
| 46 | + file.content = completedJson; |
| 47 | + |
| 48 | + return file; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param {string} path |
| 53 | + */ |
| 54 | + resolvePath(path) { |
| 55 | + if ('/' === path.charAt(0)) { |
| 56 | + return path; |
| 57 | + } |
| 58 | + |
| 59 | + return `${this.cwd}/${path}`; |
| 60 | + } |
| 61 | + |
| 62 | + resolveOverwritten(envMapping) { |
| 63 | + let object = {}; |
| 64 | + |
| 65 | + for (let abstractPath of Object.keys(envMapping)) { |
| 66 | + let envVariable = envMapping[abstractPath], |
| 67 | + value = this.constructor.getEnvironmentValue(envVariable); |
| 68 | + |
| 69 | + this.constructor.overwriteObjectFieldValue(abstractPath, value, object) |
| 70 | + } |
| 71 | + |
| 72 | + return object; |
| 73 | + } |
| 74 | + |
| 75 | + static getEnvironmentValue(index) { |
| 76 | + return process.env[index] || undefined; |
| 77 | + } |
| 78 | + |
| 79 | + static getMergedData(data, overwrittenData) { |
| 80 | + return deepmerge(data, overwrittenData); |
| 81 | + } |
| 82 | + |
| 83 | + static overwriteObjectFieldValue(abstractPath, value, object, delimiter) { |
| 84 | + if (undefined === value) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + delimiter = undefined !== delimiter ? delimiter : '.'; |
| 89 | + |
| 90 | + let indexes = abstractPath.split(delimiter), |
| 91 | + lastPartIndex = indexes.length - 1; |
| 92 | + |
| 93 | + for (let i = 0; i <= lastPartIndex; i++) { |
| 94 | + let index = indexes[i]; |
| 95 | + |
| 96 | + if (i === lastPartIndex) { |
| 97 | + object[index] = value; |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + if (undefined === object[index]) { |
| 102 | + object[index] = {}; |
| 103 | + } |
| 104 | + |
| 105 | + object = object[index]; |
| 106 | + } |
| 107 | + } |
| 108 | +}; |
0 commit comments