1
- import "https://deno.land/[email protected] /dotenv/load.ts" ;
2
- import { ensureDir } from "https://deno.land/[email protected] /fs/mod.ts" ;
3
- import {
4
- basename ,
5
- dirname ,
6
- join ,
7
- relative ,
8
- resolve ,
9
- } from "https://deno.land/[email protected] /path/mod.ts" ;
10
- import { getInput } from "npm:@actions/core@^1.10.1" ;
11
- import { context } from "npm:@actions/github@^5.1.0" ;
12
- import JSZip from "npm:jszip@^3.10.1" ;
13
- // @deno -types="npm:@types/lodash.maxby@^4"
14
- import maxBy from "npm:lodash.maxby@^4.6.0" ;
15
- import { Octokit } from "npm:octokit@^3.1.2" ;
16
- // @deno -types="npm:@types/parse-github-url@^1"
17
- import gh from "npm:parse-github-url@^1.0.2" ;
18
- import { simpleGit } from "npm:simple-git@^3.22.0" ;
19
- // @deno -types="npm:@types/semver@^7"
20
- import {
21
- clean ,
22
- coerce ,
23
- inc ,
24
- parse ,
25
- Range ,
26
- satisfies ,
27
- valid ,
28
- } from "npm:semver@^7.6.0" ;
29
- import { z } from "npm:zod@^3.22.4" ;
1
+ import { readFile , writeFile } from "node:fs/promises" ;
2
+ import { EOL } from "node:os" ;
3
+ import { basename , dirname , join , relative , resolve } from "node:path" ;
4
+ import { env , exit } from "node:process" ;
5
+ import { Glob , inspect } from "bun" ;
6
+ import { getInput } from "@actions/core" ;
7
+ import { context } from "@actions/github" ;
8
+ import { ensureDir } from "fs-extra" ;
9
+ import JSZip from "jszip" ;
10
+ import maxBy from "lodash.maxby" ;
11
+ import { Octokit } from "octokit" ;
12
+ import gh from "parse-github-url" ;
13
+ import { simpleGit } from "simple-git" ;
14
+ import { coerce , inc , parse , Range , satisfies } from "semver" ;
15
+ import { z } from "zod" ;
30
16
import payloadJson from "./payload.json" with { type : "json" } ;
31
17
32
18
const repoSchema = z . object ( {
@@ -120,51 +106,54 @@ const createMetadata = (
120
106
} ) satisfies Metadata ;
121
107
122
108
async function * getFilePaths ( dir : string ) : AsyncGenerator < string > {
123
- for await ( const entry of Deno . readDir ( dir ) ) {
124
- const path = resolve ( dir , entry . name ) ;
125
- if ( entry . isDirectory ) {
126
- yield * getFilePaths ( path ) ;
127
- } else {
128
- yield path ;
129
- }
109
+ const glob = new Glob ( "**/*" ) ;
110
+ for await (
111
+ const entry of glob . scan ( {
112
+ cwd : dir ,
113
+ onlyFiles : true ,
114
+ } )
115
+ ) {
116
+ yield resolve ( dir , entry ) ;
130
117
}
131
118
}
132
119
133
- const handleReleaseError = ( error : unknown , repo : Repo ) => {
120
+ const handleReleaseError = ( error : unknown , { owner , repo } : Repo ) => {
134
121
// an error occured while attempting to get the latest release
135
122
const parsed = httpErrorSchema . safeParse ( error ) ;
136
123
if ( parsed . success ) {
137
124
const message = parsed . data . status === 404
138
125
? "No releases were found"
139
126
: "Could not retrieve releases" ;
140
127
141
- console . error (
142
- `${ message } for repo: /${ repo . owner } /${ repo . repo } ` ,
143
- `${ parsed . data . status } ${ parsed . data . message } ` ,
144
- ) ;
128
+ return [
129
+ `${ message } for repo: /${ owner } /${ repo } ` ,
130
+ parsed . data . status ,
131
+ parsed . data . message ,
132
+ ] . join ( " " ) ;
145
133
} else {
146
- console . error (
147
- `Could not retrieve releases for repo: /${ repo . owner } /${ repo . repo } ` ,
148
- error ,
149
- ) ;
134
+ return [
135
+ `Could not retrieve releases for repo: /${ owner } /${ repo } ` ,
136
+ inspect ( error , { colors : true } ) ,
137
+ ] . join ( EOL ) ;
150
138
}
151
139
} ;
152
140
153
- const handleAssetError = ( error : unknown , asset : Asset ) => {
141
+ const handleAssetError = ( error : unknown , { name } : Asset ) => {
154
142
// an error occured while attempting to get the latest release
155
143
const parsed = httpErrorSchema . safeParse ( error ) ;
156
144
if ( parsed . success ) {
157
145
const message = parsed . data . status === 404
158
- ? `Asset ${ asset . name } not found`
159
- : `Could not retrieve asset ${ asset . name } ` ;
146
+ ? `Asset ${ name } not found`
147
+ : `Could not retrieve asset ${ name } ` ;
160
148
161
149
console . error (
162
150
`${ message } for repo: /${ BEPINEX_REPO . owner } /${ BEPINEX_REPO . repo } ` ,
163
- `${ parsed . data . status } ${ parsed . data . message } ` ,
151
+ parsed . data . status ,
152
+ parsed . data . message ,
164
153
) ;
165
154
} else {
166
155
console . error (
167
- `Could not retrieve asset ${ asset . name } for repo: /${ BEPINEX_REPO . owner } /${ BEPINEX_REPO . repo } ` ,
156
+ `Could not retrieve asset ${ name } for repo: /${ BEPINEX_REPO . owner } /${ BEPINEX_REPO . repo } ` ,
168
157
error ,
169
158
) ;
170
159
}
@@ -234,7 +223,7 @@ const embedPayload = async (archive: JSZip) => {
234
223
for (
235
224
const path of ( ( await Array . fromAsync ( getFilePaths ( PAYLOAD_DIR ) ) ) . sort ( ) )
236
225
) {
237
- archive . file ( relative ( PAYLOAD_DIR , path ) , await Deno . readFile ( path ) ) ;
226
+ archive . file ( relative ( PAYLOAD_DIR , path ) , await readFile ( path ) ) ;
238
227
}
239
228
240
229
return archive ;
@@ -263,7 +252,7 @@ const writeArchiveToDisk = (path: string, archive: JSZip) => {
263
252
console . log ( `Writing archive to disk: ${ path } ` ) ;
264
253
return archive . generateInternalStream ( { type : "uint8array" } )
265
254
. accumulate ( )
266
- . then ( ( data ) => Deno . writeFile ( resolve ( path ) , data ) ) ;
255
+ . then ( ( data ) => writeFile ( resolve ( path ) , data ) ) ;
267
256
} ;
268
257
269
258
const getBepInExArchive = async (
@@ -327,32 +316,33 @@ const mergeArchives = async (...archives: JSZip[]) => {
327
316
} ;
328
317
329
318
if ( import . meta. main ) {
330
- if ( ! Deno . env . get ( "GITHUB_PERSONAL_ACCESS_TOKEN" ) && Deno . env . get ( "CI" ) ) {
331
- console . error ( "GitHub PAT not set." ) ;
332
- Deno . exit ( 1 ) ;
319
+ const { GITHUB_PERSONAL_ACCESS_TOKEN , CI , GITHUB_ACTOR } = env ;
320
+
321
+ if ( ! GITHUB_PERSONAL_ACCESS_TOKEN && CI ) {
322
+ throw ( "GitHub PAT not set." ) ;
333
323
}
334
324
335
325
const { pusher } = context . payload ;
336
326
const gitConfigName = ( getInput ( "git-config-email" ) ||
337
327
pusher ?. name satisfies string | undefined ) ??
338
- Deno . env . get ( " GITHUB_ACTOR" ) ??
328
+ GITHUB_ACTOR ??
339
329
"GitHub Workflow Update and Release" ;
340
330
const gitConfigEmail = ( getInput ( "git-config-email" ) ||
341
331
pusher ?. email satisfies string | undefined ) ??
342
332
`${
343
- Deno . env . get ( " GITHUB_ACTOR" ) ?? "github-workflow-update-and-release"
333
+ GITHUB_ACTOR ?? "github-workflow-update-and-release"
344
334
} @users.noreply.github.com`;
345
335
346
336
const octokit = new Octokit ( {
347
- auth : Deno . env . get ( " GITHUB_PERSONAL_ACCESS_TOKEN" ) ,
337
+ auth : GITHUB_PERSONAL_ACCESS_TOKEN ,
348
338
} ) ;
349
339
350
340
console . log ( "Getting latest release..." ) ;
351
341
let latestRelease : Release | undefined ;
352
342
try {
353
343
latestRelease = ( await octokit . rest . repos . getLatestRelease ( REPO ) ) . data ;
354
344
} catch ( error ) {
355
- handleReleaseError ( error , REPO ) ;
345
+ throw handleReleaseError ( error , REPO ) ;
356
346
}
357
347
358
348
const latestReleaseVersion = latestRelease
@@ -364,8 +354,7 @@ if (import.meta.main) {
364
354
latestBepInExRelease =
365
355
( await octokit . rest . repos . getLatestRelease ( BEPINEX_REPO ) ) . data ;
366
356
} catch ( error ) {
367
- handleReleaseError ( error , BEPINEX_REPO ) ;
368
- Deno . exit ( 1 ) ;
357
+ throw handleReleaseError ( error , BEPINEX_REPO ) ;
369
358
}
370
359
371
360
const latestPayloadReleases : Release [ ] = [ ] ;
@@ -398,11 +387,10 @@ if (import.meta.main) {
398
387
}
399
388
}
400
389
} catch ( error ) {
401
- handleReleaseError ( error , {
390
+ throw handleReleaseError ( error , {
402
391
owner : repo ?. owner ?? JSON . stringify ( repo ?. owner ) ,
403
392
repo : repo ?. name ?? JSON . stringify ( repo ?. name ) ,
404
393
} ) ;
405
- Deno . exit ( 1 ) ;
406
394
}
407
395
}
408
396
@@ -454,9 +442,10 @@ if (import.meta.main) {
454
442
inc ( metadata . payload , "patch" ) ??
455
443
payloadJson . version ;
456
444
457
- await Deno . writeTextFile (
445
+ await writeFile (
458
446
"payload.json" ,
459
447
JSON . stringify ( payloadJson , null , 2 ) ,
448
+ "utf8" ,
460
449
) ;
461
450
}
462
451
}
@@ -476,7 +465,7 @@ if (import.meta.main) {
476
465
) {
477
466
// both bepinex and our payload have not released an update since last check, so we should cancel
478
467
console . log ( "No updates since last check." ) ;
479
- Deno . exit ( ) ;
468
+ exit ( ) ;
480
469
}
481
470
482
471
// we have a new release, let's handle it
@@ -504,14 +493,11 @@ if (import.meta.main) {
504
493
for ( const failure of failed ) {
505
494
console . error ( `Failed to get archive` , failure . asset ) ;
506
495
}
507
- Deno . exit ( 1 ) ;
496
+ exit ( 1 ) ;
508
497
}
509
498
510
499
if ( failed . length === archives . length ) {
511
- console . error (
512
- `No valid assets were found in repo /${ BEPINEX_REPO . owner } /${ BEPINEX_REPO . repo } ` ,
513
- ) ;
514
- Deno . exit ( 1 ) ;
500
+ throw `No valid assets were found in repo /${ BEPINEX_REPO . owner } /${ BEPINEX_REPO . repo } ` ;
515
501
}
516
502
517
503
const merged = await mergeArchives (
@@ -572,12 +558,10 @@ if (import.meta.main) {
572
558
merged ,
573
559
) ;
574
560
575
- if ( ! Deno . env . get ( "CI" ) ) {
576
- Deno . exit ( ) ;
577
- }
561
+ if ( ! CI ) exit ( ) ;
578
562
579
563
// at this point all assets have been successfully downloaded and saved to disk with our payload embedded
580
- await Deno . writeTextFile ( METADATA_FILE , JSON . stringify ( metadata ) ) ;
564
+ await writeFile ( METADATA_FILE , JSON . stringify ( metadata ) , "utf8" ) ;
581
565
582
566
const git = simpleGit ( ) ;
583
567
const status = await git . status ( ) ;
@@ -587,14 +571,15 @@ if (import.meta.main) {
587
571
) ;
588
572
589
573
if ( ! metadataPath ) {
590
- console . error ( "Metadata unchanged!" ) ;
591
- Deno . exit ( 1 ) ;
574
+ throw "Metadata unchanged!" ;
592
575
}
593
576
577
+ const { GITHUB_WORKSPACE } = env ;
578
+
594
579
await Promise . all ( [
595
580
git . addConfig (
596
581
"safe.directory" ,
597
- Deno . env . get ( " GITHUB_WORKSPACE" ) || "" ,
582
+ GITHUB_WORKSPACE || "" ,
598
583
false ,
599
584
"global" ,
600
585
) ,
665
650
...REPO ,
666
651
release_id : release . data . id ,
667
652
name : basename ( asset ) ,
668
- data : ( await Deno . readFile ( asset ) ) ,
653
+ data : await readFile ( asset ) ,
669
654
} ,
670
655
) ;
671
656
}
672
657
} catch ( error ) {
673
- console . error ( "Failed to create release" , error ) ;
674
- Deno . exit ( 1 ) ;
658
+ throw [
659
+ "Failed to create release" ,
660
+ inspect ( error , { colors : true } ) ,
661
+ ]
662
+ . join ( EOL ) ;
675
663
}
676
664
}
0 commit comments