fetch-blocks.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.routes && (!router.component || layout)) {
  41. return { ...item, routes: filterParentRouter(item.routes, false) };
  42. }
  43. if (item.redirect) {
  44. return item;
  45. }
  46. return null;
  47. })
  48. .filter(item => item);
  49. };
  50. const firstUpperCase = pathString => {
  51. return pathString
  52. .replace('.', '')
  53. .split(/\/|\-/)
  54. .map(s => s.toLowerCase().replace(/( |^)[a-z]/g, L => L.toUpperCase()))
  55. .filter(s => s)
  56. .join('');
  57. };
  58. const execCmd = shell => {
  59. return new Promise((resolve, reject) => {
  60. exec(shell, { encoding: 'utf8' }, (error, statusbar) => {
  61. if (error) {
  62. console.log(error);
  63. return reject(error);
  64. }
  65. console.log(statusbar);
  66. resolve();
  67. });
  68. });
  69. };
  70. // replace router config
  71. const parentRouter = filterParentRouter(router, true);
  72. const { routesPath, code } = getNewRouteCode(relativePath, parentRouter);
  73. // write ParentRouter
  74. fs.writeFileSync(routesPath, code);
  75. const installBlock = async () => {
  76. let gitFiles = await fetchGithubFiles();
  77. const installRouters = findAllInstallRouter(router);
  78. const installBlockIteration = async i => {
  79. const item = installRouters[i];
  80. if (!item || !item.path) {
  81. return Promise.resolve();
  82. }
  83. const gitPath = firstUpperCase(item.path);
  84. // 如果这个区块在 git 上存在
  85. if (gitFiles.find(file => file.path === gitPath)) {
  86. console.log('install ' + chalk.green(item.name) + ' to: ' + chalk.yellow(item.path));
  87. gitFiles = gitFiles.filter(file => file.path !== gitPath);
  88. const skipModifyRouter = item.routes ? '--skip-modify-routes' : '';
  89. const cmd = `umi block add https://github.com/ant-design/pro-blocks/tree/master/${gitPath} --path=${
  90. item.path
  91. } ${skipModifyRouter}`;
  92. try {
  93. await execCmd(cmd);
  94. console.log(`install ${chalk.hex('#1890ff')(item.name)} success`);
  95. } catch (error) {
  96. console.error(error);
  97. }
  98. }
  99. return installBlockIteration(i + 1);
  100. };
  101. // 安装路由中设置的区块
  102. await installBlockIteration(0);
  103. const installGitFile = async i => {
  104. const item = gitFiles[i];
  105. if (!item || !item.path) {
  106. return Promise.resolve();
  107. }
  108. console.log('install ' + chalk.green(item.path));
  109. const cmd = `umi block add https://github.com/ant-design/pro-blocks/tree/master/${item.path}`;
  110. await execCmd(cmd);
  111. return installBlockIteration(1);
  112. };
  113. // 安装 router 中没有的剩余区块.
  114. installGitFile(0);
  115. };
  116. installBlock().then(() => {
  117. // 插入 pro 需要的演示代码
  118. insertCode();
  119. });