-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.ts
More file actions
102 lines (90 loc) · 3.11 KB
/
Copy pathresolver.ts
File metadata and controls
102 lines (90 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { readFile, readdir, realpath } from 'node:fs/promises';
import fs from 'node:fs';
import path from 'node:path';
export interface PluginResolveContext {
builtinPluginsDir: string;
pluginsDir: string;
activeKitPluginsDir: string | null;
}
export interface KitResolveContext {
kitSources: Array<{ directory: string; source: string }>;
}
/**
* Resolve a plugin to its on-disk directory path.
*
* Looks up the plugin by package name `<name>` only in the explicit assembly
* directories supplied by the caller.
*
* @param name The npm-style package name of the plugin (e.g. `@scope/foo`).
* @param ctx Explicit plugin resolution directories.
* @returns The resolved absolute path to the plugin directory.
* @throws If the plugin cannot be found in the configured directories.
*/
export async function resolvePlugin(name: string, ctx: PluginResolveContext): Promise<string> {
for (const pluginsDir of [ctx.builtinPluginsDir, ctx.pluginsDir, ctx.activeKitPluginsDir].filter(Boolean) as string[]) {
const resolved = await findPluginInDir(name, pluginsDir);
if (resolved) return resolved;
}
throw new Error(`Plugin "${name}" not found`);
}
export async function resolveKit(nameOrPath: string, ctx: KitResolveContext): Promise<string> {
if (isPathLike(nameOrPath)) {
const explicitPath = await canonicalPath(nameOrPath);
if (explicitPath && fs.existsSync(path.join(explicitPath, 'package.json'))) {
const sourcePaths = await Promise.all(
(ctx.kitSources ?? []).map((source) => canonicalPath(source.directory)),
);
if (!sourcePaths.includes(explicitPath)) {
throw new Error(`Kit "${nameOrPath}" not found`);
}
return explicitPath;
}
}
for (const source of ctx.kitSources ?? []) {
try {
const directory = await realpath(source.directory);
const pkg = JSON.parse(await readFile(path.join(directory, 'package.json'), 'utf8'));
if (pkg.name === nameOrPath && pkg['ce-editor']?.kit) return directory;
} catch {
continue;
}
}
throw new Error(`Kit "${nameOrPath}" not found`);
}
async function canonicalPath(value: string): Promise<string | undefined> {
try {
return await realpath(path.resolve(value));
} catch {
return undefined;
}
}
function isPathLike(value: string): boolean {
return path.isAbsolute(value) || value.startsWith('.') || value.includes('/') || value.includes('\\');
}
async function findPluginInDir(name: string, pluginsDir: string): Promise<string | undefined> {
let entries: string[] = [];
try {
entries = await readdir(pluginsDir);
} catch (err: unknown) {
if (
err instanceof Error &&
'code' in err &&
(err as NodeJS.ErrnoException).code !== 'ENOENT'
) {
throw err;
}
return undefined;
}
for (const entry of entries) {
const pkgPath = path.join(pluginsDir, entry, 'package.json');
try {
const pkg = JSON.parse(await readFile(pkgPath, 'utf-8'));
if (pkg.name === name && pkg['ce-editor']) {
return fs.promises.realpath(path.join(pluginsDir, entry));
}
} catch {
continue;
}
}
return undefined;
}