-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathonBuild.mjs
79 lines (68 loc) · 1.66 KB
/
onBuild.mjs
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
import { rename } from 'fs'
import { readFile, writeFile } from 'fs/promises'
import md5 from 'md5'
let hashCss
let hashJs
const readCssFile = () => {
return readFile('dist/css/styles.css', (err, data) => {
if (err) console.log(err)
return data
})
}
const readJsFile = () => {
return readFile('dist/js/index.js', (err, data) => {
if (err) console.log(err)
return data
})
}
const writeVersions = () => {
const versionObject = {
css: hashCss,
js: hashJs,
}
return writeFile(
'src/_data/version.json',
JSON.stringify(versionObject),
(err) => {
if (err) throw err
console.log('The file has been saved!')
}
)
}
const renameCSS = () => {
return rename(
'dist/css/styles.css',
`dist/css/styles${hashCss}.css`,
function (err) {
if (err) return console.log('ERROR: ' + err)
console.log(`dist/css/styles.css > dist/css/styles${hashCss}.css`)
}
)
}
const renameJS = () => {
return rename(
'dist/js/index.js',
`dist/js/index${hashJs}.js`,
function (err) {
if (err) return console.log('ERROR: ' + err)
console.log(`dist/js/index.js > dist/js/index${hashJs}.js`)
}
)
}
const setVersionVariables = (cssFileContent, jsFileContent) => {
return new Promise((resolve) => {
hashCss = md5(cssFileContent).slice(0, 16)
hashJs = md5(jsFileContent).slice(0, 16)
resolve()
})
}
async function hashContent() {
const cssFileContent = await readCssFile()
const jsFileContent = await readJsFile()
await setVersionVariables(cssFileContent, jsFileContent)
await writeVersions()
await renameCSS()
await renameJS()
console.log('Files versioned')
}
hashContent()