Skip to content

Commit

Permalink
feat: Resolve a npm package passed as --config
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronhunter committed Jun 11, 2018
1 parent 3d0ccb2 commit 4a705f8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ $ ./node_modules/.bin/lint-staged --help
Options:
-V, --version output the version number
-c, --config [path] Path to configuration file
-c, --config [path] Configuration file path or package
-d, --debug Enable debug mode
-h, --help output usage information
```

* **`--config [path]`**: This can be used to manually specify the `lint-staged` config file location. However, if the specified file cannot be found, it will error out instead of performing the usual search.
* **`--config [path]`**: This can be used to manually specify the `lint-staged` config file location. However, if the specified file cannot be found, it will error out instead of performing the usual search. You may pass a npm package name for configuration also.
* **`--debug`**: Enabling the debug mode does the following:
* `lint-staged` uses the [debug](https://github.com/visionmedia/debug) module internally to log information about staged files, commands being executed, location of binaries etc. Debug logs, which are automatically enabled by passing the flag, can also be enabled by setting the environment variable `$DEBUG` to `lint-staged*`.
* Use the [`verbose` renderer](https://github.com/SamVerschueren/listr-verbose-renderer) for `listr`.
Expand Down
5 changes: 5 additions & 0 deletions __mocks__/my-lint-staged-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

module.exports = {
'*': 'mytask'
}
4 changes: 4 additions & 0 deletions __mocks__/my-lint-staged-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "my-lint-staged-config",
"version": "0.0.0"
}
22 changes: 15 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@ if (process.stdout.isTTY) {

const errConfigNotFound = new Error('Config could not be found')

function resolveConfig(configPath) {
try {
return require.resolve(configPath)
} catch (ignore) {
return configPath
}
}

function loadConfig(configPath) {
const explorer = cosmiconfig('lint-staged', {
searchPlaces: [
'package.json',
`.lintstagedrc`,
`.lintstagedrc.json`,
`.lintstagedrc.yaml`,
`.lintstagedrc.yml`,
`.lintstagedrc.js`,
`lint-staged.config.js`
'.lintstagedrc',
'.lintstagedrc.json',
'.lintstagedrc.yaml',
'.lintstagedrc.yml',
'.lintstagedrc.js',
'lint-staged.config.js'
]
})

return configPath ? explorer.load(configPath) : explorer.search()
return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lintStaged should load an npm config package when specified 1`] = `
"
LOG Running lint-staged with the following config:
LOG {
linters: {
'*': 'mytask'
},
concurrent: true,
chunkSize: 9007199254740991,
globOptions: {
matchBase: true,
dot: true
},
ignore: [],
subTaskConcurrency: 1,
renderer: 'verbose'
}"
`;

exports[`lintStaged should load config file when specified 1`] = `
"
LOG Running lint-staged with the following config:
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ describe('lintStaged', () => {
expect(logger.printHistory()).toMatchSnapshot()
})

it('should load an npm config package when specified', async () => {
expect.assertions(1)
jest.mock('my-lint-staged-config')
await lintStaged(logger, 'my-lint-staged-config', true)
expect(logger.printHistory()).toMatchSnapshot()
})

it('should print helpful error message when config file is not found', async () => {
expect.assertions(1)
mockCosmiconfigWith(null)
Expand Down

0 comments on commit 4a705f8

Please sign in to comment.