提交 333389e5fb84a00e639a9d75ee96728d3e242df8

作者 sorrycc
1 个父辈 4f93ea15

chore: add create script

  1 +const Generator = require('yeoman-generator');
  2 +const { statSync } = require('fs');
  3 +const glob = require('glob');
  4 +
  5 +class BlockGenerator extends Generator {
  6 + writing() {
  7 + glob
  8 + .sync('**/*', {
  9 + cwd: this.templatePath(),
  10 + dot: true,
  11 + })
  12 + .forEach(file => {
  13 + const filePath = this.templatePath(file);
  14 + if (statSync(filePath).isFile()) {
  15 + this.fs.copyTpl(
  16 + this.templatePath(filePath),
  17 + this.destinationPath(file.replace(/^_/, '.')),
  18 + {
  19 + name: process.env.BLOCK.split('/')[1],
  20 + },
  21 + );
  22 + }
  23 + });
  24 + }
  25 +}
  26 +
  27 +module.exports = BlockGenerator;
  1 +{
  2 + "name": "block-generator"
  3 +}
  1 +{
  2 + "name": "<%= name %>",
  3 + "description": "",
  4 + "dependencies": {
  5 + "umi-request": "^1.0.0"
  6 + }
  7 +}
  1 +export default {
  2 + 'GET /api/BLOCK_NAME/text': {
  3 + text: 'I am a blank block',
  4 + },
  5 +};
  1 +import React, { Component } from 'react';
  2 +import { Button } from 'antd';
  3 +import { connect } from 'dva';
  4 +
  5 +import styles from './style.less';
  6 +
  7 +@connect(({ BLOCK_NAME_CAMEL_CASE }) => BLOCK_NAME_CAMEL_CASE)
  8 +class Page extends Component {
  9 + componentDidMount() {
  10 + const { dispatch } = this.props;
  11 + dispatch({
  12 + type: 'BLOCK_NAME_CAMEL_CASE/fetch',
  13 + });
  14 + }
  15 +
  16 + render() {
  17 + const { text } = this.props;
  18 + return (
  19 + <div className={styles.container}>
  20 + <Button>{text}</Button>
  21 + </div>
  22 + );
  23 + }
  24 +}
  25 +
  26 +export default Page;
  1 +import { getText } from './service';
  2 +
  3 +export default {
  4 + namespace: 'BLOCK_NAME_CAMEL_CASE',
  5 + state: {
  6 + text: 'loading...',
  7 + },
  8 +
  9 + effects: {
  10 + *fetch(_, { call, put }) {
  11 + const { text } = yield call(getText);
  12 + yield put({
  13 + type: 'save',
  14 + payload: {
  15 + text,
  16 + },
  17 + });
  18 + },
  19 + },
  20 +
  21 + reducers: {
  22 + save(state, { payload }) {
  23 + return {
  24 + ...state,
  25 + ...payload,
  26 + };
  27 + },
  28 + },
  29 +};
  1 +import request from 'umi-request';
  2 +
  3 +export function getText() {
  4 + return request('/api/BLOCK_NAME/text');
  5 +}
  1 +.container {
  2 + padding: 8px;
  3 +}
  1 +const { join } = require('path');
  2 +const BlockGenerator = require('./BlockGenerator');
  3 +
  4 +if (!process.env.BLOCK) {
  5 + console.log(`
  6 +Please specify the BLOCK env,
  7 +
  8 +e.g.
  9 +
  10 +$ BLOCK=templates/new-template yarn run create
  11 +$ BLOCK=blocks/new-block yarn run create
  12 + `);
  13 + process.exit(1);
  14 +}
  15 +
  16 +const generator = new BlockGenerator({
  17 + name: 'foo',
  18 + env: {
  19 + cwd: join(__dirname, '..', process.env.BLOCK),
  20 + },
  21 + resolved: require.resolve(require.resolve('./BlockGenerator')),
  22 +});
  23 +
  24 +generator.run(() => {
  25 + console.log('done');
  26 +});
  27 +
1 -# @umi-material/blank  
2 -  
3 -A blank block for quick start a umi page develop.  
4 -  
5 -## Usage  
6 -  
7 -```sh  
8 -umi block add https://github.com/umijs/umi-blocks/tree/master/blank  
9 -```  
10 -  
11 -## LICENSE  
12 -  
13 -MIT  
@@ -8,7 +8,8 @@ @@ -8,7 +8,8 @@
8 "lint-staged": "lint-staged", 8 "lint-staged": "lint-staged",
9 "lint-staged:js": "eslint --ext .js", 9 "lint-staged:js": "eslint --ext .js",
10 "prettier": "node ./_scripts/prettier.js", 10 "prettier": "node ./_scripts/prettier.js",
11 - "build": "node ./_scripts/generate.js" 11 + "build": "node ./_scripts/build.js",
  12 + "create": "node ./_scripts/create.js"
12 }, 13 },
13 "devDependencies": { 14 "devDependencies": {
14 "@ant-design/pro-layout": "^4.5.16", 15 "@ant-design/pro-layout": "^4.5.16",
@@ -22,7 +23,7 @@ @@ -22,7 +23,7 @@
22 "eslint-plugin-jsx-a11y": "^6.1.2", 23 "eslint-plugin-jsx-a11y": "^6.1.2",
23 "eslint-plugin-markdown": "^1.0.0-beta.6", 24 "eslint-plugin-markdown": "^1.0.0-beta.6",
24 "eslint-plugin-react": "^7.11.1", 25 "eslint-plugin-react": "^7.11.1",
25 - "glob": "^7.1.3", 26 + "glob": "^7.1.4",
26 "husky": "^1.2.0", 27 "husky": "^1.2.0",
27 "lint-staged": "^8.1.0", 28 "lint-staged": "^8.1.0",
28 "mkdirp": "^0.5.1", 29 "mkdirp": "^0.5.1",
@@ -33,7 +34,8 @@ @@ -33,7 +34,8 @@
33 "umi": "^2.3.0-0", 34 "umi": "^2.3.0-0",
34 "umi-plugin-block-dev": "^2.0.0", 35 "umi-plugin-block-dev": "^2.0.0",
35 "umi-plugin-react": "^1.3.0-0", 36 "umi-plugin-react": "^1.3.0-0",
36 - "umi-request": "^1.2.6" 37 + "umi-request": "^1.2.6",
  38 + "yeoman-generator": "^4.1.0"
37 }, 39 },
38 "lint-staged": { 40 "lint-staged": {
39 "x/**/*.{js,ts,tsx,json,jsx,less}": [ 41 "x/**/*.{js,ts,tsx,json,jsx,less}": [
@@ -11,7 +11,7 @@ e.g. @@ -11,7 +11,7 @@ e.g.
11 11
12 $ BLOCK=templates/user-dashboard yarn start 12 $ BLOCK=templates/user-dashboard yarn start
13 $ BLOCK=blocks/blank yarn start 13 $ BLOCK=blocks/blank yarn start
14 - `.trim()); 14 + `);
15 process.exit(1); 15 process.exit(1);
16 } 16 }
17 17
注册登录 后发表评论