lint-staged 4.2.0 → 4.3.0

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.
package/README.md CHANGED
@@ -57,6 +57,7 @@ Starting with v3.1 you can now use different ways of configuring it:
57
57
  * `lint-staged` object in your `package.json`
58
58
  * `.lintstagedrc` file in JSON or YML format
59
59
  * `lint-staged.config.js` file in JS format
60
+ * Pass a configuration file using the `--config` or `-c` flag
60
61
 
61
62
  See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.
62
63
 
package/index.js ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+
3
+ 'use strict'
4
+
5
+ const cmdline = require('commander')
6
+ const pkg = require('./package.json')
7
+
8
+ cmdline
9
+ .version(pkg.version)
10
+ .option('-c, --config [path]', 'Path to configuration file')
11
+ .parse(process.argv)
12
+
13
+ require('./src')(console, cmdline.config)
package/package.json CHANGED
@@ -1,90 +1 @@
1
- {
2
- "name": "lint-staged",
3
- "version": "4.2.0",
4
- "description": "Lint files staged by git",
5
- "license": "MIT",
6
- "repository": "https://github.com/okonet/lint-staged",
7
- "author": "Andrey Okonetchnikov <andrey@okonet.ru>",
8
- "maintainers": [
9
- "Lufty Wiranda <lufty.wiranda@gmail.com",
10
- "Suhas Karanth <sudo.suhas@gmail.com"
11
- ],
12
- "engines": {
13
- "node": ">=4.2.0"
14
- },
15
- "bin": "index.js",
16
- "files": [
17
- "src"
18
- ],
19
- "scripts": {
20
- "precommit": "remove-lockfiles && node index.js",
21
- "cz": "git-cz",
22
- "lint": "eslint .",
23
- "lint:fix": "npm run lint -- --fix",
24
- "pretest": "npm run lint",
25
- "test": "jest --coverage",
26
- "test:watch": "jest --watch"
27
- },
28
- "dependencies": {
29
- "app-root-path": "^2.0.0",
30
- "chalk": "^2.1.0",
31
- "cosmiconfig": "^1.1.0",
32
- "execa": "^0.8.0",
33
- "is-glob": "^4.0.0",
34
- "jest-validate": "^20.0.3",
35
- "listr": "^0.12.0",
36
- "lodash": "^4.17.4",
37
- "log-symbols": "^2.0.0",
38
- "minimatch": "^3.0.0",
39
- "npm-which": "^3.0.1",
40
- "p-map": "^1.1.1",
41
- "staged-git-files": "0.0.4",
42
- "stringify-object": "^3.2.0"
43
- },
44
- "devDependencies": {
45
- "babel-jest": "^20.0.0",
46
- "babel-preset-env": "^1.6.0",
47
- "commitizen": "^2.9.6",
48
- "consolemock": "^0.2.2",
49
- "cz-conventional-changelog": "^2.0.0",
50
- "eslint": "^4.5.0",
51
- "eslint-config-okonet": "^5.0.1",
52
- "eslint-plugin-node": "^5.1.1",
53
- "husky": "^0.14.3",
54
- "jest": "^20.0.4",
55
- "jest-cli": "^20.0.4",
56
- "jsonlint": "^1.6.2",
57
- "prettier": "1.5.3",
58
- "remove-lockfiles": "^1.1.1",
59
- "strip-ansi": "^3.0.1"
60
- },
61
- "config": {
62
- "commitizen": {
63
- "path": "./node_modules/cz-conventional-changelog"
64
- }
65
- },
66
- "jest": {
67
- "testEnvironment": "node",
68
- "setupFiles": [
69
- "./testSetup.js"
70
- ]
71
- },
72
- "greenkeeper": {
73
- "ignore": [
74
- "cosmiconfig"
75
- ]
76
- },
77
- "keywords": [
78
- "lint",
79
- "git",
80
- "staged",
81
- "eslint",
82
- "prettier",
83
- "stylelint",
84
- "code",
85
- "quality",
86
- "check",
87
- "format",
88
- "validate"
89
- ]
90
- }
1
+ {"name":"lint-staged","version":"4.3.0","description":"Lint files staged by git","license":"MIT","repository":"https://github.com/okonet/lint-staged","author":"Andrey Okonetchnikov <andrey@okonet.ru>","maintainers":["Lufty Wiranda <lufty.wiranda@gmail.com","Suhas Karanth <sudo.suhas@gmail.com"],"engines":{"node":">=4.2.0"},"bin":"index.js","files":["index.js","src"],"scripts":{"precommit":"remove-lockfiles && node index.js","cz":"git-cz","lint":"eslint .","lint:fix":"npm run lint -- --fix","pretest":"npm run lint","test":"jest --coverage","test:watch":"jest --watch"},"dependencies":{"app-root-path":"^2.0.0","chalk":"^2.1.0","commander":"^2.11.0","cosmiconfig":"^1.1.0","execa":"^0.8.0","is-glob":"^4.0.0","jest-validate":"^21.1.0","listr":"^0.12.0","lodash":"^4.17.4","log-symbols":"^2.0.0","minimatch":"^3.0.0","npm-which":"^3.0.1","p-map":"^1.1.1","staged-git-files":"0.0.4","stringify-object":"^3.2.0"},"devDependencies":{"babel-jest":"^21.0.2","babel-preset-env":"^1.6.0","commitizen":"^2.9.6","consolemock":"^0.3.0","cz-conventional-changelog":"^2.0.0","eslint":"^4.5.0","eslint-config-okonet":"^5.0.1","eslint-plugin-node":"^5.1.1","husky":"^0.14.3","jest":"^21.1.0","jest-cli":"^21.1.0","jsonlint":"^1.6.2","prettier":"1.5.3","remove-lockfiles":"^1.1.1"},"config":{"commitizen":{"path":"./node_modules/cz-conventional-changelog"}},"jest":{"testEnvironment":"node","setupFiles":["./testSetup.js"]},"greenkeeper":{"ignore":["cosmiconfig"]},"keywords":["lint","git","staged","eslint","prettier","stylelint","code","quality","check","format","validate"]}
package/src/findBin.js CHANGED
@@ -2,16 +2,52 @@
2
2
 
3
3
  const npmWhich = require('npm-which')(process.cwd())
4
4
 
5
- module.exports = function findBin(cmd, packageJson, options) {
5
+ module.exports = function findBin(cmd, scripts, options) {
6
+ const npmArgs = (bin, args) =>
7
+ // We always add `--` even if args are not defined. This is required
8
+ // because we pass filenames later.
9
+ ['run', options && options.verbose ? undefined : '--silent', bin, '--']
10
+ // args could be undefined but we filter that out.
11
+ .concat(args)
12
+ .filter(arg => arg !== undefined)
13
+
6
14
  /*
7
- * If package.json has script with cmd defined
8
- * we want it to be executed first
9
- */
10
- if (packageJson.scripts && packageJson.scripts[cmd] !== undefined) {
15
+ * If package.json has script with cmd defined we want it to be executed
16
+ * first. For finding the bin from scripts defined in package.json, there
17
+ * are 2 possibilities. It's a command which does not have any arguments
18
+ * passed to it in the lint-staged config. Or it could be something like
19
+ * `kcd-scripts` which has arguments such as `format`, `lint` passed to it.
20
+ * But we always cannot assume that the cmd, which has a space in it, is of
21
+ * the format `bin argument` because it is legal to define a package.json
22
+ * script with space in it's name. So we do 2 types of lookup. First a naive
23
+ * lookup which just looks for the scripts entry with cmd. Then we split on
24
+ * space, parse the bin and args, and try again.
25
+ *
26
+ * Related:
27
+ * - https://github.com/kentcdodds/kcd-scripts/pull/3
28
+ * - https://github.com/okonet/lint-staged/issues/294
29
+ *
30
+ * Example:
31
+ *
32
+ * "scripts": {
33
+ * "my cmd": "echo deal-wth-it",
34
+ * "demo-bin": "node index.js"
35
+ * },
36
+ * "lint-staged": {
37
+ * "*.js": ["my cmd", "demo-bin hello"]
38
+ * }
39
+ */
40
+ if (scripts[cmd] !== undefined) {
11
41
  // Support for scripts from package.json
12
- const args = ['run', options && options.verbose ? undefined : '--silent', cmd].filter(Boolean)
42
+ return { bin: 'npm', args: npmArgs(cmd) }
43
+ }
13
44
 
14
- return { bin: 'npm', args }
45
+ const parts = cmd.split(' ')
46
+ let bin = parts[0]
47
+ const args = parts.splice(1)
48
+
49
+ if (scripts[bin] !== undefined) {
50
+ return { bin: 'npm', args: npmArgs(bin, args) }
15
51
  }
16
52
 
17
53
  /*
@@ -32,10 +68,6 @@ module.exports = function findBin(cmd, packageJson, options) {
32
68
  * }
33
69
  */
34
70
 
35
- const parts = cmd.split(' ')
36
- let bin = parts[0]
37
- const args = parts.splice(1)
38
-
39
71
  try {
40
72
  /* npm-which tries to resolve the bin in local node_modules/.bin */
41
73
  /* and if this fails it look in $PATH */
package/src/getConfig.js CHANGED
@@ -1,5 +1,7 @@
1
- /* eslint no-console: 0 */
2
1
  /* eslint no-prototype-builtins: 0 */
2
+
3
+ 'use strict'
4
+
3
5
  const chalk = require('chalk')
4
6
  const format = require('stringify-object')
5
7
  const intersection = require('lodash/intersection')
@@ -60,13 +62,13 @@ function unknownValidationReporter(config, example, option, options) {
60
62
  const message = ` Unknown option ${chalk.bold(`"${option}"`)} with value ${chalk.bold(
61
63
  format(config[option], { inlineCharacterLimit: Number.POSITIVE_INFINITY })
62
64
  )} was found in the config root.
63
-
64
- You are probably trying to mix simple and advanced config formats. Adding
65
-
65
+
66
+ You are probably trying to mix simple and advanced config formats. Adding
67
+
66
68
  ${chalk.bold(`"linters": {
67
69
  "${option}": ${JSON.stringify(config[option])}
68
70
  }`)}
69
-
71
+
70
72
  will fix it and remove this message.`
71
73
 
72
74
  const comment = options.comment
package/src/index.js CHANGED
@@ -1,4 +1,3 @@
1
- /* eslint no-console: 0 */
2
1
  /* eslint import/no-dynamic-require: 0 */
3
2
 
4
3
  'use strict'
@@ -26,8 +25,11 @@ const errConfigNotFound = new Error('Config could not be found')
26
25
  /**
27
26
  * Root lint-staged function that is called from .bin
28
27
  */
29
- module.exports = function lintStaged() {
28
+ module.exports = function lintStaged(injectedLogger, configPath) {
29
+ const logger = injectedLogger || console
30
+
30
31
  return cosmiconfig('lint-staged', {
32
+ configPath,
31
33
  rc: '.lintstagedrc',
32
34
  rcExtensions: true
33
35
  })
@@ -39,13 +41,15 @@ module.exports = function lintStaged() {
39
41
  const config = validateConfig(getConfig(result.config))
40
42
 
41
43
  if (config.verbose) {
42
- console.log(`
44
+ logger.log(`
43
45
  Running lint-staged with the following config:
44
46
  ${stringifyObject(config)}
45
47
  `)
46
48
  }
47
49
 
48
- runAll(packageJson, config)
50
+ const scripts = packageJson.scripts || {}
51
+
52
+ runAll(scripts, config)
49
53
  .then(() => {
50
54
  // No errors, exiting with 0
51
55
  process.exitCode = 0
@@ -58,15 +62,15 @@ ${stringifyObject(config)}
58
62
  })
59
63
  .catch(err => {
60
64
  if (err === errConfigNotFound) {
61
- console.error(`${err.message}.`)
65
+ logger.error(`${err.message}.`)
62
66
  } else {
63
67
  // It was probably a parsing error
64
- console.error(`Could not parse lint-staged config.
68
+ logger.error(`Could not parse lint-staged config.
65
69
 
66
70
  ${err}`)
67
71
  }
68
72
  // Print helpful message for all errors
69
- console.error(`
73
+ logger.error(`
70
74
  Please make sure you have created it correctly.
71
75
  See https://github.com/okonet/lint-staged#configuration.
72
76
  `)
@@ -1,5 +1,3 @@
1
- /* eslint no-console: 0 */
2
-
3
1
  'use strict'
4
2
 
5
3
  module.exports = function printErrors(errorInstance) {
@@ -1,3 +1,5 @@
1
+ 'use strict'
2
+
1
3
  const path = require('path')
2
4
 
3
5
  module.exports = function resolveGitDir(gitDir) {
package/src/runAll.js CHANGED
@@ -9,11 +9,11 @@ const resolveGitDir = require('./resolveGitDir')
9
9
 
10
10
  /**
11
11
  * Executes all tasks and either resolves or rejects the promise
12
- * @param packageJson
12
+ * @param scripts
13
13
  * @param config {Object}
14
14
  * @returns {Promise}
15
15
  */
16
- module.exports = function runAll(packageJson, config) {
16
+ module.exports = function runAll(scripts, config) {
17
17
  // Config validation
18
18
  if (!config || !has(config, 'gitDir') || !has(config, 'concurrent') || !has(config, 'renderer')) {
19
19
  throw new Error('Invalid config provided to runAll! Use getConfig instead.')
@@ -35,7 +35,7 @@ module.exports = function runAll(packageJson, config) {
35
35
  const tasks = generateTasks(config, filenames).map(task => ({
36
36
  title: `Running tasks for ${task.pattern}`,
37
37
  task: () =>
38
- new Listr(runScript(task.commands, task.fileList, packageJson, config), {
38
+ new Listr(runScript(task.commands, task.fileList, scripts, config), {
39
39
  // In sub-tasks we don't want to run concurrently
40
40
  // and we want to abort on errors
41
41
  concurrent: false,
package/src/runScript.js CHANGED
@@ -8,7 +8,7 @@ const getConfig = require('./getConfig').getConfig
8
8
  const calcChunkSize = require('./calcChunkSize')
9
9
  const findBin = require('./findBin')
10
10
 
11
- module.exports = function runScript(commands, pathsToLint, packageJson, config) {
11
+ module.exports = function runScript(commands, pathsToLint, scripts, config) {
12
12
  const normalizedConfig = getConfig(config)
13
13
  const chunkSize = normalizedConfig.chunkSize
14
14
  const concurrency = normalizedConfig.subTaskConcurrency
@@ -22,9 +22,7 @@ module.exports = function runScript(commands, pathsToLint, packageJson, config)
22
22
  title: linter,
23
23
  task: () => {
24
24
  try {
25
- const res = findBin(linter, packageJson, config)
26
-
27
- const separatorArgs = /npm(\.exe)?$/i.test(res.bin) ? ['--'] : []
25
+ const res = findBin(linter, scripts, config)
28
26
 
29
27
  // Only use gitDir as CWD if we are using the git binary
30
28
  // e.g `npm` should run tasks in the actual CWD
@@ -33,7 +31,7 @@ module.exports = function runScript(commands, pathsToLint, packageJson, config)
33
31
 
34
32
  const errors = []
35
33
  const mapper = pathsChunk => {
36
- const args = res.args.concat(separatorArgs, pathsChunk)
34
+ const args = res.args.concat(pathsChunk)
37
35
 
38
36
  return (
39
37
  execa(res.bin, args, Object.assign({}, execaOptions))
@@ -1,12 +0,0 @@
1
- {
2
- "plugins": ["node"],
3
- "extends": [
4
- "plugin:node/recommended"
5
- ],
6
- "rules": {
7
- "node/exports-style": [
8
- "error",
9
- "module.exports"
10
- ]
11
- }
12
- }