@@ -4,8 +4,9 @@ import { tmpdir } from "node:os";
44import { dirname , resolve } from "node:path" ;
55import { fileURLToPath } from "node:url" ;
66import { unzipSync } from "fflate" ;
7+ import { gt , valid } from "semver" ;
78import { x as extractTar } from "tar" ;
8- import { assertSupportedRuntime , binaryDirectory , releaseArtifact } from "./platform.js" ;
9+ import { assertSupportedRuntime , binaryDirectory , binaryRoot , releaseArtifact } from "./platform.js" ;
910
1011const packageRoot = resolve ( dirname ( fileURLToPath ( import . meta. url ) ) , ".." ) ;
1112const metadata = JSON . parse ( await readFile ( resolve ( packageRoot , "package.json" ) , "utf8" ) ) ;
@@ -54,11 +55,13 @@ async function isRegularFile(path) {
5455
5556export async function ensureBinary ( ) {
5657 assertSupportedRuntime ( ) ;
57- const artifact = releaseArtifact ( metadata . version ) ;
58+ const root = binaryRoot ( ) ;
59+ const version = await resolveBinaryVersion ( metadata . version , root ) ;
60+ const artifact = releaseArtifact ( version ) ;
5861 const releaseBaseUrl =
59- process . env . TRACKSERIES_CLI_RELEASE_BASE_URL ?? `https://github.com/TrackSeries/cli/releases/download/cli-v${ metadata . version } ` ;
62+ process . env . TRACKSERIES_CLI_RELEASE_BASE_URL ?? `https://github.com/TrackSeries/cli/releases/download/cli-v${ version } ` ;
6063 const executableName = process . platform === "win32" ? "trackseries.exe" : "trackseries" ;
61- const installedExecutable = resolve ( binaryDirectory ( metadata . version ) , executableName ) ;
64+ const installedExecutable = resolve ( binaryDirectory ( version ) , executableName ) ;
6265
6366 if ( await isRegularFile ( installedExecutable ) ) return installedExecutable ;
6467
@@ -67,7 +70,7 @@ export async function ensureBinary() {
6770 const extractionPath = resolve ( temporaryDirectory , "extracted" ) ;
6871 const pendingExecutable = `${ installedExecutable } .${ process . pid } .tmp` ;
6972
70- console . error ( `Downloading TrackSeries CLI ${ metadata . version } for ${ process . platform } -${ process . arch } ...` ) ;
73+ console . error ( `Downloading TrackSeries CLI ${ version } for ${ process . platform } -${ process . arch } ...` ) ;
7174
7275 try {
7376 const [ archive , checksumFile ] = await Promise . all ( [
@@ -119,11 +122,30 @@ export async function ensureBinary() {
119122 } catch ( error ) {
120123 const reason = error instanceof Error ? error . message : String ( error ) ;
121124 throw new Error (
122- `Unable to install TrackSeries CLI ${ metadata . version } : ${ reason } Download ${ artifact } manually from ${ releaseBaseUrl } .` ,
125+ `Unable to install TrackSeries CLI ${ version } : ${ reason } Download ${ artifact } manually from ${ releaseBaseUrl } .` ,
123126 { cause : error }
124127 ) ;
125128 } finally {
126129 await rm ( pendingExecutable , { force : true } ) ;
127130 await rm ( temporaryDirectory , { force : true , recursive : true } ) ;
128131 }
129132}
133+
134+ export async function resolveBinaryVersion ( packageVersion , root ) {
135+ const manifestPath = resolve ( root , "current.json" ) ;
136+ let manifest ;
137+ try {
138+ manifest = JSON . parse ( await readFile ( manifestPath , "utf8" ) ) ;
139+ } catch ( error ) {
140+ if ( error && typeof error === "object" && "code" in error && error . code === "ENOENT" ) {
141+ return packageVersion ;
142+ }
143+ throw new Error ( `Unable to read the TrackSeries CLI update state at ${ manifestPath } .` , { cause : error } ) ;
144+ }
145+
146+ const activeVersion = typeof manifest ?. version === "string" ? valid ( manifest . version ) : null ;
147+ if ( ! activeVersion ) {
148+ throw new Error ( `The TrackSeries CLI update state at ${ manifestPath } contains an invalid version.` ) ;
149+ }
150+ return gt ( activeVersion , packageVersion ) ? activeVersion : packageVersion ;
151+ }
0 commit comments