| 117 | * @throws KeyStoreLoadError When the path is invalid or cannot be accessed. |
| 118 | */ |
| 119 | export function loadKeystores(path: string): KeyStore[] { |
| 120 | try { |
| 121 | const stats = statSync(path); |
| 122 | |
| 123 | if (stats.isFile()) { |
| 124 | return [loadKeystoreFile(path)]; |
| 125 | } else if (stats.isDirectory()) { |
| 126 | return loadKeystoreDirectory(path); |
| 127 | } else { |
| 128 | throw new KeyStoreLoadError('Path is neither a file nor directory', path); |
| 129 | } |
| 130 | } catch (error) { |
| 131 | if (error instanceof KeyStoreLoadError) { |
| 132 | throw error; |
| 133 | } |
| 134 | |
| 135 | const err = error as NodeJS.ErrnoException; |
| 136 | if (err?.code === 'ENOENT') { |
| 137 | throw new KeyStoreLoadError('File or directory not found', path, error as Error); |
| 138 | } |
| 139 | |
| 140 | throw new KeyStoreLoadError(`Failed to access path: ${err?.code ?? 'UNKNOWN'}`, path, error as Error); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Loads keystore(s) from multiple paths (comma-separated string or array). |