Skip to content

Commit a560ac0

Browse files
authored
feat: add reserve args (#9)
1 parent 4f34268 commit a560ac0

15 files changed

+70
-31
lines changed

.github/workflows/nodejs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99

1010
strategy:
1111
matrix:
12-
node-version: [10.x, 12.x, 14.x, 15.x]
12+
node-version: [12.x, 14.x, 16.x, 18.x]
1313

1414
steps:
1515
- uses: actions/checkout@v2

bin/tsproto.js

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ const cli = usage('Extract and merge locale files.\nUsage: $0 [options]')
3333
type: 'string',
3434
normalize: true,
3535
})
36+
.option('reserve', {
37+
describe: 'reserve directory',
38+
type: 'boolean',
39+
normalize: true,
40+
})
3641
.option('target', {
3742
alias: 't',
3843
describe: 'Proto files',

jest.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
5+
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
6+
};

package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
"main": "dist/index",
66
"typings": "dist/index.d.ts",
77
"scripts": {
8-
"build": "midway-bin build -c && rm -rf ./dist/.mwcc-cache",
9-
"test": "midway-bin test --ts",
10-
"cov": "midway-bin cov --ts",
11-
"ci": "npm run test",
8+
"build": "rm -rf dist && tsc",
9+
"test": "jest",
10+
"cov": "jest --coverage",
1211
"lint": "mwts lint",
1312
"lint:fix": "mwts fix"
1413
},
@@ -29,10 +28,13 @@
2928
],
3029
"license": "MIT",
3130
"devDependencies": {
32-
"@midwayjs/cli": "^1.0.0",
31+
"jest": "^28.1.3",
32+
"ts-jest": "^28.0.8",
33+
"ts-node": "^10.9.1",
3334
"@types/jest": "^26.0.22",
3435
"@types/node": "^14.0.11",
35-
"mwts": "^1.0.5"
36+
"mwts": "^1.0.5",
37+
"typescript": "^4.8.4"
3638
},
3739
"dependencies": {
3840
"chalk": "^4.0.0",

src/compiler/handlebars/comment-helper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { registerHelper } from 'handlebars';
1+
import * as handlebars from 'handlebars';
22

3-
registerHelper('comment', function (this: any) {
3+
handlebars.registerHelper('comment', function (this: any) {
44
if (this.comment) {
55
return `// ${this.comment.replace(/\n/g, '\n// ')}`;
66
}

src/compiler/handlebars/default-value-helper.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { registerHelper } from 'handlebars';
2-
1+
import * as handlebars from 'handlebars';
32
import { ENumberTypes } from '../../types';
43

5-
registerHelper('defaultValue', field => {
4+
handlebars.registerHelper('defaultValue', field => {
65
if (field.type === 'string') {
76
return '""';
87
}

src/compiler/handlebars/enum-comment-helper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { registerHelper } from 'handlebars';
1+
import * as handlebars from 'handlebars';
22

3-
registerHelper('enumComment', (conditional, options) => {
3+
handlebars.registerHelper('enumComment', (conditional, options) => {
44
if (options.data.root.comments && options.data.root.comments[conditional]) {
55
return `// ${options.data.root.comments[conditional].replace(
66
'\n',

src/compiler/handlebars/method-generate-helper.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { registerHelper } from 'handlebars';
1+
import * as handlebars from 'handlebars';
22

3-
registerHelper(
3+
handlebars.registerHelper(
44
'server-method-signature',
55
(reqType, resType, isReqStream, isResStream) => {
66
if (reqType === 'google.protobuf.Empty') {
@@ -19,7 +19,7 @@ registerHelper(
1919
}
2020
);
2121

22-
registerHelper(
22+
handlebars.registerHelper(
2323
'client-method-signature',
2424
(reqType, resType, isReqStream, isResStream) => {
2525
if (reqType === 'google.protobuf.Empty') {
@@ -41,7 +41,7 @@ registerHelper(
4141
}
4242
);
4343

44-
registerHelper('client-method-name', serviceName => {
44+
handlebars.registerHelper('client-method-name', serviceName => {
4545
if (/Client$/.test(serviceName)) {
4646
return serviceName + 'Proxy';
4747
} else {

src/compiler/handlebars/type-helper.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { registerHelper } from 'handlebars';
2-
1+
import * as handlebars from 'handlebars';
32
import { EGoogleTypes, ENumberTypes } from '../../types';
43

54
const KNOWN_PREFIX = 'google.protobuf.';
@@ -32,7 +31,7 @@ function jsType(protoType: string): string {
3231
return null;
3332
}
3433

35-
registerHelper('type', field => {
34+
handlebars.registerHelper('type', field => {
3635
// Check for known JS types
3736
let type = jsType(field.type);
3837

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { registerHelper } from 'handlebars';
1+
import * as handlebars from 'handlebars';
22

3-
registerHelper('uncapitalize', conditional => {
3+
handlebars.registerHelper('uncapitalize', conditional => {
44
return conditional[0].toLowerCase() + conditional.slice(1);
55
});

src/compiler/handlebars/var-helper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { registerHelper } from 'handlebars';
1+
import * as handlebars from 'handlebars';
22

3-
registerHelper('var', (varName, varValue, options) => {
3+
handlebars.registerHelper('var', (varName, varValue, options) => {
44
options.data.root[varName] = varValue;
55
});

src/compiler/index.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Root, common, util, Enum } from 'protobufjs';
2-
import { resolve, basename, extname, join, dirname } from 'path';
2+
import { resolve, basename, extname, join, dirname, relative } from 'path';
33
import {
44
outputFileSync,
55
existsSync,
@@ -160,9 +160,17 @@ export class Compiler {
160160
// 清理最后的换行
161161
results = results.replace(/\n+$/, '\n');
162162

163-
const outputFile = this.options.output
164-
? join(this.options.output, basename(file))
165-
: file;
163+
let outputFile;
164+
if (this.options.reserve) {
165+
outputFile = this.options.output
166+
? join(this.options.output, relative(this.options.path[0], file))
167+
: file;
168+
} else {
169+
outputFile = this.options.output
170+
? join(this.options.output, basename(file))
171+
: file;
172+
}
173+
166174
const outputPath = join(
167175
dirname(outputFile),
168176
`${basename(file, extname(file))}.ts`

src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface IGenOptions {
1515
comments?: boolean;
1616
// Log all output to console
1717
verbose?: boolean;
18+
// Reserve output directory
19+
reserve?: boolean;
1820
}
1921

2022
export enum ENumberTypes {
File renamed without changes.

test/index.test.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('/test/index.test.ts', () => {
1313

1414
compiler.compile();
1515

16-
expect(existsSync(join(__dirname, './fixtures/common/hero.ts'))).toBeTruthy();
16+
expect(existsSync(join(__dirname, './fixtures/common/test/hero.ts'))).toBeTruthy();
1717
expect(existsSync(join(__dirname, './fixtures/common/helloworld.ts'))).toBeTruthy();
1818
expect(existsSync(join(__dirname, './fixtures/common/math.ts'))).toBeTruthy();
1919

@@ -32,7 +32,7 @@ describe('/test/index.test.ts', () => {
3232
expect(content.includes('addMany(options?: grpc.IClientOptions): grpc.IClientWritableStreamService<AddArgs, Num>;')).toBeTruthy();
3333
expect(content.includes('addEmpty(options?: grpc.IClientOptions): grpc.IClientUnaryService<any, any>;')).toBeTruthy();
3434

35-
await remove(join(__dirname, './fixtures/common/hero.ts'));
35+
await remove(join(__dirname, './fixtures/common/test/hero.ts'));
3636
await remove(join(__dirname, './fixtures/common/helloworld.ts'));
3737
await remove(join(__dirname, './fixtures/common/math.ts'));
3838
await remove(join(__dirname, './fixtures/common/hello_stream.ts'));
@@ -55,6 +55,24 @@ describe('/test/index.test.ts', () => {
5555
await remove(join(__dirname, './fixtures/common/domain'));
5656
});
5757

58+
it('test generate ts interface to specified directory with reserve directory', async () => {
59+
const compiler = new Compiler({
60+
path: ['test/fixtures/common'],
61+
target: ['.proto'],
62+
ignore: ['node_modules', 'dist'],
63+
output: 'test/fixtures/common/domain',
64+
reserve: true
65+
});
66+
67+
compiler.compile();
68+
69+
expect(existsSync(join(__dirname, './fixtures/common/domain/test/hero.ts'))).toBeTruthy();
70+
expect(existsSync(join(__dirname, './fixtures/common/domain/helloworld.ts'))).toBeTruthy();
71+
expect(existsSync(join(__dirname, './fixtures/common/domain/math.ts'))).toBeTruthy();
72+
73+
await remove(join(__dirname, './fixtures/common/domain'));
74+
});
75+
5876
it('test generate ts interface keep case', async () => {
5977
const compiler = new Compiler({
6078
path: ['test/fixtures/keep_case'],

0 commit comments

Comments
 (0)