11import * as path from "path" ;
22import * as fs from "fs" ;
33import * as tar from "tar" ;
4- import axios , { AxiosRequestConfig } from "axios" ;
4+ import axios , { AxiosRequestConfig } from "axios" ;
55import { Agent } from "https" ;
66
77const AdmZip = require ( "adm-zip" ) ;
88import { ExtensionContext , OutputChannel } from "vscode" ;
99
1010const defaultRequestConfig = {
1111 headers : { "User-Agent" : "GitGuardian-VSCode-Extension" } ,
12- timeout : 30_000
12+ timeout : 30_000 ,
1313} satisfies AxiosRequestConfig ;
1414
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+
1526/**
1627 * Get the absolute path to GGShield binary. If it doesn't exist, it will be installed.
1728 * @param platform The platform of the user
@@ -25,27 +36,25 @@ export async function getGGShield(
2536 arch : string ,
2637 context : ExtensionContext ,
2738 outputChannel : OutputChannel ,
28- ignoreSSLErrors : boolean
39+ ignoreSSLErrors : boolean ,
2940) : Promise < string > {
30- const version = fs
31- . readFileSync ( path . join ( context . extensionPath , "ggshield_version" ) , "utf8" )
32- . trim ( ) ;
41+ const version = getGGShieldVersion ( context ) ;
3342 console . log ( `Latest GGShield version: ${ version } ` ) ;
3443 const ggshieldFolder : string = path . join (
3544 context . extensionPath ,
36- "ggshield-internal"
45+ "ggshield-internal" ,
3746 ) ;
3847 const ggshieldBinaryPath : string = computeGGShieldPath (
3948 platform ,
4049 arch ,
4150 ggshieldFolder ,
42- version
51+ version ,
4352 ) ;
4453
4554 // if exists, return the path
4655 if ( fs . existsSync ( ggshieldBinaryPath ) ) {
4756 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.` ,
4958 ) ;
5059 console . log ( `GGShield already exists at ${ ggshieldBinaryPath } ` ) ;
5160 return ggshieldBinaryPath ;
@@ -56,30 +65,20 @@ export async function getGGShield(
5665 }
5766 fs . mkdirSync ( ggshieldFolder ) ;
5867 // install GGShield
59- await installGGShield ( platform , arch , ggshieldFolder , version , ignoreSSLErrors ) ;
68+ await installGGShield (
69+ platform ,
70+ arch ,
71+ ggshieldFolder ,
72+ version ,
73+ ignoreSSLErrors ,
74+ ) ;
6075 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.` ,
6277 ) ;
6378 console . log ( `GGShield binary installed at ${ ggshieldBinaryPath } ` ) ;
6479 return ggshieldBinaryPath ;
6580}
6681
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-
8382/**
8483 * Compute the folder name of the GGShield binary
8584 * @param platform The platform of the user
@@ -90,7 +89,7 @@ export async function getGGShieldLatestVersion(): Promise<string> {
9089export function computeGGShieldFolderName (
9190 platform : NodeJS . Platform ,
9291 arch : string ,
93- version : string
92+ version : string ,
9493) : string {
9594 let archString : string = "" ;
9695 let platformString : string = "" ;
@@ -153,10 +152,15 @@ export async function installGGShield(
153152 const fileName : string = `${ computeGGShieldFolderName (
154153 platform ,
155154 arch ,
156- version
155+ version ,
157156 ) } .${ extension } `;
158157 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+ ) ;
160164 extractGGShieldBinary ( path . join ( ggshieldFolder , fileName ) , ggshieldFolder ) ;
161165}
162166
@@ -167,7 +171,7 @@ export async function installGGShield(
167171 */
168172export function extractGGShieldBinary (
169173 filePath : string ,
170- ggshieldFolder : string
174+ ggshieldFolder : string ,
171175) : void {
172176 if ( filePath . endsWith ( ".tar.gz" ) ) {
173177 tar . x ( {
@@ -193,25 +197,25 @@ async function downloadGGShieldFromGitHub(
193197 fileName : string ,
194198 downloadUrl : string ,
195199 ggshieldFolder : string ,
196- ignoreSSLErrors : boolean
200+ ignoreSSLErrors : boolean ,
197201) : Promise < void > {
198202 console . log ( `Downloading GGShield from ${ downloadUrl } ` ) ;
199203
200204 const instance = ignoreSSLErrors
201205 ? new Agent ( {
202206 rejectUnauthorized : false ,
203- } )
207+ } )
204208 : undefined ;
205209
206210 const { data } = await axios . get ( downloadUrl , {
207211 ...defaultRequestConfig ,
208- responseType : ' arraybuffer' ,
209- httpsAgent : instance
212+ responseType : " arraybuffer" ,
213+ httpsAgent : instance ,
210214 } ) ;
211215
212216 fs . writeFileSync ( path . join ( ggshieldFolder , fileName ) , data ) ;
213217 console . log (
214- `GGShield archive downloaded to ${ path . join ( ggshieldFolder , fileName ) } `
218+ `GGShield archive downloaded to ${ path . join ( ggshieldFolder , fileName ) } ` ,
215219 ) ;
216220}
217221
@@ -226,7 +230,7 @@ export function computeGGShieldPath(
226230 platform : NodeJS . Platform ,
227231 arch : string ,
228232 ggshieldFolder : string ,
229- version : string
233+ version : string ,
230234) : string {
231235 console . log ( `Platform: ${ platform } ; Arch: ${ arch } ` ) ;
232236 let executable : string = "" ;
@@ -245,6 +249,6 @@ export function computeGGShieldPath(
245249 return path . join (
246250 ggshieldFolder ,
247251 computeGGShieldFolderName ( platform , arch , version ) ,
248- executable
252+ executable ,
249253 ) ;
250254}
0 commit comments