release.js 1.99 KB
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const inputPath = path.resolve(__dirname, '../packages');
const outputPath = path.resolve(__dirname, '../release');

// 删除文件夹
const delPath = path => {
  if (!path.includes(process.cwd())) {
    console.log('只能操作当前项目');
    throw path;
  }
  const info = fs.statSync(path);
  if (info.isDirectory()) {
    // 目录
    const data = fs.readdirSync(path);
    if (data.length > 0) {
      data.forEach((f, i) => {
        delPath(`${path}/${f}`); // 使用递归
        if (i == data.length - 1) {
          // 删了目录里的内容就删掉这个目录
          delPath(`${path}`);
        }
      });
    } else {
      fs.rmdirSync(path); // 删除空目录
    }
  } else if (info.isFile()) {
    fs.unlinkSync(path); // 删除文件
  }
};

// 清除输出目录
if (fs.existsSync(outputPath)) {
  delPath(outputPath);
}

// 同步编译
fs.readdirSync(inputPath).forEach(name => {
  const filePath = path.resolve(inputPath, name);
  if (fs.lstatSync(filePath).isDirectory()) {
    if (fs.existsSync(path.resolve(filePath, 'index.vue'))) {
      console.log(`Build ${name}...`);
      execSync(`vue-cli-service build --target lib --name zui-${name} --dest release/${name} packages/${name}/index.vue`, function (error, stdout, stderr) {
        if (error) {
          throw error;
        }
      });
    }
  }
});

// 编译完成后删除无用的文件
fs.readdirSync(outputPath).forEach(name => {
  const filePath = path.resolve(outputPath, name);
  if (fs.lstatSync(filePath).isDirectory()) {
    fs.readdirSync(filePath).forEach(file => {
      if (file !== `zui-${name}.css`) {
        if (file === `zui-${name}.umd.min.js`) {
          fs.renameSync(path.resolve(filePath, file), path.resolve(filePath, `zui-${name}.js`));
        } else {
          fs.unlinkSync(path.resolve(filePath, file));
        }
      }
    });
  } else {
    fs.unlinkSync(path.resolve(outputPath, name));
  }
});