forked from turms-im/turms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotoc.js
102 lines (92 loc) · 3.02 KB
/
protoc.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const exec = require('child_process').execSync;
const fs = require('fs');
const os = require('os');
const glob = require('glob');
const IS_LINUX = os.type() === 'Linux';
const IS_ALPINE = IS_LINUX && fs.existsSync('/etc/alpine-release');
const PB_REL = 'https://github.com/protocolbuffers/protobuf/releases';
const PB_VERSION = '27.2';
const DIR = './protoc';
function runCmds(cmds) {
for (const cmd of cmds) {
console.info(cmd);
exec(cmd, {stdio: 'inherit'});
}
}
function getProtocPathInLinux() {
const path = `${DIR}/bin/protoc`;
if (fs.existsSync(path)) {
return path;
}
if (fs.existsSync('/usr/bin/protoc')) {
return '/usr/bin/protoc';
}
}
function install() {
const file = IS_LINUX ? `protoc-${PB_VERSION}-linux-x86_64.zip` : `protoc-${PB_VERSION}-win64.zip`;
if (IS_LINUX) {
if (getProtocPathInLinux()) {
return;
}
} else if (fs.existsSync(`${DIR}/bin/protoc.exe`)) {
return;
}
if (!fs.existsSync(DIR)) {
fs.mkdirSync(DIR);
}
let installProtoc;
if (IS_ALPINE) {
// protoc binaries in https://github.com/protocolbuffers/protobuf/releases
// cannot run in alpine and will fail with "not found"
return runCmds(['apk add protoc']);
}
if (IS_LINUX) {
const installUnzip = IS_ALPINE ? 'apk add unzip' : 'apt install unzip';
installProtoc = `${installUnzip} && unzip ${DIR}/protoc.zip -d ${DIR} && chmod 755 -R ${DIR}`;
} else {
installProtoc = `tar -xf ${DIR}/protoc.zip -C ${DIR}`;
}
runCmds([
`curl --create-dirs -L ${PB_REL}/download/v${PB_VERSION}/${file} -o ${DIR}/protoc.zip`,
installProtoc
]);
}
function compile() {
const protoc = IS_LINUX
? getProtocPathInLinux()
: 'start /b /w ./protoc/bin/protoc.exe';
const plugin = IS_LINUX
? './node_modules/.bin/protoc-gen-ts_proto'
: 'protoc-gen-ts_proto=%CD%/node_modules/.bin/protoc-gen-ts_proto.cmd';
const outDir = './src/model/proto';
if (fs.existsSync(outDir)) {
fs.rmSync(outDir, {recursive: true});
}
fs.mkdirSync(outDir);
// Use glob instead of the param "proto_path" because protoc-3.15.x-win64 won't accept
// "./src/proto/*.proto" if there is no a proto file in the dir "./src/proto"
const files = glob.sync('./src/proto/**/*.proto');
runCmds([
protoc +
` --plugin=${plugin}` +
' --ts_proto_opt=' +
'esModuleInterop=true,' +
'forceLong=string,' +
'outputJsonMethods=false,' +
'outputPartialMethods=false,' +
'useOptionals=true' +
' --ts_proto_out=./src/model/proto' +
' -I./src/proto' +
' ' + files.join(' ')
]);
}
console.info(`Working dir: ${__dirname}`);
if (process.argv[2] === 'compile') {
console.info('Compiling proto');
compile();
console.info('proto files have been compiled to ts files');
} else {
console.info('Installing protoc');
install();
console.info('protoc has been installed');
}