const fs = require('fs'); const path = require('path'); const { exec } = 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}...`); exec(`vue-cli-service build --target lib --name zui-${name} --dest release/${name} packages/${name}/index.vue`, function (error, stdout, stderr) { if (error) { throw error; } const destPath = path.resolve(outputPath, name); fs.readdirSync(destPath).forEach(file => { if (file !== `zui-${name}.css`) { if (file === `zui-${name}.umd.min.js`) { fs.renameSync(path.resolve(destPath, file), path.resolve(destPath, `zui-${name}.js`)); } else { fs.unlinkSync(path.resolve(destPath, file)); } } }); }); } } });