上周,同事抱怨说 react 怎么不能像 angular 那样,使用命令行工具来生成一个组件。对呀,平时工做时,想要建立一个 react 的组件,都是直接 copy 一个组件,而后作一些修改。为何不能将这个过程交给程序去作呢?当天晚上,我就仿照 angular-cli 的 api,写了一个生成 react 组件的命令行工具 rcli。在这里记录一下实现的过程。javascript
rcli new PROJECT-NAME
命令,建立一个 react 项目,其中生成项目的脚手架固然是 create-react-app 啦rcli g component MyComponent
命令, 建立一个 MyComponent
组件, 这个组件是一个文件夹,在文件夹中包含 index.js
、MyComponent.js
、MyComponent.css
三个文件 后来发现 rcli g component MyComponent
命令在 平时开发过程当中是不够用的,由于这个命令只是建立了一个类组件,且继承自 React.Component
。css
在平时开发 过程当中,咱们会用到这三类组件:html
React.Component
的类组件React.PureComponent
的类组件注: 未来可使用 Hooks 来代替以前的类组件java
因而就有了 0.2.0 版本的 rcli
node
Usage: rcli [command] [options]
Commands:
new <appName>
g <componentName>
`new` command options:
-n, --use-npm Whether to use npm to download dependencies
`g` command options:
-c, --component <componentName> The name of the component
--no-folder Whether the component have not it's own folder -p, --pure-component Whether the component is a extend from PureComponent -s, --stateless Whether the component is a stateless component 复制代码
create-react-app
来建立一个应用rcli new PROJECT-NAME
cd PROJECT-NAME
yarn start
复制代码
或者你可使用 npm
安装依赖react
rcli new PROJECT-NAME --use-npm
cd PROJECT-NAME
npm start
复制代码
rcli g -c MyNewComponent -p
复制代码
rcli g -c MyNewComponent
复制代码
等于:git
rcli g -c ./MyNewComponent
复制代码
rcli g -c MyNewComponent -s
复制代码
# 默认生成的组件都会都包含在文件夹中的,若不想生成的组件被文件夹包含,则加上 --no-folder 选项
rcli g -c MyNewComponent --no-folder
复制代码
hileix-rcli
的项目npm init -y
初始化一个 npm package 的基本信息(即生成 package.json 文件)index.js
文件,用来写用户输入命令后的主要逻辑代码package.json
文件,添加 bin
字段:{
"name": "hileix-rcli",
"version": "0.2.0",
"description": "",
"main": "index.js",
"bin": {
"rcli": "./index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hileix/rcli.git"
},
"keywords": [],
"author": "hileix <304192604@qq.com> (https://github.com/hileix)",
"license": "MIT",
"bugs": {
"url": "https://github.com/hileix/rcli/issues"
},
"homepage": "https://github.com/hileix/rcli#readme",
"dependencies": {
"chalk": "^2.4.1",
"commander": "^2.19.0",
"cross-spawn": "^6.0.5",
"fs-extra": "^7.0.1"
}
}
复制代码
npm link
命令,建立软连接指向到本项目的 index.js
文件。这样,就能再开发的时候,直接使用 rcli
命令直接进行测试 ~rcli
会依赖一些包:rcli new PROJECT-NAME
#!/usr/bin/env node
'use strict';
const program = require('commander');
const log = console.log;
// new command
program
// 定义 new 命令,且后面跟一个必选的 projectName 参数
.command('new <projectName>')
// 对 new 命令的描述
.description('use create-react-app create a app')
// 定义使用 new 命令以后可使用的选项 -n(使用 npm 来安装依赖)
// 在使用 create-react-app 中,咱们能够能够添加 --use-npm 选项,来使用 npm 安装依赖(默认使用 yarn 安装依赖)
// 因此,我将这个选项添加到了 rcli 中
.option('-n, --use-npm', 'Whether to use npm to download dependencies')
// 定义执行 new 命令后调用的回调函数
// 第一个参数即是在定义 new 命令时的必选参数 projectName
// cmd 中包含了命令中选项的值,当咱们在 new 命令中使用了 --use-npm 选项时,cmd 中的 useNpm 属性就会为 true,不然为 undefined
.action(function(projectName, cmd) {
const isUseNpm = cmd.useNpm ? true : false;
// 建立 react app
createReactApp(projectName, isUseNpm);
});
program.parse(process.argv);
/** * 使用 create-react-app 建立项目 * @param {string} projectName 项目名称 * @param {boolean} isUseNpm 是否使用 npm 安装依赖 */
function createReactApp(projectName, isUseNpm) {
let args = ['create-react-app', projectName];
if (isUseNpm) {
args.push('--use-npm');
}
// 建立子进程,执行 npx create-react-app PROJECT-NAME [--use-npm] 命令
spawn.sync('npx', args, { stdio: 'inherit' });
}
复制代码
上面的代码边实现了 rcli new PROJECT-NAME
的功能:github
#!/usr/bin/env node
表示使用 node 执行本脚本rcli g [options]
#!/usr/bin/env node
'use strict';
const program = require('commander');
const spawn = require('cross-spawn');
const chalk = require('chalk');
const path = require('path');
const fs = require('fs-extra');
const log = console.log;
program
// 定义 g 命令
.command('g')
// 命令 g 的描述
.description('Generate a component')
// 定义 -c 选项,接受一个必选参数 componentName:组件名称
.option('-c, --component-name <componentName>', 'The name of the component')
// 定义 --no-folder 选项:表示当使用该选项时,组件不会被文件夹包裹
.option('--no-folder', 'Whether the component have not it is own folder')
// 定义 -p 选项:表示当使用该选项时,组件为继承自 React.PureComponent 的类组件
.option(
'-p, --pure-component',
'Whether the component is a extend from PureComponent'
)
// 定义 -s 选项:表示当使用该选项时,组件为无状态的函数组件
.option(
'-s, --stateless',
'Whether the component is a extend from PureComponent'
)
// 定义执行 g 命令后调用的回调函数
.action(function(cmd) {
// 当 -c 选项没有传参数进来时,报错、退出
if (!cmd.componentName) {
log(chalk.red('error: missing required argument `componentName`'));
process.exit(1);
}
// 建立组件
createComponent(
cmd.componentName,
cmd.folder,
cmd.stateless,
cmd.pureComponent
);
});
program.parse(process.argv);
/** * 建立组件 * @param {string} componentName 组件名称 * @param {boolean} hasFolder 是否含有文件夹 * @param {boolean} isStateless 是不是无状态组件 * @param {boolean} isPureComponent 是不是纯组件 */
function createComponent( componentName, hasFolder, isStateless = false, isPureComponent = false ) {
let dirPath = path.join(process.cwd());
// 组件在文件夹中
if (hasFolder) {
dirPath = path.join(dirPath, componentName);
const result = fs.ensureDirSync(dirPath);
// 目录已存在
if (!result) {
log(chalk.red(`${dirPath} already exists`));
process.exit(1);
}
const indexJs = getIndexJs(componentName);
const css = '';
fs.writeFileSync(path.join(dirPath, `index.js`), indexJs);
fs.writeFileSync(path.join(dirPath, `${componentName}.css`), css);
}
let component;
// 无状态组件
if (isStateless) {
component = getStatelessComponent(componentName, hasFolder);
} else {
// 有状态组件
component = getClassComponent(
componentName,
isPureComponent ? 'React.PureComponent' : 'React.Component',
hasFolder
);
}
fs.writeFileSync(path.join(dirPath, `${componentName}.js`), component);
log(
chalk.green(`The ${componentName} component was successfully generated!`)
);
process.exit(1);
}
/** * 获取类组件字符串 * @param {string} componentName 组件名称 * @param {string} extendFrom 继承自:'React.Component' | 'React.PureComponent' * @param {boolean} hasFolder 组件是否在文件夹中(在文件夹中的话,就会自动加载 css 文件) */
function getClassComponent(componentName, extendFrom, hasFolder) {
return '省略...';
}
/** * 获取无状态组件字符串 * @param {string} componentName 组件名称 * @param {boolean} hasFolder 组件是否在文件夹中(在文件夹中的话,就会自动加载 css 文件) */
function getStatelessComponent(componentName, hasFolder) {
return '省略...';
}
/** * 获取 index.js 文件内容 * @param {string} componentName 组件名称 */
function getIndexJs(componentName) {
return `import ${componentName} from './${componentName}'; export default ${componentName}; `;
}
复制代码
rcli g [options]
命令的功能