feat(cli): add "--config" option support#261
feat(cli): add "--config" option support#261marionebl merged 2 commits intoconventional-changelog:masterfrom lc-soft:add_config_option_for_cli
Conversation
marionebl
left a comment
There was a problem hiding this comment.
Got some ideas how to make this test green. Also we will want to add some additional tests:
- Test if config in parent directories is ignored if
configPathis provided (should) - Test the
options.fileparameter incore.loadtests
@commitlint/core/src/load.js
Outdated
| }); | ||
|
|
||
| const local = await explorer.load(cwd); | ||
| const local = await explorer.load(cwd, filepath); |
There was a problem hiding this comment.
I thing filepath has to be passed as configPath to the cosmiconfig constructor
@commitlint/core/src/load.js
Outdated
| }; | ||
|
|
||
| async function loadConfig(cwd) { | ||
| async function loadConfig(cwd, filepath) { |
There was a problem hiding this comment.
Reading cosmiconfig docs (yes they are a bit confusing...) this probably should be (untested)
async function loadConfig(cwd, configPath) {
const explorer = cosmiconfig('commitlint', {
rcExtensions: true,
configPath
});
const local = await explorer.load(cwd);
}There was a problem hiding this comment.
the configPath is option of cosmiconfig? I mistakenly thought you meant load([searchPath, configPath])
| const config = 'config/commitlint.config.js'; | ||
| const actual = await cli([], {cwd, config})('foo: bar'); | ||
| t.true(actual.stdout.includes('type must not be one of [foo]')); | ||
| t.is(actual.code, 1); |
There was a problem hiding this comment.
The result was correct when I ran the command manually, but the test case did not pass. what happened?
echo "foo: bar" | npx yarn run commitlint --config @commitlint/cli/fixtures/custom-config-path/config/commitlint.config.jsyarn run v1.3.2
$ /home/lc/Projects/commitlint/node_modules/.bin/commitlint --config @commitlint/cli/fixtures/custom-config-path/config/commitlint.config.js
⧗ input: foo: bar
✖ type must not be one of [foo] [type-enum]
✖ found 1 problems, 0 warnings
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
|
How to run |
@commitlint/cli/src/cli.test.js
Outdated
|
|
||
| test('should work with --config option', async t => { | ||
| const cwd = await git.bootstrap('fixtures/custom-config-path'); | ||
| const config = 'config/commitlint.config.js'; |
There was a problem hiding this comment.
const file = 'config/commitlint.config.js';
const actual = await cli(['--config', file], {cwd})('foo: bar');
npx yarn run lerna run test --stream --scope @commitlint/cli |
I added file
I do not understand this sentence, can you describe in detail? |
Please add a unit test for the new config in https://github.com/marionebl/commitlint/blob/master/%40commitlint/core/src/load.test.js |
| const file = 'config/commitlint.config.js'; | ||
| const cwd = await git.bootstrap('fixtures/specify-config-file'); | ||
| const actual = await load({rules: {foo: 'what'}}, {cwd, file}); | ||
| t.is(actual.rules.foo, 'bar'); |
There was a problem hiding this comment.
Why the path is not /home/lc/Projects/commitlint/@commitlint/fixtures/specify-config-file/config/commitlint.config.js' ?
load › rules should be loaded from specify config file
Rejected promise returned by test. Reason:
Error {
code: 'ENOENT',
errno: -2,
path: '/home/lc/Projects/commitlint/@commitlint/core/config/commitlint.config.js',
syscall: 'open',
message: 'ENOENT: no such file or directory, open \'/home/lc/Projects/commitlint/@commitlint/core/config/commitlint.config.js\'',
}
error Command failed with exit code 1.
There was a problem hiding this comment.
This change can work.
- const file = 'fconfig/commitlint.config.js';
+ const file = 'fixtures/specify-config-file/config/commitlint.config.js';There was a problem hiding this comment.
I think we have to resolve the configPath in core.load relative to cwd:
const explorer = cosmiconfig('commitlint', {
rcExtensions: true,
configPath: path.resolve(cwd, configPath)
});There was a problem hiding this comment.
Rejected promise returned by test. Reason:
TypeError {
message: 'Path must be a string. Received undefined',
}
loadConfig (src/load.js:81:1)
exports.default (src/load.js:14:16)
Test.<anonymous> (src/load.test.js:189:23)
const explorer = cosmiconfig('commitlint', {
rcExtensions: true,
- configPath: path.resolve(cwd, configPath)
+ configPath: configPath ? path.resolve(cwd, configPath) : null
});|
Thanks for your patience 👍 |

Description
Add "--config" option support for cli, and add a simple test case, but this test case are not tested, need @marionebl to help me improve this test.
Motivation and Context
#241
Usage examples
How Has This Been Tested?
Please see the above example.
Types of changes
Checklist:
configPathis provided (should)options.fileparameter incore.loadtests