UNPKG

6.01 kBJavaScriptView Raw
1"use strict";
2/**
3 * © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
4 * (see file: README.md)
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8const color = require("colors/safe");
9const fs = require("fs");
10const handlebars = require("handlebars");
11const moment = require("moment");
12const path = require("path");
13const wordwrap = require("wordwrap");
14const S = require("sanctuary");
15const testing = process.env.NODE_ENV === 'testing';
16if (testing || process.env.COLOR === 'false') {
17 color.disable();
18}
19exports.colors = color;
20const wrap = wordwrap.hard(0, 80);
21function stripHandlebarsNewLine(str) {
22 return str.replace(/[\s\t\r\n](\{\{[#\/])/g, '$1');
23}
24function 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}
35exports.debug = debug;
36function insane(...args) {
37 if (!process.env.GH_VERBOSE_INSANE) {
38 return;
39 }
40 console.log(...args);
41}
42exports.insane = insane;
43exports.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});
52function error(...args) {
53 if (typeof args[0] === 'string') {
54 args[0] = `fatal: ${args[0]}`;
55 }
56 console.error(...args);
57 process.exit(1);
58}
59exports.error = error;
60function warn(...args) {
61 args[0] = `warning: ${args[0]}`;
62 console.error(...args);
63}
64exports.warn = warn;
65function log(...args) {
66 console.log(...args);
67}
68exports.log = log;
69function 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}
78exports.getDuration = getDuration;
79function 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}
88exports.applyReplacements = applyReplacements;
89function getErrorMessage(err) {
90 var msg;
91 // General normalizer
92 if (!err) {
93 return 'No error message.';
94 }
95 if (err.errors) {
96 return err.errors;
97 }
98 // Normalize github api error
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 // Normalize git error
118 return err.message.replace('Command failed: fatal: ', '').trim();
119}
120exports.getErrorMessage = getErrorMessage;
121function compileTemplate(source, map) {
122 const template = handlebars.compile(source);
123 const replacements = applyReplacements(template(map));
124 return replacements;
125}
126exports.compileTemplate = compileTemplate;
127function logTemplate(source, map) {
128 console.log(compileTemplate(source, map || {}));
129}
130exports.logTemplate = logTemplate;
131function 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}
141exports.logTemplateFile = logTemplateFile;
142function registerHelper(name, callback) {
143 handlebars.registerHelper(name, callback);
144}
145exports.registerHelper = registerHelper;
146function 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}
190exports.registerHelpers_ = registerHelpers_;
191registerHelpers_();
192//# sourceMappingURL=logger.js.map
\No newline at end of file