# grpc-code-gen

> Code generator for grpc, for typescript and javascript.

Latest version **6.1.22** (published 2021-03-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install grpc-code-gen
pnpm add grpc-code-gen
yarn add grpc-code-gen
bun add grpc-code-gen
```

Provides the command `grpc-code-gen`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.1.22 |
| Published | 2021-03-31 |
| First published | 2018-11-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 102 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Maintainers | jinkerjiang, wangweianger |

## Links

- npm: https://www.npmjs.com/package/grpc-code-gen
- Repository: https://github.com/yunkeCN/grpc-code-gen
- Homepage: https://github.com/yunkeCN/grpc-code-gen#readme
- Issues: https://github.com/yunkeCN/grpc-code-gen/issues
- npm.io page: https://npm.io/package/grpc-code-gen

## Dependencies (6)

- [lodash](https://npm.io/package/lodash.md) ^4.17.19
- [fs-extra](https://npm.io/package/fs-extra.md) ^8.1.0
- [load-git](https://npm.io/package/load-git.md) ^1.5.2
- [commander](https://npm.io/package/commander.md) ^2.19.0
- [load-proto](https://npm.io/package/load-proto.md) 2.2.2
- [cross-spawn](https://npm.io/package/cross-spawn.md) ^6.0.5

## Recent versions

- 6.1.22 (latest) — 2021-03-31
- 6.1.21 — 2021-03-17
- 6.1.20 — 2021-03-11
- 6.1.19 — 2021-03-11
- 6.1.18 — 2021-03-09
- 6.1.16 — 2021-03-09
- 6.1.15-rc.1 — 2021-02-02
- 6.1.14 — 2020-10-23
- 6.1.13 — 2020-10-14
- 6.1.12 — 2020-09-29
- 6.1.11 — 2020-09-29
- 6.0.11 — 2020-09-29
- 6.1.10 — 2020-08-20
- 6.1.9 — 2020-08-18
- 6.1.8 — 2020-08-13
- … 115 more at https://npm.io/package/grpc-code-gen/versions

## README

# grpc-code-gen

Code generator for grpc, for typescript and javascript.

## CLI usage

```bash
grpc-code-gen gen -u git@git.myscrm.cn:2c/panther-statistics-proto.git,git@git.myscrm.cn:2c/panther-third-proto.git -b test -t ${token} -d test/code-gen-cli
```

### Set params by config file

```js
//grpc-code-gen.config.js
module.exports = {
  gitUrls: [
    {
      url: 'git@git.myscrm.cn:ykcommon/ykproto.git',
      branch: 'master',
    },
    // 服务依赖proto
    {
      url: 'git@git.myscrm.cn:ykcommon/common-proto.git',
      branch: 'master',
      type:'lib'
    },
    { 
      url: 'git@git.myscrm.cn:2c/panther-statistics-proto.git',
      // panther-statistics 服务proto依赖于 common-proto
      deps:['git@git.myscrm.cn:ykcommon/common.git']
    },
    {
      url: 'git@git.myscrm.cn:2c/panther-third-proto.git',
      host: 'panther-third', // go 服务别名
    }
  ],
  branch: 'test',
  accessToken: '${token}',
  baseDir: `${__dirname}/code-gen`,
  resolvePath: (origin, target, rootDir) => {
    if (/^git\.myscrm\.cn\/golang\/common\/proto\/(google|common)\//.test(target)) {
      return `${rootDir}/git.myscrm.cn/ykcommon/ykproto/${target.replace(/^git\.myscrm\.cn\/golang\/common\/proto\//, '')}`;
    } else if (/^proto\/(common|google)\//.test(target)) {
      return `${rootDir}/git.myscrm.cn/ykcommon/ykproto/${target.replace(/^proto\//, '')}`;
    } else if (/^proto\//.test(target)) {
      return target.replace(/^proto\/([^\/]+)(.+)/, (_target, $1, $2) => {
        return `${rootDir}/git.myscrm.cn/2c/${$1.replace(/_/g, '-')}${$2}`;
      });
    } else if (/^(common|google\/api)\//.test(target)) {
      return `${rootDir}/git.myscrm.cn/ykcommon/ykproto/${target}`;
    }
    return null;
  },
};
```

then

```bash
grpc-code-gen gen
```

### Params

参数 | 类型 | 说明 | 默认值
---|---|---|---
gitUrls | Array<string \| {url: string, branch?: string, accessToken?: host?: string }> | 仓库地址数组 | `null`
branch |  string | 分支，如参数没有，会去读取环境变量参数`branch` | `null`
accessToken | string | git access token | `null`
baseDir? | string | 生成目录 | `null`
target? | `javascript typescript` | 目标语言 | `null`
grpcNpmName? | string | grpc npm name, 可取值：`grpc`, `@grpc/grpc-js` | `grpc`
jsonSemanticTypes? | boolean | 生成json semantic types | `false`  
loaderOptions? | [LoaderOptions](https://github.com/grpc/grpc-node/tree/master/packages/proto-loader) | loader生成配置 | `{ defaults: true }`
callOptions? | [callOptions](#callOptions) | 方法调用配置 | null
logOptions? | [logOptions](#logOptions) | 日志配置 | null
filterError? | (err: Error) => Error | 错误过滤 | (err) => err
loadProtoPlugin? | (option: IGitConfigWithUrl) => Promise<ILoadResult> | proto加载插件 | unll

#### callOptions

参数 | 类型 | 说明 | 默认值
---|---|---|---
timeout? | number | 超时时间，单位：ms | undefined

#### logOptions

参数 | 类型 | 说明 | 默认值
---|---|---|---
disable? | boolean | 是否启用 | false
attributes? | string[] | 记录字段，`request, response` | ['request'] 

## Program usage
```js
const base = require('grpc-code-gen/build/base');

base.gen({
  gitUrls: [
    {
      url: 'git@git.myscrm.cn:ykcommon/ykproto.git',
      branch: 'master',
    },
    'git@git.myscrm.cn:2c/panther-statistics-proto.git',
    'git@git.myscrm.cn:2c/panther-third-proto.git',
  ],
  branch: 'test',
  accessToken: process.env.token,
  baseDir: `${__dirname}/code-gen`,
  resolvePath: (origin, target, rootDir) => {
    if (/^git\.myscrm\.cn\/golang\/common\/proto\/(google|common)\//.test(target)) {
      return `${rootDir}/git.myscrm.cn/ykcommon/ykproto/${target.replace(/^git\.myscrm\.cn\/golang\/common\/proto\//, '')}`;
    } else if (/^proto\/(common|google)\//.test(target)) {
      return `${rootDir}/git.myscrm.cn/ykcommon/ykproto/${target.replace(/^proto\//, '')}`;
    } else if (/^proto\//.test(target)) {
      return target.replace(/^proto\/([^\/]+)(.+)/, (_target, $1, $2) => {
        return `${rootDir}/git.myscrm.cn/2c/${$1.replace(/_/g, '-')}${$2}`;
      });
    } else if (/^(common|google\/api)\//.test(target)) {
      return `${rootDir}/git.myscrm.cn/ykcommon/ykproto/${target}`;
    }
    return null;
  },
})
  .catch((err)=>{
    console.error(err.stack)
  });
```
 

### 版本说明
- v6.1.13 版本
gitUrls配置新增服务别名配置项

- v6.1.0 版本
gitUrls配置新增依赖项

- v6.0.6 版本
新增 access 日志链路

- v6.0.4 版本
新增 grpc-service.dev.config.js 本地配置，去掉繁琐的本地host配置

- v6.0.3 版本
解决编译go proto枚举报错问题

- v6.0.0 版本
解决grpc-code-gen编译时，后端相同package下有相同service的冲突问题

- v5.4.4 版本
解决javascript精度文档，js在超过16位的number数字会有精度丢失问题，因此统一把（'double', 'float', 'int64', 'uint64', 'sint64', 'fixed64', 'sfixed64'）类型转换为string类型

- v5.3.0 版本
bff  grpc 新增热重启功能

---
_Source: https://npm.io/package/grpc-code-gen · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
