1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | const color = require("colors/safe");
|
9 | const fs = require("fs");
|
10 | const handlebars = require("handlebars");
|
11 | const moment = require("moment");
|
12 | const path = require("path");
|
13 | const wordwrap = require("wordwrap");
|
14 | const S = require("sanctuary");
|
15 | const testing = process.env.NODE_ENV === 'testing';
|
16 | if (testing || process.env.COLOR === 'false') {
|
17 | color.disable();
|
18 | }
|
19 | exports.colors = color;
|
20 | const wrap = wordwrap.hard(0, 80);
|
21 | function stripHandlebarsNewLine(str) {
|
22 | return str.replace(/[\s\t\r\n](\{\{[#\/])/g, '$1');
|
23 | }
|
24 | function debug(...args) {
|
25 | if (!process.env.GH_VERBOSE) {
|
26 | return;
|
27 | }
|
28 | if (typeof args[0] === 'string') {
|
29 | args[0] = `DEBUG: ${args[0]}`;
|
30 | console.log(...args);
|
31 | return;
|
32 | }
|
33 | console.log('DEBUG:', ...args);
|
34 | }
|
35 | exports.debug = debug;
|
36 | function insane(...args) {
|
37 | if (!process.env.GH_VERBOSE_INSANE) {
|
38 | return;
|
39 | }
|
40 | console.log(...args);
|
41 | }
|
42 | exports.insane = insane;
|
43 | exports.exitWithError = S.curry2((message, stack) => {
|
44 | if (process.env.GH_VERBOSE || process.env.GH_VERBOSE_INSANE) {
|
45 | console.error(`${message}\n${stack}`);
|
46 | }
|
47 | else {
|
48 | console.error(message);
|
49 | }
|
50 | process.exit(1);
|
51 | });
|
52 | function error(...args) {
|
53 | if (typeof args[0] === 'string') {
|
54 | args[0] = `fatal: ${args[0]}`;
|
55 | }
|
56 | console.error(...args);
|
57 | process.exit(1);
|
58 | }
|
59 | exports.error = error;
|
60 | function warn(...args) {
|
61 | args[0] = `warning: ${args[0]}`;
|
62 | console.error(...args);
|
63 | }
|
64 | exports.warn = warn;
|
65 | function log(...args) {
|
66 | console.log(...args);
|
67 | }
|
68 | exports.log = log;
|
69 | function getDuration(start, formatter) {
|
70 | if (testing) {
|
71 | return moment(start).from(new Date('December 17, 2013'));
|
72 | }
|
73 | if (formatter) {
|
74 | return moment(start).format(formatter);
|
75 | }
|
76 | return moment(start).fromNow();
|
77 | }
|
78 | exports.getDuration = getDuration;
|
79 | function applyReplacements(output, replaceMap) {
|
80 | var regexPattern;
|
81 | for (regexPattern in replaceMap) {
|
82 | if (replaceMap.hasOwnProperty(regexPattern)) {
|
83 | output = output.replace(new RegExp(regexPattern, 'g'), replaceMap[regexPattern]);
|
84 | }
|
85 | }
|
86 | return output;
|
87 | }
|
88 | exports.applyReplacements = applyReplacements;
|
89 | function getErrorMessage(err) {
|
90 | var msg;
|
91 |
|
92 | if (!err) {
|
93 | return 'No error message.';
|
94 | }
|
95 | if (err.errors) {
|
96 | return err.errors;
|
97 | }
|
98 |
|
99 | if (!err.message) {
|
100 | return err;
|
101 | }
|
102 | try {
|
103 | msg = JSON.parse(err.message);
|
104 | }
|
105 | catch (e) {
|
106 | return err.message;
|
107 | }
|
108 | if (typeof msg === 'string') {
|
109 | return msg;
|
110 | }
|
111 | if (msg.errors && msg.errors[0] && msg.errors[0].message) {
|
112 | return msg.errors[0].message;
|
113 | }
|
114 | if (msg.message) {
|
115 | return msg.message;
|
116 | }
|
117 |
|
118 | return err.message.replace('Command failed: fatal: ', '').trim();
|
119 | }
|
120 | exports.getErrorMessage = getErrorMessage;
|
121 | function compileTemplate(source, map) {
|
122 | const template = handlebars.compile(source);
|
123 | const replacements = applyReplacements(template(map));
|
124 | return replacements;
|
125 | }
|
126 | exports.compileTemplate = compileTemplate;
|
127 | function logTemplate(source, map) {
|
128 | console.log(compileTemplate(source, map || {}));
|
129 | }
|
130 | exports.logTemplate = logTemplate;
|
131 | function logTemplateFile(file, map) {
|
132 | let templatePath;
|
133 | let source;
|
134 | templatePath = path.join(file);
|
135 | if (!fs.existsSync(templatePath)) {
|
136 | templatePath = path.join(__dirname, 'cmds/templates', file);
|
137 | }
|
138 | source = fs.readFileSync(templatePath).toString();
|
139 | logTemplate(stripHandlebarsNewLine(source), map);
|
140 | }
|
141 | exports.logTemplateFile = logTemplateFile;
|
142 | function registerHelper(name, callback) {
|
143 | handlebars.registerHelper(name, callback);
|
144 | }
|
145 | exports.registerHelper = registerHelper;
|
146 | function registerHelpers_() {
|
147 | handlebars.registerHelper('date', date => {
|
148 | return getDuration(date);
|
149 | });
|
150 | handlebars.registerHelper('compareLink', function () {
|
151 | const { github_host, user, repo, pullHeadSHA, currentSHA } = this.options;
|
152 | return `${github_host}/${user}/${repo}/compare/${pullHeadSHA}...${currentSHA}`;
|
153 | });
|
154 | handlebars.registerHelper('forwardedLink', function () {
|
155 | const { github_host, fwd, repo, forwardedPull } = this.options;
|
156 | return `${github_host}/${fwd}/${repo}/pull/${forwardedPull}`;
|
157 | });
|
158 | handlebars.registerHelper('link', function () {
|
159 | const { github_host, user, repo, number } = this.options;
|
160 | return `${github_host}/${user}/${repo}/pull/${number}`;
|
161 | });
|
162 | handlebars.registerHelper('submittedLink', function () {
|
163 | const { github_host, submit, repo, submittedPull } = this.options;
|
164 | return `${github_host}/${submit}/${repo}/pull/${submittedPull}`;
|
165 | });
|
166 | handlebars.registerHelper('issueLink', function () {
|
167 | const { github_host, user, repo, number } = this.options;
|
168 | return `${github_host}/${user}/${repo}/issues/${number}`;
|
169 | });
|
170 | handlebars.registerHelper('gistLink', function () {
|
171 | const { github_gist_host, loggedUser, id } = this.options;
|
172 | return `${github_gist_host}/${loggedUser}/${id}`;
|
173 | });
|
174 | handlebars.registerHelper('repoLink', function () {
|
175 | const { github_gist_host, user, repo } = this.options;
|
176 | return `${github_gist_host}/${user}/${repo}`;
|
177 | });
|
178 | handlebars.registerHelper('wordwrap', (text, padding, stripNewLines) => {
|
179 | let gutter = '';
|
180 | if (stripNewLines !== false) {
|
181 | text = text.replace(/[\r\n\s\t]+/g, ' ');
|
182 | }
|
183 | text = wrap(text).split('\n');
|
184 | if (padding > 0) {
|
185 | gutter = ' '.repeat(padding);
|
186 | }
|
187 | return text.join(`\n${gutter}`);
|
188 | });
|
189 | }
|
190 | exports.registerHelpers_ = registerHelpers_;
|
191 | registerHelpers_();
|
192 |
|
\ | No newline at end of file |