Skip to content

Commit

Permalink
Add create command.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 10, 2018
1 parent 4a40233 commit bfbec80
Show file tree
Hide file tree
Showing 17 changed files with 508 additions and 21 deletions.
21 changes: 18 additions & 3 deletions .bin/kkt.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ program
program
.command('create <app-name>')
.description('create a new project powered by kkt')
.option('-c, --clone', 'Use git clone when fetching remote preset')
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
.option('-f, --force', 'Overwrite target directory if it exists')
.on('--help', () => {
console.log()
Expand All @@ -28,8 +28,8 @@ program
console.log(' $ kkt init username/repo my-project')
console.log()
})
.action((cmd) => {
// require('../src/create')(cmd)
.action((name, cmd) => {
require('../src/create')(name, cleanArgs(cmd))
})

program
Expand Down Expand Up @@ -86,3 +86,18 @@ program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}

// 命令将Command对象本身作为选项传递,
// 仅将实际选项提取到新对象中。
function cleanArgs(cmd) {
const args = {}
cmd.options.forEach(o => {
const key = o.long.replace(/^--/, '')
// 如果一个选项不存在并且Command有一个同名的方法
    // 它不应该被复制
if (typeof cmd[key] !== 'function') {
args[key] = cmd[key]
}
})
return args
}
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,36 @@ Cli tool for creating react apps.
npm install -g kkt
```

## 使用

```bash
$ kkt create my-project
$ cd my-project && npm start
```

## webpack 配置修改

在根目录新建 `.kktrc.js` 这里返回两个参数 `webpackConf``ServerConf`,返回的是 `webpack` 配置,webpack 配置,区分开发模式和生成模式,是通过 `webpackConf.mode` 的值为 `development | production` 来判断;
在根目录新建 `.kktrc.js` 这里返回两个参数 `webpackConf``devServer`,返回的是 `webpack` 配置,webpack 配置,区分开发模式和生成模式,是通过 `webpackConf.mode` 的值为 `development | production` 来判断;

```js
module.exports = function (webpackConf, ServerConf) {
module.exports = function (webpackConf, devServer) {
if (webpackConf) {
if (webpackConf.mode == 'development') {
if (webpackConf.mode === 'development') {
// 开发模式下更改的 webpack 配置
}
if (webpackConf.mode == 'production') {
if (webpackConf.mode === 'production') {
// 生产模式下更改的 webpack 配置
}
return webpackConf
};
if (ServerConf) {
ServerConf.proxy = {
if (devServer) {
devServer.proxy = {
'/api': {
target: 'http://127.0.0.1:1130',
changeOrigin: true,
},
}
return ServerConf;
return devServer;
}
}
```
Expand All @@ -45,16 +52,16 @@ const proxy = {
'GET /api/user': { id: 1, username: 'kenny', sex: 6 },
'GET /api/user/list': [
{ id: 1, username: 'kenny', sex: 6 },
{ id: 2, username: 'kenny', sex: 6 }
{ id: 2, username: 'kkt', sex: 6 }
],
'POST /api/login/account': (req, res) => {
const { password, username } = req.body;
if (password === '888888' && username === 'admin') {
return res.json({
status: 'ok',
code: 0,
token: "sdfsdfsdfdsf",
data: { id: 1, username: 'kenny', sex: 6 }
token: "kkt",
data: { id: 1, username: 'kktname', sex: 6 }
});
} else {
return res.json({
Expand Down
116 changes: 109 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"clean-webpack-plugin": "^0.1.19",
"colors-cli": "^1.0.13",
"commander": "^2.15.1",
"copy-template-dir": "^1.4.0",
"create-spare-webpack-plugin": "^2.0.0",
"css-loader": "^0.28.11",
"detect-port": "^1.2.3",
Expand All @@ -41,14 +42,15 @@
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.8.2",
"execa": "^0.10.0",
"file-loader": "^1.1.11",
"fs-extra": "^6.0.1",
"gh-pages": "^1.2.0",
"html-webpack-plugin": "^3.2.0",
"inquirer": "^6.0.0",
"less": "^3.0.4",
"less-loader": "^4.1.0",
"loading-cli": "^1.0.6",
"loading-cli": "^1.0.7",
"open-browsers": "^1.1.1",
"postcss-flexbugs-fixes": "^3.3.1",
"postcss-loader": "^2.1.5",
Expand Down

0 comments on commit bfbec80

Please sign in to comment.