-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
edenhliu
committed
Mar 13, 2019
1 parent
c8c86b7
commit cfe6c8b
Showing
8 changed files
with
202 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const path = require('path'); | ||
const semver = require('semver'); | ||
const chalk = require('chalk'); | ||
|
||
const Generator = require('yeoman-generator'); | ||
const { basename, join } = require('path'); | ||
const logger = require('@a8k/cli-utils/logger'); | ||
const { PROJECT_MODE_SINGLE } = require('a8k/lib/const'); | ||
const { toArray, createMultiExamplePage, createSingleExamplePage } = require('./heper'); | ||
|
||
// logger.setOptions({ debug: true }); | ||
|
||
class CreateGenerator extends Generator { | ||
constructor(args, opts) { | ||
super(args, opts); | ||
|
||
this.name = basename(process.cwd()); | ||
this.props = { | ||
config: opts.a8kconfig, | ||
}; | ||
|
||
this.pagesPath = path.resolve(args.env.cwd, 'src/pages'); | ||
|
||
this.sourceRoot(join(__dirname, '../templates/')); | ||
} | ||
|
||
async prompting() { | ||
const { name } = await this.prompt([ | ||
{ | ||
name: 'name', | ||
message: '页面名称(使用下划线分割单词)', | ||
type: 'input', | ||
validate(input) { | ||
if (!input) { | ||
return '请输入组件名称'; | ||
} | ||
if (!/^([a-z_\d]+)$/.test(input)) { | ||
return '格式错误(支持英文小写字母、数字、下划线)'; | ||
} | ||
return true; | ||
}, | ||
}, | ||
]); | ||
this.props.name = name; | ||
} | ||
|
||
_copyFiles(files = []) { | ||
files.forEach(([src, dest]) => { | ||
src = toArray(src); | ||
dest = toArray(dest); | ||
this.fs.copy(this.templatePath(...src), this.destinationPath(...dest)); | ||
}); | ||
} | ||
|
||
_copyTpls(files = []) { | ||
files.forEach(([src, dest]) => { | ||
src = toArray(src); | ||
dest = toArray(dest); | ||
this.fs.copyTpl(this.templatePath(...src), this.destinationPath(...dest), this.props); | ||
}); | ||
} | ||
|
||
writing() { | ||
logger.debug(`this.props: ${JSON.stringify(this.props)}`); | ||
if (this.props.config.mode === PROJECT_MODE_SINGLE) { | ||
createSingleExamplePage(this, this.props.name); | ||
} else { | ||
createMultiExamplePage(this, this.props.name); | ||
} | ||
} | ||
} | ||
|
||
module.exports = ({ config, options }) => { | ||
if (!semver.satisfies(process.version, '>= 8.0.0')) { | ||
console.error(chalk.red('✘ The generator will only work with Node v8.0.0 and up!')); | ||
process.exit(1); | ||
} | ||
|
||
return new Promise(resolve => { | ||
new CreateGenerator( | ||
{ | ||
name: 'basic', | ||
env: { cwd: options.baseDir }, | ||
resolved: __filename, | ||
}, | ||
{ a8kconfig: config } | ||
).run(resolve); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const toArray = a => { | ||
return Array.isArray(a) ? a : [a]; | ||
}; | ||
|
||
/** | ||
* | ||
* @param {Generator} context Generator 上下文 | ||
* @param {string} dist 目标目录:src/components,src/pages/xxx/components | ||
* @param {string} name 组件名称 | ||
* @param {boolean} useConnect 是否使用connect | ||
*/ | ||
const createExampleComponent = (context, dist, name, useConnect) => { | ||
[ | ||
['common/componentTemplate/index.jsx.tpl', `${dist}/${name}/index.jsx`], | ||
['common/componentTemplate/index.scss.tpl', `${dist}/${name}/index.scss`], | ||
].forEach(([src, dest]) => { | ||
src = context.templatePath(...toArray(src)); | ||
dest = context.destinationPath(...toArray(dest)); | ||
context.fs.copyTpl(src, dest, { | ||
name, | ||
className: `x-component-${name.toLowerCase()}`, | ||
useConnect, | ||
}); | ||
}); | ||
}; | ||
|
||
const createMultiExamplePage = (context, name) => { | ||
[ | ||
['multi/pageTemplate/action_creators.js', `src/pages/${name}/action_creators.js`], | ||
['multi/pageTemplate/action_types.js', `src/pages/${name}/action_types.js`], | ||
['multi/pageTemplate/index.html', `src/pages/${name}/index.html`], | ||
['multi/pageTemplate/index.jsx', `src/pages/${name}/index.jsx`], | ||
['multi/pageTemplate/index.scss', `src/pages/${name}/index.scss`], | ||
['multi/pageTemplate/ProviderContainer.jsx', `src/pages/${name}/ProviderContainer.jsx`], | ||
['multi/pageTemplate/reducer.js', `src/pages/${name}/reducer.js`], | ||
['multi/pageTemplate/store.js', `src/pages/${name}/store.js`], | ||
].forEach(([src, dest]) => { | ||
src = toArray(src); | ||
dest = toArray(dest); | ||
context.fs.copyTpl(context.templatePath(...src), context.destinationPath(...dest), { | ||
name, | ||
className: `x-page-${name.toLowerCase()}`, | ||
}); | ||
}); | ||
}; | ||
|
||
const createSingleExamplePage = (context, name) => { | ||
[ | ||
['single/pageTemplate/action_creators.js', `src/pages/${name}/action_creators.js`], | ||
['single/pageTemplate/action_types.js', `src/pages/${name}/action_types.js`], | ||
['single/pageTemplate/index.jsx', `src/pages/${name}/index.jsx`], | ||
['single/pageTemplate/index.scss', `src/pages/${name}/index.scss`], | ||
['single/pageTemplate/reducer.js', `src/pages/${name}/reducer.js`], | ||
].forEach(([src, dest]) => { | ||
src = toArray(src); | ||
dest = toArray(dest); | ||
context.fs.copyTpl(context.templatePath(...src), context.destinationPath(...dest), { | ||
name, | ||
className: `x-page-${name.toLowerCase()}`, | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
toArray, | ||
createExampleComponent, | ||
createMultiExamplePage, | ||
createSingleExamplePage, | ||
}; |
Oops, something went wrong.