1
1
#!/usr/bin/env node
2
- const sander = require ( '@marionebl/sander' ) ;
3
2
const execa = require ( 'execa' ) ;
4
- const findUp = require ( 'find-up ' ) ;
3
+ const commitlint = require ( '@commitlint/cli ' ) ;
5
4
6
5
// Allow to override used bins for testing purposes
7
6
const GIT = process . env . TRAVIS_COMMITLINT_GIT_BIN || 'git' ;
8
- const COMMITLINT =
9
- process . env . TRAVIS_COMMITLINT_BIN || require ( '@commitlint/cli' ) ; // eslint-disable-line import/newline-after-import
10
- const REQUIRED = [ 'TRAVIS_COMMIT' , 'TRAVIS_BRANCH' ] ;
7
+ const COMMITLINT = process . env . TRAVIS_COMMITLINT_BIN ;
11
8
12
- const TRAVIS_BRANCH = process . env . TRAVIS_BRANCH ;
13
- const TRAVIS_COMMIT = process . env . TRAVIS_COMMIT ;
9
+ const REQUIRED = [
10
+ 'TRAVIS_COMMIT' ,
11
+ 'TRAVIS_COMMIT_RANGE' ,
12
+ 'TRAVIS_REPO_SLUG' ,
13
+ 'TRAVIS_PULL_REQUEST_SLUG'
14
+ ] ;
15
+
16
+ const COMMIT = process . env . TRAVIS_COMMIT ;
17
+ const REPO_SLUG = process . env . TRAVIS_REPO_SLUG ;
18
+ const PR_SLUG = process . env . TRAVIS_PULL_REQUEST_SLUG || REPO_SLUG ;
19
+ const RANGE = process . env . TRAVIS_COMMIT_RANGE ;
14
20
15
21
main ( ) . catch ( err => {
16
22
console . log ( err ) ;
17
23
process . exit ( 1 ) ;
18
24
} ) ;
19
25
20
26
async function main ( ) {
21
- if ( process . env . CI !== 'true' || process . env . TRAVIS !== 'true' ) {
22
- throw new Error (
23
- `@commitlint/travis-cli is inteded to be used on Travis CI`
24
- ) ;
25
- }
26
-
27
- const gitRoot = await findUp ( '.git' ) ;
28
- const missing = REQUIRED . filter ( envVar => ! ( envVar in process . env ) ) ;
29
-
30
- if ( missing . length > 0 ) {
31
- const stanza = missing . length > 1 ? 'they were not' : 'it was not' ;
32
- throw new Error (
33
- `Expected ${ missing . join ( ', ' ) } to be defined globally, ${ stanza } .`
34
- ) ;
35
- }
27
+ validate ( ) ;
36
28
29
+ // Stash changes in working copy if needed
37
30
const pop = await stash ( ) ;
38
31
39
- await git ( [ 'remote' , 'set-branches' , 'origin' , TRAVIS_BRANCH ] ) ;
40
-
41
- if ( await sander . exists ( gitRoot , 'shallow' ) ) {
42
- await git ( [ 'fetch' , '--unshallow' , '--quiet' ] ) ;
43
- }
44
-
45
- await git ( [ 'checkout' , TRAVIS_BRANCH , '--quiet' ] ) ;
46
- await git ( [ 'checkout' , '-' , '--quiet' ] ) ;
32
+ // Make base and source available as dedicated remotes
33
+ await Promise . all ( [
34
+ ( ) => fetch ( { name : 'base' , url : `https://github.com/${ REPO_SLUG } .git` } ) ,
35
+ ( ) => fetch ( { name : 'source' , url : `https://github.com/${ PR_SLUG } .git` } )
36
+ ] ) ;
47
37
38
+ // Restore stashed changes if any
48
39
await pop ( ) ;
49
40
50
- await lint ( [ '--from' , TRAVIS_BRANCH , '--to' , TRAVIS_COMMIT ] ) ;
41
+ // Lint all commits in TRAVIS_COMMIT_RANGE if available
42
+ if ( RANGE ) {
43
+ const [ start , end ] = RANGE . split ( '.' ) . filter ( Boolean ) ;
44
+ await lint ( [ '--from' , start , '--to' , end ] ) ;
45
+ }
46
+
47
+ // Always lint the triggering commit indicated by TRAVIS_COMMIT
48
+ await lint ( [ '--from' , COMMIT ] ) ;
51
49
}
52
50
53
51
async function git ( args , options ) {
54
52
return execa ( GIT , args , Object . assign ( { } , { stdio : 'inherit' } , options ) ) ;
55
53
}
56
54
55
+ async function fetch ( { name, url} ) {
56
+ await git ( [ 'remote' , 'add' , name , url ] ) ;
57
+ await git ( [ 'fetch' , name , '--quiet' ] ) ;
58
+ }
59
+
57
60
async function isClean ( ) {
58
61
const result = await git ( [ 'status' , '--porcelain' ] , {
59
62
stdio : [ 'pipe' , 'pipe' , 'pipe' ]
@@ -63,7 +66,7 @@ async function isClean() {
63
66
64
67
async function lint ( args , options ) {
65
68
return execa (
66
- COMMITLINT ,
69
+ COMMITLINT || commitlint ,
67
70
args ,
68
71
Object . assign ( { } , { stdio : 'inherit' } , options )
69
72
) ;
@@ -73,6 +76,23 @@ async function stash() {
73
76
if ( await isClean ( ) ) {
74
77
return async ( ) => { } ;
75
78
}
76
- await git ( [ 'stash' ] ) ;
77
- return ( ) => git ( [ 'stash' , 'pop' ] ) ;
79
+ await git ( [ 'stash' , '-k' , '-u' , '--quiet' ] ) ;
80
+ return ( ) => git ( [ 'stash' , 'pop' , '--quiet' ] ) ;
81
+ }
82
+
83
+ function validate ( ) {
84
+ if ( process . env . CI !== 'true' || process . env . TRAVIS !== 'true' ) {
85
+ throw new Error (
86
+ `@commitlint/travis-cli is inteded to be used on Travis CI`
87
+ ) ;
88
+ }
89
+
90
+ const missing = REQUIRED . filter ( envVar => ! ( envVar in process . env ) ) ;
91
+
92
+ if ( missing . length > 0 ) {
93
+ const stanza = missing . length > 1 ? 'they were not' : 'it was not' ;
94
+ throw new Error (
95
+ `Expected ${ missing . join ( ', ' ) } to be defined globally, ${ stanza } .`
96
+ ) ;
97
+ }
78
98
}
0 commit comments