因公司vpn只能在ie里执行的缘故,需要将mac下编译后内容cp到虚拟机共享目录下,看效果,简单实用webpack到plugin钩子写了一个插件去执行shell脚本。
1、在webpack配置用引入插件 const cp = require("./你的目录/cp.js");
2、在plugins数组中加入此控件并配置
调用
new cp( //copy
[
{
from:"/Users/mazhenxiao/yuanyang/sinoocean/NationalMarketing/08_Code/Webapp/public",
to:"/Volumes/sinoocean/public",
arg:"-r"
]
)//自定义插件
参数名 | 参数 |
---|---|
from | copy自 |
to | copy 到 |
arg | "默认参数 -r 覆盖" |
源码
/**
* 处理执行cp到服务器发布
*/
const fs = require("fs");
const childProcess = require("child_process");
const util = require("util");
const os = require('os');
let exec = childProcess.exec;
class cp {
constructor(arg){
this.list = arg||[];
}
apply(compiler){
let th = this;
compiler.plugin("done", (compilation)=>{
setImmediate(arg=>{
th.copy();
})
});
}
copy(){
let arr = [];
let iswin = os.platform().includes("win32");
this.list.forEach(parame=>{
arr.push(new Promise((resolve,reject)=>{
let str = `${iswin? "copy":"cp"} ${parame["arg"]? parame["arg"]:"-r"} ${parame["from"]} ${parame["to"]}`
exec(str,arg=>{
if(!arg){
resolve()
}else{
reject();
}
})
}))
})
Promise.all(arr)
.then(arg=>{
console.log(arg);
})
.catch(e=>{
// console.log(e)
})
}
}
module.exports = cp;
网友评论