-
Notifications
You must be signed in to change notification settings - Fork 51
/
install.mjs
193 lines (171 loc) · 5.63 KB
/
install.mjs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { got } from 'got';
import { createFetch } from 'got-fetch';
import npmlog from 'npmlog';
import { HttpProxyAgent } from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import * as fs from 'fs';
import { temporaryFile } from 'tempy';
import * as tar from 'tar';
import * as pipeline from 'stream';
const _arch_mapping = { "x64": "amd64" };
const _platform_mapping = { "win32": "windows" };
const _platform = process.platform;
const _arch = process.arch;
// TODO FIND correct paths -> "./bin" goes to node_modules
const script_name = "protolint-install";
const module_name = "protolint";
const protolint_host = process.env.PROTOLINT_MIRROR_HOST ?? "https://github.com";
const protolint_path = process.env.PROTOLINT_MIRROR_REMOTE_PATH ?? `yoheimuta/${module_name}/releases/download/`;
const protolint_version = process.env.npm_package_version;
const platform = _platform_mapping[_platform] ?? _platform;
const arch = _arch_mapping[_arch] ?? _arch;
const url = `${protolint_host}/${protolint_path}/v${protolint_version}/${module_name}_${protolint_version}_${platform}_${arch}.tar.gz`;
let httpAgent;
let httpsAgent;
let proxy_address;
if (!process.env.PROTOLINT_NO_PROXY) {
proxy_address = process.env.PROTOLINT_PROXY;
if (!proxy_address)
{
proxy_address = protolint_host.startsWith("https") ? process.env.HTTPS_PROXY : process.env.HTTP_PROXY;
}
}
if (proxy_address) {
httpAgent = new HttpProxyAgent(proxy_address);
httpsAgent = new HttpsProxyAgent(proxy_address);
}
const agent_config = {
http: httpAgent,
https: httpsAgent
};
const got_config = {
followRedirect: true,
maxRedirects: 3,
username: process.env.PROTOLINT_MIRROR_USERNAME ?? '',
password: process.env.PROTOLINT_MIRROR_PASSWORD ?? '',
agent: agent_config,
};
const instance = got.extend(got_config);
function get_filename_with_extension(fileName) {
const ext = process.platform == "win32" ? ".exe" : "";
return `${fileName}${ext}`;
}
npmlog.info(script_name, "Fetching protolint executable from %s", url);
const fetch = createFetch(instance);
fetch(url).then(
async response => {
if (response.ok)
{
const targetFile = temporaryFile({ name: "_protolint.tar.gz"});
const out = fs.createWriteStream(targetFile, {
flags: "w+"
});
var success = undefined;
const streaming = pipeline.pipeline(
response.body,
out,
(err) => {
if (err)
{
npmlog.error(script_name, "Failed to save downloaded file: %s", err);
success = false;
}
else
{
npmlog.info(script_name, "Protolint saved to %s", targetFile);
success = true;
}
}
);
while (success === undefined)
{
await new Promise(resolve => setTimeout(resolve, 1000));
}
if (success)
{
return targetFile;
}
return null;
}
else
{
npmlog.error(script_name, "Failed to download %s. Got status: %i", response.url, response.status);
return null;
}
}
).then(
previous => {
if (!fs.existsSync("./bin"))
{
fs.mkdirSync("./bin");
}
return previous;
}
).then(
async file => {
if (file)
{
const result = await tar.x(
{
"keep-existing": false,
cwd: "./bin/",
sync: false,
file: file,
strict: true,
},
[
get_filename_with_extension("protolint"),
get_filename_with_extension("protoc-gen-protolint"),
],
(err) => {
if (err) {
npmlog.error(script_name, "Failed to extract protlint executables: %s");
}
},
)
.then(
() => {
return {
protolint: `./bin/${get_filename_with_extension("protolint")}`,
protoc_gen_protolint: `./bin/${get_filename_with_extension("protoc-gen-protolint")}`,
};
}
).catch(
(err) => {
npmlog.error(script_name, "Failed to extract files from downloaded tar file: %s", err);
return {
protolint: undefined,
protoc_gen_protolint: undefined,
};
}
);
return result;
}
else
{
npmlog.warn(script_name, "Could not find downloaded protolint archive.");
return {
protolint: undefined,
protoc_gen_protolint: undefined,
};
}
}
).then(
(protolint_obj) => {
return (protolint_obj != null && protolint_obj.protolint != null && protolint_obj.protoc_gen_protolint != null);
}
).then(
(result) => {
if (result){
npmlog.info(script_name, "Protolint installed successfully.");
}
else {
npmlog.warn(script_name, "Failed to download protolint. See previous messages for details");
}
}
).catch(
reason => {
npmlog.error(script_name, "Failed to install protolint: %s", reason);
process.exit(1);
}
);