Skip to content
This repository has been archived by the owner on Oct 20, 2021. It is now read-only.

Commit

Permalink
feat: add npm run create
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimi-Gao committed May 17, 2018
1 parent e41527c commit b2eab0b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
38 changes: 38 additions & 0 deletions bin/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const mkdirp = require('mkdirp')
const _ = require('lodash')
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')

let option = process.argv.slice(2)
let name = option[0]

if (name) {
let dir = path.join(__dirname, '../src/components/' + name + '/')
let template = path.join(__dirname, 'template/')

mkdirp.sync(dir)

let files = [{
source: template + 'index.js',
target: dir + 'index.js'
}, {
source: template + 'index.less',
target: dir + 'index.less'
}, {
source: template + 'doc.js',
target: path.join(__dirname, '../site/pages/components/docs/') + name + '.doc'
}]

let context = {
name: name,
className: name.replace(/([A-Z])/g, '-$1').slice(1).toLowerCase()
}
files.forEach(function (item) {
fs.writeFileSync(item.target, _.template(fs.readFileSync(item.source, 'utf8'))(context))
})

console.log(chalk.green('Build success!'))
} else {
console.log(chalk.red('No component name provided, try `npm run create MyComponent` instead.'))
}
13 changes: 13 additions & 0 deletions bin/template/doc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @title 基本功能
* @desc 描述
*/
import <%= name %> from 'earth-ui/<%= name %>'

const <%= name %>Basic = () => {
return (
<<%= name %> />
)
}

@component <%= name %>
31 changes: 31 additions & 0 deletions bin/template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import './index.less'

class <%= name %> extends Component {
constructor () {
super()
this.state = {

}
}

render () {
const { className, ...other } = this.props
return (
<div className={cx('cmui-<%= className %>', className)} {...other}>
something...
</div>
)
}
}

<%= name %>.propTypes = {
className: PropTypes.string,

// intro to test prop
test: PropTypes.string
}

export default <%= name %>
3 changes: 3 additions & 0 deletions bin/template/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.cmui-<%= className %> {

}

0 comments on commit b2eab0b

Please sign in to comment.