import { got } from 'got';
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 fetch_protolint = got.extend(got_config);
function get_filename_with_extension(fileName) {
const ext = process.platform == "win32" ? ".exe" : "";
return `${fileName}${ext}`;
}
console.info("%s: Fetching protolint executable from %s", script_name, url);
fetch_protolint(url, { responseType: "buffer" }).then(
async response => {
if (response.ok)
{
try {
const targetFile = temporaryFile({ name: "_protolint.tar.gz"});
fs.writeFileSync(targetFile, response.body);
console.info("%s: Protolint saved to %s", script_name, targetFile);
return targetFile;
}
catch (error) {
console.error("%s: Failed to save downloaded file: %s", script_name, err);
}
}
else
{
console.error("%s: Failed to download %s. Got status: %i", script_name, 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) {
console.error("%s: Failed to extract protlint executables: %s", script_name, err);
}
},
)
.then(
() => {
return {
protolint: `./bin/${get_filename_with_extension("protolint")}`,
protoc_gen_protolint: `./bin/${get_filename_with_extension("protoc-gen-protolint")}`,
};
}
).catch(
(err) => {
console.error("%s: Failed to extract files from downloaded tar file: %s", script_name, err);
return {
protolint: undefined,
protoc_gen_protolint: undefined,
};
}
);
return result;
}
else
{
console.warn("%s: Could not find downloaded protolint archive.", script_name);
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){
console.info("%s: Protolint installed successfully.", script_name);
}
else {
console.warn("%s: Failed to download protolint. See previous messages for details", script_name);
}
}
).catch(
reason => {
console.error("%s: Failed to install protolint: %s", script_name, reason);
process.exit(1);
}
);