afterbuild.js
931 Bytes
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
const fs = require('fs');
const encoding = 'UTF-8';
/**
* 写入version信息方便判断版本
*/
function sendfile() {
try {
const pathFolder = './dist/version.text';
deleteFolder(pathFolder);
fs.writeFileSync(pathFolder, new Date().getTime().toString(), { encoding });
} catch (e) {
console.error('sourcemap文件写入失败', e);
}
}
/**删除文件或文件夹 */
function deleteFolder(path) {
let files = [];
if (fs.existsSync(path)) {
if (fs.statSync(path).isFile()) {
fs.unlinkSync(path); //删除文件
} else {
files = fs.readdirSync(path);
files.forEach((file, index) => {
let curPath = path + '/' + file;
if (fs.statSync(curPath).isDirectory()) {
// recurse
deleteFolder(curPath);
} else {
// delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
}
sendfile();