github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/bdist/js/install.mjs (about) 1 import { got } from 'got'; 2 import { createFetch } from 'got-fetch'; 3 import npmlog from 'npmlog'; 4 import { HttpProxyAgent } from 'http-proxy-agent'; 5 import * as fs from 'fs'; 6 import { temporaryFile } from 'tempy'; 7 import * as tar from 'tar'; 8 import * as pipeline from 'stream'; 9 10 const _arch_mapping = { "x64": "amd64" }; 11 const _platform_mapping = { "win32": "windows" }; 12 13 const _platform = process.platform; 14 const _arch = process.arch; 15 16 // TODO FIND correct paths -> "./bin" goes to node_modules 17 18 const script_name = "protolint-install"; 19 20 const module_name = "protolint"; 21 const protolint_host = process.env.PROTOLINT_MIRROR_HOST ?? "https://github.com"; 22 const protolint_path = process.env.PROTOLINT_MIRROR_REMOTE_PATH ?? `yoheimuta/${module_name}/releases/download/`; 23 const protolint_version = process.env.npm_package_version; 24 const platform = _platform_mapping[_platform] ?? _platform; 25 const arch = _arch_mapping[_arch] ?? _arch; 26 27 const url = `${protolint_host}/${protolint_path}/v${protolint_version}/${module_name}_${protolint_version}_${platform}_${arch}.tar.gz`; 28 29 let agent; 30 if (process.env.PROTOLINT_PROXY) { 31 agent = HttpProxyAgent(process.env.PROTOLINT_PROXY); 32 } 33 34 const agent_config = { 35 http: agent 36 }; 37 38 const got_config = { 39 followRedirect: true, 40 maxRedirects: 3, 41 username: process.env.PROTOLINT_MIRROR_USERNAME ?? '', 42 password: process.env.PROTOLINT_MIRROR_PASSWORD ?? '', 43 agent: agent_config, 44 }; 45 46 const instance = got.extend(got_config); 47 48 function get_filename_with_extension(fileName) { 49 const ext = process.platform == "win32" ? ".exe" : ""; 50 return `${fileName}${ext}`; 51 } 52 53 npmlog.info(script_name, "Fetching protolint executable from %s", url); 54 55 const fetch = createFetch(instance); 56 57 fetch(url).then( 58 async response => { 59 if (response.ok) 60 { 61 const targetFile = temporaryFile({ name: "_protolint.tar.gz"}); 62 const out = fs.createWriteStream(targetFile, { 63 flags: "w+" 64 }); 65 var success = undefined; 66 const streaming = pipeline.pipeline( 67 response.body, 68 out, 69 (err) => { 70 if (err) 71 { 72 npmlog.error(script_name, "Failed to save downloaded file: %s", err); 73 success = false; 74 } 75 else 76 { 77 npmlog.info(script_name, "Protolint saved to %s", targetFile); 78 success = true; 79 } 80 } 81 ); 82 83 while (success === undefined) 84 { 85 await new Promise(resolve => setTimeout(resolve, 1000)); 86 } 87 88 if (success) 89 { 90 return targetFile; 91 } 92 93 return null; 94 } 95 else 96 { 97 npmlog.error(script_name, "Failed to download %s. Got status: %i", response.url, response.status); 98 return null; 99 } 100 } 101 ).then( 102 previous => { 103 if (!fs.existsSync("./bin")) 104 { 105 fs.mkdirSync("./bin"); 106 } 107 108 return previous; 109 } 110 ).then( 111 async file => { 112 if (file) 113 { 114 const result = await tar.x( 115 { 116 "keep-existing": false, 117 cwd: "./bin/", 118 sync: false, 119 file: file, 120 strict: true, 121 }, 122 [ 123 get_filename_with_extension("protolint"), 124 get_filename_with_extension("protoc-gen-protolint"), 125 ], 126 (err) => { 127 if (err) { 128 npmlog.error(script_name, "Failed to extract protlint executables: %s"); 129 } 130 }, 131 ) 132 .then( 133 () => { 134 return { 135 protolint: `./bin/${get_filename_with_extension("protolint")}`, 136 protoc_gen_protolint: `./bin/${get_filename_with_extension("protoc-gen-protolint")}`, 137 }; 138 } 139 ).catch( 140 (err) => { 141 npmlog.error(script_name, "Failed to extract files from downloaded tar file: %s", err); 142 return { 143 protolint: undefined, 144 protoc_gen_protolint: undefined, 145 }; 146 } 147 ); 148 149 return result; 150 } 151 else 152 { 153 npmlog.warn(script_name, "Could not find downloaded protolint archive."); 154 return { 155 protolint: undefined, 156 protoc_gen_protolint: undefined, 157 }; 158 } 159 } 160 ).then( 161 (protolint_obj) => { 162 return (protolint_obj != null && protolint_obj.protolint != null && protolint_obj.protoc_gen_protolint != null); 163 } 164 ).then( 165 (result) => { 166 if (result){ 167 npmlog.info(script_name, "Protolint installed successfully."); 168 } 169 else { 170 npmlog.warn(script_name, "Failed to download protolint. See previous messages for details"); 171 } 172 } 173 ).catch( 174 reason => { 175 npmlog.error(script_name, "Failed to install protolint: %s", reason); 176 process.exit(1); 177 } 178 );