fetch-blocks.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const path = require('path');
  2. const fs = require('fs');
  3. const fetch = require('node-fetch');
  4. const exec = require('child_process').exec;
  5. const getNewRouteCode = require('./repalceRouter');
  6. const router = require('./router.config');
  7. const chalk = require('chalk');
  8. const insertCode = require('./insertCode');
  9. const fetchGithubFiles = async () => {
  10. const ignoreFile = ['_scripts'];
  11. const data = await fetch(`https://api.github.com/repos/ant-design/pro-blocks/git/trees/master`);
  12. if (data.status !== 200) {
  13. return;
  14. }
  15. const { tree } = await data.json();
  16. const files = tree.filter(file => file.type === 'tree' && !ignoreFile.includes(file.path));
  17. return Promise.resolve(files);
  18. };
  19. const relativePath = path.join(__dirname, '../config/config.ts');
  20. const findAllInstallRouter = router => {
  21. let routers = [];
  22. router.forEach(item => {
  23. if (item.component && item.path) {
  24. if (item.path !== '/user' || item.path !== '/') {
  25. routers.push({
  26. ...item,
  27. routes: !!item.routes,
  28. });
  29. }
  30. }
  31. if (item.routes) {
  32. routers = routers.concat(findAllInstallRouter(item.routes));
  33. }
  34. });
  35. return routers;
  36. };
  37. const filterParentRouter = (router, layout) => {
  38. return [...router]
  39. .map(item => {
  40. if (!item.path && item.component === '404') {
  41. return item;
  42. }
  43. if (item.routes && (!router.component || layout)) {
  44. return { ...item, routes: filterParentRouter(item.routes, false) };
  45. }
  46. if (item.redirect) {
  47. return item;
  48. }
  49. return null;
  50. })
  51. .filter(item => item);
  52. };
  53. const firstUpperCase = pathString => {
  54. return pathString
  55. .replace('.', '')
  56. .split(/\/|\-/)
  57. .map(s => s.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()))
  58. .filter(s => s)
  59. .join('');
  60. };
  61. const execCmd = shell => {
  62. return new Promise((resolve, reject) => {
  63. exec(shell, { encoding: 'utf8' }, (error, statusbar) => {
  64. if (error) {
  65. console.log(error);
  66. return reject(error);
  67. }
  68. console.log(statusbar);
  69. resolve();
  70. });
  71. });
  72. };
  73. // replace router config
  74. const parentRouter = filterParentRouter(router, true);
  75. const { routesPath, code } = getNewRouteCode(relativePath, parentRouter);
  76. // write ParentRouter
  77. fs.writeFileSync(routesPath, code);
  78. const installBlock = async () => {
  79. let gitFiles = await fetchGithubFiles();
  80. const installRouters = findAllInstallRouter(router);
  81. const installBlockIteration = async i => {
  82. const item = installRouters[i];
  83. if (!item || !item.path) {
  84. return Promise.resolve();
  85. }
  86. const gitPath = firstUpperCase(item.path);
  87. // 如果这个区块在 git 上存在
  88. if (gitFiles.find(file => file.path === gitPath)) {
  89. console.log('install ' + chalk.green(item.name) + ' to: ' + chalk.yellow(item.path));
  90. gitFiles = gitFiles.filter(file => file.path !== gitPath);
  91. const skipModifyRouter = item.routes ? '--skip-modify-routes' : '';
  92. const cmd = `umi block add https://github.com/ant-design/pro-blocks/tree/master/${gitPath} --path=${item.path} ${skipModifyRouter}`;
  93. try {
  94. await execCmd(cmd);
  95. console.log(`install ${chalk.hex('#1890ff')(item.name)} success`);
  96. } catch (error) {
  97. console.error(error);
  98. }
  99. }
  100. return installBlockIteration(i + 1);
  101. };
  102. // 安装路由中设置的区块
  103. await installBlockIteration(0);
  104. const installGitFile = async i => {
  105. const item = gitFiles[i];
  106. if (!item || !item.path) {
  107. return Promise.resolve();
  108. }
  109. console.log('install ' + chalk.green(item.path));
  110. const cmd = `umi block add https://github.com/ant-design/pro-blocks/tree/master/${item.path}`;
  111. await execCmd(cmd);
  112. return installBlockIteration(1);
  113. };
  114. // 安装 router 中没有的剩余区块.
  115. installGitFile(0);
  116. };
  117. installBlock().then(() => {
  118. // 插入 pro 需要的演示代码
  119. insertCode();
  120. });