Skip to content

Commit

Permalink
🚸 Allow user to add custom tools
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Jan 19, 2025
1 parent e91e393 commit f5c0938
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ export {
} from './pm';
export { pm, runtime, git };

type ToolConstructor<T extends Tool> = new (name: string) => T;
type UsedAs<K extends string, T extends Tool = Tool> = {
as: <P extends Panam>(
alias: K
) => P & { tools: P['tools'] & { [Key in K]: T } };
};

export class Panam extends Runtime {
readonly pm: PackageManager;
readonly git: Git = git;
readonly tools: Record<string, Tool> = {};

constructor(
runtime: RuntimeName | RuntimeInfo | Runtime,
Expand All @@ -55,6 +63,40 @@ export class Panam extends Runtime {
bind(this, Panam.prototype);
}

use<K extends string, T extends Tool>(tool: T): UsedAs<K, T>;
use<K extends string, T extends Tool>(
tool: ToolConstructor<T>,
name: K
): UsedAs<K, T>;
use(arg1: any, arg2?: any) {
if (arg2 !== undefined) {
if (typeof arg1 !== 'function') {
throw new Error('Tool must be a function');
}

arg1 = new arg1(arg2);
}

const name = arg1.name;

if (!name) {
throw new Error('Tool must have a name');
}

this.tools[name] = arg1;

return {
as: (alias: string) => {
if (this.tools[alias]) {
throw new Error(`Tool alias "${alias}" already exists.`);
}
this.tools[alias] = arg1;

return this;
}
};
}

async init(
packageManager: PackageManagerName,
options?: ProcessOptions
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class Tool {
bind(this, Tool.prototype);
}

get name(): string {
return this._name;
}

get realname(): string {
return which.sync(this._name);
}
Expand Down

0 comments on commit f5c0938

Please sign in to comment.