Commit e8cdb4a4 authored by 李苏's avatar 李苏 💬

服务器编译脚本

parent 57197373
const fs = require('fs');
const path = require('path');
// JSON文件路径
const filePath = path.join(__dirname, 'package.json');
const destFile = path.join(__dirname, 'package_bac.json');
// 复制文件的函数
function copyFile(src, dest, callback) {
const readStream = fs.createReadStream(src);
const writeStream = fs.createWriteStream(dest);
// 监听错误事件
readStream.on('error', (err) => {
fs.unlink(dest, () => {
if (callback) callback(err);
});
});
// 监听完成事件
writeStream.on('finish', callback);
// 管道读取流到写入流
readStream.pipe(writeStream);
}
copyFile(filePath, destFile, (err) => {
if (err) {
return console.error(err);
}
console.log('文件复制并重命名成功');
console.log('开始调整json');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
try {
// 将字符串转换为JSON对象
const jsonData = JSON.parse(data);
jsonData.dependencies.common="file:../frontpackage"
fs.writeFile(filePath, JSON.stringify(jsonData, null, 2) , 'utf8', (err) => {
if (err) {
console.error(err);
return;
}
console.log('package.json文件已修改');
});
} catch (e) {
console.error('解析JSON时出错:', e);
}
});
});
const fs = require('fs');
// 源文件路径
const oldPath = './package_bac.json';
// 新文件路径
const newPath = './package.json';
fs.unlink(newPath, (err) => {
if (err) throw err;
console.log('文件已被删除');
fs.rename(oldPath, newPath, (err) => {
if (err) throw err;
console.log('文件重命名成功');
});
});
// 使用fs.rename()重命名文件
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment