( manifestPath: string, pluginName: string, source: string, )
| 1147 | * @throws Error if manifest exists but is invalid (corrupt JSON or schema validation failure) |
| 1148 | */ |
| 1149 | export async function loadPluginManifest( |
| 1150 | manifestPath: string, |
| 1151 | pluginName: string, |
| 1152 | source: string, |
| 1153 | ): Promise<PluginManifest> { |
| 1154 | // Check if manifest file exists |
| 1155 | // If not, create a minimal manifest to allow plugin to function |
| 1156 | if (!(await pathExists(manifestPath))) { |
| 1157 | // Return default manifest with provided name and source |
| 1158 | return { |
| 1159 | name: pluginName, |
| 1160 | description: `Plugin from ${source}`, |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | try { |
| 1165 | // Read and parse the manifest JSON file |
| 1166 | const content = await readFile(manifestPath, { encoding: 'utf-8' }) |
| 1167 | const parsedJson = jsonParse(content) |
| 1168 | |
| 1169 | // Validate against the PluginManifest schema |
| 1170 | const result = PluginManifestSchema().safeParse(parsedJson) |
| 1171 | |
| 1172 | if (result.success) { |
| 1173 | // Valid manifest - return the validated data |
| 1174 | return result.data |
| 1175 | } |
| 1176 | |
| 1177 | // Schema validation failed but JSON was valid |
| 1178 | const errors = result.error.issues |
| 1179 | .map(err => |
| 1180 | err.path.length > 0 |
| 1181 | ? `${err.path.join('.')}: ${err.message}` |
| 1182 | : err.message, |
| 1183 | ) |
| 1184 | .join(', ') |
| 1185 | |
| 1186 | logForDebugging( |
| 1187 | `Plugin ${pluginName} has an invalid manifest file at ${manifestPath}. Validation errors: ${errors}`, |
| 1188 | { level: 'error' }, |
| 1189 | ) |
| 1190 | |
| 1191 | throw new Error( |
| 1192 | `Plugin ${pluginName} has an invalid manifest file at ${manifestPath}.\n\nValidation errors: ${errors}`, |
| 1193 | ) |
| 1194 | } catch (error) { |
| 1195 | // Check if this is the error we just threw (validation error) |
| 1196 | if ( |
| 1197 | error instanceof Error && |
| 1198 | error.message.includes('invalid manifest file') |
| 1199 | ) { |
| 1200 | throw error |
| 1201 | } |
| 1202 | |
| 1203 | // JSON parsing failed or file read error |
| 1204 | const errorMsg = errorMessage(error) |
| 1205 | |
| 1206 | logForDebugging( |
no test coverage detected