用 nodejs 写一个命令行工具 :创建 react 组件的命令行工具

前言

上周,同事抱怨说 react 怎么不能像 angular 那样,使用命令行工具来生成一个组件。对呀,平时工作时,想要创建一个 react 的组件,都是直接 copy 一个组件,然后做一些修改。为什么不能将这个过程交给程序去做呢?当天晚上,我就仿照 angular-cli 的 api,写了一个生成 react 组件的命令行工具 rcli。在这里记录一下实现的过程。

api 设计

0.1.0 版本的 rcli 参照 angular-cli 的设计,有两个功能:

  1. 使用 rcli new PROJECT-NAME 命令,创建一个 react 项目,其中生成项目的脚手架当然是 create-react-app
  2. 使用 rcli g component MyComponent 命令, 创建一个 MyComponent 组件, 这个组件是一个文件夹,在文件夹中包含 index.jsMyComponent.jsMyComponent.css 三个文件

后来发现 rcli g component MyComponent 命令在 平时开发过程中是不够用的,因为这个命令只是创建了一个类组件,且继承自 React.Component

在平时开发 过程中,我们会用到这三类组件:

  1. 继承自 React.Component 的类组件
  2. 继承自 React.PureComponent 的类组件
  3. 函数组件(无状态组件)

注: 将来可以使用 Hooks 来代替之前的类组件

于是就有了 0.2.0 版本的 rcli

0.2.0 版本的 rcli

用法


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 安装依赖

```rcli new PROJECT-NAME --use-npm
cd PROJECT-NAME
npm start
```

生成纯组件(继承自 PureComponent,以提高性能)

```rcli g -c MyNewComponent -p
```

生成类组件(有状态组件)

```rcli g -c MyNewComponent
```

等于:

```rcli g -c ./MyNewComponent
```

生成函数组件(无状态组件)

```rcli g -c MyNewComponent -s
```

生成组件不在文件夹内(也不包含 css 文件和 index.js 文件)

```# 默认生成的组件都会都包含在文件夹中的,若不想生成的组件被文件夹包含,则加上 --no-folder 选项
rcli g -c MyNewComponent --no-folder
```

实现过程

1. 创建项目

  • 创建名为 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 命令直接进行测试 ~

2. rcli 会依赖一些包:

  • commander:tj 大神写的一款专门处理命令行的工具。主要用来解析用户输入的命令、选项
  • cross-spawn:nodejs spawn 的跨平台的版本。主要用来创建子进程执行一些命令
  • chalk:给命令行中的文字添加样式。
  • path:nodejs path 模块
  • fs-extra:提供对文件操作的方法

3.实现 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 的功能:

  • #!/usr/bin/env node 表示使用 node 执行本脚本

4.实现 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] 命令的功能

总结

  • api 设计是很重要的:好的 api 设计能让使用者更加方便地使用,且变动少
  • 当自己想不到该怎么设计 api 时,可以参考别人的 api,看看别人是怎么设计的好用的

来源:https://segmentfault.com/a/1190000017392058

用 nodejs 写一个命令行工具 :创建 react 组件的命令行工具的更多相关文章

  1. 从零开始写一个npm包,一键生成react组件(偷懒==提高效率)

    前言 最近写项目开发新模块的时候,每次写新模块的时候需要创建一个组件的时候(包含组件css,index.js,组件js),就只能会拷贝其他组件修改名称 ,但是写了1-2个后发现效率太低了,而且极容易出 ...

  2. Vue折腾记 - (3)写一个不大靠谱的typeahead组件

    Vue折腾记 - (3)写一个不大靠谱的typeahead组件 2017年07月20日 15:17:05 阅读数:691 前言 typeahead在网站中的应用很多..今天跟着我来写一个不大靠谱的ty ...

  3. webStrom快捷键快速创建React组件

    1. rcc + tab键 - - 用ES6模块系统创建一个React组件类 2. rccp + tab键 - - 创建一个带有PropTypes和ES6模块系统的React组件类 3. rcfc + ...

  4. 【每天一个Linux命令】19. 创建文件夹目录命令mkdir

    命令用途 mkdir 命令用来创建指定的名称的目录 使用说明 1.  创建目录的用户在当前目录中具有写权限 2. 指定的目录名不能是当前目录中已有的目录. 命令实例 0. 帮助文件 bixiaopen ...

  5. 手写一个React-Redux,玩转React的Context API

    上一篇文章我们手写了一个Redux,但是单纯的Redux只是一个状态机,是没有UI呈现的,所以一般我们使用的时候都会配合一个UI库,比如在React中使用Redux就会用到React-Redux这个库 ...

  6. 创建React组件

    组件概述 组件可以将UI切分成一些独立的.可复用的部件,这样你就只需专注于构建每一个单独的部件. 组件从概念上看就像是函数,它可以接收任意的输入值(称之为“props”),并返回一个需要在页面上展示的 ...

  7. 手把手教你写一个windows服务 【基于.net】 附实用小工具{注册服务/开启服务/停止服务/删除服务}

    1,本文适用范围 语言:.net 服务类型:windows服务,隔一段时间执行 2,服务搭建: 1,在vs中创建 console程序 2,在console项目所在类库右键 添加-新建项-选择Windo ...

  8. nodejs写一个简单的Web服务器

    目录文件如 httpFile.js如下: const httpd = require("http"); const fs = require("fs"); // ...

  9. [ionic3.x开发记录]参考ionic的float-label动效,写一个项目内通用的input组件,易扩展

    上图: module: import {NgModule} from "@angular/core"; import {CommonModule} from "@angu ...

随机推荐

  1. 【树状数组区间修改单点查询】HDU 4031 Attack

    http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后 ...

  2. HashTable的构造函数有哪些

    HashTable:在并发的环境下,使用synchronized将整张表锁住: HashTable构造函数有: public Hashtable(int initialCapacity, float ...

  3. LightOJ1106 Gone Fishing

    Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...

  4. asp.net 错误 类型"xxxxx"同时存在于"xxx.dll"和"xxxx.dll" 中

    http://walttoney.blog.163.com/blog/static/127685797201051112839328/错误 类型“System.Web.UI.ScriptManager ...

  5. miller_rabin + pollard_rho模版

    #include<stdio.h> #include<stdlib.h> #include<time.h> #include<math.h> #incl ...

  6. php生成压缩包

    $filename = "./" . date ( 'YmdH' ) . ".zip"; // 最终生成的文件名(含路径) // 生成文件 $zip = new ...

  7. python操作excel--生成图表

    [问题] 想要折腾Python中的Excel中的图标,Chart,Graph. [解决过程] 1.参考: use python to generate graph in excel 说是可以用pywi ...

  8. Perl语言--一些关于赋值、引用的东西

    引用详解 一.定义引用有两种: 1.用斜线“\” 2.匿名引用 1.用反斜线的引用:数组.哈希.标量 数组的引用 my @array = (1,2,3); my $aref = \@array; 哈希 ...

  9. Easy sssp(spfa)(负环)

    vijos    1053    Easy sssp 方法:用spfa判断是否存在负环 描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,00 ...

  10. luogu P2085 最小函数值

    题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个 ...