(pluginRoot: string)
| 40 | } |
| 41 | |
| 42 | export async function parseManifest(pluginRoot: string): Promise<ParsedManifestResult> { |
| 43 | const rootJsonPath = path.join(pluginRoot, KIMI_PLUGIN_ROOT_PATH); |
| 44 | const dirJsonPath = path.join(pluginRoot, KIMI_PLUGIN_DIR_PATH); |
| 45 | const rootJsonExists = await isFile(rootJsonPath); |
| 46 | const dirJsonExists = await isFile(dirJsonPath); |
| 47 | |
| 48 | if (!rootJsonExists && !dirJsonExists) { |
| 49 | return { |
| 50 | diagnostics: [ |
| 51 | { |
| 52 | severity: 'error', |
| 53 | message: `No manifest at ${KIMI_PLUGIN_ROOT_PATH} or ${KIMI_PLUGIN_DIR_PATH}`, |
| 54 | }, |
| 55 | ], |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | const manifestPath = rootJsonExists ? rootJsonPath : dirJsonPath; |
| 60 | const manifestKind: PluginManifestKind = rootJsonExists ? 'kimi-plugin-root' : 'kimi-plugin-dir'; |
| 61 | const shadowedManifestPath = rootJsonExists && dirJsonExists ? dirJsonPath : undefined; |
| 62 | |
| 63 | let raw: unknown; |
| 64 | try { |
| 65 | raw = JSON.parse(await readFile(manifestPath, 'utf8')); |
| 66 | } catch (error) { |
| 67 | return { |
| 68 | manifestKind, |
| 69 | manifestPath, |
| 70 | shadowedManifestPath, |
| 71 | diagnostics: [ |
| 72 | { |
| 73 | severity: 'error', |
| 74 | message: `Failed to parse ${path.relative(pluginRoot, manifestPath)}: ${(error as Error).message}`, |
| 75 | }, |
| 76 | ], |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | if (!isObject(raw)) { |
| 81 | return { |
| 82 | manifestKind, |
| 83 | manifestPath, |
| 84 | shadowedManifestPath, |
| 85 | diagnostics: [{ severity: 'error', message: 'manifest must be a JSON object' }], |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | const diagnostics: PluginDiagnostic[] = []; |
| 90 | |
| 91 | const name = typeof raw['name'] === 'string' ? raw['name'].trim() : ''; |
| 92 | if (name.length === 0) { |
| 93 | diagnostics.push({ severity: 'error', message: '"name" is required' }); |
| 94 | return { manifestKind, manifestPath, shadowedManifestPath, diagnostics }; |
| 95 | } |
| 96 | if (!PLUGIN_NAME_REGEX.test(name)) { |
| 97 | diagnostics.push({ |
| 98 | severity: 'error', |
| 99 | message: `"name" must match ${PLUGIN_NAME_REGEX} (got "${name}")`, |
no test coverage detected