release.js
1.99 KB
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 path = require('path');
const { execSync } = require('child_process');
const inputPath = path.resolve(__dirname, '../packages');
const outputPath = path.resolve(__dirname, '../release');
// 删除文件夹
const delPath = path => {
if (!path.includes(process.cwd())) {
console.log('只能操作当前项目');
throw path;
}
const info = fs.statSync(path);
if (info.isDirectory()) {
// 目录
const data = fs.readdirSync(path);
if (data.length > 0) {
data.forEach((f, i) => {
delPath(`${path}/${f}`); // 使用递归
if (i == data.length - 1) {
// 删了目录里的内容就删掉这个目录
delPath(`${path}`);
}
});
} else {
fs.rmdirSync(path); // 删除空目录
}
} else if (info.isFile()) {
fs.unlinkSync(path); // 删除文件
}
};
// 清除输出目录
if (fs.existsSync(outputPath)) {
delPath(outputPath);
}
// 同步编译
fs.readdirSync(inputPath).forEach(name => {
const filePath = path.resolve(inputPath, name);
if (fs.lstatSync(filePath).isDirectory()) {
if (fs.existsSync(path.resolve(filePath, 'index.vue'))) {
console.log(`Build ${name}...`);
execSync(`vue-cli-service build --target lib --name zui-${name} --dest release/${name} packages/${name}/index.vue`, function (error, stdout, stderr) {
if (error) {
throw error;
}
});
}
}
});
// 编译完成后删除无用的文件
fs.readdirSync(outputPath).forEach(name => {
const filePath = path.resolve(outputPath, name);
if (fs.lstatSync(filePath).isDirectory()) {
fs.readdirSync(filePath).forEach(file => {
if (file !== `zui-${name}.css`) {
if (file === `zui-${name}.umd.min.js`) {
fs.renameSync(path.resolve(filePath, file), path.resolve(filePath, `zui-${name}.js`));
} else {
fs.unlinkSync(path.resolve(filePath, file));
}
}
});
} else {
fs.unlinkSync(path.resolve(outputPath, name));
}
});