forked from mrotaru/cache-bust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (88 loc) · 2.46 KB
/
index.js
File metadata and controls
97 lines (88 loc) · 2.46 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict'
let glob = require('glob')
let fs = require('fs')
let path = require('path')
let crypto = require('crypto')
/**
* Allow multiple glob patterns, separated by spaces
*/
function filesFn (str, globOptions) {
if (!str || str && str.length === 0) {
return []
}
let ret = str.split(' ')
.reduce((globbed, pattern) => globbed.concat(glob.sync(pattern, globOptions)), [])
return ret
}
/**
*
*/
function hash (absFilePath) {
if (!fs.lstatSync(absFilePath).isFile()) {
return ''
}
return crypto
.createHash('md5')
.update(fs.readFileSync(absFilePath))
.digest('hex')
}
/**
* Generate an array to store each referenced file's absolute path, relative
* path (from `baseDir`) and the MD5 hash. Uses the `exclude` option
*/
function getReferencedFiles (options) {
let referencedFiles = filesFn(options.referenced, {ignore: []})
.map((f) => {
const absPath = path.resolve(f)
return {
abs: absPath,
relative: path.relative(options.baseDir, f),
hash: hash(absPath)
}
})
.filter((f) => fs.lstatSync(f.abs).isFile())
return referencedFiles
}
/**
* Generate the basename of the file `p` with the hash `hash`
*/
function newNameFn (p, hash, options) {
let ext = path.extname(p)
let base = path.basename(p, ext)
let hashPart = hash.substring(0, options.hexLength)
return `${base}-${hashPart}${ext}`
}
/**
* Start the process
*/
function run (options) {
let referencedFiles = getReferencedFiles(options)
if (!referencedFiles.length) {
console.log('No referenced files; exiting...')
process.exit(1)
}
/**
* Perform renamings, and update `referencedFiles` objects to contain the
* relative path (to `options.baseDir`) to the new, hashed file
*/
for (let f of referencedFiles) {
let newName = newNameFn(f.abs, f.hash, options)
let newFullPath = path.join(path.dirname(f.abs), newName)
fs.renameSync(f.abs, newFullPath)
f.hashedRelative = path.join(path.dirname(f.relative), newName)
}
/**
* Go over each file containing references to `referencedFiles` and perform the
* replacements
*/
for (let fileName of filesFn(options['referencing'], {})) {
let fileContents = fs.readFileSync(fileName, {encoding: 'utf8'})
for (let referenced of referencedFiles) {
fileContents = fileContents
.split(referenced.relative)
.join(referenced.hashedRelative)
}
fs.writeFileSync(fileName, fileContents)
}
}
module.exports = run