Skip to content

Commit

Permalink
feat: detect installed agents
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Aug 4, 2022
1 parent 41586b2 commit 4c480ac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/agent.ts
@@ -0,0 +1,29 @@
import { tryGetCommandVersion } from './version.js';

export const agents = ['bun', 'pnpm', 'yarn', 'npm'] as const;

export type AgentName = typeof agents[number];

export interface Agent {
name: AgentName;
version: string | undefined;
}

export type AgentMap = Map<AgentName, Agent>;

/**
* Detects all (known) agents installed on the system.
*/
export async function detectInstalledAgents(): Promise<AgentMap> {
const agentsFound: AgentMap = new Map();

for (const name of agents) {
const version = await tryGetCommandVersion(name);

if (version) {
agentsFound.set(name, { name, version });
}
}

return agentsFound;
}
1 change: 1 addition & 0 deletions src/index.ts
@@ -1,2 +1,3 @@
export * from './process.js';
export * from './version.js';
export * from './agent.js';
9 changes: 9 additions & 0 deletions test/agent.test.ts
@@ -0,0 +1,9 @@
import { expect, it } from 'vitest';

import * as api from '../src/index.js';

it('should find almost one agent (pnpm)', async () => {
const agents = await api.detectInstalledAgents();

expect(agents.has('pnpm')).toBe(true);
});

0 comments on commit 4c480ac

Please sign in to comment.