Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text
# These files are text and should be normalized (convert crlf => lf)
* ext eol=lf
# 二进制文件,防止修改
*.jpg binary
*.png binary
*.gif binary
#*.svg binary
#*.eot binary
#*.woff binary
#*.ttf binary
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
node_modules/
*.log
*.swp
.tmp
*.log
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 李梦龙

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.
3 changes: 3 additions & 0 deletions bin/rcli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../lib/cli/');
51 changes: 51 additions & 0 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env node

'use strict';

// https://www.npmjs.com/package/colorful
require('colorful').colorful();
// https://www.npmjs.com/package/commander
const program = require('commander');
const packageInfo = require('../../package.json');


program
.version(packageInfo.version)

program
.command('init') // rcli init
.description('生成一个项目')
.alias('i') // 简写
.action(() => {
require('../cmd/init')();
});

program
.command('add') // rcli add
.description('添加新模板')
.alias('a') // 简写
.action(() => {
require('../cmd/add')();
});

program
.command('list') // rcli list
.description('查看模板列表')
.alias('l') // 简写
.action(() => {
require('../cmd/list')();
});

program
.command('delete') // rcli delete
.description('查看模板列表')
.alias('d') // 简写
.action(() => {
require('../cmd/delete')();
});

program.parse(process.argv);

if(!program.args.length){
program.help()
}
48 changes: 48 additions & 0 deletions lib/cmd/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'
const co = require('co');
const prompt = require('co-prompt');
const chalk = require('chalk');
const fs = require('fs');

const config = require('../../templates');

module.exports = () => {

co(function *() {
// 分步接收用户输入的参数
const tplName = yield prompt('模板名字: ');
const gitUrl = yield prompt('Git https 链接: ');
const branch = yield prompt('Git 分支: ');
const description = yield prompt('模板描述: ');

// 避免重复添加
if (!config.tpl[tplName]) {
config.tpl[tplName] = {};
config.tpl[tplName]['url'] = gitUrl.replace(/[\u0000-\u0019]/g, ''); // 过滤unicode字符
config.tpl[tplName]['branch'] = branch;
config.tpl[tplName]['description'] = description;
} else {
console.log(chalk.red.bold('\n ❌ 模板已经存在!\n');

process.exit();
};

// 把模板信息写入templates.json
fs.writeFile(__dirname + '/../../templates.json', JSON.stringify(config), 'utf-8', (err) => {
// 处理错误
if (err) {
console.log(err);
console.log(chalk.red.bold('\n ❌ 请重新运行!\n'));

process.exit();
}

console.log(chalk.green.bold('\n ✅ 新模板添加成功!\n'));
console.log(chalk.blue('\n模板列表是: \n'));
console.log(config);
console.log('\n');

process.exit();
});
});
};
41 changes: 41 additions & 0 deletions lib/cmd/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'
const co = require('co');
const prompt = require('co-prompt');
const chalk = require('chalk');
const fs = require('fs');

const config = require('../../templates');

module.exports = () => {
co(function *() {
// 接收用户输入的参数
let tplName = yield prompt('模板名字: ');

// 删除对应的模板
if (config.tpl[tplName]) {
config.tpl[tplName] = undefined;
} else {
console.log(chalk.red.bold('\n ❌ 模板不经存在!'));
console.log('\n');

process.exit();
}

// 写入template.json
fs.writeFile(__dirname + '/../../templates.json', JSON.stringify(config), 'utf-8', (err) => {
if (err) {
console.log(err);
console.log(chalk.red.bold('\n ❌ 请重新运行!\n'));

process.exit();
}

console.log(chalk.green.bold('\n ✅ 新模板删除成功!'));
console.log(chalk.blue('\n模板列表是: \n'));
console.log(config);
console.log('\n');

process.exit();
});
});
};
60 changes: 60 additions & 0 deletions lib/cmd/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'
const exec = require('child_process').exec; // 操作命令行
const path = require('path');
const co = require('co');
const ora = require('ora');
const prompt = require('co-prompt');
const chalk = require('chalk');

const config = require('../../templates');

const spinner = ora('正在生成...');

module.exports = () => {

co(function *() {
// 处理用户输入
const tplName = yield prompt('模板名字: ');
const projectName = yield prompt('项目名字: ');
let gitUrl;
let branch;

if (!config.tpl[tplName]) {
console.log(chalk.red.bold('\n ❌ 模板并没有没添加,请运行 rcli add 进行添加!'));

process.exit();
}

gitUrl = config.tpl[tplName].url;
branch = config.tpl[tplName].branch;

// git命令,远程拉取项目并自定义项目名
const cmdStr = `git clone ${gitUrl} ${projectName} && cd ${projectName} && git checkout ${branch}`;

spinner.start();

exec(cmdStr, (error, stdout, stderr) => {
if (error) {
console.log(error);
console.log(chalk.red.bold('\n ❌ 请重新运行!\n'));

process.exit();
}
// 删除 git 文件
exec('cd ' + projectName + ' && rm -rf .git', (err, out) => {
if (err) {
console.log(err);

process.exit();
}

spinner.stop();

console.log(chalk.green.bold('\n ✅ 初始化完成!'))
console.log(`\n cd ${projectName} && npm install \n`)

process.exit();
});
})
})
}
11 changes: 11 additions & 0 deletions lib/cmd/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'
const chalk = require('chalk');

const config = require('../../templates');

module.exports = () => {
console.log(chalk.blue('\n模板列表是: \n'));
console.log(config.tpl);
console.log('\n');
process.exit();
};
1 change: 1 addition & 0 deletions lib/templates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"tpl":{}}
62 changes: 62 additions & 0 deletions package-lock.json

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

33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@fe6/rcli",
"version": "0.0.1",
"description": "开发 react 组件的脚手架",
"bin": {
"rcli": "./bin/rcli.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fe6/rcli.git"
},
"keywords": [
"react",
"react-component",
"react-components",
"react-cli",
"cli"
],
"author": "李梦龙",
"license": "MIT",
"bugs": {
"url": "https://github.com/fe6/rcli/issues"
},
"homepage": "https://github.com/fe6/rcli#readme",
"dependencies": {
"chalk": "^2.3.2",
"co": "^4.6.0",
"co-prompt": "^1.0.0",
"colorful": "^2.1.0",
"commander": "^2.15.1",
"ora": "^2.0.0"
}
}
1 change: 1 addition & 0 deletions templates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"tpl":{}}
Loading