@percy/logger 1.6.3 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/logger.js +37 -18
  2. package/package.json +2 -2
package/dist/logger.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { colors } from './utils.js';
2
- const URL_REGEXP = /\bhttps?:\/\/[^\s/$.?#].[^\s]*\b/i;
2
+ const URL_REGEXP = /https?:\/\/[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:;%_+.~#?&//=[\]]*)/i;
3
3
  const LOG_LEVELS = {
4
4
  debug: 0,
5
5
  info: 1,
@@ -92,6 +92,8 @@ export class PercyLogger {
92
92
 
93
93
 
94
94
  format(debug, level, message, elapsed) {
95
+ let color = (n, m) => this.isTTY ? colors[n](m) : m;
96
+
95
97
  let label = 'percy';
96
98
  let suffix = '';
97
99
 
@@ -108,24 +110,30 @@ export class PercyLogger {
108
110
  if (debug) label += `:${debug}`; // include elapsed time since last log
109
111
 
110
112
  if (elapsed != null) {
111
- suffix = ' ' + colors.grey(`(${elapsed}ms)`);
113
+ suffix = ' ' + color('grey', `(${elapsed}ms)`);
112
114
  }
113
- }
115
+ } // add colors
116
+
114
117
 
115
- label = colors.magenta(label);
118
+ label = color('magenta', label);
116
119
 
117
120
  if (level === 'error') {
118
121
  // red errors
119
- message = colors.red(message);
122
+ message = color('red', message);
120
123
  } else if (level === 'warn') {
121
124
  // yellow warnings
122
- message = colors.yellow(message);
125
+ message = color('yellow', message);
123
126
  } else if (level === 'info' || level === 'debug') {
124
127
  // blue info and debug URLs
125
- message = message.replace(URL_REGEXP, colors.blue('$&'));
128
+ message = message.replace(URL_REGEXP, color('blue', '$&'));
126
129
  }
127
130
 
128
131
  return `[${label}] ${message}${suffix}`;
132
+ } // True if stdout is a TTY interface
133
+
134
+
135
+ get isTTY() {
136
+ return !!this.constructor.stdout.isTTY;
129
137
  } // Replaces the current line with a log message
130
138
 
131
139
 
@@ -135,11 +143,11 @@ export class PercyLogger {
135
143
  stdout
136
144
  } = this.constructor;
137
145
 
138
- if (stdout.isTTY || !this._progress) {
146
+ if (this.isTTY || !this._progress) {
139
147
  message && (message = this.format(debug, message));
140
- if (stdout.isTTY) stdout.cursorTo(0);else message && (message = message + '\n');
148
+ if (this.isTTY) stdout.cursorTo(0);else message && (message = message + '\n');
141
149
  if (message) stdout.write(message);
142
- if (stdout.isTTY) stdout.clearLine(1);
150
+ if (this.isTTY) stdout.clearLine(1);
143
151
  }
144
152
 
145
153
  this._progress = !!message && {
@@ -174,34 +182,45 @@ export class PercyLogger {
174
182
  level,
175
183
  message,
176
184
  meta,
177
- timestamp
185
+ timestamp,
186
+ error: !!err
178
187
  };
179
188
  this.messages.add(entry); // maybe write the message to stdio
180
189
 
181
190
  if (this.shouldLog(debug, level)) {
191
+ // unless the loglevel is debug, write shorter error messages
182
192
  if (err && this.level !== 'debug') message = err;
183
- let elapsed = timestamp - (this.lastlog || timestamp);
184
- this.write(level, this.format(debug, err ? 'error' : level, message, elapsed));
193
+ this.write({ ...entry,
194
+ message
195
+ });
185
196
  this.lastlog = timestamp;
186
197
  }
187
- } // Writes a message to stdio based on the loglevel
198
+ } // Writes a log entry to stdio based on the loglevel
188
199
 
189
200
 
190
- write(level, message) {
201
+ write({
202
+ debug,
203
+ level,
204
+ message,
205
+ timestamp,
206
+ error
207
+ }) {
191
208
  var _this$_progress;
192
209
 
210
+ let elapsed = timestamp - (this.lastlog || timestamp);
211
+ let msg = this.format(debug, error ? 'error' : level, message, elapsed);
212
+ let progress = this.isTTY && this._progress;
193
213
  let {
194
214
  stdout,
195
215
  stderr
196
- } = this.constructor;
197
- let progress = stdout.isTTY && this._progress;
216
+ } = this.constructor; // clear any logged progress
198
217
 
199
218
  if (progress) {
200
219
  stdout.cursorTo(0);
201
220
  stdout.clearLine(0);
202
221
  }
203
222
 
204
- (level === 'info' ? stdout : stderr).write(message + '\n');
223
+ (level === 'info' ? stdout : stderr).write(msg + '\n');
205
224
  if (!((_this$_progress = this._progress) !== null && _this$_progress !== void 0 && _this$_progress.persist)) delete this._progress;else if (progress) stdout.write(progress.message);
206
225
  }
207
226
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/logger",
3
- "version": "1.6.3",
3
+ "version": "1.7.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,5 +31,5 @@
31
31
  "test": "node ../../scripts/test",
32
32
  "test:coverage": "yarn test --coverage"
33
33
  },
34
- "gitHead": "ab48a150317a4af36d536a5671701532c669d2d1"
34
+ "gitHead": "012892cdaf7a07aa1b5aa355017639cee0e8a19e"
35
35
  }