|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const os = require('os'); |
| 4 | +const copySync = require('../../file-system').copySync; |
| 5 | +const readFileSync = require('../../file-system').readFileSync; |
| 6 | +const writeFile = require('../../file-system').writeFile; |
| 7 | + |
| 8 | +class Configuration { |
| 9 | + constructor(options) { |
| 10 | + this.options = options; |
| 11 | + this.aureliaJsonPath = options.originalBaseDir + '/aurelia_project/aurelia.json'; |
| 12 | + this.project = JSON.parse(readFileSync(this.aureliaJsonPath)); |
| 13 | + } |
| 14 | + |
| 15 | + configEntry(key, createKey) { |
| 16 | + let entry = this.project; |
| 17 | + let keys = key.split('.'); |
| 18 | + |
| 19 | + if (!keys[0]) { |
| 20 | + return entry; |
| 21 | + } |
| 22 | + |
| 23 | + while (entry && keys.length) { |
| 24 | + key = this.parsedKey(keys.shift()); |
| 25 | + if (entry[key.value] === undefined || entry[key.value] === null) { |
| 26 | + if (!createKey) { |
| 27 | + return entry[key.value]; |
| 28 | + } |
| 29 | + let checkKey = this.parsedKey(keys.length ? keys[0] : createKey); |
| 30 | + if (checkKey.index) { |
| 31 | + entry[key.value] = []; |
| 32 | + } |
| 33 | + else if (checkKey.key) { |
| 34 | + entry[key.value] = {}; |
| 35 | + } |
| 36 | + } |
| 37 | + entry = entry[key.value]; |
| 38 | + |
| 39 | + // TODO: Add support for finding objects based on input values? |
| 40 | + // TODO: Add support for finding string in array? |
| 41 | + } |
| 42 | + |
| 43 | + return entry; |
| 44 | + } |
| 45 | + |
| 46 | + parsedKey(key) { |
| 47 | + if (/\[(\d+)\]/.test(key)) { |
| 48 | + return { index: true, key: false, value: +(RegExp.$1) }; |
| 49 | + } |
| 50 | + else { |
| 51 | + return { index: false, key: true, value: key }; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + normalizeKey(key) { |
| 56 | + const re = /([^.])\[/; |
| 57 | + while (re.exec(key)) { |
| 58 | + key = key.replace(re, RegExp.$1 + '.['); |
| 59 | + } |
| 60 | + |
| 61 | + let keys = key.split('.'); |
| 62 | + for (let i = 0; i < keys.length; i++) { |
| 63 | + if (/\[(\d+)\]/.test(keys[i])) { |
| 64 | + // console.log(`keys[${i}] is index: ${keys[i]}`); |
| 65 | + } |
| 66 | + else if (/\[(.+)\]/.test(keys[i])) { |
| 67 | + // console.log(`keys[${i}] is indexed name: ${keys[i]}`); |
| 68 | + keys[i] = RegExp.$1; |
| 69 | + } |
| 70 | + else { |
| 71 | + // console.log(`keys[${i}] is name: ${keys[i]}`); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return keys.join('.'); |
| 76 | + } |
| 77 | + |
| 78 | + execute(action, key, value) { |
| 79 | + let originalKey = key; |
| 80 | + |
| 81 | + key = this.normalizeKey(key); |
| 82 | + |
| 83 | + if (action === 'get') { |
| 84 | + return `Configuration key '${key}' is:` + os.EOL + JSON.stringify(this.configEntry(key), null, 2); |
| 85 | + } |
| 86 | + |
| 87 | + let keys = key.split('.'); |
| 88 | + key = this.parsedKey(keys.pop()); |
| 89 | + let parent = keys.join('.'); |
| 90 | + |
| 91 | + if (action === 'set') { |
| 92 | + let entry = this.configEntry(parent, key.value); |
| 93 | + if (entry) { |
| 94 | + entry[key.value] = value; |
| 95 | + } |
| 96 | + else { |
| 97 | + console.log('Failed to set property', this.normalizeKey(originalKey), '!'); |
| 98 | + } |
| 99 | + } |
| 100 | + else if (action === 'clear') { |
| 101 | + let entry = this.configEntry(parent); |
| 102 | + if (entry && (key.value in entry)) { |
| 103 | + delete entry[key.value]; |
| 104 | + } |
| 105 | + else { |
| 106 | + console.log('No property', this.normalizeKey(originalKey), 'to clear!'); |
| 107 | + } |
| 108 | + } |
| 109 | + else if (action === 'add') { |
| 110 | + let entry = this.configEntry(parent, key.value); |
| 111 | + if (Array.isArray(entry[key.value]) && !Array.isArray(value)) { |
| 112 | + value = [value]; |
| 113 | + } |
| 114 | + if (Array.isArray(value) && !Array.isArray(entry[key.value])) { |
| 115 | + entry[key.value] = (entry ? [entry[key.value]] : []); |
| 116 | + } |
| 117 | + if (Array.isArray(value)) { |
| 118 | + entry[key.value].push(...value); |
| 119 | + } |
| 120 | + else if (Object(value) === value) { |
| 121 | + if (Object(entry[key.value]) !== entry[key.value]) { |
| 122 | + entry[key.value] = {}; |
| 123 | + } |
| 124 | + Object.assign(entry[key.value], value); |
| 125 | + } |
| 126 | + else { |
| 127 | + entry[key.value] = value; |
| 128 | + } |
| 129 | + } |
| 130 | + else if (action === 'remove') { |
| 131 | + let entry = this.configEntry(parent); |
| 132 | + |
| 133 | + if (Array.isArray(entry) && key.index) { |
| 134 | + entry.splice(key.value, 1); |
| 135 | + } |
| 136 | + else if (Object(entry) === entry && key.key) { |
| 137 | + delete entry[key.value]; |
| 138 | + } |
| 139 | + else if (!entry) { |
| 140 | + console.log('No property', this.normalizeKey(originalKey), 'to remove from!'); |
| 141 | + } |
| 142 | + else { |
| 143 | + console.log("Can't remove value from", entry[key.value], "!"); |
| 144 | + } |
| 145 | + } |
| 146 | + key = this.normalizeKey(originalKey); |
| 147 | + return `Configuration key '${key}' is now:` + os.EOL + JSON.stringify(this.configEntry(key), null, 2); |
| 148 | + } |
| 149 | + |
| 150 | + save(backup = true) { |
| 151 | + const unique = new Date().toISOString().replace(/[T\D]/g, ''); |
| 152 | + let arr = this.aureliaJsonPath.split(/[\\\/]/); |
| 153 | + const name = arr.pop(); |
| 154 | + const path = arr.join('/'); |
| 155 | + const bak = `${name}.${unique}.bak`; |
| 156 | + |
| 157 | + if (backup) { |
| 158 | + copySync(this.aureliaJsonPath, [path, bak].join('/')); |
| 159 | + } |
| 160 | + return writeFile(this.aureliaJsonPath, JSON.stringify(this.project, null, 2), 'utf8') |
| 161 | + .then(() => { return bak }); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +module.exports = Configuration; |
0 commit comments