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
| const fs = require('fs'); const projectNames = ['main-app'];
const args = process.argv.slice(2); if(args.length==0){ console.warn('请输入参数',projectNames) return; } let projectName = args[0]; if(!projectName.endsWith('-app')){ console.log("项目名称转换:",projectName,' -> ',projectName+'-app') projectName+='-app' }
if(projectNames.indexOf(projectName)==-1){ console.warn('参数错误',projectName) console.warn('请输入正确参数',projectNames) return; }
const filePath = '../dist/'+projectName+'/config.js'; const fileData = fs.readFileSync(filePath, 'utf8');
const oldAddress = '\'(?:https?:\\/\\/)?((([0-9]{0,3}\\.){3}[0-9]{0,3}:[0-9]{1,})|([\\w.-]+(?:\\.[\\w.-]+)+))'; const newAddress = '\''; const oldPassword = 'password: \'(.*)\''; const newPassword = 'password: \'\''; const modifiedData = fileData.replace(new RegExp(oldAddress, 'g'), newAddress); const modifiedData = modifiedData.replace(new RegExp(oldPassword, 'g'), newPassword);
fs.writeFileSync(filePath, modifiedData, 'utf8');
console.log('文件修改完成!'); console.log('文件打包开始!'); const archiver = require('../'+projectName+'/node_modules/archiver');
const output = fs.createWriteStream(__dirname + '/../dist/'+projectName+'.zip'); const archive = archiver('zip', { zlib: { level: 9 } });
archive.pipe(output);
archive.directory(__dirname + '/../dist/'+projectName, false);
archive.finalize(); console.log('文件打包完成!', output.path);
|