MattIPv4
4 years ago
7 changed files with 23 additions and 241 deletions
@ -1,61 +0,0 @@
|
||||
/* |
||||
Copyright 2020 DigitalOcean |
||||
|
||||
This code is licensed under the MIT License. |
||||
You may obtain a copy of the License at |
||||
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions : |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
||||
*/ |
||||
|
||||
import { diffLines } from 'diff'; |
||||
import escape from 'escape-html'; |
||||
|
||||
export default (newConfFile, oldConfFile) => { |
||||
// Get the diff
|
||||
const diff = diffLines(oldConfFile, newConfFile); |
||||
|
||||
// Wrap additions in <mark>, drop removals
|
||||
return diff.map((change, index, array) => { |
||||
if (change.removed) return ''; |
||||
|
||||
const escaped = escape(change.value); |
||||
|
||||
// Don't mark as diff if nothing changed
|
||||
if (!change.added) return escaped; |
||||
|
||||
// Don't mark as diff if only whitespace changed
|
||||
if (index > 0 && array[index - 1].removed) { |
||||
if (array[index - 1].value.replace(/\s/g, '') === change.value.replace(/\s/g, '')) { |
||||
return escaped; |
||||
} |
||||
} |
||||
if (index < array.length - 1 && array[index + 1].removed) { |
||||
if (array[index + 1].value.replace(/\s/g, '') === change.value.replace(/\s/g, '')) { |
||||
return escaped; |
||||
} |
||||
} |
||||
|
||||
// Mark the diff, without highlighting whitespace
|
||||
return escaped |
||||
.split('\n') |
||||
.map(part => part.replace(/^(\s*)(.*)(\s*)$/, '$1<mark>$2</mark>$3')) |
||||
.join('\n'); |
||||
}).join(''); |
||||
}; |
@ -1,68 +0,0 @@
|
||||
/* |
||||
Copyright 2020 DigitalOcean |
||||
|
||||
This code is licensed under the MIT License. |
||||
You may obtain a copy of the License at |
||||
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions : |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
||||
*/ |
||||
|
||||
import escape from 'escape-html'; |
||||
import renames from './renames'; |
||||
import confLines from './conf_lines'; |
||||
import names from './names'; |
||||
|
||||
export default (newConf, oldConf) => { |
||||
// Consider renames
|
||||
const renameMap = renames(newConf, oldConf); |
||||
|
||||
// Store the diff config files
|
||||
const newFiles = {}; |
||||
|
||||
// Work through each file in the new config
|
||||
for (const name in newConf) { |
||||
if (!Object.prototype.hasOwnProperty.call(newConf, name)) continue; |
||||
|
||||
let newFileName = escape(name); |
||||
let newFileConf = escape(newConf[name]); |
||||
|
||||
// If this file was in the old config (same name or renamed & similar)
|
||||
// Calculate the diff of the configs
|
||||
const old = oldConf && oldConf[renameMap[name]]; |
||||
if (old && old !== newConf[name]) { |
||||
console.info(`Diffing ${name}...`); |
||||
newFileConf = confLines(newConf[name], old); |
||||
} |
||||
|
||||
// If the file was renamed we should diff that too
|
||||
if (name in renameMap && renameMap[name] !== name) { |
||||
newFileName = names(name, renameMap[name]); |
||||
} |
||||
|
||||
// Store!
|
||||
newFiles[name] = [ |
||||
newFileName, |
||||
newFileConf, |
||||
]; |
||||
} |
||||
|
||||
// Done
|
||||
return newFiles; |
||||
}; |
@ -1,46 +0,0 @@
|
||||
/* |
||||
Copyright 2020 DigitalOcean |
||||
|
||||
This code is licensed under the MIT License. |
||||
You may obtain a copy of the License at |
||||
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions : |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
||||
*/ |
||||
|
||||
import { diffChars } from 'diff'; |
||||
import escape from 'escape-html'; |
||||
|
||||
export default (newConfName, oldConfName) => { |
||||
// Get the diff
|
||||
const diff = diffChars(oldConfName, newConfName); |
||||
|
||||
// Wrap additions in <mark>, drop removals
|
||||
return diff.map(change => { |
||||
if (change.removed) return ''; |
||||
|
||||
const escaped = escape(change.value); |
||||
|
||||
// Don't mark as diff if nothing changed
|
||||
if (!change.added) return escaped; |
||||
|
||||
// Mark the diff, without highlighting whitespace
|
||||
return escaped.replace(/^(\s*)(.*)(\s*)$/, '$1<mark>$2</mark>$3'); |
||||
}).join(''); |
||||
}; |
@ -1,60 +0,0 @@
|
||||
/* |
||||
Copyright 2020 DigitalOcean |
||||
|
||||
This code is licensed under the MIT License. |
||||
You may obtain a copy of the License at |
||||
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions : |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
||||
*/ |
||||
|
||||
import { compareTwoStrings } from 'string-similarity'; |
||||
|
||||
export default (newConf, oldConf) => { |
||||
const newNames = Object.keys(newConf); |
||||
const oldNames = Object.keys(oldConf); |
||||
const removed = oldNames.filter(name => !newNames.includes(name)); |
||||
const added = newNames.filter(name => !oldNames.includes(name)); |
||||
|
||||
// For each newly added file, compare it to all the old files
|
||||
const addedSimilarity = {}; |
||||
for (const newName of added) { |
||||
addedSimilarity[newName] = { old: '', similarity: 0 }; |
||||
|
||||
for (const oldName of removed) { |
||||
const similarity = compareTwoStrings(newConf[newName], oldConf[oldName]); |
||||
|
||||
// Only care about > 50% similarity
|
||||
if (similarity <= .5) continue; |
||||
|
||||
// Store the most similar
|
||||
if (similarity > addedSimilarity[newName].similarity) |
||||
addedSimilarity[newName] = { old: oldName, similarity }; |
||||
} |
||||
} |
||||
|
||||
// Create a rename map
|
||||
return newNames.reduce((prev, current) => { |
||||
if (current in addedSimilarity && addedSimilarity[current].similarity) |
||||
prev[current] = addedSimilarity[current].old; |
||||
else |
||||
prev[current] = current; |
||||
return prev; |
||||
}, {}); |
||||
}; |
Loading…
Reference in new issue