diff --git a/package.json b/package.json index f62c913bce0acbd179aefd44e7134744e4529a2b..d4b7f5f86b179ca2649f35483013dda3b9f2f8af 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "lint:style": "stylelint --fix 'src/**/*.less' --syntax less", "lint:ts": "tslint -p . -c tslint.yml", "prettier": " check-prettier write", - "site": "umi build && npm run functions:build", + "site": "node ./script/fetch-block.js && umi build && npm run functions:build", "start": "umi dev", "start:no-mock": "cross-env MOCK=none umi dev", "test": "umi test", @@ -61,11 +61,13 @@ "bizcharts-plugin-slider": "^2.1.1-beta.1", "classnames": "^2.2.6", "dva": "^2.4.0", + "gg-editor": "^2.0.4", "hash.js": "^1.1.7", "lodash": "^4.17.10", "lodash-decorators": "^6.0.0", "memoize-one": "^5.0.0", "moment": "^2.22.2", + "node-fetch": "^2.6.0", "numeral": "^2.0.6", "nzh": "^1.0.4", "omit.js": "^1.0.0", @@ -151,4 +153,4 @@ "config/**/*.js*", "scripts/**/*.js" ] -} +} \ No newline at end of file diff --git a/scripts/fetch-block.js b/scripts/fetch-block.js index 4f770426ce69923fd6cc62a211c0797717fbe42e..abbbbe792ed5cc542c4f81cbca02929f077d6a53 100644 --- a/scripts/fetch-block.js +++ b/scripts/fetch-block.js @@ -4,25 +4,35 @@ const fetch = require('node-fetch'); const exec = require('child_process').exec; const getNewRouteCode = require('./repalceRouter'); const router = require('./router.config'); +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); +}; 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; + if (item.path !== '/user' || item.path !== '/') { + routers.push({ + ...item, + routes: '', + }); } - routers.push({ - ...item, - routes: '', - }); } - return null; + if (item.routes) { + routers = routers.concat(findAllInstallRouter(item.routes)); + } }); return routers; }; @@ -65,24 +75,47 @@ const { routesPath, code } = getNewRouteCode(relativePath, parentRouter); // write ParentRouter fs.writeFileSync(routesPath, code); -const installBlock = () => { +const installBlock = async () => { + let gitFiles = await fetchGithubFiles(); const installRouters = findAllInstallRouter(router); const installBlockIteration = async i => { const item = installRouters[i]; if (!item || !item.path) { - return; + return Promise.resolve(); } - 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)}`, - ); + 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}`; await execCmd(cmd); - installBlockIteration(i + 1); + return installBlockIteration(1); }; - installBlockIteration(0); + + // 安装 router 中没有的剩余区块. + installGitFile(0); }; installBlock();