1
1
import * as path from "path" ;
2
2
import * as fs from "fs" ;
3
3
import * as tar from "tar" ;
4
+ import axios , { AxiosRequestConfig } from "axios" ;
5
+
4
6
const AdmZip = require ( "adm-zip" ) ;
5
7
import { ExtensionContext , OutputChannel } from "vscode" ;
6
8
9
+ const defaultRequestConfig = {
10
+ headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } ,
11
+ timeout : 30_000
12
+ } satisfies AxiosRequestConfig ;
13
+
7
14
/**
8
15
* Get the absolute path to GGShield binary. If it doesn't exist, it will be installed.
9
16
* @param platform The platform of the user
@@ -12,12 +19,12 @@ import { ExtensionContext, OutputChannel } from "vscode";
12
19
* @param outputChannel The output channel to use
13
20
* @returns The absolute path to the GGShield binary
14
21
*/
15
- export function getGGShield (
22
+ export async function getGGShield (
16
23
platform : NodeJS . Platform ,
17
24
arch : string ,
18
25
context : ExtensionContext ,
19
26
outputChannel : OutputChannel
20
- ) : string {
27
+ ) : Promise < string > {
21
28
const version = fs
22
29
. readFileSync ( path . join ( context . extensionPath , "ggshield_version" ) , "utf8" )
23
30
. trim ( ) ;
@@ -47,7 +54,7 @@ export function getGGShield(
47
54
}
48
55
fs . mkdirSync ( ggshieldFolder ) ;
49
56
// install GGShield
50
- installGGShield ( platform , arch , ggshieldFolder , version ) ;
57
+ await installGGShield ( platform , arch , ggshieldFolder , version ) ;
51
58
outputChannel . appendLine (
52
59
`Updated to GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.`
53
60
) ;
@@ -59,14 +66,16 @@ export function getGGShield(
59
66
* Get the latest version of GGShield
60
67
* @returns The latest version of GGShield
61
68
*/
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 } > (
65
71
"https://api.github.com/repos/GitGuardian/ggshield/releases/latest" ,
66
- { headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } }
72
+ {
73
+ ...defaultRequestConfig ,
74
+ responseEncoding : 'utf8'
75
+ }
67
76
) ;
68
- const data = JSON . parse ( response . getBody ( "utf8" ) ) ;
69
- return data . tag_name ?. replace ( / ^ v / , "" ) ;
77
+
78
+ return data . tag_name ?. replace ( / ^ v / , "" ) ?? "" ;
70
79
}
71
80
72
81
/**
@@ -119,12 +128,12 @@ export function computeGGShieldFolderName(
119
128
* @param ggshieldFolder The folder of the GGShield binary
120
129
* @param version The version of GGShield
121
130
*/
122
- export function installGGShield (
131
+ export async function installGGShield (
123
132
platform : NodeJS . Platform ,
124
133
arch : string ,
125
134
ggshieldFolder : string ,
126
135
version : string
127
- ) : void {
136
+ ) : Promise < void > {
128
137
let extension : string = "" ;
129
138
switch ( platform ) {
130
139
case "win32" :
@@ -144,7 +153,7 @@ export function installGGShield(
144
153
version
145
154
) } .${ extension } `;
146
155
const downloadUrl : string = `https://github.com/GitGuardian/ggshield/releases/download/v${ version } /${ fileName } ` ;
147
- downloadGGShieldFromGitHub ( fileName , downloadUrl , ggshieldFolder ) ;
156
+ await downloadGGShieldFromGitHub ( fileName , downloadUrl , ggshieldFolder ) ;
148
157
extractGGShieldBinary ( path . join ( ggshieldFolder , fileName ) , ggshieldFolder ) ;
149
158
}
150
159
@@ -177,16 +186,18 @@ export function extractGGShieldBinary(
177
186
* @param downloadUrl The URL of the GGShield binary
178
187
* @param ggshieldFolder The folder of the GGShield binary
179
188
*/
180
- function downloadGGShieldFromGitHub (
189
+ async function downloadGGShieldFromGitHub (
181
190
fileName : string ,
182
191
downloadUrl : string ,
183
192
ggshieldFolder : string
184
- ) : void {
193
+ ) : Promise < void > {
185
194
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'
188
198
} ) ;
189
- fs . writeFileSync ( path . join ( ggshieldFolder , fileName ) , response . getBody ( ) ) ;
199
+
200
+ fs . writeFileSync ( path . join ( ggshieldFolder , fileName ) , data ) ;
190
201
console . log (
191
202
`GGShield archive downloaded to ${ path . join ( ggshieldFolder , fileName ) } `
192
203
) ;
0 commit comments