Skip to content

Commit 69be3ca

Browse files
authored
chore: fork code from origin repo (#1)
1 parent 1b54289 commit 69be3ca

35 files changed

+3022
-115
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 🎨 editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_style = space
9+
indent_size = 2
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true

.eslintrc.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./node_modules/mwts/",
3+
"ignorePatterns": ["node_modules", "dist", "test", "jest.config.js", "interface.ts", "app.js", "agent.js"],
4+
"env": {
5+
"jest": true
6+
}
7+
}

.prettierrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
...require('mwts/.prettierrc.json')
3+
}

README.md

+4-10
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ import { Metadata } from '@grpc/grpc-js';
4646

4747
export namespace hero {
4848
export interface HeroesService {
49-
findOne(data: HeroById, metadata?: Metadata): Promise<Hero>;
49+
findOne(data: HeroById, metadata?: Metadata): Promise<Hero>;
5050
}
5151
export interface HeroById {
52-
id?: number;
52+
id?: number;
5353
}
5454
export interface Hero {
55-
id?: number;
56-
name?: string;
55+
id?: number;
56+
name?: string;
5757
}
5858
}
5959
```
@@ -76,10 +76,6 @@ Ignore directories or files:
7676
```bash
7777
$ tsproto --path grpc-proto --ignore grpc-proto/ignore-dir
7878
```
79-
Custom handlebar's template for output:
80-
```bash
81-
$ tsproto --path grpc-proto --template custom-template.hbs
82-
```
8379

8480
## Options
8581

@@ -90,8 +86,6 @@ The following options are available:
9086
--help, -h Show help [boolean]
9187
--path, -p Path to root directory [array] [required]
9288
--output, -o Path to output directory [string]
93-
--template Handlebar's template for output
94-
[string] [default: "templates/nestjs-grpc.hbs"]
9589
--target, -t Proto files [array] [default: [".proto"]]
9690
--ignore, -i Ignore file or directories
9791
[array] [default: ["node_modules","dist"]]

bin/tsproto.js

+75-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,77 @@
11
#! /usr/bin/env node
2+
const { Compiller } = require('./dist/compiller');
3+
const { usage } = require('yargs');
4+
const { red } = require('chalk');
5+
const { join } = require('path');
26

3-
require('../dist/cli');
7+
const options = {
8+
path: [],
9+
output: null,
10+
target: ['.proto'],
11+
ignore: ['node_modules', 'dist'],
12+
keepCase: false,
13+
comments: true,
14+
verbose: true,
15+
};
16+
/** Set CLI */
17+
const cli = usage('Extract and merge locale files.\nUsage: $0 [options]')
18+
/* eslint-disable @typescript-eslint/no-var-requires */
19+
.version(require(join(__dirname, '../package.json')).version)
20+
/* eslint-enable @typescript-eslint/no-var-requires */
21+
.alias('version', 'v')
22+
.help('help')
23+
.alias('help', 'h')
24+
.option('path', {
25+
alias: 'p',
26+
describe: 'Path to root directory',
27+
type: 'array',
28+
normalize: true,
29+
})
30+
.option('output', {
31+
alias: 'o',
32+
describe: 'Path to output directory',
33+
type: 'string',
34+
normalize: true,
35+
})
36+
.option('target', {
37+
alias: 't',
38+
describe: 'Proto files',
39+
default: options.target,
40+
type: 'array',
41+
})
42+
.option('ignore', {
43+
alias: 'i',
44+
describe: 'Ignore file or directories',
45+
default: options.ignore,
46+
type: 'array',
47+
})
48+
.option('comments', {
49+
alias: 'c',
50+
describe: 'Add comments from proto',
51+
default: options.comments,
52+
type: 'boolean',
53+
})
54+
.option('verbose', {
55+
describe: 'Log all output to console',
56+
default: options.verbose,
57+
type: 'boolean',
58+
})
59+
.demandOption(
60+
['path'],
61+
red.bold(
62+
'Please provide both run and [path] argument to work with this tool'
63+
)
64+
)
65+
.exitProcess(true)
66+
.parse(process.argv);
67+
68+
/**
69+
* Init Compiller
70+
*
71+
* @type {Compiller}
72+
* @param {IGenOptions}
73+
*/
74+
const compiller = new Compiller({ ...options, ...cli });
75+
76+
/** CLI Task Run */
77+
compiller.compile();

package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
"main": "dist/index",
66
"typings": "dist/index.d.ts",
77
"scripts": {
8-
"build": "midway-bin build -c",
8+
"build": "midway-bin build -c && rm -rf ./dist/.mwcc-cache",
99
"test": "midway-bin test --ts",
1010
"cov": "midway-bin cov --ts",
11-
"ci": "npm run test"
11+
"ci": "npm run test",
12+
"lint": "mwts lint",
13+
"lint:fix": "mwts fix"
1214
},
1315
"bin": {
1416
"tsproto": "bin/tsproto.js"
@@ -28,13 +30,14 @@
2830
"license": "MIT",
2931
"devDependencies": {
3032
"@types/node": "^14.0.11",
31-
"@midwayjs/cli": "^1.2.36",
32-
"fs-extra": "^8.0.1",
33+
"@midwayjs/cli": "^1.0.0",
3334
"mwts": "^1.0.5"
3435
},
3536
"dependencies": {
36-
"nestjs-proto-gen-ts": "^1.0.0",
3737
"chalk": "^4.0.0",
38+
"fs-extra": "^9.0.1",
39+
"handlebars": "^4.7.6",
40+
"protobufjs": "^6.9.0",
3841
"yargs": "^15.3.1"
3942
},
4043
"author": "Harry Chen <[email protected]>",

src/cli.ts

-77
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { registerHelper } from 'handlebars';
2+
3+
registerHelper('comment', function (this: any) {
4+
if (this.comment) {
5+
return `// ${this.comment.replace(/\n/g, '\n// ')}`;
6+
}
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { registerHelper } from 'handlebars';
2+
3+
import { ENumberTypes } from '../../types';
4+
5+
registerHelper('defaultValue', field => {
6+
if (field.type === 'string') {
7+
return '""';
8+
}
9+
10+
if (field.type === 'bool') {
11+
return 'false';
12+
}
13+
14+
if (field.type === 'bytes') {
15+
return 'new Uint8Array(0)';
16+
}
17+
18+
if (field.type in ENumberTypes || field.options.enum) {
19+
return '0';
20+
}
21+
22+
// Map
23+
if (field.keyType) {
24+
return '{}';
25+
}
26+
27+
// Default
28+
return 'null';
29+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { registerHelper } from 'handlebars';
2+
3+
registerHelper('enumComment', (conditional, options) => {
4+
if (options.data.root.comments && options.data.root.comments[conditional]) {
5+
return `// ${options.data.root.comments[conditional].replace(
6+
'\n',
7+
'\n// '
8+
)}`;
9+
}
10+
});
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { registerHelper } from 'handlebars';
2+
3+
import { EGoogleTypes, ENumberTypes } from '../../types';
4+
5+
const KNOWN_PREFIX = 'google.protobuf.';
6+
7+
function jsType(protoType: string): string {
8+
if (protoType === 'string') {
9+
return 'string';
10+
}
11+
12+
if (protoType === 'bool') {
13+
return 'boolean';
14+
}
15+
16+
if (protoType === 'bytes') {
17+
return 'Uint8Array';
18+
}
19+
20+
if (protoType in ENumberTypes) {
21+
return 'number';
22+
}
23+
24+
if (protoType in EGoogleTypes) {
25+
return `${KNOWN_PREFIX}${protoType}`;
26+
}
27+
28+
if (protoType.substr(0, KNOWN_PREFIX.length) === KNOWN_PREFIX) {
29+
return protoType;
30+
}
31+
32+
return null;
33+
}
34+
35+
registerHelper('type', field => {
36+
// Check for known JS types
37+
let type = jsType(field.type);
38+
39+
if (!type) {
40+
// If it's not a known type, default to the field type
41+
type = field.type;
42+
43+
// Check for a parent
44+
if (field.options && field.options.parent) {
45+
type = `${field.options.parent}.${type}`;
46+
}
47+
}
48+
49+
// Maps
50+
if (field.keyType) {
51+
type = `{ [key: ${jsType(field.keyType)}]: ${type} }`;
52+
}
53+
54+
// Repeated
55+
if (field.repeated) {
56+
type += '[]';
57+
}
58+
59+
return type;
60+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { registerHelper } from 'handlebars';
2+
3+
registerHelper('uncapitalize', conditional => {
4+
return conditional[0].toLowerCase() + conditional.slice(1);
5+
});
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { registerHelper } from 'handlebars';
2+
3+
registerHelper('var', (varName, varValue, options) => {
4+
options.data.root[varName] = varValue;
5+
});

0 commit comments

Comments
 (0)