Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: zenxds/generator-ctu
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9770592f6219a329c7690554422f6ff1f2cd89c8
Choose a base ref
..
head repository: zenxds/generator-ctu
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a3075703d76597fd1ec2cedd3abbeda1604fa393
Choose a head ref
Showing with 100 additions and 40 deletions.
  1. +20 −1 README.md
  2. +31 −25 generators/app/config.js
  3. +41 −13 generators/app/index.js
  4. +7 −0 lib/util.js
  5. +1 −1 package.json
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -21,4 +21,23 @@ yo ctu
1. ES6
2. React
3. React SPA
4. Koa
4. NightKay App
5. H5运营
6. PC运营
7. Koa
8. Node Lib
9. React Lib
10. 组件库
11. 组件库组件

## 开发

配置说明
```
{
name: "", // 模板名,非必填
description: "", // 模板说明,必填
mode: "", // 脚手架模式,非必填,目前只有 simple(简单模式,只下载解压模板)
repository: "" // 外网可访问的模板压缩包地址地址,必填
}
```
56 changes: 31 additions & 25 deletions generators/app/config.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
module.exports = {
projects: [
{
name: "es6-boilerplate",
description: "ES6",
repository: "https://github.com/zenxds/es6-boilerplate/archive/master.tar.gz"
},
{
name: "react-boilerplate",
description: "React",
repository: "https://github.com/zenxds/react-boilerplate/archive/master.tar.gz"
},
{
name: "react-spa-boilerplate",
description: "React SPA",
repository: "https://github.com/zenxds/react-spa-boilerplate/archive/master.tar.gz"
},
{
name: "h5-boilerplate",
description: "H5运营",
repository: "https://github.com/zenxds/h5-boilerplate/archive/master.tar.gz"
name: "nightkay-app-boilerplate",
description: "nightKay App",
repository: "https://github.com/zenxds/nightkay-app-boilerplate/archive/master.tar.gz"
},
{
name: "pc-boilerplate",
description: "PC运营",
repository: "https://github.com/zenxds/pc-boilerplate/archive/master.tar.gz"
name: "boilerplate-component-gallery",
description: "组件库",
repository: "https://github.com/gafish/boilerplate-component-gallery/archive/master.tar.gz"
},
{
name: "koa-boilerplate",
description: "Koa",
repository: "https://github.com/zenxds/koa-boilerplate/archive/master.tar.gz"
name: "boilerplate-simple-component",
description: "组件库组件",
mode: "simple",
repository: "https://github.com/gafish/boilerplate-simple-component/archive/master.tar.gz"
},
{
name: "node-lib-boilerplate",
@@ -41,14 +32,29 @@ module.exports = {
repository: "https://github.com/zenxds/react-lib-boilerplate/archive/master.tar.gz"
},
{
name: "boilerplate-component-gallery",
description: "组件库",
repository: "https://github.com/gafish/boilerplate-component-gallery/archive/master.tar.gz"
name: "h5-boilerplate",
description: "H5运营",
repository: "https://github.com/zenxds/h5-boilerplate/archive/master.tar.gz"
},
{
name: "boilerplate-simple-component",
description: "单个组件",
repository: "https://github.com/gafish/boilerplate-simple-component/archive/master.tar.gz"
name: "pc-boilerplate",
description: "PC运营",
repository: "https://github.com/zenxds/pc-boilerplate/archive/master.tar.gz"
},
{
name: "koa-boilerplate",
description: "Koa",
repository: "https://github.com/zenxds/koa-boilerplate/archive/master.tar.gz"
},
{
name: "es6-boilerplate",
description: "ES6",
repository: "https://github.com/zenxds/es6-boilerplate/archive/master.tar.gz"
},
{
name: "react-boilerplate",
description: "React",
repository: "https://github.com/zenxds/react-boilerplate/archive/master.tar.gz"
}
]
}
54 changes: 41 additions & 13 deletions generators/app/index.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@ const Generator = require('yeoman-generator')
const config = require('./config')
const util = require('../../lib/util')

const isNotSimpleMode = answers => !util.isSimpleMode(answers.type)

class CTU extends Generator {
constructor(args, opts) {
super(args, opts)
@@ -24,6 +26,7 @@ class CTU extends Generator {

prompting() {
const basename = path.basename(this.destinationRoot())

return this.prompt([
{
type: 'list',
@@ -32,36 +35,46 @@ class CTU extends Generator {
choices: config.projects.map((item, index) => {
return {
name: item.description,
value: index
value: { ...item, index },
}
}),
store: true
store: true,
pageSize: 20
},
{
type: 'input',
name: 'name',
message: '请输入项目名',
default: basename
default: basename,
// 简单模式不需要输入项目名
when: isNotSimpleMode
},
{
type: 'input',
name: 'version',
message: '请输入项目版本号',
default: '0.1.0'
default: '0.1.0',
// 简单模式不需要输入版本号
when: isNotSimpleMode
},
{
type: 'input',
name: 'description',
message: '请输入项目描述',
default: basename
default: basename,
// 简单模式不需要输入项目描述
when: isNotSimpleMode
},
{
type: 'input',
name: 'author',
message: '请输入项目作者',
default: os.userInfo().username
default: os.userInfo().username,
// 简单模式不需要输入作者
when: isNotSimpleMode
}
]).then((answers) => {
])
.then((answers) => {
this.answers = answers
})
}
@@ -71,16 +84,25 @@ class CTU extends Generator {
}

writing() {
const root = this.destinationRoot()
const jsonPath = path.join(root, 'package.json')
const answers = this.answers
const repository = config.projects[answers.type].repository
const repository = config.projects[answers.type.index].repository
const isSimpleMode = util.isSimpleMode(answers.type)
const root = isSimpleMode ? process.cwd(): this.destinationRoot()
const jsonPath = path.join(root, 'package.json')

if (!util.isEmptyDir(root)) {
return Promise.reject('目标目录不为空')
}

this.log('正在下载项目模板'.green)

// 简单模式仅需要下载模板到当前目录
if (isSimpleMode) {
return util.downloadAndUnzip(repository, process.cwd()).then(() => {
this.log('当前任务已完成'.blue)
})
}

return util.downloadAndUnzip(repository, root).then(() => {
const json = require(jsonPath)
Object.assign(json, {
@@ -97,11 +119,17 @@ class CTU extends Generator {
}

install() {
const answers = this.answers
// 简单模式不需要执行安装
if (util.isSimpleMode(answers.type)) {
return
}

const root = this.destinationRoot()
const useYarn = fs.existsSync(path.join(root, 'yarn.lock'))

this.log('正在安装项目依赖'.green)
this.spawnCommandSync(useYarn ? 'yarn' : 'npm', ['install', '--registry=https://registry.dingxiang-inc.net'])
this.log('正在安装项目依赖'.green)
this.spawnCommandSync(useYarn ? 'yarn' : 'npm', ['install', '--registry=https://registry.dingxiang-inc.com'])
this.log('正在启动项目'.green)
this.spawnCommandSync('npm', ['start'])
}
7 changes: 7 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -27,4 +27,11 @@ exports.downloadAndUnzip = function(url, dest) {
*/
exports.isEmptyDir = (dest) => {
return !fs.existsSync(dest) || (fs.statSync(dest).isDirectory() && !fs.readdirSync(dest).length)
}

/**
* 是否是简单模式
*/
exports.isSimpleMode = (type) => {
return type.mode === 'simple'
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "generator-ctu",
"version": "0.2.0",
"version": "0.2.4",
"description": "ctu generator",
"files": [
"generators",