用 nodejs 寫一個命令行工具 :創建 react 組件的命令行工具

用 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 <[email protected]> (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,看看別人是怎麼設計的好用的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章