|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is'; |
| 4 | +import { injectFromHierarchy, injectable } from 'inversify'; |
| 5 | +import { BaseInstallService } from '../install-tool/base-install.service'; |
| 6 | +import { ToolVersionResolver } from '../install-tool/tool-version-resolver'; |
| 7 | + |
| 8 | +@injectable() |
| 9 | +@injectFromHierarchy() |
| 10 | +export class MiseInstallService extends BaseInstallService { |
| 11 | + readonly name = 'mise'; |
| 12 | + |
| 13 | + private get arch(): string { |
| 14 | + switch (this.envSvc.arch) { |
| 15 | + case 'arm64': |
| 16 | + return 'arm64'; |
| 17 | + case 'amd64': |
| 18 | + return 'x64'; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + override async install(version: string): Promise<void> { |
| 23 | + /** |
| 24 | + * @example |
| 25 | + * @see {@href https://github.com/jdx/mise/releases/tag/v2026.2.13} |
| 26 | + */ |
| 27 | + const baseUrl = `https://github.com/jdx/mise/releases/download/v${version}/`; |
| 28 | + |
| 29 | + const filename = `mise-v${version}-linux-${this.arch}.tar.xz`; |
| 30 | + |
| 31 | + const checksumFile = await this.http.download({ |
| 32 | + url: `${baseUrl}SHASUMS256.txt`, |
| 33 | + }); |
| 34 | + const expectedChecksum = (await fs.readFile(checksumFile, 'utf-8')) |
| 35 | + .split('\n') |
| 36 | + .find((l) => l.endsWith(` ./${filename}`)) |
| 37 | + ?.split(/\s+/)[0]; |
| 38 | + |
| 39 | + if (!expectedChecksum) { |
| 40 | + throw new Error( |
| 41 | + `Cannot find checksum for '${filename}' in SHASUMS256.txt`, |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + const file = await this.http.download({ |
| 46 | + url: `${baseUrl}${filename}`, |
| 47 | + checksumType: 'sha256', |
| 48 | + expectedChecksum, |
| 49 | + }); |
| 50 | + |
| 51 | + await this.pathSvc.ensureToolPath(this.name); |
| 52 | + |
| 53 | + const path = await this.pathSvc.createVersionedToolPath(this.name, version); |
| 54 | + await this.compress.extract({ |
| 55 | + file, |
| 56 | + cwd: path, |
| 57 | + strip: 1, |
| 58 | + files: ['mise/bin/mise'], |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + override async link(version: string): Promise<void> { |
| 63 | + const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin'); |
| 64 | + await this.shellwrapper({ srcDir: src }); |
| 65 | + } |
| 66 | + |
| 67 | + override async test(_version: string): Promise<void> { |
| 68 | + await this._spawn(this.name, ['version']); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +@injectable() |
| 73 | +@injectFromHierarchy() |
| 74 | +export class MiseVersionResolver extends ToolVersionResolver { |
| 75 | + readonly tool = 'mise'; |
| 76 | + |
| 77 | + async resolve(version: string | undefined): Promise<string | undefined> { |
| 78 | + if (!isNonEmptyStringAndNotWhitespace(version) || version === 'latest') { |
| 79 | + return (await this.http.get('https://mise.jdx.dev/VERSION')).trim(); |
| 80 | + } |
| 81 | + return version; |
| 82 | + } |
| 83 | +} |
0 commit comments