fetch-block.js 3.44 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
commit  
้™ˆๅธ… committed
40 41 42 43 44 45 46 47 48 49
const filterParentRouter = router => {
  return [...router]
    .map(item => {
      if (item.routes) {
        return { ...item, routes: filterParentRouter(item.routes) };
      }
      return null;
    })
    .filter(item => item);
};
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
50 51
const firstUpperCase = pathString => {
  return pathString
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
52
    .replace('.', '')
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
53 54 55 56 57 58
    .split(/\/|\-/)
    .map(s => s.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()))
    .filter(s => s)
    .join('');
};

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

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

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

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

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