Skip to content

Commit 2ef9df6

Browse files
committed
chore: introduce cwd awareness
1 parent a1c36a3 commit 2ef9df6

File tree

17 files changed

+219
-496
lines changed

17 files changed

+219
-496
lines changed

@commitlint/cli/cli.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ const rules = {
2323
};
2424

2525
const configuration = {
26-
string: ['from', 'to', 'extends', 'parser-preset'],
26+
string: ['cwd', 'from', 'to', 'extends', 'parser-preset'],
2727
boolean: ['edit', 'help', 'version', 'quiet', 'color'],
2828
alias: {
2929
c: 'color',
30+
d: 'cwd',
3031
e: 'edit',
3132
f: 'from',
3233
t: 'to',
@@ -38,15 +39,18 @@ const configuration = {
3839
},
3940
description: {
4041
color: 'toggle colored output',
42+
cwd: 'directory to execute in',
4143
edit: 'read last commit message found in ./git/COMMIT_EDITMSG',
4244
extends: 'array of shareable configurations to extend',
4345
from: 'lower end of the commit range to lint; applies if edit=false',
4446
to: 'upper end of the commit range to lint; applies if edit=false',
4547
quiet: 'toggle console output',
46-
'parser-preset': 'configuration preset to use for conventional-commits-parser'
48+
'parser-preset':
49+
'configuration preset to use for conventional-commits-parser'
4750
},
4851
default: {
4952
color: true,
53+
cwd: process.cwd(),
5054
edit: false,
5155
from: null,
5256
to: null,
@@ -67,21 +71,21 @@ const cli = meow(
6771
configuration
6872
);
6973

70-
const load = seed => core.load(seed);
74+
const load = (seed, opts) => core.load(seed, opts);
7175

7276
function main(options) {
7377
const raw = options.input;
7478
const flags = options.flags;
7579
const fromStdin = rules.fromStdin(raw, flags);
7680

7781
const range = pick(flags, 'edit', 'from', 'to');
78-
const input = fromStdin ? stdin() : core.read(range);
82+
const input = fromStdin ? stdin() : core.read(range, {cwd: flags.cwd});
7983
const fmt = new chalk.constructor({enabled: flags.color});
8084

8185
return input.then(raw => (Array.isArray(raw) ? raw : [raw])).then(messages =>
8286
Promise.all(
8387
messages.map(commit => {
84-
return load(getSeed(flags))
88+
return load(getSeed(flags), {cwd: flags.cwd})
8589
.then(loaded => {
8690
const parserOpts = selectParserOpts(loaded.parserPreset);
8791
const opts = parserOpts ? {parserOpts} : undefined;
@@ -127,7 +131,6 @@ main(cli).catch(err =>
127131
})
128132
);
129133

130-
131134
function selectParserOpts(parserPreset) {
132135
if (typeof parserPreset !== 'object') {
133136
return undefined;

@commitlint/cli/cli.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ test('should work with husky commitmsg hook in sub packages', async () => {
137137

138138
test('should pick up parser preset', async t => {
139139
const cwd = PARSER_PRESET;
140-
141140
const actual = await t.throws(cli([], {cwd})('type(scope)-ticket subject'));
141+
142142
t.true(includes(actual.stdout, 'message may not be empty [subject-empty]'));
143143

144144
await cli(['--parser-preset', './parser-preset'], {cwd})(

@commitlint/core/fixtures/legacy/.conventional-changelog-lintrc

-5
This file was deleted.

@commitlint/core/fixtures/overriden-legacy/.conventional-changelog-lintrc

-5
This file was deleted.

@commitlint/core/fixtures/overriden-legacy/commitlint.config.js

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['./second-extended'],
3+
rules: {
4+
one: 1
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
module.exports = {
2-
extends: ['./second-extended'],
3-
rules: {
4-
one: 1
5-
}
6-
};
1+
module.exports = require('./commitlint.config.js');
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const defaultOpts = require('conventional-changelog-angular');
2-
const _ = require('lodash');
3-
4-
module.exports = defaultOpts.then(data => {
5-
const extented = _.cloneDeep(data);
6-
extented.parserOpts.headerPattern = /^(\w*)(?:\((.*)\))?-(.*)$/;
7-
return extented;
1+
module.exports = Promise.resolve().then(() => {
2+
return {
3+
parserOpts: {
4+
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
5+
}
6+
};
87
});

@commitlint/core/package.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"license": "MIT",
6464
"devDependencies": {
6565
"@commitlint/utils": "^3.1.1",
66-
"ansi-styles": "3.1.0",
6766
"ava": "0.22.0",
6867
"babel-cli": "^6.26.0",
6968
"babel-preset-commitlint": "^3.2.0",
@@ -73,27 +72,26 @@
7372
"dependency-check": "2.7.0",
7473
"execa": "0.6.3",
7574
"globby": "6.1.0",
76-
"has-ansi": "3.0.0",
7775
"import-from": "2.1.0",
7876
"nyc": "10.3.2",
7977
"path-exists": "3.0.0",
8078
"resolve-from": "3.0.0",
79+
"rimraf": "2.6.1",
8180
"xo": "0.18.2"
8281
},
8382
"dependencies": {
84-
"@marionebl/sander": "^0.6.1",
83+
"@marionebl/git-raw-commits": "^1.2.0",
84+
"@marionebl/sander": "^0.6.0",
8585
"babel-runtime": "^6.23.0",
8686
"chalk": "^2.0.1",
8787
"conventional-changelog-angular": "^1.3.3",
8888
"conventional-commits-parser": "^1.3.0",
8989
"cosmiconfig": "^3.0.1",
9090
"find-up": "^2.1.0",
9191
"franc": "^2.0.0",
92-
"git-raw-commits": "^1.1.2",
9392
"lodash": "^4.17.4",
9493
"path-exists": "^3.0.0",
9594
"pos": "^0.4.2",
96-
"rc": "^1.1.7",
9795
"resolve-from": "^3.0.0",
9896
"semver": "^5.3.0"
9997
}

@commitlint/core/src/format.test.js

-66
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import test from 'ava';
2-
import hasAnsi from 'has-ansi';
32
import chalk from 'chalk';
4-
import {yellow, red, magenta, blue} from 'ansi-styles';
53
import {includes} from 'lodash';
64
import format from './format';
75

@@ -49,19 +47,6 @@ test('returns a correct of empty .errors and .warnings', t => {
4947
t.true(includes(msg, '1 problems, 1 warnings'));
5048
});
5149

52-
test('colors messages by default', t => {
53-
const [msg] = format({
54-
errors: [],
55-
warnings: []
56-
});
57-
t.true(hasAnsi(msg));
58-
});
59-
60-
test('does not color messages if configured', t => {
61-
const [msg] = format({}, {color: false});
62-
t.false(hasAnsi(msg));
63-
});
64-
6550
test('uses appropriate signs by default', t => {
6651
const [err, warn] = format({
6752
errors: [
@@ -110,54 +95,3 @@ test('uses signs as configured', t => {
11095
t.true(includes(err, 'ERR'));
11196
t.true(includes(warn, 'WRN'));
11297
});
113-
114-
test('uses appropriate colors by default', t => {
115-
const [err, warn] = format({
116-
errors: [
117-
{
118-
level: 2,
119-
name: 'error-name',
120-
message: 'There was an error'
121-
}
122-
],
123-
warnings: [
124-
{
125-
level: 1,
126-
name: 'warning-name',
127-
message: 'There was a problem'
128-
}
129-
]
130-
});
131-
132-
t.true(includes(err, red.open));
133-
t.true(includes(warn, yellow.open));
134-
});
135-
136-
if (process.platform !== 'win32') {
137-
test('uses colors as configured', t => {
138-
const [err, warn] = format(
139-
{
140-
errors: [
141-
{
142-
level: 2,
143-
name: 'error-name',
144-
message: 'There was an error'
145-
}
146-
],
147-
warnings: [
148-
{
149-
level: 1,
150-
name: 'warning-name',
151-
message: 'There was a problem'
152-
}
153-
]
154-
},
155-
{
156-
colors: ['white', 'magenta', 'blue']
157-
}
158-
);
159-
160-
t.true(includes(err, blue.open));
161-
t.true(includes(warn, magenta.open));
162-
});
163-
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import path from 'path';
2+
import up from 'find-up';
3+
4+
export default toplevel;
5+
6+
// Find the next git root
7+
// (start: string) => Promise<string | null>
8+
async function toplevel(cwd) {
9+
const found = await up('.git', {cwd});
10+
11+
if (typeof found !== 'string') {
12+
return found;
13+
}
14+
15+
return path.join(found, '..');
16+
}

@commitlint/core/src/load.js

+16-62
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
import path from 'path';
2-
import {entries, merge, mergeWith, pick} from 'lodash';
3-
import rc from 'rc';
42
import cosmiconfig from 'cosmiconfig';
3+
import {entries, merge, mergeWith, pick} from 'lodash';
54
import resolveFrom from 'resolve-from';
6-
import up from 'find-up';
75

8-
import resolveExtends from './library/resolve-extends';
96
import executeRule from './library/execute-rule';
7+
import resolveExtends from './library/resolve-extends';
8+
import toplevel from './library/toplevel';
109

1110
const w = (a, b) => (Array.isArray(b) ? b : undefined);
1211
const valid = input => pick(input, 'extends', 'rules', 'parserPreset');
1312

14-
export default async (seed = {}) => {
15-
// Obtain config from .rc files
16-
const raw = await file();
13+
export default async (seed = {}, options = {cwd: ''}) => {
14+
const explorer = cosmiconfig('commitlint', {
15+
rcExtensions: true,
16+
stopDir: await toplevel(options.cwd)
17+
});
18+
19+
const raw = (await explorer.load(options.cwd)) || {};
20+
const base = raw.filepath ? path.dirname(raw.filepath) : options.cwd;
21+
1722
// Merge passed config with file based options
18-
const config = valid(merge(raw, seed));
23+
const config = valid(merge(raw.config, seed));
1924
const opts = merge({extends: [], rules: {}}, pick(config, 'extends'));
2025

2126
// Resolve parserPreset key
2227
if (typeof config.parserPreset === 'string') {
23-
const resolvedParserPreset = resolveFrom(
24-
process.cwd(),
25-
config.parserPreset
26-
);
28+
const resolvedParserPreset = resolveFrom(base, config.parserPreset);
2729

2830
config.parserPreset = {
2931
name: config.parserPreset,
30-
path: `./${path.posix.relative(process.cwd(), resolvedParserPreset)}`
31-
.split(path.sep)
32-
.join('/'),
32+
path: resolvedParserPreset,
3333
opts: require(resolvedParserPreset)
3434
};
3535
}
3636

3737
// Resolve extends key
3838
const extended = resolveExtends(opts, {
3939
prefix: 'commitlint-config',
40-
cwd: raw.config ? path.dirname(raw.config) : process.cwd(),
40+
cwd: base,
4141
parserPreset: config.parserPreset
4242
});
4343

@@ -80,49 +80,3 @@ export default async (seed = {}) => {
8080
return registry;
8181
}, preset);
8282
};
83-
84-
async function file() {
85-
const legacy = rc('conventional-changelog-lint');
86-
const legacyFound = typeof legacy.config === 'string';
87-
const explorer = cosmiconfig('commitlint', {
88-
rcExtensions: true,
89-
stopDir: await toplevel()
90-
});
91-
const config = await explorer.load('.');
92-
93-
if (legacyFound && !config) {
94-
console.warn(
95-
`Using legacy ${path.relative(
96-
process.cwd(),
97-
legacy.config
98-
)}. Rename to commitlint.config.js to silence this warning.`
99-
);
100-
}
101-
102-
if (legacyFound && config) {
103-
console.warn(
104-
`Ignored legacy ${path.relative(
105-
process.cwd(),
106-
legacy.config
107-
)} as commitlint.config.js superseeds it. Remove .conventional-changelog-lintrc to silence this warning.`
108-
);
109-
}
110-
111-
if (config) {
112-
return config.config;
113-
}
114-
115-
return legacy;
116-
}
117-
118-
// Find the next git root
119-
// (start: string) => Promise<string | null>
120-
async function toplevel(cwd = process.cwd()) {
121-
const found = await up('.git', {cwd});
122-
123-
if (typeof found !== 'string') {
124-
return found;
125-
}
126-
127-
return path.join(found, '..');
128-
}

0 commit comments

Comments
 (0)