UNPKG

4.34 kBJavaScriptView Raw
1const { spawnSync } = require("child_process");
2
3const { existsSync, mkdirSync } = require("fs");
4const { resolve } = require("path");
5
6const protocVersion = "3.15.5";
7
8const examplesGeneratedPath = resolve(__dirname, "examples", "generated");
9const examplesGeneratedGrpcWebPath = resolve(__dirname, "examples", "generated-grpc-web");
10const examplesGeneratedGrpcNodePath = resolve(__dirname, "examples", "generated-grpc-node");
11const examplesGeneratedGrpcJsNodePath = resolve(__dirname, "examples", "generated-grpc-js-node");
12
13const binSuffix = process.platform === "win32" ? ".cmd" : "";
14const nodeModulesBin = resolve(__dirname, "node_modules", ".bin");
15
16const downloadPath = resolve(nodeModulesBin, "download") + binSuffix;
17
18const protocRoot = resolve(__dirname, "protoc");
19const protocPath = resolve(protocRoot, "bin", "protoc");
20
21const protocPluginPath = resolve(__dirname, "bin", "protoc-gen-ts") + binSuffix;
22
23const rimrafPath = resolve(nodeModulesBin, "rimraf") + binSuffix;
24
25const supportedPlatforms = {
26 darwin: {
27 downloadSuffix: "osx-x86_64",
28 name: "Mac"
29 },
30 linux: {
31 downloadSuffix: "linux-x86_64",
32 name: "Linux"
33 },
34 win32: {
35 downloadSuffix: "win32",
36 name: "Windows"
37 }
38};
39
40const platform = supportedPlatforms[process.platform];
41const platformName = platform ?
42 platform.name :
43 `UNKNOWN:${process.platform}`;
44console.log("You appear to be running on", platformName);
45
46requireBuild();
47
48const glob = require("glob");
49
50requireProtoc();
51
52requireDir(examplesGeneratedPath);
53requireDir(examplesGeneratedGrpcWebPath);
54requireDir(examplesGeneratedGrpcNodePath);
55requireDir(examplesGeneratedGrpcJsNodePath);
56
57// Generate no services
58
59run(protocPath,
60 `--proto_path=${__dirname}`,
61 `--plugin=protoc-gen-ts=${protocPluginPath}`,
62 `--js_out=import_style=commonjs,binary:${examplesGeneratedPath}`,
63 `--ts_out=${examplesGeneratedPath}`,
64 ...glob.sync(resolve(__dirname, "proto", "**/*.proto"))
65);
66
67// Generate grpc-web services
68
69run(protocPath,
70 `--proto_path=${__dirname}`,
71 `--plugin=protoc-gen-ts=${protocPluginPath}`,
72 `--js_out=import_style=commonjs,binary:${examplesGeneratedGrpcWebPath}`,
73 `--ts_out=service=grpc-web:${examplesGeneratedGrpcWebPath}`,
74 ...glob.sync(resolve(__dirname, "proto", "**/*.proto"))
75);
76
77// Generate grpc-node services
78
79run(protocPath,
80 `--proto_path=${__dirname}`,
81 `--plugin=protoc-gen-ts=${protocPluginPath}`,
82 `--plugin=protoc-gen-grpc=node_modules/.bin/grpc_tools_node_protoc_plugin`,
83 `--js_out=import_style=commonjs,binary:${examplesGeneratedGrpcNodePath}`,
84 `--ts_out=service=grpc-node:${examplesGeneratedGrpcNodePath}`,
85 `--grpc_out=${examplesGeneratedGrpcNodePath}`,
86 ...glob.sync(resolve(__dirname, "proto", "**/*.proto"))
87);
88
89// Generate grpc-node services using grpc-js mode
90
91run(protocPath,
92 `--proto_path=${__dirname}`,
93 `--plugin=protoc-gen-ts=${protocPluginPath}`,
94 `--plugin=protoc-gen-grpc=node_modules/.bin/grpc_tools_node_protoc_plugin`,
95 `--js_out=import_style=commonjs,binary:${examplesGeneratedGrpcJsNodePath}`,
96 `--ts_out=service=grpc-node,mode=grpc-js:${examplesGeneratedGrpcJsNodePath}`,
97 `--grpc_out=grpc_js:${examplesGeneratedGrpcJsNodePath}`,
98 ...glob.sync(resolve(__dirname, "proto", "**/*.proto"))
99);
100
101run(rimrafPath, protocRoot);
102
103function requireBuild() {
104 console.log("Ensuring we have NPM packages installed...");
105 run("npm", "install");
106
107 console.log("Compiling ts-protoc-gen...");
108 run("npm", "run", "build");
109}
110
111function requireProtoc() {
112 if (existsSync(protocPath)) {
113 return;
114 }
115
116 if (!platform) {
117 throw new Error(
118 "Cannot download protoc. " +
119 platformName +
120 " is not currently supported by ts-protoc-gen"
121 );
122 }
123
124 console.log(`Downloading protoc v${protocVersion} for ${platform.name}`);
125 const protocUrl =
126 `https://github.com/google/protobuf/releases/download/v${protocVersion}/protoc-${protocVersion}-${platform.downloadSuffix}.zip`;
127
128 run(downloadPath,
129 "--extract",
130 "--out", protocRoot,
131 protocUrl);
132}
133
134function requireDir(path) {
135 if (existsSync(path)) {
136 run(rimrafPath, path);
137 }
138
139 mkdirSync(path);
140}
141
142function run(executablePath, ...args) {
143 const result = spawnSync(executablePath, args, { shell: true, stdio: "inherit" });
144 if (result.status !== 0) {
145 throw new Error(`Exited ${executablePath} with status ${result.status}`);
146 }
147}