npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lx-frontend/node-grpc-cli

v0.3.6

Published

proto 生成 grpc npm 包工具库

Downloads

27

Readme

@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/**'