1+ #!/usr/bin/env bun
12import { CerUtils } from './cerUtils.js' ;
23
34interface DownloadResult {
@@ -6,7 +7,7 @@ interface DownloadResult {
67 error ?: string ;
78}
89
9- class CaDownloader {
10+ export class CaDownloader {
1011 private readonly caDirectory = 'CA-PEM' ;
1112 private readonly certificates = [
1213 'AppleWWDRCA' ,
@@ -20,7 +21,7 @@ class CaDownloader {
2021 private getCertificateUrl ( filename : string ) : string {
2122 // The first CA certificate (AppleWWDRCA) is stored in a different place for some reason...
2223 if ( filename . endsWith ( 'A' ) ) {
23- return ' https://developer.apple.com/certificationauthority/AppleWWDRCA .cer' ;
24+ return ` https://developer.apple.com/certificationauthority/${ filename } .cer` ;
2425 }
2526 return `https://www.apple.com/certificateauthority/${ filename } .cer` ;
2627 }
@@ -30,7 +31,7 @@ class CaDownloader {
3031 const url = this . getCertificateUrl ( filename ) ;
3132 const tempFileName = `cctmp-${ filename } .cer` ;
3233
33- console . log ( `[*] Downloading ${ filename } .cer...` ) ;
34+ console . error ( `[*] Downloading ${ filename } .cer...` ) ;
3435 const response = await fetch ( url ) ;
3536
3637 if ( ! response . ok ) {
@@ -54,7 +55,7 @@ class CaDownloader {
5455 try {
5556 const tempCerPath = `cctmp-${ filename } .cer` ;
5657
57- console . log ( `[*] '${ filename } .cer' has been downloaded, converting to PEM format...` ) ;
58+ console . error ( `[*] '${ filename } .cer' has been downloaded, converting to PEM format...` ) ;
5859
5960 await new CerUtils ( tempCerPath ) . convertToPem ( ) ;
6061
@@ -75,7 +76,7 @@ class CaDownloader {
7576 await Bun . file ( tempCerPath ) . unlink ( ) ;
7677 await Bun . file ( tempPemPath ) . unlink ( ) ;
7778
78- console . log ( `[*] Successfully converted and moved ${ filename } .pem to CA-PEM directory` ) ;
79+ console . error ( `[*] Successfully converted and moved ${ filename } .pem to CA-PEM directory` ) ;
7980 } catch ( error ) {
8081 const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
8182 console . error ( `[!] Failed to convert ${ filename } .cer: ${ errorMessage } ` ) ;
@@ -85,12 +86,12 @@ class CaDownloader {
8586 private async ensureCaDirectory ( ) : Promise < void > {
8687 if ( ! ( await Bun . file ( this . caDirectory ) . exists ( ) ) ) {
8788 await Bun . write ( `${ this . caDirectory } /.gitkeep` , '' ) ;
88- console . log ( `[*] Created ${ this . caDirectory } directory` ) ;
89+ console . error ( `[*] Created ${ this . caDirectory } directory` ) ;
8990 }
9091 }
9192
9293 public async downloadAllCertificates ( ) : Promise < void > {
93- console . log ( '[*] Downloading resources...\n[*] Once complete, you can run the main script again.\n' ) ;
94+ console . error ( '[*] Downloading resources...\n[*] Once complete, you can run the main script again.\n' ) ;
9495
9596 await this . ensureCaDirectory ( ) ;
9697
@@ -105,15 +106,23 @@ class CaDownloader {
105106 } ) ;
106107
107108 await Promise . all ( downloadPromises ) ;
108- console . log ( '\n[*] Download process completed!' ) ;
109+ console . error ( '\n[*] Download process completed!' ) ;
109110 }
110111}
111112
112- // Main execution
113- const downloader = new CaDownloader ( ) ;
114- await downloader . downloadAllCertificates ( ) . catch ( error => {
115- console . error ( '[!] Unexpected error during download:' , error ) ;
116- process . exit ( 1 ) ;
117- } ) ;
113+ // Export a standalone function for importing
114+ export async function downloadCaCertificates ( ) : Promise < void > {
115+ const downloader = new CaDownloader ( ) ;
116+ await downloader . downloadAllCertificates ( ) ;
117+ }
118+
119+ // Main execution (only run if this file is executed directly)
120+ if ( import . meta. main ) {
121+ const downloader = new CaDownloader ( ) ;
122+ await downloader . downloadAllCertificates ( ) . catch ( error => {
123+ console . error ( '[!] Unexpected error during download:' , error ) ;
124+ process . exit ( 1 ) ;
125+ } ) ;
126+ }
118127
119128export { } ;
0 commit comments