Skip to content

Commit c9af9f1

Browse files
committed
chore: support vscode
1 parent c222e15 commit c9af9f1

File tree

14 files changed

+1237
-1877
lines changed

14 files changed

+1237
-1877
lines changed

packages/core/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# pbts
22

3+
## 4.0.6
4+
5+
### Patch Changes
6+
7+
- support vscode for new feature
8+
39
## 4.0.5
410

511
### Patch Changes

packages/core/README.md

+32-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[download-image]: https://img.shields.io/npm/dm/pbts.svg?style=flat-square
99
[download-url]: https://www.npmjs.com/package/pbts
1010

11-
It is the cli for pbts. You can convert pb to ts file by command line
11+
It is the cli for pbts. You can convert protobuf to typescript file by command line
1212

1313
## CLI Usage
1414

@@ -20,12 +20,32 @@ step 1 Install pbts
2020
npm i pbts -g
2121
```
2222

23-
step 2 Convert your protobuffer to Typescript Definition File
23+
step 2 Convert your protobuf to Typescript Definition File
2424

2525
```shell
2626
pbts -i input/app/order.proto -o output/order.ts
2727
```
2828

29+
```shell
30+
31+
Description
32+
convert based on local protobuf
33+
34+
Usage
35+
$ pbts [options]
36+
37+
Options
38+
-i, --input input file path
39+
-o, --output output file path
40+
-outputType, --<outputType> output file type, including typescript,definition,jsdoc, default typescript
41+
-mode, --<mode> edge case for int64(long type), including normal,strict, default strict
42+
-v, --version Displays current version
43+
-h, --help Displays this message
44+
45+
Examples
46+
$ pbts -i test.proto -o test.ts
47+
```
48+
2949
### No Installation
3050
3151
Please use npx for short.
@@ -48,9 +68,16 @@ message MyRequest {
4868
}
4969
`;
5070

51-
const ts = parseProto(source);
71+
const ts = parseProto(source, {
72+
// Options
73+
});
5274
```
5375
76+
parameter | type | description
77+
---------|----------|---------
78+
outputType | string | output file type, including typescript,definition,jsdoc, default typescript
79+
mode | string | edge case for int64(long type), including normal,strict, default strict
80+
5481
The result is as follow.
5582
5683
```typescript
@@ -61,6 +88,8 @@ interface MyRequest {
6188
6289
## Node Library Usage
6390
91+
More file operation is supported.
92+
6493
```javascript
6594
import { parseProto } from 'pbts';
6695

packages/core/bin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ updater({
2222
.option('-i, --input <input>', 'input file path')
2323
.option('-o, --output <output>', 'output file path')
2424
.option('--outputType <outputType>', 'output file type, including typescript,definition,jsdoc, default typescript')
25-
.option('--mode <mode>', 'edge case for int64(long type), including normal,strict, default normal')
25+
.option('--mode <mode>', 'edge case for int64(long type), including normal,strict, default strict')
2626
.action(convertCommand);
2727

2828
prog.parse(process.argv);

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pbts",
3-
"version": "4.0.5",
3+
"version": "4.0.6",
44
"description": "protobuf-to-typescript",
55
"author": "weiping.xiang <[email protected]>",
66
"homepage": "https://github.com/brandonxiang/protobuf-to-typescript#readme",

packages/core/src/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { Root, Enum, Service, Type } = protobuf;
1414
*/
1515
const defaultOptions = {
1616
outputType: 'typescript',
17-
mode: 'normal',
17+
mode: 'strict',
1818
};
1919

2020
/**

packages/core/tests/mock/int64.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('Method type should be converted to number', () => {
2424
Teacher teacher = 3;
2525
}
2626
`;
27-
const ts = mockResponse(source, 'SayHello');
27+
const ts = mockResponse(source, 'SayHello', { mode: 'normal' });
2828
assert.equal(ts, {
2929
message: 'Hello',
3030
test: 20,

packages/core/tests/print/int64.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test('Field type with uint64 should be converted to number', () => {
99
uint64 path = 1;
1010
}
1111
`;
12-
const ts = parseProto(source);
12+
const ts = parseProto(source, { mode: 'normal' });
1313
assert.match(ts, 'interface MyRequest');
1414
assert.match(ts, 'path?: number');
1515
});
@@ -33,7 +33,7 @@ test('Field type with sint64 should be converted to number', () => {
3333
sint64 path = 1;
3434
}
3535
`;
36-
const ts = parseProto(source);
36+
const ts = parseProto(source, { mode: 'normal' });
3737
assert.match(ts, 'interface MyRequest');
3838
assert.match(ts, 'path?: number');
3939
});
@@ -57,7 +57,7 @@ test('Field type with fixed64 should be converted to number', () => {
5757
fixed64 path = 1;
5858
}
5959
`;
60-
const ts = parseProto(source);
60+
const ts = parseProto(source, { mode: 'normal' });
6161
assert.match(ts, 'interface MyRequest');
6262
assert.match(ts, 'path?: number');
6363
});
@@ -82,7 +82,7 @@ test('Field type with sfixed64 should be converted to number', () => {
8282
sfixed64 path = 1;
8383
}
8484
`;
85-
const ts = parseProto(source);
85+
const ts = parseProto(source, { mode: 'normal' });
8686
assert.match(ts, 'interface MyRequest');
8787
assert.match(ts, 'path?: number');
8888
});

packages/vscode/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ We provide two ways to **convert protobuf to typescript**.
2828

2929
- STEP 3: Search **"pbts:from clipboard"**, execute,the converted typescript will be filled at the cursor of the currently active window
3030

31+
### Settings
32+
33+
Users can then control this setting by creating a .vscode/settings.json file in their project root
34+
35+
```json
36+
{
37+
//Specify the output type for the converted files
38+
"protobufToTypescript.outputType": "jsdoc",
39+
//Specify the edge case for int64(long type)
40+
"protobufToTypescript.mode": "strict"
41+
}
42+
```
43+
3144
## Problem
3245

33-
If you have any questions, you can contact me by email **@kukudeshiyi1999@gmail.com**
46+
If you have any questions, you can contact me by email **@brandon.xiang@gmail.com**

packages/vscode/package.json

+59-40
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,81 @@
1313
"categories": [
1414
"Other"
1515
],
16-
"repository": {
17-
"type": "git",
18-
"url": "git+https://github.com/brandonxiang/protobuf-to-typescript.git"
19-
},
20-
"activationEvents": [
21-
"onCommand:pbToTypescript.fromSelection",
22-
"onCommand:pbToTypescript.fromClipboard",
23-
"onCommand:pbToTypescript.convertOnAir"
24-
],
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/brandonxiang/protobuf-to-typescript.git"
19+
},
2520
"main": "./out/extension.js",
2621
"contributes": {
22+
"configuration": {
23+
"title": "Protobuf to TypeScript",
24+
"properties": {
25+
"protobufToTypescript.outputType": {
26+
"type": "string",
27+
"enum": [
28+
"jsdoc",
29+
"typescript",
30+
"definition"
31+
],
32+
"default": "typescript",
33+
"description": "Specify the output type for the converted files"
34+
},
35+
"protobufToTypescript.mode": {
36+
"type": "string",
37+
"enum": [
38+
"normal",
39+
"strict"
40+
],
41+
"default": "strict",
42+
"description": "Specify the edge case for int64(long type)"
43+
}
44+
}
45+
},
2746
"commands": [
2847
{
29-
"command": "pbToTypescript.fromSelection",
48+
"command": "protobufToTypescript.fromSelection",
3049
"title": "from selection",
3150
"category": "pbts"
3251
},
3352
{
34-
"command": "pbToTypescript.fromClipboard",
53+
"command": "protobufToTypescript.fromClipboard",
3554
"title": "from clipboard",
3655
"category": "pbts"
3756
},
3857
{
39-
"command": "pbToTypescript.transformOnPanel",
40-
"title": "Open Convert Panel",
41-
"category": "pbts"
42-
}
58+
"command": "protobufToTypescript.transformOnPanel",
59+
"title": "Open Convert Panel",
60+
"category": "pbts"
61+
}
4362
],
44-
"viewsContainers": {
45-
"activitybar": [
46-
{
47-
"id": "pbts",
48-
"title": "protobuf to typescript",
49-
"icon": "media/cat2.svg"
50-
}
51-
]
52-
},
53-
"views": {
54-
"pbts": [
55-
{
56-
"id": "introduction",
57-
"name": "welcome to pbts"
58-
}
59-
]
60-
},
63+
"viewsContainers": {
64+
"activitybar": [
65+
{
66+
"id": "pbts",
67+
"title": "protobuf to typescript",
68+
"icon": "media/cat2.svg"
69+
}
70+
]
71+
},
72+
"views": {
73+
"pbts": [
74+
{
75+
"id": "introduction",
76+
"name": "welcome to pbts"
77+
}
78+
]
79+
},
6180
"viewsWelcome": [
62-
{
63-
"view": "introduction",
64-
"contents": "This tool is able to help you convert protobuf file to typescript definition file. \n[Open Convert Panel](command:pbToTypescript.transformOnPanel)\n You can transform it from selection \n[from selection](command:pbToTypescript.fromSelection)\n You can transform it from clipboard \n[from clipboard](command:pbToTypescript.fromClipboard)\n"
65-
}
66-
]
81+
{
82+
"view": "introduction",
83+
"contents": "This tool is able to help you convert protobuf file to typescript definition file. \n[Open Convert Panel](command:protobufToTypescript.transformOnPanel)\n You can transform it from clipboard \n[from clipboard](command:protobufToTypescript.fromClipboard)\n You can transform it from selection \n[from selection](command:protobufToTypescript.fromSelection)\n"
84+
}
85+
]
6786
},
6887
"scripts": {
6988
"vscode:prepublish": "npm run -S esbuild-base -- --minify",
7089
"postinstall": "patch-package",
71-
"esbuild-base": "rimraf out && esbuild ./src/extension.ts --bundle --outfile=out/extension.js --external:vscode --format=cjs --platform=node",
90+
"esbuild-base": "rimraf out && esbuild ./src/extension.ts --bundle --outdir=out --external:vscode --format=cjs --platform=node",
7291
"build": "npm run -S esbuild-base -- --sourcemap",
7392
"watch": "npm run -S esbuild-base -- --sourcemap --watch",
7493
"pretest": "npm run build && npm run lint",
@@ -93,7 +112,7 @@
93112
"rimraf": "^3.0.2"
94113
},
95114
"dependencies": {
96-
"pbts": "^4.0.3",
115+
"pbts": "^4.0.5",
97116
"copy-paste": "1.3.0"
98117
}
99-
}
118+
}

packages/vscode/src/extension.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
1-
import { ExtensionContext, ViewColumn, commands, window } from "vscode";
1+
import { ExtensionContext, ViewColumn, commands, window, workspace } from "vscode";
22
import {
33
handleError,
44
getClipboardText,
55
pasteToMarker,
66
getSelectedText,
77
validateLength,
8-
} from "./lib";
8+
} from "./utils/lib";
99
import { parseProto } from 'pbts/core';
1010
import fs from 'fs';
1111
import path from 'path';
12-
import { cwd } from "process";
12+
// import { FileExplorer } from './fileExplorer';
1313

1414
const PB3_HEADER = `syntax = "proto3";`;
1515

1616
export function activate(context: ExtensionContext) {
1717
context.subscriptions.push(
18-
commands.registerCommand("pbToTypescript.fromSelection", transformFromSelection)
18+
commands.registerCommand("protobufToTypescript.fromSelection", transformFromSelection)
1919
);
2020
context.subscriptions.push(
21-
commands.registerCommand("pbToTypescript.fromClipboard", transformFromClipboard)
21+
commands.registerCommand("protobufToTypescript.fromClipboard", transformFromClipboard)
2222
);
2323
context.subscriptions.push(
24-
commands.registerCommand("pbToTypescript.transformOnPanel", () => transformOnPanel(context))
24+
commands.registerCommand("protobufToTypescript.transformOnPanel", () => transformOnPanel(context))
2525
);
26+
27+
// new FileExplorer(context);
2628
}
2729

2830
function transformFromSelection() {
31+
const config = workspace.getConfiguration('protobufToTypescript');
32+
const outputType = config.get('outputType') as string;
33+
const mode = config.get('mode') as string;
2934

3035
getSelectedText()
3136
.then(validateLength)
3237
.then(str => {
33-
return parseProto(PB3_HEADER + str);
38+
return parseProto(PB3_HEADER + str, { outputType, mode });
3439
})
3540
.then(interfaces => {
3641
pasteToMarker(interfaces);
@@ -41,10 +46,14 @@ function transformFromSelection() {
4146
}
4247

4348
function transformFromClipboard() {
49+
const config = workspace.getConfiguration('protobufToTypescript');
50+
const outputType = config.get('outputType') as string;
51+
const mode = config.get('mode') as string;
52+
4453
getClipboardText()
4554
.then(validateLength)
4655
.then(str => {
47-
return parseProto(PB3_HEADER + str);
56+
return parseProto(PB3_HEADER + str, { outputType, mode });
4857
})
4958
.then(interfaces => {
5059
pasteToMarker(interfaces);
@@ -71,4 +80,4 @@ function getWebviewContent(ctx: ExtensionContext) {
7180
fileEntry,
7281
'utf-8',
7382
);
74-
}
83+
}

0 commit comments

Comments
 (0)