fetch-blocks.js 3.76 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
const chalk = require('chalk');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
8
const insertCode = require('./insertCode');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
9 10 11 12 13 14 15 16 17 18 19

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
20 21 22 23 24 25 26

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

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

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

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

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

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

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

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

// ๆ’ๅ…ฅ pro ้œ€่ฆ็š„ๆผ”็คบไปฃ็ 
insertCode();