-
Notifications
You must be signed in to change notification settings - Fork 24
/
pluginCompress.js
116 lines (100 loc) · 2.9 KB
/
pluginCompress.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const fs = require('fs');
const ft = require('fancy-terminal');
// eslint-disable-next-line import/no-extraneous-dependencies
const process = require('process');
const path = require('path');
const archiver = require('archiver');
const { version } = require('./package.json');
/**
* Get current time in nanoseconds.
*
* @return {number} current time in nanoseconds
*/
function getHrTime() {
const [seconds, nanoseconds] = process.hrtime();
return seconds * 1e9 + nanoseconds;
}
/**
* Compress plugin into a zip file.
*
* @param {string} outputPath path to the output zip file
* @param {Array} excludeList list of files/directories to be excluded from the zip file
*/
async function compress(outputPath, excludeList) {
const startTime = getHrTime();
const zip = archiver('zip', {
zlib: { level: 9 },
});
const output = fs.createWriteStream(outputPath);
// handle errors
output.on('error', (err) => {
// eslint-disable-next-line no-console
console.log(err);
});
// handle the end event
output.on('close', () => {
const endTime = getHrTime();
const elapsedTime = (endTime - startTime) / 1e9;
const bytes = zip.pointer();
const zipSize = bytes / 1024 / 1024;
const fileName = path.basename(outputPath);
// eslint-disable-next-line no-console
console.log(
`${ft.green(fileName)}: ${ft.blue(
zipSize.toFixed(2)
)}MB, generated in ${ft.blue(elapsedTime.toFixed(2))}ms`
);
});
zip.pipe(output);
zip.glob('**/*.*', {
cwd: __dirname,
ignore: excludeList,
});
await zip.finalize();
}
/**
* Generate ignore list for files/directories to be excluded from the zip file.
*
* @return {(string|*)[]} list of files/directories to be excluded from the zip file
*/
const generateIgnoreList = () => {
// read both .gitignore and .compressignore to exclude files
const compressIgnore = fs
.readFileSync('.compressignore')
.toString()
.split('\n');
return compressIgnore
.filter((val) => val !== '')
.map((ignorePath) => {
return ignorePath.endsWith('/') ? ignorePath + '**' : ignorePath;
});
};
/**
* Write version info to a JSON file.
*
* @param {string} outputPath path to version info file
* @param {string} zipFilePath path to zip file
*/
const writeFileInfo = (outputPath, zipFilePath) => {
const { size } = fs.statSync(zipFilePath);
const currentEnv = process.env.NODE_ENV || 'development';
const writeData = {
version,
mode: currentEnv,
size: `${(size / 1024 / 1024).toFixed(2)}mb`,
date: new Date().toISOString(),
};
fs.writeFileSync(
path.join(outputPath, 'info.json'),
JSON.stringify(writeData, null, 2)
);
};
const outputPath = path.resolve(__dirname, 'zip');
const targetFileName = process.argv[process.argv.length - 1];
const zipOutputPath = path.join(outputPath, targetFileName);
const ignoreList = generateIgnoreList();
// compress plugin
(async () => {
await compress(zipOutputPath, ignoreList);
writeFileInfo(outputPath, zipOutputPath);
})();