Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor: remove advanced configuration options
BREAKING CHANGE: The advanced configuration options have been deprecated in favour of the simple format
  • Loading branch information
iiroj authored and okonet committed Jul 1, 2019
1 parent bed9127 commit 04190c8
Show file tree
Hide file tree
Showing 20 changed files with 203 additions and 999 deletions.
8 changes: 3 additions & 5 deletions .lintstagedrc.json
@@ -1,7 +1,5 @@
{
"linters": {
"*.{js,json,md}": ["prettier --write", "git add"],
"*.js": ["npm run lint:base --fix", "git add"],
".*{rc, json}": ["jsonlint", "git add"]
}
"*.{js,json,md}": ["prettier --write", "git add"],
"*.js": ["npm run lint:base --fix", "git add"],
".*{rc, json}": ["jsonlint", "git add"]
}
75 changes: 1 addition & 74 deletions README.md
Expand Up @@ -72,11 +72,7 @@ Starting with v3.1 you can now use different ways of configuring it:

See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.

Lint-staged supports simple and advanced config formats.

### Simple config format

Should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses [micromatch](https://github.com/micromatch/micromatch) for glob patterns.
Configuration should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses [micromatch](https://github.com/micromatch/micromatch) for glob patterns.

#### `package.json` example:

Expand All @@ -102,34 +98,6 @@ So, considering you did `git add file1.ext file2.ext`, lint-staged will run the

`your-cmd file1.ext file2.ext`

### Advanced config format

To extend and customise lint-staged, advanced options are available. To use these options the format should be as the following:

#### `package.json` example with `ignore` option:

```json
{
"lint-staged": {
"linters": {
"*.{js,scss}": ["some command", "git add"]
},
"ignore": ["**/dist/*.min.js"]
}
}
```

Notice that the linting commands now are nested into the `linters` object. The following options are available for advanced configuration:

#### Options

* `concurrent`_true_ — runs linters for each glob pattern simultaneously. If you don’t want this, you can set `concurrent: false`
* `globOptions``{ matchBase: true, dot: true }`[micromatch options](https://github.com/micromatch/micromatch#options) to
customize how glob patterns match files.
* `ignore` - `['**/docs/**/*.js']` - array of glob patterns to entirely ignore from any task.
* `linters``Object` — keys (`String`) are glob patterns, values (`Array<String> | String`) are commands to execute.
* `relative``false` — if `true` it will give the relative path from your `package.json` directory to your linter arguments.

## Using JS functions to customize linter commands

When supplying configuration in JS format it is possible to define the linter command as a function which receives an array of staged filenames/paths and returns the complete linter command as a string. It is also possible to return an array of complete command strings, for example when the linter command supports only a single file input.
Expand Down Expand Up @@ -178,36 +146,6 @@ module.exports = {
}
```

## Filtering files

It is possible to run linters for certain paths only by using glob patterns. [micromatch](https://github.com/micromatch/micromatch) is used to filter the staged files according to these patterns. File patterns should be specified _relative to the `package.json` location_ (i.e. where `lint-staged` is installed).

**NOTE:** If you're using `lint-staged<5` globs have to be _relative to the git root_.

```js
{
// .js files anywhere in the root directory of the project
"*.js": "eslint",
// .js files anywhere in the project
"**/*.js": "eslint",
// .js file in the src directory
"src/*.js": "eslint",
// .js file anywhere within and below the src directory
"src/**/*.js": "eslint",
}
```

When matching, `lint-staged` will do the following

* Resolve the git root automatically, no configuration needed.
* Pick the staged files which are present inside the project directory.
* Filter them using the specified glob patterns.
* Pass absolute paths to the linters as arguments.

**NOTE:** `lint-staged` will pass _absolute_ paths to the linters to avoid any confusion in case they're executed in a different working directory (i.e. when your `.git` directory isn't the same as your `package.json` directory).

Also see [How to use `lint-staged` in a multi package monorepo?](#how-to-use-lint-staged-in-a-multi-package-monorepo)

## What commands are supported?

Supported are any executables installed locally or globally via `npm` as well as any executable from your $PATH.
Expand Down Expand Up @@ -328,17 +266,6 @@ For example, here is `jest` running on all `.js` files with the `NODE_ENV` varia
}
```

### Use ng lint with angular cli >= 7.0.0

```json
{
"linters": {
"*.ts": "ng lint myProjectName --files"
},
"relative": true
}
```

### Stylelint for CSS with defaults and for SCSS with SCSS syntax

```json
Expand Down
4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -34,16 +34,14 @@
"dedent": "^0.7.0",
"del": "^4.1.1",
"execa": "^2.0.1",
"is-glob": "^4.0.0",
"listr": "^0.14.2",
"listr-update-renderer": "^0.5.0",
"lodash": "^4.17.11",
"log-symbols": "^2.2.0",
"micromatch": "^3.1.8",
"path-is-inside": "^1.0.2",
"please-upgrade-node": "^3.0.2",
"stringify-object": "^3.2.2",
"yup": "^0.27.0"
"stringify-object": "^3.2.2"
},
"devDependencies": {
"babel-core": "^6.26.3",
Expand Down
21 changes: 6 additions & 15 deletions src/generateTasks.js
Expand Up @@ -4,16 +4,11 @@ const micromatch = require('micromatch')
const pathIsInside = require('path-is-inside')
const path = require('path')

const { getConfig } = require('./getConfig')

const debug = require('debug')('lint-staged:gen-tasks')

module.exports = async function generateTasks(config, gitDir, stagedRelFiles) {
module.exports = async function generateTasks(linters, gitDir, stagedRelFiles) {
debug('Generating linter tasks')

const normalizedConfig = getConfig(config) // Ensure we have a normalized config
const { linters, globOptions, ignore } = normalizedConfig

const cwd = process.cwd()
const stagedFiles = stagedRelFiles.map(file => path.resolve(gitDir, file))

Expand All @@ -30,17 +25,13 @@ module.exports = async function generateTasks(config, gitDir, stagedRelFiles) {
.map(file => path.relative(cwd, file)),
pattern,
{
...globOptions,
ignore
}
).map(file => {
// if you set relative option, then the file path will be relative to your package.json
if (config.relative) {
return path.normalize(file)
matchBase: true,
dot: true
}
).map(file =>
// Return absolute path after the filter is run
return path.resolve(cwd, file)
})
path.resolve(cwd, file)
)

const task = { pattern, commands, fileList }
debug('Generated task: \n%O', task)
Expand Down
224 changes: 0 additions & 224 deletions src/getConfig.js

This file was deleted.

0 comments on commit 04190c8

Please sign in to comment.