lint-staged 12.1.1 → 12.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,10 +44,13 @@ cmdline
44
44
  )
45
45
  .parse(process.argv)
46
46
 
47
- const debugLog = debug('lint-staged:bin')
48
- if (cmdline.debug) {
47
+ const cmdlineOptions = cmdline.opts()
48
+
49
+ if (cmdlineOptions.debug) {
49
50
  debug.enable('lint-staged*')
50
51
  }
52
+
53
+ const debugLog = debug('lint-staged:bin')
51
54
  debugLog('Running `lint-staged@%s`', version)
52
55
 
53
56
  /**
@@ -68,8 +71,6 @@ const getMaxArgLength = () => {
68
71
  }
69
72
  }
70
73
 
71
- const cmdlineOptions = cmdline.opts()
72
-
73
74
  const options = {
74
75
  allowEmpty: !!cmdlineOptions.allowEmpty,
75
76
  concurrent: JSON.parse(cmdlineOptions.concurrent),
package/lib/index.js CHANGED
@@ -58,20 +58,14 @@ const lintStaged = async (
58
58
  ) => {
59
59
  await validateOptions({ shell }, logger)
60
60
 
61
- debugLog('Loading config using `lilconfig`')
61
+ const inputConfig = configObject || (await loadConfig({ configPath, cwd }, logger))
62
62
 
63
- const resolved = configObject
64
- ? { config: configObject, filepath: '(input)' }
65
- : await loadConfig(configPath)
66
-
67
- if (!resolved) {
63
+ if (!inputConfig) {
68
64
  logger.error(`${ConfigNotFoundError.message}.`)
69
65
  throw ConfigNotFoundError
70
66
  }
71
67
 
72
- debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
73
-
74
- const config = validateConfig(resolved.config, logger)
68
+ const config = validateConfig(inputConfig, logger)
75
69
 
76
70
  if (debug) {
77
71
  // Log using logger to be able to test through `consolemock`.
package/lib/loadConfig.js CHANGED
@@ -1,6 +1,13 @@
1
+ /** @typedef {import('./index').Logger} Logger */
2
+
3
+ import { pathToFileURL } from 'url'
4
+
5
+ import debug from 'debug'
1
6
  import { lilconfig } from 'lilconfig'
2
7
  import YAML from 'yaml'
3
8
 
9
+ const debugLog = debug('lint-staged:loadConfig')
10
+
4
11
  /**
5
12
  * The list of files `lint-staged` will read configuration
6
13
  * from, in the declared order.
@@ -20,16 +27,12 @@ const searchPlaces = [
20
27
  ]
21
28
 
22
29
  /** exported for tests */
23
- export const dynamicImport = (path) =>
24
- import(path)
25
- .then((module) => module.default)
26
- .catch((error) => {
27
- console.error(error)
28
- throw error
29
- })
30
+ export const dynamicImport = (path) => import(pathToFileURL(path)).then((module) => module.default)
30
31
 
31
32
  const jsonParse = (path, content) => JSON.parse(content)
32
33
 
34
+ const yamlParse = (path, content) => YAML.parse(content)
35
+
33
36
  /**
34
37
  * `lilconfig` doesn't support yaml files by default,
35
38
  * so we add custom loaders for those. Files without
@@ -41,9 +44,9 @@ const loaders = {
41
44
  '.json': jsonParse,
42
45
  '.mjs': dynamicImport,
43
46
  '.cjs': dynamicImport,
44
- '.yaml': YAML.parse,
45
- '.yml': YAML.parse,
46
- noExt: YAML.parse,
47
+ '.yaml': yamlParse,
48
+ '.yml': yamlParse,
49
+ noExt: yamlParse,
47
50
  }
48
51
 
49
52
  const resolveConfig = (configPath) => {
@@ -55,15 +58,34 @@ const resolveConfig = (configPath) => {
55
58
  }
56
59
 
57
60
  /**
58
- * @param {string} [configPath]
61
+ * @param {object} options
62
+ * @param {string} [options.configPath] - Explicit path to a config file
63
+ * @param {string} [options.cwd] - Current working directory
59
64
  */
60
- export const loadConfig = async (configPath) => {
61
- const explorer = lilconfig('lint-staged', { searchPlaces, loaders })
62
- const result = await (configPath ? explorer.load(resolveConfig(configPath)) : explorer.search())
63
- if (!result) return null
65
+ export const loadConfig = async ({ configPath, cwd }, logger) => {
66
+ try {
67
+ if (configPath) {
68
+ debugLog('Loading configuration from `%s`...', configPath)
69
+ } else {
70
+ debugLog('Searching for configuration from `%s`...', cwd)
71
+ }
64
72
 
65
- const { config, filepath } = result
73
+ const explorer = lilconfig('lint-staged', { searchPlaces, loaders })
66
74
 
67
- // config is a promise when using the `dynamicImport` loader
68
- return { config: await config, filepath }
75
+ const result = await (configPath
76
+ ? explorer.load(resolveConfig(configPath))
77
+ : explorer.search(cwd))
78
+ if (!result) return null
79
+
80
+ // config is a promise when using the `dynamicImport` loader
81
+ const config = await result.config
82
+
83
+ debugLog('Successfully loaded config from `%s`:\n%O', result.filepath, config)
84
+
85
+ return config
86
+ } catch (error) {
87
+ debugLog('Failed to load configuration!')
88
+ logger.error(error)
89
+ return null
90
+ }
69
91
  }
@@ -54,7 +54,7 @@ export const resolveGitRepo = async (cwd = process.cwd()) => {
54
54
 
55
55
  // read the path of the current directory relative to the top-level directory
56
56
  // don't read the toplevel directly, it will lead to an posix conform path on non posix systems (cygwin)
57
- const gitRel = normalize(await execGit(['rev-parse', '--show-prefix']))
57
+ const gitRel = normalize(await execGit(['rev-parse', '--show-prefix'], { cwd }))
58
58
  const gitDir = determineGitDir(normalize(cwd), gitRel)
59
59
  const gitConfigDir = normalize(await resolveGitConfigDir(gitDir))
60
60
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lint-staged",
3
- "version": "12.1.1",
3
+ "version": "12.1.5",
4
4
  "description": "Lint files staged by git",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/okonet/lint-staged",
@@ -32,34 +32,33 @@
32
32
  "cli-truncate": "^3.1.0",
33
33
  "colorette": "^2.0.16",
34
34
  "commander": "^8.3.0",
35
- "debug": "^4.3.2",
36
- "enquirer": "^2.3.6",
35
+ "debug": "^4.3.3",
37
36
  "execa": "^5.1.1",
38
37
  "lilconfig": "2.0.4",
39
- "listr2": "^3.13.3",
38
+ "listr2": "^3.13.5",
40
39
  "micromatch": "^4.0.4",
41
40
  "normalize-path": "^3.0.0",
42
- "object-inspect": "^1.11.0",
41
+ "object-inspect": "^1.11.1",
43
42
  "string-argv": "^0.3.1",
44
- "supports-color": "^9.0.2",
43
+ "supports-color": "^9.2.1",
45
44
  "yaml": "^1.10.2"
46
45
  },
47
46
  "devDependencies": {
48
- "@babel/core": "^7.16.0",
49
- "@babel/eslint-parser": "^7.16.3",
50
- "@babel/preset-env": "^7.16.0",
51
- "babel-jest": "^27.3.1",
47
+ "@babel/core": "^7.16.5",
48
+ "@babel/eslint-parser": "^7.16.5",
49
+ "@babel/preset-env": "^7.16.5",
50
+ "babel-jest": "^27.4.5",
52
51
  "consolemock": "^1.1.0",
53
- "eslint": "^8.2.0",
52
+ "eslint": "^8.4.1",
54
53
  "eslint-config-prettier": "^8.3.0",
55
54
  "eslint-plugin-import": "^2.25.3",
56
55
  "eslint-plugin-node": "^11.1.0",
57
56
  "eslint-plugin-prettier": "^4.0.0",
58
57
  "fs-extra": "^10.0.0",
59
58
  "husky": "^7.0.4",
60
- "jest": "^27.3.1",
59
+ "jest": "^27.4.5",
61
60
  "jest-snapshot-serializer-ansi": "^1.0.0",
62
- "prettier": "^2.4.1"
61
+ "prettier": "^2.5.1"
63
62
  },
64
63
  "keywords": [
65
64
  "lint",