-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
70 lines (59 loc) · 1.79 KB
/
main.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
const fs = require('fs');
const archiver = require('archiver');
const childProcess = require('child_process');
const modclean = require('modclean');
const distDir = 'dist';
const targetDir = 'node_modules';
const distPath = 'dist/node_modules.zip';
const zipArchive = async () => {
const zipPath = `${targetDir}.zip`;
const output = fs.createWriteStream(zipPath);
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
fs.copyFileSync(zipPath, distPath);
fs.unlinkSync(zipPath);
console.log("done");
});
const archive = archiver('zip', {
zlib: { level: 9 }
});
archive.pipe(output);
archive.directory(targetDir, false);
await archive.finalize();
}
const cleanDir = (dir) => {
if (fs.existsSync(dir)) {
fs.rmdirSync(dir, { recursive: true });
}
}
const runNpm = (cmd, args) => {
return new Promise((resolve) => {
let n = childProcess.spawn(cmd,args);
n.stdout.setEncoding('utf-8');
n.stdout.on('data', (data) => {
console.log(`${data}`);
});
n.stderr.on('data', (data) => {
console.error(`${data}`);
});
n.on('close', (code) => {
console.log(`child process exited with code ${code}`);
resolve();
});
});
}
(async() => {
cleanDir(distDir);
fs.mkdirSync(distDir);
cleanDir(targetDir);
fs.unlinkSync('package-lock.json');
try {
await runNpm('npm',['i','--production']);
let mc = modclean({ignorePatterns:['example*', 'examples']});
mc.clean();
await zipArchive();
}catch(e){
console.error(e.stack);
}
})();