publish.js 3.15 KB
Newer Older
范雪寒's avatar
范雪寒 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/****
 * 批量编译打包模板工具
 * 运行 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 命令来打包所有模板
 */

const spawn = require('child_process').spawn;
const path = require("path");
const fs = require("fs");
const os = require('os');
const compressing = require("compressing");

//Linux系统上'Linux'
//macOS 系统上'Darwin'
//Windows系统上'Windows_NT'
let sysType = os.type();

19
Date.prototype.Format = function (fmt) {
范雪寒's avatar
范雪寒 committed
20
  var o = {
21 22 23 24 25 26 27
    "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()
范雪寒's avatar
范雪寒 committed
28 29
  };
  if (/(y+)/.test(fmt))
30
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
范雪寒's avatar
范雪寒 committed
31
  for (var k in o)
32
    if (new RegExp("(" + k + ")").test(fmt))
范雪寒's avatar
范雪寒 committed
33 34 35 36
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

37 38
function clean(zipPath) {
  if (fs.existsSync(zipPath)) {
范雪寒's avatar
范雪寒 committed
39 40 41 42
    fs.unlinkSync(zipPath);
  }
}

43
const runSpawn = async function () {
范雪寒's avatar
范雪寒 committed
44

45
  await new Promise(function (resolve, reject) {
范雪寒's avatar
范雪寒 committed
46 47 48 49

    let pkg = require("../package.json");

    let ls;
50
    if (sysType === "Windows_NT") {
范雪寒's avatar
范雪寒 committed
51
      //ng build --prod --build--optimizer --base-href /ng-one/
52 53 54
      ls = spawn("cmd.exe", ['/c', 'ng', 'build', '--prod', '--build--optimizer', '--base-href', '/template-base-href/']);
    } else {
      ls = spawn("ng", ['build', '--prod', '--build--optimizer', '--base-href', '/template-base-href/']);
范雪寒's avatar
范雪寒 committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    }

    ls.stdout.on('data', (data) => {
      console.log(` ${data}`);
    });

    ls.stderr.on('data', (data) => {
      console.log(`stderr: ${data}`);
      reject();
    });

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

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

    });

  });
}

// let projects = "";
// if(process.argv.length<3){
//   console.log("缺少参数");
//   return;
// }
// projects = process.argv[2];

107
let exec = async function () {
范雪寒's avatar
范雪寒 committed
108 109 110 111 112 113 114 115
  //压缩模板
  await runSpawn();
}

exec();