1- import { $ , ShellOutput } from "bun" ;
1+ // @ts -nocheck
2+ import { $ } from "bun" ;
23import * as path from "path" ;
34import * as fs from "fs/promises" ;
45
@@ -11,7 +12,10 @@ type Jdk = {
1112 sha256 : string ;
1213} ;
1314
14- const USER_HOME = process . env . HOME ! ;
15+ const USER_HOME = process . env . HOME || process . env . USERPROFILE ;
16+ if ( ! USER_HOME ) {
17+ throw new Error ( "Cannot determine home directory: neither HOME nor USERPROFILE is set" ) ;
18+ }
1519const JDK_ROOT = path . join ( USER_HOME , "jdks" ) ;
1620
1721const JDK_LIST : Jdk [ ] = [
@@ -66,17 +70,16 @@ async function isFileExists(filepath: string) {
6670 }
6771}
6872
69- async function fileSha256 ( filepath : string ) {
70- const out = await $ `shasum -a 256 ${ filepath } ` . text ( ) ;
71- return out . split ( " " ) [ 0 ] ;
73+ async function fileSha256 ( filepath : string ) : Promise < string > {
74+ const file = Bun . file ( filepath ) ;
75+ const buffer = await file . arrayBuffer ( ) ;
76+ const hasher = new Bun . CryptoHasher ( "sha256" ) ;
77+ hasher . update ( buffer ) ;
78+ return hasher . digest ( "hex" ) ;
7279}
7380
74- async function throwIfStderrNotEmpty ( output : ShellOutput ) {
75- const err = output . stderr . toString ( ) . trim ( ) ;
76- if ( err . length > 0 ) {
77- throw new Error ( err ) ;
78- }
79- return output ;
81+ async function downloadFile ( url : string , destPath : string ) {
82+ await $ `curl -fsSL -o ${ destPath } ${ url } ` ;
8083}
8184
8285async function downloadAndExtractJdk ( jdk : Jdk ) {
@@ -94,21 +97,25 @@ async function downloadAndExtractJdk(jdk: Jdk) {
9497 downloaded = true ;
9598 } else {
9699 console . log ( `Removing broken ${ jdkFullName } ...` ) ;
97- throwIfStderrNotEmpty ( await $ `rm ${ filepath } ` ) ;
100+ await fs . unlink ( filepath ) ;
98101 }
99102 }
100103 if ( ! downloaded ) {
101- throwIfStderrNotEmpty ( await $ `wget --quiet -P ${ JDK_ROOT } ${ jdk . url } ` ) ;
104+ await downloadFile ( jdk . url , filepath ) ;
102105 }
103106
104107 console . log ( `Extracting ${ jdkFullName } ...` ) ;
105108 const extractPath = path . join ( JDK_ROOT , jdk . name ) ;
106109
107- await $ ` mkdir ${ extractPath } ` ;
110+ await fs . mkdir ( extractPath , { recursive : true } ) ;
108111 if ( filename . endsWith ( ".gz" ) ) {
109- throwIfStderrNotEmpty ( await $ `tar -xzf ${ filepath } -C ${ extractPath } ` ) ;
112+ await $ `tar -xzf ${ filepath } -C ${ extractPath } ` ;
110113 } else if ( filename . endsWith ( ".zip" ) ) {
111- throwIfStderrNotEmpty ( await $ `unzip -o -q ${ filepath } -d ${ extractPath } ` ) ;
114+ if ( process . platform === "win32" ) {
115+ await $ `tar -xf ${ filepath } -C ${ extractPath } ` ;
116+ } else {
117+ await $ `unzip -o -q ${ filepath } -d ${ extractPath } ` ;
118+ }
112119 } else {
113120 throw new Error ( `Unknown JDK archive: ${ filename } ` ) ;
114121 }
@@ -117,22 +124,26 @@ async function downloadAndExtractJdk(jdk: Jdk) {
117124}
118125
119126async function createEnvVars ( vars : ( readonly [ string , string ] ) [ ] ) {
127+ // Write directly to GITHUB_ENV if in CI
128+ const githubEnvFile = process . env . GITHUB_ENV ;
129+ if ( githubEnvFile ) {
130+ let content = "" ;
131+ for ( const [ varName , varValue ] of vars ) {
132+ content += `${ varName } =${ varValue } \n` ;
133+ }
134+ await fs . appendFile ( githubEnvFile , content ) ;
135+ console . log ( "Exported env variables to GITHUB_ENV" ) ;
136+ return ;
137+ }
138+
139+ // Otherwise write shell scripts for local use
120140 const varExportsFilepath = path . join ( USER_HOME , "java-home-vars.sh" ) ;
121- const ghVarsExportsFilepath = path . join (
122- USER_HOME ,
123- "java-home-vars-github.sh"
124- ) ;
125141
126142 let varExports = "# Generated by the JDK setup script." ;
127- let ghVarExports = "# Generated by the JDK setup script." ;
128- for ( const envVar of vars ) {
129- const varName = envVar [ 0 ] ;
130- const varValue = envVar [ 1 ] ;
143+ for ( const [ varName , varValue ] of vars ) {
131144 varExports += `\nexport ${ varName } =${ varValue } ` ;
132- ghVarExports += `\necho "${ varName } =${ varValue } " >> $GITHUB_ENV` ;
133145 }
134146 await fs . writeFile ( varExportsFilepath , varExports ) ;
135- await fs . writeFile ( ghVarsExportsFilepath , ghVarExports ) ;
136147
137148 console . log ( ) ;
138149 console . log (
@@ -141,10 +152,6 @@ async function createEnvVars(vars: (readonly [string, string])[]) {
141152 console . log ( ) ;
142153 console . log ( `>>> source ~/${ path . basename ( varExportsFilepath ) } ` ) ;
143154 console . log ( ) ;
144- console . log ( "Or export env variables in GitHub CI:" ) ;
145- console . log ( ) ;
146- console . log ( `>>> source ~/${ path . basename ( ghVarsExportsFilepath ) } ` ) ;
147- console . log ( ) ;
148155}
149156
150157const start = Date . now ( ) ;
@@ -155,7 +162,7 @@ console.log();
155162console . log ( "JDK ROOT:" , JDK_ROOT ) ;
156163console . log ( ) ;
157164
158- await $ ` mkdir ${ JDK_ROOT } ` ;
165+ await fs . mkdir ( JDK_ROOT , { recursive : true } ) ;
159166
160167// Download and extract
161168const envVars = await Promise . all (
0 commit comments