-
Notifications
You must be signed in to change notification settings - Fork 27
/
stats.js
56 lines (54 loc) · 1.57 KB
/
stats.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
/* global Promise */
const exec = require('child_process').exec
const fs = require('fs')
const zlib = require('zlib')
const FILE_PATH = './build'
new Promise((resolve, reject) => {
exec('npm run build -- --env.stats', (error, stdout, stderr) => {
if (error) reject(stderr)
resolve()
})
}).then(() => {
exec(`open ${FILE_PATH}/statistics.html`)
}).then((result) => {
return new Promise((resolve, reject) => {
fs.readdir(FILE_PATH, (err, files) => {
if (err) reject(err)
resolve(files)
})
})
}).then((files) => {
return files.filter((file) => {
return /\.js$/.test(file)
})
}).then((files) => {
const promises = files.map((filename) => {
return new Promise((resolve, reject) => {
fs.readFile(`${FILE_PATH}/${filename}`, 'utf8', (err, data) => {
if (err) reject(err)
resolve({ filename, content: data })
})
})
})
return Promise.all(promises)
}).then((files) => {
const promises = files.map((file) => {
const { filename, content } = file
return new Promise((resolve, reject) => {
zlib.gzip(content, (err, buffer) => {
if (err) reject(err)
resolve({ filename, original: content.length, compressed: buffer.length })
})
})
})
return Promise.all(promises)
}).then((results) => {
results.forEach((result) => {
console.log(`${FILE_PATH}/${result.filename}`)
console.log('original', result.original.toString().padStart(15))
console.log('original gzip', result.compressed.toString().padStart(10))
console.log('')
})
}).catch((err) => {
console.error(err)
})