UNPKG

6.23 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3
4var _regenerator = require('babel-runtime/regenerator');
5
6var _regenerator2 = _interopRequireDefault(_regenerator);
7
8var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
9
10var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
11
12// Assemble the engine
13var main = function () {
14 var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(options) {
15 var _this = this;
16
17 var raw, flags, fromStdin, input;
18 return _regenerator2.default.wrap(function _callee2$(_context2) {
19 while (1) {
20 switch (_context2.prev = _context2.next) {
21 case 0:
22 raw = options.input, flags = options.flags;
23 fromStdin = rules.fromStdin(raw, flags);
24
25 if (!fromStdin) {
26 _context2.next = 9;
27 break;
28 }
29
30 _context2.next = 5;
31 return (0, _getStdin2.default)();
32
33 case 5:
34 _context2.t1 = _context2.sent;
35 _context2.t0 = [_context2.t1];
36 _context2.next = 12;
37 break;
38
39 case 9:
40 _context2.next = 11;
41 return (0, _.getMessages)((0, _lodash.pick)(flags, 'edit', 'from', 'to'));
42
43 case 11:
44 _context2.t0 = _context2.sent;
45
46 case 12:
47 input = _context2.t0;
48 return _context2.abrupt('return', Promise.all(input.map(function () {
49 var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(commit) {
50 var fmt, seed, report, formatted, error;
51 return _regenerator2.default.wrap(function _callee$(_context) {
52 while (1) {
53 switch (_context.prev = _context.next) {
54 case 0:
55 fmt = new _chalk2.default.constructor({ enabled: flags.color });
56 seed = {};
57
58 if (flags.extends) {
59 seed.extends = flags.extends.split(',');
60 }
61
62 _context.t0 = _2.default;
63 _context.t1 = commit;
64 _context.next = 7;
65 return (0, _.getPreset)(flags.preset);
66
67 case 7:
68 _context.t2 = _context.sent;
69 _context.next = 10;
70 return (0, _.getConfiguration)('conventional-changelog-lint', {
71 prefix: 'conventional-changelog-lint-config'
72 }, seed);
73
74 case 10:
75 _context.t3 = _context.sent;
76 _context.t4 = {
77 preset: _context.t2,
78 configuration: _context.t3
79 };
80 _context.next = 14;
81 return (0, _context.t0)(_context.t1, _context.t4);
82
83 case 14:
84 report = _context.sent;
85 formatted = (0, _.format)(report, {
86 color: flags.color,
87 signs: [' ', '⚠', '✖'],
88 colors: ['white', 'yellow', 'red']
89 });
90
91
92 if (!flags.quiet) {
93 console.log(`${fmt.grey('⧗')} input: ${fmt.bold(commit.split('\n')[0])}`);
94 console.log(formatted.join('\n'));
95 }
96
97 if (!(report.errors.length > 0)) {
98 _context.next = 21;
99 break;
100 }
101
102 error = new Error(formatted[formatted.length - 1]);
103
104 error.type = _package2.default.name;
105 throw error;
106
107 case 21:
108 return _context.abrupt('return', console.log(''));
109
110 case 22:
111 case 'end':
112 return _context.stop();
113 }
114 }
115 }, _callee, _this);
116 }));
117
118 return function (_x2) {
119 return _ref2.apply(this, arguments);
120 };
121 }())));
122
123 case 14:
124 case 'end':
125 return _context2.stop();
126 }
127 }
128 }, _callee2, this);
129 }));
130
131 return function main(_x) {
132 return _ref.apply(this, arguments);
133 };
134}();
135
136// Start the engine
137
138
139require('babel-polyfill');
140
141var _chalk = require('chalk');
142
143var _chalk2 = _interopRequireDefault(_chalk);
144
145var _meow = require('meow');
146
147var _meow2 = _interopRequireDefault(_meow);
148
149var _lodash = require('lodash');
150
151var _getStdin = require('get-stdin');
152
153var _getStdin2 = _interopRequireDefault(_getStdin);
154
155var _help = require('./help');
156
157var _help2 = _interopRequireDefault(_help);
158
159var _ = require('./');
160
161var _2 = _interopRequireDefault(_);
162
163var _package = require('../package');
164
165var _package2 = _interopRequireDefault(_package);
166
167function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
168
169/**
170 * Behavioural rules
171 */
172
173
174// local modules
175
176
177// npm modules
178var rules = {
179 fromStdin: function fromStdin(input, flags) {
180 return input.length === 0 && flags.from === null && flags.to === null && !flags.edit;
181 }
182};
183// polyfills
184
185
186var configuration = {
187 // flags of string type
188 string: ['from', 'to', 'preset', 'extends'],
189 // flags of array type
190 // flags of bool type
191 boolean: ['edit', 'help', 'version', 'quiet', 'color'],
192 // flag aliases
193 alias: {
194 c: 'color',
195 e: 'edit',
196 f: 'from',
197 p: 'preset',
198 t: 'to',
199 q: 'quiet',
200 h: 'help',
201 v: 'version',
202 x: 'extends'
203 },
204 description: {
205 color: 'toggle formatted output',
206 edit: 'read last commit message found in ./git/COMMIT_EDITMSG',
207 extends: 'array of shareable configurations to extend',
208 from: 'lower end of the commit range to lint; applies if edit=false',
209 preset: 'conventional-changelog-preset to use for commit message parsing',
210 to: 'upper end of the commit range to lint; applies if edit=false',
211 quiet: 'toggle console output'
212 },
213 // flag defaults
214 default: {
215 color: true,
216 edit: false,
217 from: null,
218 preset: 'angular',
219 to: null,
220 quiet: false
221 },
222 // fail on unknown
223 unknown(arg) {
224 throw new Error(`unknown flags: ${arg}`);
225 }
226};
227
228// Init meow 😸cli
229var cli = (0, _meow2.default)({
230 help: `[input] reads from stdin if --edit, --from, --to are omitted\n${(0, _help2.default)(configuration)}`,
231 description: `${_package2.default.name}@${_package2.default.version} - ${_package2.default.description}`
232}, configuration);main(cli).catch(function (error) {
233 return setTimeout(function () {
234 if (error.type === _package2.default.name) {
235 process.exit(1);
236 }
237 throw error;
238 });
239});
240
241// Catch unhandled rejections globally
242process.on('unhandledRejection', function (reason, promise) {
243 console.log('Unhandled Rejection at: Promise ', promise, ' reason: ', reason);
244 throw reason;
245});
\No newline at end of file