-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
bin.js
executable file
·76 lines (68 loc) · 2.13 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
const protobuf = require('./')
const fs = require('fs')
const path = require('path')
let filename = null
let output = null
let watch = false
let encodings = null
const importPaths = []
// handrolled parser to not introduce minimist as this is used a bunch of prod places
// TODO: if this becomes more complicated / has bugs, move to minimist
for (let i = 2; i < process.argv.length; i++) {
const parts = process.argv[i].split('=')
const key = parts[0]
const value = parts.slice(1).join('=')
if (key[0] !== '-') {
filename = path.resolve(key)
} else if (key === '--output' || key === '-o' || key === '-wo') {
if (key === '-wo') watch = true
output = value || process.argv[++i]
} else if (key === '--watch' || key === '-w') {
watch = true
} else if (key === '--encodings' || key === '-e') {
encodings = value || process.argv[++i]
} else if (key === '--proto_path' || key === '-I') {
importPaths.push(path.resolve(value || process.argv[++i]))
}
}
importPaths.push(process.cwd())
if (!filename) {
console.error('Usage: protocol-buffers [schema-file.proto] [options]')
console.error()
console.error(' --output, -o [output-file.js]')
console.error(' --watch, -w (recompile on schema change)')
console.error(' --proto_path, -I [path-root] # base to lookup imports, multiple supported')
console.error()
process.exit(1)
}
filename = path.relative(process.cwd(), filename)
if (watch && !output) {
console.error('--watch requires --output')
process.exit(1)
}
if (!output) {
process.stdout.write(compile())
} else {
write()
if (watch) fs.watch(filename, write)
}
function write () {
fs.writeFileSync(output, compile())
}
function resolveImport (filename) {
for (let i = 0; i < importPaths.length; i++) {
const importPath = importPaths[i]
try {
return fs.readFileSync(path.join(importPath, filename))
} catch (err) {}
}
throw new Error('File "' + filename + '" not found in import path:\n - ' + importPaths.join('\n - '))
}
function compile () {
return protobuf.toJS(null, {
encodings: encodings,
filename: filename,
resolveImport: resolveImport
})
}