-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli/node): install latest node and npm tool versions (#1828)
- Loading branch information
Showing
15 changed files
with
235 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { injectable, multiInject } from 'inversify'; | ||
import { | ||
TOOL_VERSION_RESOLVER, | ||
type ToolVersionResolver, | ||
} from './tool-version-resolver'; | ||
|
||
@injectable() | ||
export class ToolVersionResolverService { | ||
constructor( | ||
@multiInject(TOOL_VERSION_RESOLVER) private resolver: ToolVersionResolver[], | ||
) {} | ||
|
||
async resolve( | ||
tool: string, | ||
version: string | undefined, | ||
): Promise<string | undefined> { | ||
const resolver = this.resolver.find((r) => r.tool === tool); | ||
return (await resolver?.resolve(version)) ?? version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { injectable } from 'inversify'; | ||
import type { EnvService, HttpService } from '../services'; | ||
|
||
export const TOOL_VERSION_RESOLVER = Symbol('TOOL_VERSION_RESOLVER'); | ||
|
||
@injectable() | ||
export abstract class ToolVersionResolver { | ||
abstract readonly tool: string; | ||
|
||
constructor( | ||
protected readonly http: HttpService, | ||
protected readonly env: EnvService, | ||
) {} | ||
|
||
abstract resolve(version: string | undefined): Promise<string | undefined>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import is from '@sindresorhus/is'; | ||
import { inject, injectable } from 'inversify'; | ||
import { ToolVersionResolver } from '../../install-tool/tool-version-resolver'; | ||
import { EnvService, HttpService } from '../../services'; | ||
import type { NodeVersionMeta, NpmPackageMeta } from './types'; | ||
|
||
@injectable() | ||
export class NodeVersionResolver extends ToolVersionResolver { | ||
readonly tool = 'node'; | ||
|
||
constructor( | ||
@inject(HttpService) http: HttpService, | ||
@inject(EnvService) env: EnvService, | ||
) { | ||
super(http, env); | ||
} | ||
|
||
async resolve(version: string | undefined): Promise<string | undefined> { | ||
if (!is.nonEmptyStringAndNotWhitespace(version) || version === 'latest') { | ||
const url = this.env.replaceUrl('https://nodejs.org/dist/index.json'); | ||
const meta = await this.http.getJson<NodeVersionMeta[]>(url, { | ||
headers: { | ||
accept: | ||
'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*', | ||
}, | ||
}); | ||
// we know that the latest version is the first entry, so search for first lts | ||
return meta.find((v) => v.lts)?.version.replace(/^v/, ''); | ||
} | ||
return version; | ||
} | ||
} | ||
|
||
@injectable() | ||
export abstract class NpmVersionResolver extends ToolVersionResolver { | ||
constructor( | ||
@inject(HttpService) http: HttpService, | ||
@inject(EnvService) env: EnvService, | ||
) { | ||
super(http, env); | ||
} | ||
|
||
async resolve(version: string | undefined): Promise<string | undefined> { | ||
if (!is.nonEmptyStringAndNotWhitespace(version) || version === 'latest') { | ||
const meta = await this.http.getJson<NpmPackageMeta>( | ||
`https://registry.npmjs.org/${this.tool}`, | ||
{ | ||
headers: { | ||
accept: | ||
'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*', | ||
}, | ||
}, | ||
); | ||
return meta['dist-tags'].latest; | ||
} | ||
return version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface NodeVersionMeta { | ||
version: string; | ||
lts?: boolean; | ||
} | ||
|
||
export interface NpmPackageMeta { | ||
'dist-tags': Record<string, string>; | ||
name: string; | ||
} |
Oops, something went wrong.