forked from AtoraSuunva/booru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify.js
48 lines (40 loc) · 1.57 KB
/
minify.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
// Modified version of https://github.com/terser/terser/issues/544#issuecomment-680113112
// Changed to add .json compression support, work using async promises (for speed?)
// Original is under no specified license
// This file is also under no license (unlike the rest of booru.) Use/modify it however.
const { minify } = require('terser')
const { join, resolve, extname } = require('path')
const { stat, readdir, readFile, writeFile } = require('fs').promises
async function getAllFiles({ path }) {
const files = await readdir(path)
const fPromises = files.map(async (file) => {
if ((await stat(`${path}/${file}`)).isDirectory()) {
return getAllFiles({ path: `${path}/${file}` })
} else if (file.match(/\.json$/)) {
return join(resolve(), path, file)
}
return []
})
return (await Promise.all(fPromises)).flat()
}
async function minifyFiles(filePaths) {
const fPromises = filePaths.map(async (path) => {
switch (extname(path)) {
// case '.js':
// const miniJS = await minify(await readFile(path, 'utf8'))
// return writeFile(path, miniJS.code, 'utf8')
case '.json':
const content = (await readFile(path, 'utf8')).trim()
const miniJSON = JSON.stringify(JSON.parse(content))
return writeFile(path, miniJSON, 'utf8')
}
})
return Promise.all(fPromises)
}
;(async () => {
const files = await getAllFiles({ path: './dist' })
console.log('Minifying files: ')
console.log(files.join('\n'))
await minifyFiles(files)
console.log('Done!')
})()