UNPKG

1.38 kBJavaScriptView Raw
1import { constants } from 'node:fs'
2import fs from 'node:fs/promises'
3import path from 'node:path'
4
5import debug from 'debug'
6
7import { invalidOption } from './messages.js'
8import { InvalidOptionsError } from './symbols.js'
9
10const debugLog = debug('lint-staged:validateOptions')
11
12/**
13 * Validate lint-staged options, either from the Node.js API or the command line flags.
14 * @param {*} options
15 * @param {boolean|string} [options.cwd] - Current working directory
16 * @param {boolean|string} [options.shell] - Skip parsing of tasks for better shell support
17 *
18 * @throws {InvalidOptionsError}
19 */
20export const validateOptions = async (options = {}, logger) => {
21 debugLog('Validating options...')
22
23 /** Ensure the passed cwd option exists; it might also be relative */
24 if (typeof options.cwd === 'string') {
25 try {
26 const resolved = path.resolve(options.cwd)
27 await fs.access(resolved, constants.F_OK)
28 } catch (error) {
29 logger.error(invalidOption('cwd', options.cwd, error.message))
30 throw InvalidOptionsError
31 }
32 }
33
34 /** Ensure the passed shell option is executable */
35 if (typeof options.shell === 'string') {
36 try {
37 await fs.access(options.shell, constants.X_OK)
38 } catch (error) {
39 logger.error(invalidOption('shell', options.shell, error.message))
40 throw InvalidOptionsError
41 }
42 }
43
44 debugLog('Validated options!')
45}