(
value: string,
options?: {
subject?: string;
cwd?: string;
expandPath?: (value: string, cwd?: string) => string;
},
)
| 14 | | { kind: 'inline'; text: string }; |
| 15 | |
| 16 | export function resolvePayloadInput( |
| 17 | value: string, |
| 18 | options?: { |
| 19 | subject?: string; |
| 20 | cwd?: string; |
| 21 | expandPath?: (value: string, cwd?: string) => string; |
| 22 | }, |
| 23 | ): ResolvedPayloadInput { |
| 24 | const subject = options?.subject ?? 'Payload'; |
| 25 | const trimmed = value.trim(); |
| 26 | if (!trimmed) { |
| 27 | throw new AppError('INVALID_ARGS', `${subject} cannot be empty`); |
| 28 | } |
| 29 | |
| 30 | const resolvedPath = options?.expandPath ? options.expandPath(trimmed, options.cwd) : trimmed; |
| 31 | |
| 32 | try { |
| 33 | const stat = fs.statSync(resolvedPath); |
| 34 | if (!stat.isFile()) { |
| 35 | throw new AppError('INVALID_ARGS', `${subject} path is not a file: ${resolvedPath}`); |
| 36 | } |
| 37 | return { kind: 'file', path: resolvedPath }; |
| 38 | } catch (error) { |
| 39 | if (error instanceof AppError) throw error; |
| 40 | const code = (error as NodeJS.ErrnoException).code; |
| 41 | if (code === 'EACCES' || code === 'EPERM') { |
| 42 | throw new AppError('INVALID_ARGS', `${subject} file is not readable: ${resolvedPath}`); |
| 43 | } |
| 44 | if (code && code !== 'ENOENT') { |
| 45 | throw new AppError('COMMAND_FAILED', `Unable to read ${subject} file: ${resolvedPath}`, { |
| 46 | cause: String(error), |
| 47 | }); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (looksLikeInlineJson(trimmed)) { |
| 52 | return { kind: 'inline', text: trimmed }; |
| 53 | } |
| 54 | throw new AppError('INVALID_ARGS', `${subject} file not found: ${resolvedPath}`); |
| 55 | } |
no test coverage detected
searching dependent graphs…