fetch-blocks.js 3.8 KB
Newer Older
陈帅 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
const path = require('path');
const fs = require('fs');
const fetch = require('node-fetch');
const exec = require('child_process').exec;
const getNewRouteCode = require('./repalceRouter');
const router = require('./router.config');
const chalk = require('chalk');
const insertCode = require('./insertCode');

const fetchGithubFiles = async () => {
  const ignoreFile = ['_scripts'];
  const data = await fetch(`https://api.github.com/repos/ant-design/pro-blocks/git/trees/master`);
  if (data.status !== 200) {
    return;
  }
  const { tree } = await data.json();
  const files = tree.filter(file => file.type === 'tree' && !ignoreFile.includes(file.path));
  return Promise.resolve(files);
};

const relativePath = path.join(__dirname, '../config/config.ts');

const findAllInstallRouter = router => {
  let routers = [];
  router.forEach(item => {
    if (item.component && item.path) {
      if (item.path !== '/user' || item.path !== '/') {
        routers.push({
          ...item,
          routes: !!item.routes,
        });
      }
    }
    if (item.routes) {
      routers = routers.concat(findAllInstallRouter(item.routes));
    }
  });
  return routers;
};

const filterParentRouter = (router, layout) => {
  return [...router]
    .map(item => {
      if (item.routes && (!router.component || layout)) {
        return { ...item, routes: filterParentRouter(item.routes, false) };
      }
      if (item.redirect) {
        return item;
      }
      return null;
    })
    .filter(item => item);
};
const firstUpperCase = pathString => {
  return pathString
    .replace('.', '')
    .split(/\/|\-/)
    .map(s => s.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()))
    .filter(s => s)
    .join('');
};

const execCmd = shell => {
  return new Promise((resolve, reject) => {
    exec(shell, { encoding: 'utf8' }, (error, statusbar) => {
      if (error) {
        console.log(error);
        return reject(error);
      }
      console.log(statusbar);
      resolve();
    });
  });
};

// replace router config
const parentRouter = filterParentRouter(router, true);
const { routesPath, code } = getNewRouteCode(relativePath, parentRouter);
// write ParentRouter
fs.writeFileSync(routesPath, code);

const installBlock = async () => {
  let gitFiles = await fetchGithubFiles();
  const installRouters = findAllInstallRouter(router);
  const installBlockIteration = async i => {
    const item = installRouters[i];

    if (!item || !item.path) {
      return Promise.resolve();
    }
    const gitPath = firstUpperCase(item.path);
    // 如果这个区块在 git 上存在
    if (gitFiles.find(file => file.path === gitPath)) {
      console.log('install ' + chalk.green(item.name) + ' to: ' + chalk.yellow(item.path));
      gitFiles = gitFiles.filter(file => file.path !== gitPath);
      const skipModifyRouter = item.routes ? '--skip-modify-routes' : '';
陈帅 已提交
97
      const cmd = `umi block add https://github.com/ant-design/pro-blocks/tree/master/${gitPath}  --path=${
陈帅 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        item.path
      } ${skipModifyRouter}`;
      try {
        await execCmd(cmd);
        console.log(`install ${chalk.hex('#1890ff')(item.name)} success`);
      } catch (error) {
        console.error(error);
      }
    }
    return installBlockIteration(i + 1);
  };
  // 安装路由中设置的区块
  await installBlockIteration(0);

  const installGitFile = async i => {
    const item = gitFiles[i];
    if (!item || !item.path) {
      return Promise.resolve();
    }
    console.log('install ' + chalk.green(item.path));
    const cmd = `umi block add https://github.com/ant-design/pro-blocks/tree/master/${item.path}`;
    await execCmd(cmd);
    return installBlockIteration(1);
  };

  // 安装 router 中没有的剩余区块.
  installGitFile(0);
};
陈帅 已提交
126 127 128 129
installBlock().then(() => {
  // 插入 pro 需要的演示代码
  insertCode();
});