Skip to content

Commit

Permalink
feat: get getPlugins by tree
Browse files Browse the repository at this point in the history
  • Loading branch information
hal-wang committed Apr 7, 2024
1 parent 5c1687b commit 1dda643
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/services/deps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ export class DepsService {
return this.getDeps(packagePath, /^@halsp\//, paths, true);
}

public async getPlugins<T>(
private async getDirPlugins<T>(
name: string | symbol,
cwd = process.cwd(),
dir: string,
): Promise<InterfaceItem<T>[]> {
const pkgPath = this.getPackagePath(cwd, [cwd]);
const pkgPath = this.getPackagePath(dir, [dir]);
if (!pkgPath) return [];

const deps = this.getDeps(pkgPath, () => true, [cwd]);
const deps = this.getDeps(pkgPath, () => true, [dir]);
const scripts: InterfaceItem<T>[] = [];
for (const dep of deps) {
let module: any;
try {
const depPath = _require.resolve(dep.key, {
paths: [cwd],
paths: [dir],
});
try {
module = await import(pathToFileURL(depPath).toString());
Expand All @@ -125,6 +125,23 @@ export class DepsService {
return scripts;
}

public async getPlugins<T>(
name: string | symbol,
cwd = process.cwd(),
): Promise<InterfaceItem<T>[]> {
const result: InterfaceItem<T>[] = [];
let dir = cwd;
while (dir != path.dirname(dir) && dir.startsWith(path.dirname(dir))) {
for (const newItem of await this.getDirPlugins<T>(name, cwd)) {
if (!result.some((r) => r.package == newItem.package)) {
result.push(newItem);
}
}
dir = path.dirname(dir);
}
return result;
}

public async getInterfaces<T>(name: string | symbol, cwd = process.cwd()) {
const plugins = await this.getPlugins<T>(name, cwd);
return plugins.map((p) => p.interface);
Expand Down

0 comments on commit 1dda643

Please sign in to comment.