fetch-block.js 2.3 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 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
const router = require('./router.config');

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

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

ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
30 31 32 33 34 35 36 37 38 39
const filterParentRouter = router => {
  return [...router]
    .map(item => {
      if (item.routes) {
        return { ...item, routes: filterParentRouter(item.routes) };
      }
      return null;
    })
    .filter(item => item);
};
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40 41
const firstUpperCase = pathString => {
  return pathString
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
42
    .replace('.', '')
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
43 44 45 46 47 48
    .split(/\/|\-/)
    .map(s => s.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()))
    .filter(s => s)
    .join('');
};

ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
49 50 51 52 53 54 55 56 57 58
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
59 60
  });
};
ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
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

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

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

    if (!item || !item.path) {
      return;
    }
    console.log('install  ' + item.name + '   to:  ' + item.component);
    const cmd = `umi block add https://github.com/ant-design/pro-blocks/tree/master/${firstUpperCase(
      item.path,
    )} --npm-client=cnpm  --path=${item.path}`;
    const data = await fetch(
      ` https://github.com/ant-design/pro-blocks/tree/master/${firstUpperCase(item.path)}`,
    );
    await execCmd(cmd);
    installBlockIteration(i + 1);
  };
  installBlockIteration(0);
};
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
88
installBlock();