1- import semver from 'semver'
21import satisfies from 'semver/functions/satisfies.js'
2+ import valid from 'semver/functions/valid.js'
33
44export type ExePlatform = 'win' | 'darwin' | 'linux'
55export type ExeArch = 'x64' | 'arm64'
@@ -8,7 +8,12 @@ export interface ExeTarget {
88 platform : ExePlatform
99 arch : ExeArch
1010 /**
11- * Node.js version to use for the executable. Should be a valid Node.js version string (e.g., "25.7.0").
11+ * Node.js version to use for the executable.
12+ *
13+ * Accepts a valid semver string (e.g., `"25.7.0"`), or the special values
14+ * `"latest"` / `"latest-lts"` which resolve the version automatically from
15+ * {@link https://nodejs.org/dist/index.json}.
16+ *
1217 * The minimum required version is 25.7.0, which is when SEA support was added to Node.js.
1318 */
1419 nodeVersion : string
@@ -53,11 +58,40 @@ export function getBinaryPathInArchive(target: ExeTarget): string {
5358 return `${ dirName } /bin/node`
5459}
5560
56- export function normalizeNodeVersion ( target : ExeTarget ) : string {
57- const version = semver . valid ( target . nodeVersion )
61+ interface NodeRelease {
62+ version : string
63+ lts : string | false
64+ }
65+
66+ const NODE_DIST_INDEX_URL = 'https://nodejs.org/dist/index.json'
67+
68+ export async function resolveNodeVersion ( nodeVersion : string ) : Promise < string > {
69+ if ( nodeVersion === 'latest' || nodeVersion === 'latest-lts' ) {
70+ const response = await fetch ( NODE_DIST_INDEX_URL )
71+ if ( ! response . ok ) {
72+ throw new Error (
73+ `Failed to fetch Node.js releases: HTTP ${ response . status } from ${ NODE_DIST_INDEX_URL } ` ,
74+ )
75+ }
76+
77+ const releases = ( await response . json ( ) ) as NodeRelease [ ]
78+
79+ const release =
80+ nodeVersion === 'latest'
81+ ? releases [ 0 ]
82+ : releases . find ( ( r ) => r . lts !== false )
83+
84+ if ( ! release ) {
85+ throw new Error ( `No matching Node.js release found for "${ nodeVersion } ".` )
86+ }
87+
88+ nodeVersion = release . version . replace ( / ^ v / , '' )
89+ }
90+
91+ const version = valid ( nodeVersion )
5892 if ( ! version ) {
5993 throw new Error (
60- `Invalid Node.js version: ${ target . nodeVersion } . ` +
94+ `Invalid Node.js version: ${ nodeVersion } . ` +
6195 `Please provide a valid version string (e.g., "25.7.0").` ,
6296 )
6397 }
0 commit comments