Conf files should be treated as a dict, file names are unique

This commit is contained in:
MattIPv4
2020-06-04 14:58:41 +01:00
parent 7444fa6f77
commit bc04209423
3 changed files with 39 additions and 35 deletions

View File

@@ -80,11 +80,11 @@ THE SOFTWARE.
<div :class="`column ${splitColumn ? 'is-half' : 'is-full'} is-full-mobile is-full-tablet`">
<h2>{{ i18n.templates.app.configFiles }}</h2>
<div ref="files" class="columns is-multiline">
<NginxPrism v-for="(conf, i) in confFilesOutput"
:key="`${conf[0]}-${i}-${hash(conf[1])}`"
:name="`${nginxDir}/${conf[0]}`"
:conf="conf[1]"
:half="confFilesOutput.length > 1 && !splitColumn"
<NginxPrism v-for="(confContents, confName) in confFilesOutput"
:key="`${confName}-${hash(confContents)}`"
:name="`${nginxDir}/${confName}`"
:conf="confContents"
:half="Object.keys(confFilesOutput).length > 1 && !splitColumn"
></NginxPrism>
</div>
</div>
@@ -132,8 +132,8 @@ THE SOFTWARE.
ready: false,
splitColumn: false,
confWatcherWaiting: false,
confFilesPrevious: [],
confFilesOutput: [],
confFilesPrevious: {},
confFilesOutput: {},
};
},
computed: {
@@ -222,17 +222,21 @@ THE SOFTWARE.
this.$nextTick(() => this.checkChange(this.confFiles));
},
updateDiff(newConf, oldConf) {
const newFiles = {};
// Work through each file in the new config
const newFiles = [];
for (const [newFileName, newFileConf] of newConf) {
for (const newFileName in newConf) {
if (!Object.prototype.hasOwnProperty.call(newConf, newFileName)) continue;
const newFileConf = newConf[newFileName];
// If a file with the same name existed before, diff!
// TODO: Handle diffing across file renames (eg. when a user changes a domain name)
const old = oldConf && oldConf.find(c => c[0] === newFileName);
if (old && this.hash(old[1]) !== this.hash(newFileConf)) {
const old = oldConf && oldConf[newFileName];
if (old && this.hash(old) !== this.hash(newFileConf)) {
console.info(`Diffing ${newFileName}...`);
// Get the diff
const diff = diffLines(old[1], newFileConf);
const diff = diffLines(old, newFileConf);
// Wrap additions in <mark>, drop removals
const output = diff.map((change, index, array) => {
@@ -263,13 +267,15 @@ THE SOFTWARE.
}).join('');
// Store
newFiles.push([newFileName, output]);
newFiles[newFileName] = output;
continue;
}
// No diffing, just store the new file
newFiles.push([newFileName, newFileConf]);
newFiles[newFileName] = newFileConf;
}
// Store
this.$data.confFilesOutput = newFiles;
this.$nextTick(() => this.$data.confWatcherWaiting = false);
},

View File

@@ -114,15 +114,16 @@ THE SOFTWARE.
const tar = pack();
// Add all our config files to the tar
for (const conf of this.$props.data.confFiles) {
tar.entry({ name: conf[0] }, conf[1]);
for (const fileName in this.$props.data.confFiles) {
if (!Object.prototype.hasOwnProperty.call(this.$props.data.confFiles, fileName)) continue;
tar.entry({ name: fileName }, this.$props.data.confFiles[fileName]);
// If symlinks are enabled and this is in sites-available, symlink to sites-enabled
if (this.$props.data.global.tools.symlinkVhost.computed && conf[0].startsWith('sites-available'))
if (this.$props.data.global.tools.symlinkVhost.computed && fileName.startsWith('sites-available'))
tar.entry({
name: conf[0].replace(/^sites-available/, 'sites-enabled'),
name: fileName.replace(/^sites-available/, 'sites-enabled'),
type: 'symlink',
linkname: `../${conf[0]}`,
linkname: `../${fileName}`,
});
}