1 | const { spawnSync } = require("child_process");
|
2 |
|
3 | const { existsSync, mkdirSync } = require("fs");
|
4 | const { resolve } = require("path");
|
5 |
|
6 | const protocVersion = "3.15.5";
|
7 |
|
8 | const examplesGeneratedPath = resolve(__dirname, "examples", "generated");
|
9 | const examplesGeneratedGrpcWebPath = resolve(__dirname, "examples", "generated-grpc-web");
|
10 | const examplesGeneratedGrpcNodePath = resolve(__dirname, "examples", "generated-grpc-node");
|
11 | const examplesGeneratedGrpcJsNodePath = resolve(__dirname, "examples", "generated-grpc-js-node");
|
12 |
|
13 | const binSuffix = process.platform === "win32" ? ".cmd" : "";
|
14 | const nodeModulesBin = resolve(__dirname, "node_modules", ".bin");
|
15 |
|
16 | const downloadPath = resolve(nodeModulesBin, "download") + binSuffix;
|
17 |
|
18 | const protocRoot = resolve(__dirname, "protoc");
|
19 | const protocPath = resolve(protocRoot, "bin", "protoc");
|
20 |
|
21 | const protocPluginPath = resolve(__dirname, "bin", "protoc-gen-ts") + binSuffix;
|
22 |
|
23 | const rimrafPath = resolve(nodeModulesBin, "rimraf") + binSuffix;
|
24 |
|
25 | const 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 |
|
40 | const platform = supportedPlatforms[process.platform];
|
41 | const platformName = platform ?
|
42 | platform.name :
|
43 | `UNKNOWN:${process.platform}`;
|
44 | console.log("You appear to be running on", platformName);
|
45 |
|
46 | requireBuild();
|
47 |
|
48 | const glob = require("glob");
|
49 |
|
50 | requireProtoc();
|
51 |
|
52 | requireDir(examplesGeneratedPath);
|
53 | requireDir(examplesGeneratedGrpcWebPath);
|
54 | requireDir(examplesGeneratedGrpcNodePath);
|
55 | requireDir(examplesGeneratedGrpcJsNodePath);
|
56 |
|
57 |
|
58 |
|
59 | run(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 |
|
68 |
|
69 | run(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 |
|
78 |
|
79 | run(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 |
|
90 |
|
91 | run(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 |
|
101 | run(rimrafPath, protocRoot);
|
102 |
|
103 | function 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 |
|
111 | function 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 |
|
134 | function requireDir(path) {
|
135 | if (existsSync(path)) {
|
136 | run(rimrafPath, path);
|
137 | }
|
138 |
|
139 | mkdirSync(path);
|
140 | }
|
141 |
|
142 | function 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 | }
|