UNPKG

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