Skip to content

Commit 53205ea

Browse files
initial commit
0 parents  commit 53205ea

12 files changed

+979
-0
lines changed

.github/workflows/test.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Testing on devices
2+
3+
on: [ push, pull_request ]
4+
5+
jobs:
6+
build:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
include:
11+
- id: ubuntu:node:10
12+
build: true
13+
os: ubuntu-latest
14+
type: node
15+
node-version: 10
16+
- id: windows:node:12
17+
build: false
18+
os: windows-latest
19+
type: node
20+
node-version: 12
21+
- id: ubuntu:node:14
22+
build: true
23+
os: ubuntu-latest
24+
type: node
25+
node-version: 14
26+
27+
steps:
28+
- uses: actions/checkout@v2
29+
- name: Use Node.js ${{ matrix.node-version }}
30+
uses: actions/setup-node@v1
31+
with:
32+
node-version: ${{ matrix.node-version }}
33+
- name: Install dependencies
34+
run: npm i
35+
- name: Test in node
36+
run: npm test

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
example.d.ts
2+
example.js

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 Martin Heidegger
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Readme.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# `.d.ts` companion to `protocol-buffers`
2+
3+
`protocol-buffers-dts` allows you to create TypeScript definition files for javascript files generated
4+
with [protocol-buffers][pbs].
5+
6+
[pbs]: https://github.com/mafintosh/protocol-buffers
7+
8+
## Usage
9+
10+
```bash
11+
npx protocol-buffers-dts messages.proto -o messages.d.ts
12+
```
13+
14+
## License
15+
16+
[MIT](./LICENSE)

bin/protocol-buffers-dts

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env node
2+
function argv (argv) {
3+
const args = {}
4+
const rest = []
5+
const max = argv.length
6+
const optionReg = /^--?/
7+
const isOption = key => optionReg.test(key)
8+
for (let i = 0; i < max; i++) {
9+
let key = argv[i]
10+
let val = true
11+
const parts = key.split('=')
12+
if (parts.length > 1) {
13+
key = parts.shift()
14+
val = parts.join('=')
15+
} else if (i < max - 1) {
16+
const next = argv[i + 1]
17+
if (next === '=' && (i += 2) < max) {
18+
val = argv[i]
19+
} else if (isOption(key) && !isOption(next)) {
20+
val = next
21+
i += 1
22+
}
23+
}
24+
if (!isOption(key)) {
25+
rest.push(key)
26+
} else {
27+
args[key.replace(optionReg, '')] = val
28+
}
29+
}
30+
let watch = args.w || args.watch || false
31+
const file = rest[0] || null
32+
let help = args.h || args.help || !file || false
33+
let output = args.o || args.output || null
34+
if (args.wo) {
35+
output = args.wo
36+
watch = true
37+
}
38+
39+
if (typeof output !== 'string') {
40+
// No output file defined
41+
help = true
42+
}
43+
return { help, watch, output, file }
44+
}
45+
46+
function rethrow (msg, e) {
47+
e.message = `${msg}:\n\n${e.stack.split('\n').map(line => ` ${line}`).join('\n')}`
48+
throw e
49+
}
50+
51+
const controlZ = new Promise(resolve => {
52+
process.once('SIGINT', () => resolve(true))
53+
})
54+
55+
async function start ({ help, watch, output, file } = {}) {
56+
if (help) {
57+
console.log(`
58+
Usage: protocol-buffers-dts [schema-file.proto] [options]
59+
60+
--output, -o [output-file.d.ts]
61+
--watch, -w (recompile on schema change)
62+
`)
63+
return 1
64+
}
65+
const { parse } = require('protocol-buffers-schema')
66+
const { fromSchema } = require('..')
67+
const { readFile, writeFile } = require('fs').promises
68+
let watcher
69+
while (true) {
70+
let rawSchema
71+
try {
72+
try {
73+
rawSchema = await readFile(file, 'utf-8')
74+
} catch (e) {
75+
rethrow(`Error while reading ${file}`, e)
76+
}
77+
let schema
78+
try {
79+
schema = parse(rawSchema)
80+
} catch (e) {
81+
rethrow(`Error while parsing ${file}`, e)
82+
}
83+
let dts
84+
try {
85+
dts = fromSchema(schema)
86+
} catch (e) {
87+
rethrow(`Error while creating dts from ${file}`, e)
88+
}
89+
try {
90+
await writeFile(output, dts)
91+
} catch (e) {
92+
rethrow(`Error while writing dts to ${output}`, e)
93+
}
94+
console.log(`Successfully wrote ${output}.`)
95+
} catch (err) {
96+
if (!watch) throw err
97+
console.error(err)
98+
}
99+
if (!watch) {
100+
break
101+
}
102+
if (!watcher) {
103+
watcher = require('fs').watch(file, { interval: 100 })
104+
}
105+
console.log('Watching for changes.')
106+
if (await Promise.race([
107+
controlZ,
108+
new Promise(resolve => watcher.once('change', () => resolve(false)))
109+
])) {
110+
break
111+
}
112+
}
113+
if (watcher) {
114+
console.log('\nClosing watcher.')
115+
watcher.close()
116+
}
117+
return 0
118+
}
119+
120+
if (require.main === module) {
121+
start(argv(process.argv.slice(2))).then(
122+
code => process.exit(code),
123+
e => {
124+
console.error(e.stack)
125+
process.exit(1)
126+
}
127+
)
128+
} else {
129+
module.exports = {
130+
argv,
131+
start
132+
}
133+
}

bin/protocol-buffers-dts.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare namespace bin {
2+
interface Options {
3+
help: boolean
4+
watch: boolean
5+
output: string | undefined
6+
file: string | undefined
7+
}
8+
function argv (argv: string[]): Options
9+
function start (opts: Options): Promise<number>
10+
}
11+
export = bin

example.proto

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// example file
2+
enum Corpus {
3+
A = 0;
4+
B = 1;
5+
}
6+
message Test {
7+
map<string, uint32> data2 = 7;
8+
required string hello = 2;
9+
oneof test {
10+
uint32 age = 3;
11+
uint32 year = 4;
12+
}
13+
oneof sample {
14+
string hula = 5;
15+
uint32 world = 6;
16+
}
17+
message Nested {
18+
optional bytes thing = 1;
19+
}
20+
repeated Corpus corpus = 8;
21+
repeated bytes foo = 9;
22+
enum Corpus {
23+
option allow_alias = true;
24+
UNIVERSAL = 0;
25+
WEB = 1;
26+
NET = 1;
27+
IMAGES = 2;
28+
LOCAL = 3;
29+
NEWS = 4;
30+
PRODUCTS = 5;
31+
VIDEO = 6;
32+
}
33+
Test2 bar = 10;
34+
Test3 baz = 11;
35+
message Test3 {
36+
uint32 some = 1;
37+
}
38+
}
39+
40+
message Test2 {
41+
Corpus corpus = 1;
42+
}

index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Schema } from 'protocol-buffers-schema/types'
2+
declare namespace dts {
3+
function fromSchema (schema: Schema): string
4+
}
5+
export = dts

0 commit comments

Comments
 (0)