|
2 | 2 |
|
3 | 3 | import path from 'node:path' |
4 | 4 |
|
| 5 | +import { betterAjvErrors } from '@apideck/better-ajv-errors' |
| 6 | +import { readSocketConfig, SocketValidationError } from '@socketsecurity/config' |
5 | 7 | import meow from 'meow' |
6 | 8 | import ora from 'ora' |
| 9 | +import { ErrorWithCause } from 'pony-cause' |
7 | 10 |
|
8 | 11 | import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' |
9 | 12 | import { ChalkOrMarkdown, logSymbols } from '../../utils/chalk-markdown.js' |
| 13 | +import { InputError } from '../../utils/errors.js' |
10 | 14 | import { printFlagList } from '../../utils/formatting.js' |
11 | 15 | import { createDebugLogger } from '../../utils/misc.js' |
12 | 16 | import { getPackageFiles } from '../../utils/path-resolve.js' |
13 | 17 | import { setupSdk } from '../../utils/sdk.js' |
14 | | -import { readSocketConfig } from '../../utils/socket-config.js' |
15 | 18 | import { fetchReportData, formatReportDataOutput } from './view.js' |
16 | 19 |
|
17 | 20 | /** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ |
@@ -166,6 +169,25 @@ async function setupCommand (name, description, argv, importMeta) { |
166 | 169 | const absoluteConfigPath = path.join(cwd, 'socket.yml') |
167 | 170 |
|
168 | 171 | const config = await readSocketConfig(absoluteConfigPath) |
| 172 | + .catch(/** @param {unknown} cause */ cause => { |
| 173 | + if (cause && typeof cause === 'object' && cause instanceof SocketValidationError) { |
| 174 | + // Inspired by workbox-build: https://github.com/GoogleChrome/workbox/blob/95f97a207fd51efb3f8a653f6e3e58224183a778/packages/workbox-build/src/lib/validate-options.ts#L68-L71 |
| 175 | + const betterErrors = betterAjvErrors({ |
| 176 | + basePath: 'config', |
| 177 | + data: cause.data, |
| 178 | + errors: cause.validationErrors, |
| 179 | + // @ts-ignore |
| 180 | + schema: cause.schema, |
| 181 | + }) |
| 182 | + throw new InputError( |
| 183 | + 'The socket.yml config is not valid', |
| 184 | + betterErrors.map((err) => `[${err.path}] ${err.message}.${err.suggestion ? err.suggestion : ''}`).join('\n') |
| 185 | + ) |
| 186 | + } else { |
| 187 | + throw new ErrorWithCause('Failed to read socket.yml config', { cause }) |
| 188 | + } |
| 189 | + }) |
| 190 | + |
169 | 191 | const packagePaths = await getPackageFiles(cwd, cli.input, config, debugLog) |
170 | 192 |
|
171 | 193 | return { |
|
0 commit comments