fetch-blocks.js 3.57 KB
Newer Older
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
1 2
const path = require('path');
const fs = require('fs');
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
3
const fetch = require('node-fetch');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
4
const exec = require('child_process').exec;
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
5
const getNewRouteCode = require('./repalceRouter');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
6
const router = require('./router.config');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
7 8 9 10 11 12 13 14 15 16 17 18
const chalk = require('chalk');

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);
};
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
19 20 21 22 23 24 25

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

const findAllInstallRouter = router => {
  let routers = [];
  router.forEach(item => {
    if (item.component && item.path) {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
26 27 28 29 30
      if (item.path !== '/user' || item.path !== '/') {
        routers.push({
          ...item,
          routes: '',
        });
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
31 32
      }
    }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
33 34 35
    if (item.routes) {
      routers = routers.concat(findAllInstallRouter(item.routes));
    }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
36 37 38 39
  });
  return routers;
};

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
40
const filterParentRouter = (router, layout) => {
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
41 42
  return [...router]
    .map(item => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
43 44 45 46 47
      if (item.routes && (!router.component || layout)) {
        return { ...item, routes: filterParentRouter(item.routes, false) };
      }
      if (item.redirect) {
        return item;
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
48 49 50 51 52
      }
      return null;
    })
    .filter(item => item);
};
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
53 54
const firstUpperCase = pathString => {
  return pathString
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
55
    .replace('.', '')
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
56 57 58 59 60 61
    .split(/\/|\-/)
    .map(s => s.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()))
    .filter(s => s)
    .join('');
};

้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
62 63 64 65 66 67 68 69 70 71
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();
    });
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
72 73
  });
};
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
74 75

// replace router config
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
76
const parentRouter = filterParentRouter(router, true);
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
77 78 79 80
const { routesPath, code } = getNewRouteCode(relativePath, parentRouter);
// write ParentRouter
fs.writeFileSync(routesPath, code);

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
81 82
const installBlock = async () => {
  let gitFiles = await fetchGithubFiles();
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
83 84 85 86 87
  const installRouters = findAllInstallRouter(router);
  const installBlockIteration = async i => {
    const item = installRouters[i];

    if (!item || !item.path) {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
88
      return Promise.resolve();
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
89
    }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
90 91 92 93 94
    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);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
95
      const cmd = `umi block add https://github.com/ant-design/pro-blocks/tree/master/${gitPath} --npm-client=cnpm  --path=${
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        item.path
      }`;
      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}`;
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
117
    await execCmd(cmd);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
118
    return installBlockIteration(1);
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
119
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
120 121 122

  // ๅฎ‰่ฃ… router ไธญๆฒกๆœ‰็š„ๅ‰ฉไฝ™ๅŒบๅ—.
  installGitFile(0);
้™ˆๅธ…'s avatar
commit  
้™ˆๅธ… committed
123
};
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
124
installBlock();