11import * as path from "path" ;
22import * as fs from "fs" ;
33import * as tar from "tar" ;
4+ import axios , { AxiosRequestConfig } from "axios" ;
5+
46const AdmZip = require ( "adm-zip" ) ;
57import { ExtensionContext , OutputChannel } from "vscode" ;
68
9+ const defaultRequestConfig = {
10+ headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } ,
11+ timeout : 30_000
12+ } satisfies AxiosRequestConfig ;
13+
714/**
815 * Get the absolute path to GGShield binary. If it doesn't exist, it will be installed.
916 * @param platform The platform of the user
@@ -12,12 +19,12 @@ import { ExtensionContext, OutputChannel } from "vscode";
1219 * @param outputChannel The output channel to use
1320 * @returns The absolute path to the GGShield binary
1421 */
15- export function getGGShield (
22+ export async function getGGShield (
1623 platform : NodeJS . Platform ,
1724 arch : string ,
1825 context : ExtensionContext ,
1926 outputChannel : OutputChannel
20- ) : string {
27+ ) : Promise < string > {
2128 const version = fs
2229 . readFileSync ( path . join ( context . extensionPath , "ggshield_version" ) , "utf8" )
2330 . trim ( ) ;
@@ -47,7 +54,7 @@ export function getGGShield(
4754 }
4855 fs . mkdirSync ( ggshieldFolder ) ;
4956 // install GGShield
50- installGGShield ( platform , arch , ggshieldFolder , version ) ;
57+ await installGGShield ( platform , arch , ggshieldFolder , version ) ;
5158 outputChannel . appendLine (
5259 `Updated to GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.`
5360 ) ;
@@ -59,14 +66,16 @@ export function getGGShield(
5966 * Get the latest version of GGShield
6067 * @returns The latest version of GGShield
6168 */
62- export function getGGShieldLatestVersion ( ) : string {
63- const response = require ( "sync-request" ) (
64- "GET" ,
69+ export async function getGGShieldLatestVersion ( ) : Promise < string > {
70+ const { data } = await axios . get < { tag_name ?: string } > (
6571 "https://api.github.com/repos/GitGuardian/ggshield/releases/latest" ,
66- { headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } }
72+ {
73+ ...defaultRequestConfig ,
74+ responseEncoding : 'utf8'
75+ }
6776 ) ;
68- const data = JSON . parse ( response . getBody ( "utf8" ) ) ;
69- return data . tag_name ?. replace ( / ^ v / , "" ) ;
77+
78+ return data . tag_name ?. replace ( / ^ v / , "" ) ?? "" ;
7079}
7180
7281/**
@@ -119,12 +128,12 @@ export function computeGGShieldFolderName(
119128 * @param ggshieldFolder The folder of the GGShield binary
120129 * @param version The version of GGShield
121130 */
122- export function installGGShield (
131+ export async function installGGShield (
123132 platform : NodeJS . Platform ,
124133 arch : string ,
125134 ggshieldFolder : string ,
126135 version : string
127- ) : void {
136+ ) : Promise < void > {
128137 let extension : string = "" ;
129138 switch ( platform ) {
130139 case "win32" :
@@ -144,7 +153,7 @@ export function installGGShield(
144153 version
145154 ) } .${ extension } `;
146155 const downloadUrl : string = `https://github.com/GitGuardian/ggshield/releases/download/v${ version } /${ fileName } ` ;
147- downloadGGShieldFromGitHub ( fileName , downloadUrl , ggshieldFolder ) ;
156+ await downloadGGShieldFromGitHub ( fileName , downloadUrl , ggshieldFolder ) ;
148157 extractGGShieldBinary ( path . join ( ggshieldFolder , fileName ) , ggshieldFolder ) ;
149158}
150159
@@ -177,16 +186,18 @@ export function extractGGShieldBinary(
177186 * @param downloadUrl The URL of the GGShield binary
178187 * @param ggshieldFolder The folder of the GGShield binary
179188 */
180- function downloadGGShieldFromGitHub (
189+ async function downloadGGShieldFromGitHub (
181190 fileName : string ,
182191 downloadUrl : string ,
183192 ggshieldFolder : string
184- ) : void {
193+ ) : Promise < void > {
185194 console . log ( `Downloading GGShield from ${ downloadUrl } ` ) ;
186- const response = require ( "sync-request" ) ( "GET" , downloadUrl , {
187- headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } ,
195+ const { data } = await axios . get ( downloadUrl , {
196+ ...defaultRequestConfig ,
197+ responseType : 'arraybuffer'
188198 } ) ;
189- fs . writeFileSync ( path . join ( ggshieldFolder , fileName ) , response . getBody ( ) ) ;
199+
200+ fs . writeFileSync ( path . join ( ggshieldFolder , fileName ) , data ) ;
190201 console . log (
191202 `GGShield archive downloaded to ${ path . join ( ggshieldFolder , fileName ) } `
192203 ) ;
0 commit comments