publish.js 3.1 KB
Newer Older
liujiangnan's avatar
liujiangnan committed
1 2 3 4 5 6
/****
 * 批量编译打包模板工具
 * 运行 npm run publish T_01 命令来打包T_01模板
 * 运行 npm run publish T_01,T_02,T_03,T_04  命令来分别打包 T_01,T_02,T_03,T_04 这四个模板,注意逗号要用英文的
 * 运行 npm run publish all 命令来打包所有模板
 */
liujiaxin's avatar
liujiaxin committed
7

liujiangnan's avatar
liujiangnan committed
8 9 10 11 12 13 14
const spawn = require('child_process').spawn;
const path = require("path");
const fs = require("fs");
const os = require('os');
const compressing = require("compressing");

//Linux系统上'Linux'
liujiaxin's avatar
liujiaxin committed
15
//macOS 系统上'Darwin'
liujiangnan's avatar
liujiangnan committed
16
//Windows系统上'Windows_NT'
liujiaxin's avatar
liujiaxin committed
17
let sysType = os.type();
liujiangnan's avatar
liujiangnan committed
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

Date.prototype.Format = function(fmt) {
  var o = {
      "M+" : this.getMonth() + 1,
      "d+" : this.getDate(),
      "h+" : this.getHours(),
      "m+" : this.getMinutes(),
      "s+" : this.getSeconds(),
      "q+" : Math.floor((this.getMonth() + 3) / 3),
      "S" : this.getMilliseconds()
  };
  if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

function clean(zipPath){
  if(fs.existsSync(zipPath)){
    fs.unlinkSync(zipPath);
  }
}

const runSpawn = async function (){

  await new Promise(function(resolve,reject){

liujiaxin's avatar
liujiaxin committed
47
    let pkg = require("../package.json");
liujiangnan's avatar
liujiangnan committed
48

liujiaxin's avatar
liujiaxin committed
49
    let ls;
liujiangnan's avatar
liujiangnan committed
50 51
    if(sysType==="Windows_NT"){
      //ng build --prod --build--optimizer --base-href /ng-one/
liujiangnan's avatar
liujiangnan committed
52
      ls = spawn("cmd.exe", ['/c', 'ng', 'build', '--prod', '--build--optimizer', '--base-href', '/template-base-href/'] );
liujiangnan's avatar
liujiangnan committed
53
    }else{
liujiangnan's avatar
liujiangnan committed
54
      ls = spawn("ng", ['build', '--prod', '--build--optimizer', '--base-href', '/template-base-href/'] );
liujiangnan's avatar
liujiangnan committed
55 56 57 58 59
    }

    ls.stdout.on('data', (data) => {
      console.log(` ${data}`);
    });
liujiaxin's avatar
liujiaxin committed
60

liujiangnan's avatar
liujiangnan committed
61 62 63 64 65 66 67 68
    ls.stderr.on('data', (data) => {
      console.log(`stderr: ${data}`);
      reject();
    });

    ls.on('close', (code) => {
      console.log(`child process exited with code ${code}`);
      //要压缩的目录
liujiaxin's avatar
liujiaxin committed
69
      let zippath = path.resolve(__dirname,"../dist", pkg.name);
liujiangnan's avatar
liujiangnan committed
70 71 72 73 74
      //压缩包的存放目录
      let date = new Date();
      let zipname = pkg.name+"_"+date.Format("yyyyMMdd hh-mm-ss");
      let zipdir = path.resolve(__dirname,"../publish/"+zipname+".zip");
      clean(zipdir); //删除原有的包
liujiaxin's avatar
liujiaxin committed
75

liujiangnan's avatar
liujiangnan committed
76 77 78 79 80 81 82 83 84 85 86
      const tarStream = new compressing.zip.Stream();
      fs.readdir(zippath,function(err,files){
        if(err){
          console.log("======文件打开异常======");
          console.log(err);
          reject();
        }
        for(let i=0;i<files.length;i++){
          tarStream.addEntry(zippath+"/"+files[i]);
        }
        let writeStream = fs.createWriteStream(zipdir);
liujiaxin's avatar
liujiaxin committed
87
        tarStream.pipe(writeStream);
liujiangnan's avatar
liujiangnan committed
88 89 90 91
        writeStream.on('close', () => {
            console.log(`模板 ${pkg.name} 打包已完成!`);
            resolve();
        })
liujiaxin's avatar
liujiaxin committed
92 93
      });

liujiangnan's avatar
liujiangnan committed
94 95
    });

liujiaxin's avatar
liujiaxin committed
96
  });
liujiangnan's avatar
liujiangnan committed
97 98 99 100 101 102 103
}

// let projects = "";
// if(process.argv.length<3){
//   console.log("缺少参数");
//   return;
// }
liujiaxin's avatar
liujiaxin committed
104
// projects = process.argv[2];
liujiangnan's avatar
liujiangnan committed
105 106 107 108 109 110 111 112 113

let exec = async function(){
  //压缩模板
  await runSpawn();
}

exec();


liujiaxin's avatar
liujiaxin committed
114