Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cli/command/install-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import { Command, Option } from 'clipanion';
import prettyMilliseconds from 'pretty-ms';
import { installTool, resolveVersion } from '../install-tool/index.ts';
import { DeprecatedTools, ResolverMap } from '../tools/index.ts';
import { DeprecatedTools, ResolverMap, getToolType } from '../tools/index.ts';
import type { InstallToolType } from '../utils';
import { MissingVersion } from '../utils/codes.ts';
import { logger } from '../utils/index.ts';
Expand Down Expand Up @@ -46,14 +46,14 @@ export class InstallToolCommand extends Command {

let version = this.version?.replace(/^v/, ''); // trim optional 'v' prefix

let type = DeprecatedTools[this.name];
let type = getToolType(DeprecatedTools, this.name);

if (type) {
logger.warn(
`The 'install-tool ${this.name}' command is deprecated. Please use the 'install-${type} ${this.name}'.`,
);
} else {
type = ResolverMap[this.name] ?? this.type;
type = getToolType(ResolverMap, this.name) ?? this.type;
}

if (!isNonEmptyStringAndNotWhitespace(version)) {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/command/uninstall-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import { Command, Option } from 'clipanion';
import prettyMilliseconds from 'pretty-ms';
import { uninstallTool } from '../install-tool/index.ts';
import { ResolverMap } from '../tools/index.ts';
import { ResolverMap, getToolType } from '../tools/index.ts';
import type { InstallToolType } from '../utils';
import { MissingVersion } from '../utils/codes.ts';
import { logger } from '../utils/index.ts';
Expand Down Expand Up @@ -55,7 +55,7 @@ export class UninstallToolCommand extends Command {

let version = this.version;

const type = ResolverMap[tool] ?? this.type;
const type = getToolType(ResolverMap, tool) ?? this.type;

if (!isNonEmptyStringAndNotWhitespace(version) && !all) {
logger.error(`No version found for ${tool}`);
Expand Down
4 changes: 2 additions & 2 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { CabalInstallService } from '../tools/haskell/cabal.ts';
import { GhcInstallService } from '../tools/haskell/ghc.ts';
import { HelmInstallService } from '../tools/helm.ts';
import { HelmfileInstallService } from '../tools/helmfile.ts';
import { ResolverMap } from '../tools/index.ts';
import { ResolverMap, getToolType } from '../tools/index.ts';
import {
AndroidSdkCmdlineToolsInstallService,
AndroidSdkCmdlineToolsVersionResolver,
Expand Down Expand Up @@ -332,7 +332,7 @@ export async function installTool(
// some pip packages may not have a `--version` flag
await super.test(version);
} catch (err) {
if (ResolverMap[tool] === 'pip') {
if (getToolType(ResolverMap, tool) === 'pip') {
// those tools are known and should work
throw err;
}
Expand Down
24 changes: 24 additions & 0 deletions src/cli/tools/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, test } from 'vitest';
import { getToolType } from './index.ts';

describe('cli/tools/index', () => {
describe('getToolType', () => {
const map = { poetry: 'pip', pnpm: 'npm' } as const;

test('returns the type of a mapped tool', () => {
expect(getToolType(map, 'poetry')).toBe('pip');
expect(getToolType(map, 'pnpm')).toBe('npm');
});

test('returns undefined for an unknown tool', () => {
expect(getToolType(map, 'unknown')).toBeUndefined();
});

test.each(['constructor', 'toString', '__proto__'])(
'returns undefined for the prototype key %s',
(name) => {
expect(getToolType(map, name)).toBeUndefined();
},
);
});
});
18 changes: 16 additions & 2 deletions src/cli/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const NoInitTools = [
* Tools in this map are implicit mapped from `install-tool` to `install-<type>`.
* So no need for an extra install service.
*/
export const ResolverMap: Record<string, InstallToolType | undefined> = {
export const ResolverMap: Record<string, InstallToolType> = {
bundler: 'gem',
checkov: 'pip',
copier: 'pip',
Expand All @@ -87,7 +87,21 @@ export const ResolverMap: Record<string, InstallToolType | undefined> = {
* This tools are deprecated and should not be used anymore via `install-tool`.
* They are implicit mapped from `install-tool` to `install-<type>`.
*/
export const DeprecatedTools: Record<string, InstallToolType | undefined> = {
export const DeprecatedTools: Record<string, InstallToolType> = {
bower: 'npm',
lerna: 'npm',
};

/**
* Looks up the install type of a tool in the given map.
* Only own keys resolve, so names like `constructor` never match.
* @param map - tool to install type map
* @param name - tool name
* @returns the mapped install type or `undefined`
*/
export function getToolType(
map: Record<string, InstallToolType>,
name: string,
): InstallToolType | undefined {
return Object.hasOwn(map, name) ? map[name] : undefined;
}
Loading