forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·113 lines (95 loc) · 2.69 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env node
import execa from 'execa';
import commitlint from '@commitlint/cli';
// Allow to override used bins for testing purposes
const GIT = process.env.TRAVIS_COMMITLINT_GIT_BIN || 'git';
const COMMITLINT = process.env.TRAVIS_COMMITLINT_BIN;
const REQUIRED = [
'TRAVIS_COMMIT',
'TRAVIS_COMMIT_RANGE',
'TRAVIS_EVENT_TYPE',
'TRAVIS_REPO_SLUG',
'TRAVIS_PULL_REQUEST_SLUG'
];
const COMMIT = process.env.TRAVIS_COMMIT;
const REPO_SLUG = process.env.TRAVIS_REPO_SLUG;
const PR_SLUG = process.env.TRAVIS_PULL_REQUEST_SLUG || REPO_SLUG;
const RANGE = process.env.TRAVIS_COMMIT_RANGE;
const IS_PR = process.env.TRAVIS_EVENT_TYPE === 'pull_request';
main().catch(err => {
console.log(err);
process.exit(1);
});
async function main() {
validate();
// Stash changes in working copy if needed
const pop = await stash();
// Make base and source available as dedicated remotes
await Promise.all([
() => fetch({name: 'base', url: `https://github.com/${REPO_SLUG}.git`}),
IS_PR
? () => fetch({name: 'source', url: `https://github.com/${PR_SLUG}.git`})
: async () => {}
]);
// Restore stashed changes if any
await pop();
// Lint all commits in TRAVIS_COMMIT_RANGE if available
if (IS_PR && RANGE) {
const [start, end] = RANGE.split('.').filter(Boolean);
await lint(['--from', start, '--to', end]);
} else {
const input = await log(COMMIT);
await lint([], {input});
}
}
async function git(args, options) {
return execa(GIT, args, Object.assign({}, {stdio: 'inherit'}, options));
}
async function fetch({name, url}) {
await git(['remote', 'add', name, url]);
await git(['fetch', name, '--quiet']);
}
async function isClean() {
const result = await git(['status', '--porcelain'], {
stdio: ['pipe', 'pipe', 'pipe']
});
return !(result.stdout && result.stdout.trim());
}
async function lint(args, options) {
return execa(
COMMITLINT || commitlint,
args,
Object.assign({}, {stdio: ['pipe', 'inherit', 'inherit']}, options)
);
}
async function log(hash) {
const result = await execa(GIT, [
'log',
'-n',
'1',
'--pretty=format:%B',
hash
]);
return result.stdout;
}
async function stash() {
if (await isClean()) {
return async () => {};
}
await git(['stash', '-k', '-u', '--quiet']);
return () => git(['stash', 'pop', '--quiet']);
}
function validate() {
if (process.env.CI !== 'true' || process.env.TRAVIS !== 'true') {
throw new Error(
`@commitlint/travis-cli is intended to be used on Travis CI`
);
}
const missing = REQUIRED.filter(envVar => !(envVar in process.env));
if (missing.length > 0) {
const stanza = missing.length > 1 ? 'they were not' : 'it was not';
throw new Error(
`Expected ${missing.join(', ')} to be defined globally, ${stanza}.`
);
}
}