UNPKG

5.9 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3
4var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
5
6var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
7
8function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
10const execa = require('execa');
11const commitlint = require('@commitlint/cli');
12
13// Allow to override used bins for testing purposes
14const GIT = process.env.TRAVIS_COMMITLINT_GIT_BIN || 'git';
15const COMMITLINT = process.env.TRAVIS_COMMITLINT_BIN;
16
17const REQUIRED = ['TRAVIS_COMMIT', 'TRAVIS_COMMIT_RANGE', 'TRAVIS_EVENT_TYPE', 'TRAVIS_REPO_SLUG', 'TRAVIS_PULL_REQUEST_SLUG'];
18
19const COMMIT = process.env.TRAVIS_COMMIT;
20const REPO_SLUG = process.env.TRAVIS_REPO_SLUG;
21const PR_SLUG = process.env.TRAVIS_PULL_REQUEST_SLUG || REPO_SLUG;
22const RANGE = process.env.TRAVIS_COMMIT_RANGE;
23const IS_PR = process.env.TRAVIS_EVENT_TYPE === 'pull_request';
24
25main().catch(err => {
26 console.log(err);
27 process.exit(1);
28});
29
30function main() {
31 return new Promise(function ($return, $error) {
32 var pop, _RANGE$split$filter, _RANGE$split$filter2, start, end, input;
33
34 validate();
35
36 // Stash changes in working copy if needed
37 return Promise.resolve(stash()).then(function ($await_2) {
38 try {
39 pop = $await_2;
40 return Promise.resolve(Promise.all([() => fetch({ name: 'base', url: `https://github.com/${REPO_SLUG}.git` }), IS_PR ? () => fetch({ name: 'source', url: `https://github.com/${PR_SLUG}.git` }) : () => new Promise(function ($return, $error) {
41 return $return();
42 }.bind(this))])).then(function ($await_3) {
43 try {
44 return Promise.resolve(pop()).then(function ($await_4) {
45 try {
46
47 // Lint all commits in TRAVIS_COMMIT_RANGE if available
48 if (IS_PR && RANGE) {
49 _RANGE$split$filter = RANGE.split('.').filter(Boolean), _RANGE$split$filter2 = (0, _slicedToArray3.default)(_RANGE$split$filter, 2);
50 start = _RANGE$split$filter2[0], end = _RANGE$split$filter2[1];
51 return Promise.resolve(lint(['--from', start, '--to', end])).then(function ($await_5) {
52 try {
53 return $If_1.call(this);
54 } catch ($boundEx) {
55 return $error($boundEx);
56 }
57 }.bind(this), $error);
58 } else {
59 return Promise.resolve(log(COMMIT)).then(function ($await_6) {
60 try {
61 input = $await_6;
62 return Promise.resolve(lint([], { input })).then(function ($await_7) {
63 try {
64 return $If_1.call(this);
65 } catch ($boundEx) {
66 return $error($boundEx);
67 }
68 }.bind(this), $error);
69 } catch ($boundEx) {
70 return $error($boundEx);
71 }
72 }.bind(this), $error);
73 }
74
75 function $If_1() {
76 return $return();
77 }
78 } catch ($boundEx) {
79 return $error($boundEx);
80 }
81 }.bind(this), $error);
82 } catch ($boundEx) {
83 return $error($boundEx);
84 }
85 }.bind(this), $error);
86 } catch ($boundEx) {
87 return $error($boundEx);
88 }
89 }.bind(this), $error);
90 }.bind(this));
91}
92
93function git(args, options) {
94 return new Promise(function ($return, $error) {
95 return $return(execa(GIT, args, Object.assign({}, { stdio: 'inherit' }, options)));
96 }.bind(this));
97}
98
99function fetch({ name, url }) {
100 return new Promise(function ($return, $error) {
101 return Promise.resolve(git(['remote', 'add', name, url])).then(function ($await_8) {
102 try {
103 return Promise.resolve(git(['fetch', name, '--quiet'])).then(function ($await_9) {
104 try {
105 return $return();
106 } catch ($boundEx) {
107 return $error($boundEx);
108 }
109 }.bind(this), $error);
110 } catch ($boundEx) {
111 return $error($boundEx);
112 }
113 }.bind(this), $error);
114 }.bind(this));
115}
116
117function isClean() {
118 return new Promise(function ($return, $error) {
119 var result;
120 return Promise.resolve(git(['status', '--porcelain'], {
121 stdio: ['pipe', 'pipe', 'pipe']
122 })).then(function ($await_10) {
123 try {
124 result = $await_10;
125
126 return $return(!(result.stdout && result.stdout.trim()));
127 } catch ($boundEx) {
128 return $error($boundEx);
129 }
130 }.bind(this), $error);
131 }.bind(this));
132}
133
134function lint(args, options) {
135 return new Promise(function ($return, $error) {
136 return $return(execa(COMMITLINT || commitlint, args, Object.assign({}, { stdio: ['pipe', 'inherit', 'inherit'] }, options)));
137 }.bind(this));
138}
139
140function log(hash) {
141 return new Promise(function ($return, $error) {
142 var result;
143 return Promise.resolve(execa('git', ['log', '-n', '1', '--pretty=format:%B', hash])).then(function ($await_11) {
144 try {
145 result = $await_11;
146
147 return $return(result.stdout);
148 } catch ($boundEx) {
149 return $error($boundEx);
150 }
151 }.bind(this), $error);
152 }.bind(this));
153}
154
155function stash() {
156 return new Promise(function ($return, $error) {
157 return Promise.resolve(isClean()).then(function ($await_12) {
158 try {
159 if ($await_12) {
160 return $return(() => new Promise(function ($return, $error) {
161 return $return();
162 }.bind(this)));
163 }
164 return Promise.resolve(git(['stash', '-k', '-u', '--quiet'])).then(function ($await_13) {
165 try {
166 return $return(() => git(['stash', 'pop', '--quiet']));
167 } catch ($boundEx) {
168 return $error($boundEx);
169 }
170 }.bind(this), $error);
171 } catch ($boundEx) {
172 return $error($boundEx);
173 }
174 }.bind(this), $error);
175 }.bind(this));
176}
177
178function validate() {
179 if (process.env.CI !== 'true' || process.env.TRAVIS !== 'true') {
180 throw new Error(`@commitlint/travis-cli is intended to be used on Travis CI`);
181 }
182
183 const missing = REQUIRED.filter(envVar => !(envVar in process.env));
184
185 if (missing.length > 0) {
186 const stanza = missing.length > 1 ? 'they were not' : 'it was not';
187 throw new Error(`Expected ${missing.join(', ')} to be defined globally, ${stanza}.`);
188 }
189}
190//# sourceMappingURL=cli.js.map
\No newline at end of file