Skip to content

Commit

Permalink
fix(cli): validate version (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Jul 14, 2023
1 parent 3fc9c29 commit 0f55bd9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/cli/install-tool/install-tool-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { chmod, chown, stat, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { injectable } from 'inversify';
import type { EnvService, PathService } from '../services';
import { fileRights, logger } from '../utils';
import { fileRights, isValid, logger } from '../utils';

export interface ShellWrapperConfig {
name: string;
name?: string;
srcDir: string;
exports?: string;
}
Expand All @@ -32,6 +32,10 @@ export abstract class InstallToolBaseService {

abstract link(version: string): Promise<void>;

needsPrepare(): boolean {
return true;
}

test(_version: string): Promise<void> {
return Promise.resolve();
}
Expand All @@ -40,16 +44,16 @@ export abstract class InstallToolBaseService {
return this.name;
}

validate(_version: string): boolean {
return true;
validate(version: string): boolean {
return isValid(version);
}

protected async shellwrapper({
name,
srcDir,
exports,
}: ShellWrapperConfig): Promise<void> {
const tgt = join(this.pathSvc.binDir, name);
const tgt = join(this.pathSvc.binDir, name ?? this.name);

let content = `#!/bin/bash
Expand All @@ -62,7 +66,7 @@ export abstract class InstallToolBaseService {
content += `export ${exports}\n`;
}

content += `${srcDir}/${name} "$@"\n`;
content += `${srcDir}/${name ?? this.name} "$@"\n`;

await writeFile(tgt, content, { encoding: 'utf8' });
await this.setOwner({ file: tgt });
Expand Down
2 changes: 1 addition & 1 deletion src/cli/install-tool/install-tool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class InstallToolService {
return;
}

if (!(await this.pathSvc.findToolPath(tool))) {
if (toolSvc.needsPrepare() && !(await this.pathSvc.findToolPath(tool))) {
logger.debug({ tool }, 'tool not prepared');
const res = await prepareTools([tool], dryRun);
if (res) {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/tools/dart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class InstallDartService extends InstallToolBaseService {
override async link(version: string): Promise<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');

await this.shellwrapper({ name: 'dart', srcDir: src });
await this.shellwrapper({ srcDir: src });
}

override async test(_version: string): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/tools/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class InstallDockerService extends InstallToolBaseService {
override async link(version: string): Promise<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');

await this.shellwrapper({ name: 'docker', srcDir: src });
await this.shellwrapper({ srcDir: src });
}

override async test(_version: string): Promise<void> {
Expand Down
4 changes: 4 additions & 0 deletions src/cli/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import semver from 'semver';
import { type StrictValidator, makeValidator } from 'typanion';

export function isValid(version: string): boolean {
return semver.valid(version) !== null;
}

export function validateSemver(): StrictValidator<string, string> {
return makeValidator<string, string>({
test: (value, state): value is string => {
Expand Down

0 comments on commit 0f55bd9

Please sign in to comment.