0.3.6 • Published 5 months ago

@lx-frontend/node-grpc-cli v0.3.6

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

@lx-frontend/node-grpc-cli

用于生产 protoc 导出的 js 代码,转换成 npm 包。

如何运行示例

  1. 终端切换到 node-grpc-cli 目录
  2. node example/main.js

Usage1:作为 cli 使用方式

  1. 安装 grpc node cli 包:pnpm add @lx-frontend/node-grpc-cli -D

  2. 如果与 typescript 项目使用,需要修改项目根目录 tsconfig.json,需要指定 moduleResolution 版本为 Node16 以上,原因 exports 是后面增加的字段。早期 nodej.js 不支持。

"moduleResolution": "Node16", /* Specify how TypeScript looks up a file from a given module specifier. */
  1. 在项目根目录,新增 grpc 构建配置文件 grpc.build.js
const path = require('path')
const { makeGrpcBuildConfig } = require('@lx-frontend/node-grpc-cli')

const projectBasePath = process.cwd()
const { version } = require(path.resolve(projectBasePath, 'package.json'))

module.exports = makeGrpcBuildConfig({
  /**
   * protoc 输出源码根目录,要求 node、web 都输出到 sourceBaseDir 目录下的各自目录。
   *
   * @example
   *
   * node输出目录:项目根目录/dist/njs
   * web输出目录:项目根目录/dist/bjs
   */
  sourceBaseDir: path.resolve(projectBasePath, 'example/protocDist'),

  /**
   * 平台配置
   */
  platformConfigs: {
    web: {
      /**
       * web 输出目录,相对于 sourceBaseDir
       */
      sourceRelativeDir: 'bjs',
      globRules: '**/**/*.js',
    },
    node: {
      /**
       * node 输出目录,相对于 sourceBaseDir
       */
      sourceRelativeDir: 'njs',
      globRules: '**/**/*.js',
    },
    http: {
      /**
       * http 输出目录,相对于 sourceBaseDir
       */
      sourceRelativeDir: 'http',
      globRules: '**/**/*.ts',
    }
  },


  /**
   * 项目命令空间文件夹路径,即输出的资源第一个文件夹
   * lixin/dos/aftersale-sheet-spray-center-bff/v1/svc.js
   */
  sourceNamespaceDir: 'lixin',


  /**
   * 发布npm包所属组织
   */
  organizationName: '@lixin-sdk',

  /**
   * 输出文件夹,生成的包都会输出到这,该目录必须在 pnpm-workspace.yaml 指定,用于包的发布
   */
  outputDir:  path.resolve(projectBasePath, 'output'),

  /**
   * 版本号
   */
  version: version,

  /**
   * 需要打包的 proto 资源,数组有2个值,[projectDir(项目相对路径,从命令空间开始算), ['node', 'web']]
   *
   * @example
   *
   * 双平台都打包:
   *
   * ['dos/aftersale-sheet-spray-center-bff', ['node', 'web']]
   *
   * 仅仅打包单个平台,传递单个字符串 web/node。或者数组中仅有一个值都可以。
   *
   * 示例1:['dos/aftersale-sheet-spray-center-bff', 'node']
   * 示例2:['dos/aftersale-sheet-spray-center-bff', 'web']
   * 示例3:['dos/aftersale-sheet-spray-center-bff', 'http']
   * 示例3:['dos/aftersale-sheet-spray-center-bff', ['web']]
   * 示例4:['dos/aftersale-sheet-spray-center-bff', ['node']]
   * 示例5:['dos/aftersale-sheet-spray-center-bff', ['node', 'web', 'http']]
   *
   */
  packages: [
    ['dos/aftersale-sheet-spray-center-bff', ['node', 'web', 'http']]
  ]
})

注意,如果配置文件名为:grpc.build.js,参数 -c 可以不指定。只有在使用自定义配置名是才需要指定。

npx grpcBuild -c grpc.build.js

Usage2:作为 node.js 包使用方式

  1. 安装 grpc node cli 包:pnpm add @lx-frontend/node-grpc-cli -D
  2. 新增 main.js
const {GrpcBuild} = require('@lx-frontend/node-grpc-cli');

const grpcBuild = new GrpcBuild({
  // 同样配置
})

grpcBuild.run()

注意事项

生成的 npm 包代码,为了缩短路径,采用 exports 配置了自定义短路径,也保留了原来的路径。

举例:

{
  "exports": {
    "./v1/svc_pb": "./lixin/dos/aftersale-sheet-spray-center-bff/v1/svc_pb.js",
    "./lixin/dos/aftersale-sheet-spray-center-bff/v1/svc_pb": "./lixin/dos/aftersale-sheet-spray-center-bff/v1/svc_pb.js",
    "./v1/svc_grpc_pb": "./lixin/dos/aftersale-sheet-spray-center-bff/v1/svc_grpc_pb.js",
    "./lixin/dos/aftersale-sheet-spray-center-bff/v1/svc_grpc_pb": "./lixin/dos/aftersale-sheet-spray-center-bff/v1/svc_grpc_pb.js"
  }
}

其他

  1. 建议与 pnpm-workspace 集成,pnpm-workspace 指定 packages 为配置文件 output 目录,这样能利用 pnpm 包发布版本。

示例配置:pnpm-workspace.yaml

packages:
  # all packages in direct subdirs of packages/
  - 'output/*'
  # exclude packages that are inside test directories
  - '!**/test/**'
0.3.6

5 months ago

0.3.5

5 months ago

0.3.4

6 months ago

0.3.3

6 months ago

0.3.2

6 months ago

0.3.1

6 months ago

0.2.1

8 months ago

0.2.0

9 months ago

0.1.1

9 months ago