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