Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Resolve a npm package passed as --config #464

Merged
merged 1 commit into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be a mock implementation as well? Also, there should be an assertion to check that the specified package was resolved and required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is... the __mocks__/my-lint-staged-config directory is the mock implementation. The mock implementation contains the config { '*': 'mytask' } which is included in the snapshot providing an end-to-end assertion that the package was required and resolved.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes I see that now. My bad.

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