forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deduplicate-enterprise-assets.js
executable file
·71 lines (64 loc) · 2.63 KB
/
deduplicate-enterprise-assets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const walk = require('walk-sync')
const jimp = require('jimp')
// iterate through enterprise images from most recent to oldest
// check if the image in the /assets/enterprise/... directory
// is an exact match to the assets/images in relative path and content
// if exact match, delete the /assets/enterprise/... version
const enterpriseAssetDirectories = [
'/assets/enterprise/3.0',
'/assets/enterprise/github-ae',
'/assets/enterprise/2.22',
'/assets/enterprise/2.21',
'/assets/enterprise/2.20'
]
async function main () {
for (const directory of enterpriseAssetDirectories) {
const fullDirectoryPath = path.join(process.cwd(), directory)
const files = walk(fullDirectoryPath, {
includeBasePath: true,
directories: false
})
for (const file of files) {
// get the /assets/images file that currently exists, which
// would be the equivalent to the enterprise asset
const enterpriseRegex = /\/assets\/enterprise\/(2\.20|2\.21|2\.22|3\.0|github-ae)/
const existingFileToCompare = file.replace(enterpriseRegex, '')
const fileExt = path.extname(file)
// if the file in the enterprise directory is an exact copy of
// the image in the local /assets/images directory, then we can
// delete the enterprise image and the reference in the Markdown
// will just work
if (fs.existsSync(existingFileToCompare)) {
// Buffer.compare and Jimp both return 0 if files match
let compareResult = 1
try {
// Jimp gives slightly better results comparing image files
// over using a buffer compare. Of the assets we have,
// Jimp only supports png and gif
if (fileExt === '.png' || fileExt === '.gif') {
const existingImageToCompare = await jimp.read(existingFileToCompare)
const enterpriseImage = await jimp.read(file)
// if the diff.percent value is 0, images are identical
const diff = await jimp.diff(existingImageToCompare, enterpriseImage)
compareResult = diff.percent
} else {
const existingImageToCompare = await fs.readFileSync(existingFileToCompare)
const enterpriseImage = await fs.readFileSync(file)
compareResult = Buffer.compare(Buffer.from(existingImageToCompare),
Buffer.from(enterpriseImage))
}
} catch (err) {
console.log(file)
console.log(err)
}
if (compareResult === 0) fs.unlinkSync(file)
}
}
}
}
main()
.catch(console.error)
.finally(() => console.log('Done!'))