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" ;
4
+ import axios , { AxiosRequestConfig } from "axios" ;
5
5
import { Agent } from "https" ;
6
6
7
7
const AdmZip = require ( "adm-zip" ) ;
8
8
import { ExtensionContext , OutputChannel } from "vscode" ;
9
9
10
10
const defaultRequestConfig = {
11
11
headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } ,
12
- timeout : 30_000
12
+ timeout : 30_000 ,
13
13
} satisfies AxiosRequestConfig ;
14
14
15
+ /**
16
+ * Get the version of GGShield
17
+ * @param context The extension context
18
+ * @returns The version of GGShield
19
+ */
20
+ export function getGGShieldVersion ( context : ExtensionContext ) : string {
21
+ return fs
22
+ . readFileSync ( path . join ( context . extensionPath , "ggshield_version" ) , "utf8" )
23
+ . trim ( ) ;
24
+ }
25
+
15
26
/**
16
27
* Get the absolute path to GGShield binary. If it doesn't exist, it will be installed.
17
28
* @param platform The platform of the user
@@ -25,27 +36,25 @@ export async function getGGShield(
25
36
arch : string ,
26
37
context : ExtensionContext ,
27
38
outputChannel : OutputChannel ,
28
- ignoreSSLErrors : boolean
39
+ ignoreSSLErrors : boolean ,
29
40
) : Promise < string > {
30
- const version = fs
31
- . readFileSync ( path . join ( context . extensionPath , "ggshield_version" ) , "utf8" )
32
- . trim ( ) ;
41
+ const version = getGGShieldVersion ( context ) ;
33
42
console . log ( `Latest GGShield version: ${ version } ` ) ;
34
43
const ggshieldFolder : string = path . join (
35
44
context . extensionPath ,
36
- "ggshield-internal"
45
+ "ggshield-internal" ,
37
46
) ;
38
47
const ggshieldBinaryPath : string = computeGGShieldPath (
39
48
platform ,
40
49
arch ,
41
50
ggshieldFolder ,
42
- version
51
+ version ,
43
52
) ;
44
53
45
54
// if exists, return the path
46
55
if ( fs . existsSync ( ggshieldBinaryPath ) ) {
47
56
outputChannel . appendLine (
48
- `Using GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.`
57
+ `Using GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.` ,
49
58
) ;
50
59
console . log ( `GGShield already exists at ${ ggshieldBinaryPath } ` ) ;
51
60
return ggshieldBinaryPath ;
@@ -56,30 +65,20 @@ export async function getGGShield(
56
65
}
57
66
fs . mkdirSync ( ggshieldFolder ) ;
58
67
// install GGShield
59
- await installGGShield ( platform , arch , ggshieldFolder , version , ignoreSSLErrors ) ;
68
+ await installGGShield (
69
+ platform ,
70
+ arch ,
71
+ ggshieldFolder ,
72
+ version ,
73
+ ignoreSSLErrors ,
74
+ ) ;
60
75
outputChannel . appendLine (
61
- `Updated to GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.`
76
+ `Updated to GGShield v${ version } . Checkout https://github.com/GitGuardian/ggshield for more info.` ,
62
77
) ;
63
78
console . log ( `GGShield binary installed at ${ ggshieldBinaryPath } ` ) ;
64
79
return ggshieldBinaryPath ;
65
80
}
66
81
67
- /**
68
- * Get the latest version of GGShield
69
- * @returns The latest version of GGShield
70
- */
71
- export async function getGGShieldLatestVersion ( ) : Promise < string > {
72
- const { data } = await axios . get < { tag_name ?: string } > (
73
- "https://api.github.com/repos/GitGuardian/ggshield/releases/latest" ,
74
- {
75
- ...defaultRequestConfig ,
76
- responseEncoding : 'utf8'
77
- }
78
- ) ;
79
-
80
- return data . tag_name ?. replace ( / ^ v / , "" ) ?? "" ;
81
- }
82
-
83
82
/**
84
83
* Compute the folder name of the GGShield binary
85
84
* @param platform The platform of the user
@@ -90,7 +89,7 @@ export async function getGGShieldLatestVersion(): Promise<string> {
90
89
export function computeGGShieldFolderName (
91
90
platform : NodeJS . Platform ,
92
91
arch : string ,
93
- version : string
92
+ version : string ,
94
93
) : string {
95
94
let archString : string = "" ;
96
95
let platformString : string = "" ;
@@ -153,10 +152,15 @@ export async function installGGShield(
153
152
const fileName : string = `${ computeGGShieldFolderName (
154
153
platform ,
155
154
arch ,
156
- version
155
+ version ,
157
156
) } .${ extension } `;
158
157
const downloadUrl : string = `https://github.com/GitGuardian/ggshield/releases/download/v${ version } /${ fileName } ` ;
159
- await downloadGGShieldFromGitHub ( fileName , downloadUrl , ggshieldFolder , ignoreSSLErrors ) ;
158
+ await downloadGGShieldFromGitHub (
159
+ fileName ,
160
+ downloadUrl ,
161
+ ggshieldFolder ,
162
+ ignoreSSLErrors ,
163
+ ) ;
160
164
extractGGShieldBinary ( path . join ( ggshieldFolder , fileName ) , ggshieldFolder ) ;
161
165
}
162
166
@@ -167,7 +171,7 @@ export async function installGGShield(
167
171
*/
168
172
export function extractGGShieldBinary (
169
173
filePath : string ,
170
- ggshieldFolder : string
174
+ ggshieldFolder : string ,
171
175
) : void {
172
176
if ( filePath . endsWith ( ".tar.gz" ) ) {
173
177
tar . x ( {
@@ -193,25 +197,25 @@ async function downloadGGShieldFromGitHub(
193
197
fileName : string ,
194
198
downloadUrl : string ,
195
199
ggshieldFolder : string ,
196
- ignoreSSLErrors : boolean
200
+ ignoreSSLErrors : boolean ,
197
201
) : Promise < void > {
198
202
console . log ( `Downloading GGShield from ${ downloadUrl } ` ) ;
199
203
200
204
const instance = ignoreSSLErrors
201
205
? new Agent ( {
202
206
rejectUnauthorized : false ,
203
- } )
207
+ } )
204
208
: undefined ;
205
209
206
210
const { data } = await axios . get ( downloadUrl , {
207
211
...defaultRequestConfig ,
208
- responseType : ' arraybuffer' ,
209
- httpsAgent : instance
212
+ responseType : " arraybuffer" ,
213
+ httpsAgent : instance ,
210
214
} ) ;
211
215
212
216
fs . writeFileSync ( path . join ( ggshieldFolder , fileName ) , data ) ;
213
217
console . log (
214
- `GGShield archive downloaded to ${ path . join ( ggshieldFolder , fileName ) } `
218
+ `GGShield archive downloaded to ${ path . join ( ggshieldFolder , fileName ) } ` ,
215
219
) ;
216
220
}
217
221
@@ -226,7 +230,7 @@ export function computeGGShieldPath(
226
230
platform : NodeJS . Platform ,
227
231
arch : string ,
228
232
ggshieldFolder : string ,
229
- version : string
233
+ version : string ,
230
234
) : string {
231
235
console . log ( `Platform: ${ platform } ; Arch: ${ arch } ` ) ;
232
236
let executable : string = "" ;
@@ -245,6 +249,6 @@ export function computeGGShieldPath(
245
249
return path . join (
246
250
ggshieldFolder ,
247
251
computeGGShieldFolderName ( platform , arch , version ) ,
248
- executable
252
+ executable ,
249
253
) ;
250
254
}
0 commit comments