Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@parcel/logger

Package Overview
Dependencies
Maintainers
1
Versions
888
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@parcel/logger - npm Package Compare versions

Comparing version 1.11.0 to 2.0.0-alpha.1

lib/emoji.js

28

package.json
{
"name": "@parcel/logger",
"version": "1.11.0",
"version": "2.0.0-alpha.1",
"description": "Blazing fast, zero configuration web application bundler",
"main": "index.js",
"main": "lib/Logger.js",
"license": "MIT",

@@ -12,3 +12,3 @@ "repository": {

"engines": {
"node": ">= 6.0.0"
"node": ">= 8.0.0"
},

@@ -19,22 +19,8 @@ "publishConfig": {

"scripts": {
"test": "cross-env NODE_ENV=test mocha",
"test-ci": "yarn build && yarn test",
"format": "prettier --write \"./{src,bin,test}/**/*.{js,json,md}\"",
"lint": "eslint . && prettier \"./{src,bin,test}/**/*.{js,json,md}\" --list-different",
"build": "babel src -d lib",
"prepublish": "yarn build"
"test-ci": "yarn test"
},
"dependencies": {
"@parcel/workers": "^1.11.0",
"chalk": "^2.1.0",
"grapheme-breaker": "^0.3.2",
"ora": "^2.1.0",
"strip-ansi": "^4.0.0"
"@parcel/events": "^2.0.0-alpha.1"
},
"devDependencies": {
"@parcel/babel-register": "^1.11.0",
"mocha": "^5.2.0",
"sinon": "^5.0.1"
},
"gitHead": "34eb91e8e6991073e594bff731c333d09b0403b5"
}
"gitHead": "11d21d56b97f1b6ae3cd3671ccbb39adf80c438f"
}

@@ -1,248 +0,102 @@

const chalk = require('chalk');
const readline = require('readline');
const prettyError = require('./prettyError');
const emoji = require('./emoji');
const {countBreaks} = require('grapheme-breaker');
const stripAnsi = require('strip-ansi');
const ora = require('ora');
const WorkerFarm = require('@parcel/workers');
const path = require('path');
const fs = require('fs');
// @flow strict-local
class Logger {
constructor(options) {
this.lines = 0;
this.spinner = null;
this.setOptions(options);
}
import type {IDisposable, LogEvent} from '@parcel/types';
setOptions(options) {
this.logLevel =
options && isNaN(options.logLevel) === false
? Number(options.logLevel)
: 3;
this.color =
options && typeof options.color === 'boolean'
? options.color
: chalk.supportsColor;
this.emoji = (options && options.emoji) || emoji;
this.chalk = new chalk.constructor({enabled: this.color});
this.isTest =
options && typeof options.isTest === 'boolean'
? options.isTest
: process.env.NODE_ENV === 'test';
}
import {ValueEmitter} from '@parcel/events';
import {inspect} from 'util';
countLines(message) {
return stripAnsi(message)
.split('\n')
.reduce((p, line) => {
if (process.stdout.columns) {
return p + Math.ceil((line.length || 1) / process.stdout.columns);
}
class Logger {
#logEmitter = new ValueEmitter<LogEvent>();
return p + 1;
}, 0);
onLog(cb: (event: LogEvent) => mixed): IDisposable {
return this.#logEmitter.addListener(cb);
}
writeRaw(message) {
this.stopSpinner();
this.lines += this.countLines(message) - 1;
process.stdout.write(message);
verbose(message: string): void {
this.#logEmitter.emit({
type: 'log',
level: 'verbose',
message
});
}
write(message, persistent = false) {
if (this.logLevel > 3) {
return this.verbose(message);
}
if (!persistent) {
this.lines += this.countLines(message);
}
this.stopSpinner();
this._log(message);
info(message: string): void {
this.log(message);
}
verbose(message) {
if (this.logLevel < 4) {
return;
}
let currDate = new Date();
message = `[${currDate.toLocaleTimeString()}]: ${message}`;
if (this.logLevel > 4) {
if (!this.logFile) {
this.logFile = fs.createWriteStream(
path.join(process.cwd(), `parcel-debug-${currDate.toISOString()}.log`)
);
}
this.logFile.write(stripAnsi(message) + '\n');
}
this._log(message);
log(message: string): void {
this.#logEmitter.emit({
type: 'log',
level: 'info',
message
});
}
log(message) {
if (this.logLevel < 3) {
return;
}
this.write(message);
warn(err: Error | string): void {
this.#logEmitter.emit({
type: 'log',
level: 'warn',
message: err
});
}
persistent(message) {
if (this.logLevel < 3) {
return;
}
this.write(this.chalk.bold(message), true);
error(err: Error | string): void {
this.#logEmitter.emit({
type: 'log',
level: 'error',
message: err
});
}
warn(err) {
if (this.logLevel < 2) {
return;
}
this._writeError(err, this.emoji.warning, this.chalk.yellow);
progress(message: string): void {
this.#logEmitter.emit({
type: 'log',
level: 'progress',
message
});
}
}
error(err) {
if (this.logLevel < 1) {
return;
}
const logger = new Logger();
export default logger;
this._writeError(err, this.emoji.error, this.chalk.red.bold);
}
let consolePatched = false;
success(message) {
this.log(`${this.emoji.success} ${this.chalk.green.bold(message)}`);
// Patch `console` APIs within workers to forward their messages to the Logger
// at the appropriate levels.
// TODO: Implement the rest of the console api as needed.
// TODO: Does this need to be disposable/reversible?
export function patchConsole() {
if (consolePatched) {
return;
}
formatError(err, opts) {
return prettyError(err, opts);
}
/* eslint-disable no-console */
// $FlowFixMe
console.log = console.info = (...messages: Array<mixed>) => {
logger.info(joinLogMessages(messages));
};
_writeError(err, emoji, color) {
let {message, stack} = this.formatError(err, {color: this.color});
this.write(color(`${emoji} ${message}`));
if (stack) {
this.write(stack);
}
}
// $FlowFixMe
console.debug = (...messages: Array<mixed>) => {
// TODO: dedicated debug level?
logger.verbose(joinLogMessages(messages));
};
clear() {
if (!this.color || this.isTest || this.logLevel > 3) {
return;
}
// $FlowFixMe
console.warn = (...messages: Array<mixed>) => {
logger.warn(joinLogMessages(messages));
};
while (this.lines > 0) {
readline.clearLine(process.stdout, 0);
readline.moveCursor(process.stdout, 0, -1);
this.lines--;
}
// $FlowFixMe
console.error = (...messages: Array<mixed>) => {
logger.error(joinLogMessages(messages));
};
readline.cursorTo(process.stdout, 0);
this.stopSpinner();
}
progress(message) {
if (this.logLevel < 3) {
return;
}
if (this.logLevel > 3) {
return this.verbose(message);
}
let styledMessage = this.chalk.gray.bold(message);
if (!this.spinner) {
this.spinner = ora({
text: styledMessage,
stream: process.stdout,
enabled: this.isTest ? false : undefined // fall back to ora default unless we need to explicitly disable it.
}).start();
} else {
this.spinner.text = styledMessage;
}
}
stopSpinner() {
if (this.spinner) {
this.spinner.stop();
this.spinner = null;
}
}
handleMessage(options) {
this[options.method](...options.args);
}
_log(message) {
// eslint-disable-next-line no-console
console.log(message);
}
table(columns, table) {
// Measure column widths
let colWidths = [];
for (let row of table) {
let i = 0;
for (let item of row) {
colWidths[i] = Math.max(colWidths[i] || 0, stringWidth(item));
i++;
}
}
// Render rows
for (let row of table) {
let items = row.map((item, i) => {
// Add padding between columns unless the alignment is the opposite to the
// next column and pad to the column width.
let padding =
!columns[i + 1] || columns[i + 1].align === columns[i].align ? 4 : 0;
return pad(item, colWidths[i] + padding, columns[i].align);
});
this.log(items.join(''));
}
}
/* eslint-enable no-console */
consolePatched = true;
}
// Pad a string with spaces on either side
function pad(text, length, align = 'left') {
let pad = ' '.repeat(length - stringWidth(text));
if (align === 'right') {
return pad + text;
}
return text + pad;
function joinLogMessages(messages: Array<mixed>): string {
return messages.map(m => (typeof m === 'string' ? m : inspect(m))).join(' ');
}
// Count visible characters in a string
function stringWidth(string) {
return countBreaks(stripAnsi('' + string));
}
// If we are in a worker, make a proxy class which will
// send the logger calls to the main process via IPC.
// These are handled in WorkerFarm and directed to handleMessage above.
if (WorkerFarm.isWorker()) {
class LoggerProxy {}
for (let method of Object.getOwnPropertyNames(Logger.prototype)) {
LoggerProxy.prototype[method] = (...args) => {
WorkerFarm.callMaster(
{
location: __filename,
method,
args
},
false
);
};
}
module.exports = new LoggerProxy();
} else {
module.exports = new Logger();
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc