Skip to content

Commit

Permalink
feat(core): Allow to configure with json, yaml and package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Sep 17, 2017
1 parent e6ef072 commit 2aabb1d
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./first-extended'],
rules: {
zero: 0
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
two: 2
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["./first-extended"],
"rules": {
"zero": 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
two: 2
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
two: 2
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"commitlint": {
"extends": ["./first-extended"],
"rules": {
"zero": 0
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extends:
- "./first-extended"
rules:
zero: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
two: 2
}
};
3 changes: 1 addition & 2 deletions @commitlint/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"execa": "0.6.3",
"globby": "6.1.0",
"has-ansi": "3.0.0",
"import-from": "2.1.0",
"nyc": "10.3.2",
"path-exists": "3.0.0",
"resolve-from": "3.0.0",
Expand All @@ -86,10 +85,10 @@
"chalk": "^2.0.1",
"conventional-changelog-angular": "^1.3.3",
"conventional-commits-parser": "^1.3.0",
"cosmiconfig": "^3.0.1",
"find-up": "^2.1.0",
"franc": "^2.0.0",
"git-raw-commits": "^1.1.2",
"import-from": "^2.1.0",
"lodash": "^4.17.4",
"mz": "^2.6.0",
"path-exists": "^3.0.0",
Expand Down
43 changes: 22 additions & 21 deletions @commitlint/core/src/load.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import importFrom from 'import-from';
import {entries, merge, mergeWith, pick} from 'lodash';
import rc from 'rc';
import cosmiconfig from 'cosmiconfig';
import resolveFrom from 'resolve-from';

import resolveExtends from './library/resolve-extends';
Expand All @@ -13,18 +13,22 @@ const valid = input => pick(input, 'extends', 'rules', 'parserPreset');
export default async (seed = {}) => {
// Obtain config from .rc files
const raw = file();

// Merge passed config with file based options
const config = valid(merge(raw, seed));
const opts = merge({extends: [], rules: {}}, pick(config, 'extends'));

// Resolve parserPreset key
if (typeof config.parserPreset === 'string') {
const resolvedParserPreset = resolveFrom(process.cwd(), config.parserPreset);
const resolvedParserPreset = resolveFrom(
process.cwd(),
config.parserPreset
);

config.parserPreset = {
name: config.parserPreset,
path: `./${path.posix.relative(process.cwd(), resolvedParserPreset)}`.split(path.sep).join('/'),
path: `./${path.posix.relative(process.cwd(), resolvedParserPreset)}`
.split(path.sep)
.join('/'),
opts: require(resolvedParserPreset)
};
}
Expand All @@ -39,7 +43,10 @@ export default async (seed = {}) => {
const preset = valid(mergeWith(extended, config, w));

// Await parser-preset if applicable
if (typeof preset.parserPreset === 'object' && typeof preset.parserPreset.opts === 'object') {
if (
typeof preset.parserPreset === 'object' &&
typeof preset.parserPreset.opts === 'object'
) {
preset.parserPreset.opts = await preset.parserPreset.opts;
}

Expand Down Expand Up @@ -76,11 +83,14 @@ export default async (seed = {}) => {
function file() {
const legacy = rc('conventional-changelog-lint');
const legacyFound = typeof legacy.config === 'string';
const explorer = cosmiconfig('commitlint', {
rcExtensions: true,
sync: true,
stopDir: process.cwd()
});
const config = explorer.load('.');

const found = resolveable('./commitlint.config');
const raw = found ? importFrom(process.cwd(), './commitlint.config') : {};

if (legacyFound && !found) {
if (legacyFound && !config) {
console.warn(
`Using legacy ${path.relative(
process.cwd(),
Expand All @@ -89,7 +99,7 @@ function file() {
);
}

if (legacyFound && found) {
if (legacyFound && config) {
console.warn(
`Ignored legacy ${path.relative(
process.cwd(),
Expand All @@ -98,18 +108,9 @@ function file() {
);
}

if (found) {
return raw;
if (config) {
return config.config;
}

return legacy;
}

function resolveable(id) {
try {
resolveFrom(process.cwd(), id);
return true;
} catch (err) {
return false;
}
}
61 changes: 59 additions & 2 deletions @commitlint/core/src/load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ test('uses seed as configured', async t => {
test('uses seed with parserPreset', async t => {
t.context.back = chdir('fixtures/parser-preset');

const {parserPreset: actual} = await load({parserPreset: './conventional-changelog-custom'});
const {parserPreset: actual} = await load({
parserPreset: './conventional-changelog-custom'
});
t.is(actual.name, './conventional-changelog-custom');
t.deepEqual(actual.opts, {
parserOpts: {
Expand Down Expand Up @@ -63,6 +65,58 @@ test('recursive extends', async t => {
});
});

test('recursive extends with json file', async t => {
t.context.back = chdir('fixtures/recursive-extends-json');
const actual = await load();
t.deepEqual(actual, {
extends: ['./first-extended'],
rules: {
zero: 0,
one: 1,
two: 2
}
});
});

test('recursive extends with yaml file', async t => {
t.context.back = chdir('fixtures/recursive-extends-yaml');
const actual = await load();
t.deepEqual(actual, {
extends: ['./first-extended'],
rules: {
zero: 0,
one: 1,
two: 2
}
});
});

test('recursive extends with js file', async t => {
t.context.back = chdir('fixtures/recursive-extends-js');
const actual = await load();
t.deepEqual(actual, {
extends: ['./first-extended'],
rules: {
zero: 0,
one: 1,
two: 2
}
});
});

test('recursive extends with package.json file', async t => {
t.context.back = chdir('fixtures/recursive-extends-package');
const actual = await load();
t.deepEqual(actual, {
extends: ['./first-extended'],
rules: {
zero: 0,
one: 1,
two: 2
}
});
});

test('parser preset overwrites completely instead of merging', async t => {
t.context.back = chdir('fixtures/parser-preset-override');
const actual = await load();
Expand All @@ -83,7 +137,10 @@ test('recursive extends with parserPreset', async t => {

t.is(actual.parserPreset.name, './conventional-changelog-custom');
t.is(typeof actual.parserPreset.opts, 'object');
t.deepEqual(actual.parserPreset.opts.parserOpts.headerPattern, /^(\w*)(?:\((.*)\))?-(.*)$/);
t.deepEqual(
actual.parserPreset.opts.parserOpts.headerPattern,
/^(\w*)(?:\((.*)\))?-(.*)$/
);
});

test('ignores unknow keys', async t => {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ echo "module.exports = {extends: ['@commitlint/config-angular']}" > commitlint.c

## Config

* Configuration is picked up from `commitlint.config.js` files
* Configuration is picked up from `commitlint.config.js`, `.commitlintrc.js`, `.commitlintrc.json`, or `.commitlintrc.yml` file or a `commitlint` field in `package.json`
* Packages: [cli](./@commitlint/cli), [core](./@commitlint/core)
* See [Rules](./docs/reference-rules.md) for a complete list of possible rules
* An example configuration can be found at [@commitlint/config-angular](./@commitlint/config-angular/index.js)
Expand Down
2 changes: 2 additions & 0 deletions docs/guides-ci-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ npm install --save-dev @commitlint-{cli,angular}
echo "module.exports = {extends: ['@commitlint/config-angular']};" > commitlint.config.js
```

Alternatively the configuration can be defined in `.commitlintrc.js`, `.commitlintrc.json`, or `.commitlintrc.yml` file or a `commitlint` field in `package.json`.

## First test run with Travis

Add a `.travis.yml` to your project root
Expand Down
2 changes: 2 additions & 0 deletions docs/guides-local-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ npm install --save-dev @commitlint-{cli,angular}
echo "module.exports = {extends: ['@commitlint/config-angular']};" > commitlint.config.js
```

Alternatively the configuration can be defined in `.commitlintrc.js`, `.commitlintrc.json`, or `.commitlintrc.yml` file or a `commitlint` field in `package.json`.

## Install husky

Install `husky` as devDependency, a handy git hook helper available on npm.
Expand Down

0 comments on commit 2aabb1d

Please sign in to comment.