Skip to content

Commit

Permalink
feat: support create umi block (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu authored and sorrycc committed Nov 10, 2018
1 parent 2dd9f42 commit a12d859
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
/templates/**/.umi
/templates/**/.umi-production
/templates/**/dist
/templates/block/pages
/.idea
/yarn.lock
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ $ yarn create umi <appName> [Options]
## Options

* `plugin`, create umi plugin
* `block`, create umi block

## LICENSE

Expand Down
9 changes: 8 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ if (args._.length) {
process.chdir(args._[0]);
}

const Generator = args.plugin ? require('../lib/PluginGenerator') : require('../lib/AppGenerator');
let Generator;
if (args.plugin) {
Generator = require('../lib/PluginGenerator');
} else if (args.block) {
Generator = require('../lib/BlockGenerator');
} else {
Generator = require('../lib/AppGenerator');
};
const generator = new Generator(process.argv.slice(2), {
name: 'basic',
env: {
Expand Down
59 changes: 59 additions & 0 deletions lib/BlockGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const Generator = require('yeoman-generator');
const { basename } = require('path');
const debug = require('debug')('create-umi');

module.exports = class PluginGenerator extends Generator {
constructor(args, opts) {
super(args, opts);

this.name = basename(process.cwd());
this.props = {};
}

prompting() {
const prompts = [
{
name: 'name',
message: `What's the block name?`,
default: this.name,
},
{
name: 'description',
message: `What's your block description?`,
},
{
name: 'mail',
message: `What's your email?`,
},
{
name: 'author',
message: `What's your name?`,
},
{
name: 'repo',
message: `Which repo is your block stored under github?`,
default: 'umijs/umi-blocks',
},
];
return this.prompt(prompts).then(props => {
this.props = Object.assign(this.props, props);
});
}

writing() {
debug(`this.name: ${this.name}`);
debug(`this.props: ${JSON.stringify(this.props)}`);

const context = {
name: this.name,
props: this.props,
};

this.fs.copy(this.templatePath('block', '_gitignore'), this.destinationPath('.gitignore'));
this.fs.copy(this.templatePath('block', '.umirc.js'), this.destinationPath('.umirc.js'));
this.fs.copy(this.templatePath('block', 'src'), this.destinationPath('src'));
this.fs.copyTpl(this.templatePath('block', 'package.json'), this.destinationPath('package.json'), context);
this.fs.copyTpl(this.templatePath('block', 'LICENSE'), this.destinationPath('LICENSE'), context);
this.fs.copyTpl(this.templatePath('block', 'README.md'), this.destinationPath('README.md'), context);
}
};
5 changes: 5 additions & 0 deletions templates/block/.umirc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
plugins: [
['umi-plugin-block-dev', {}],
],
}
21 changes: 21 additions & 0 deletions templates/block/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018-present <%= props.author %> (<%= props.mail %>)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
13 changes: 13 additions & 0 deletions templates/block/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @umi-material/<%= props.name %>

<%= props.description %>

## Usage

```sh
umi block https://github.com/<%= props.repo %>/tree/master/<%= props.name %>
```

## LICENSE

MIT
5 changes: 5 additions & 0 deletions templates/block/_gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/yarn.lock
/package-lock.json
/dist
/node_modules
/pages
24 changes: 24 additions & 0 deletions templates/block/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@umi-material/<%= props.name %>",
"version": "0.0.1",
"description": "<%= props.description %>",
"main": "src/index.js",
"scripts": {
"dev": "umi dev"
},
"repository": {
"type": "git",
"url": "https://github.com/<%= props.repo %>/<%= props.name %>"
},
"dependencies": {
"react": ">=16.0.0"
},
"devDependencies": {
"umi": "^2.0.0",
"umi-plugin-block-dev": "^1.0.0"
},
"authors": [
"<%= props.author %> <<%= props.mail %>>"
],
"license": "ISC"
}
3 changes: 3 additions & 0 deletions templates/block/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.normal {
background: #79F2AA;
}
9 changes: 9 additions & 0 deletions templates/block/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import styles from './index.css';

export default function() {
return (
<div className={styles.normal}>
<h1>I am a umi block!</h1>
</div>
);
}

0 comments on commit a12d859

Please sign in to comment.