Socket
Socket
Sign inDemoInstall

metro-core

Package Overview
Dependencies
Maintainers
4
Versions
155
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

metro-core - npm Package Compare versions

Comparing version 0.24.1 to 0.24.2

src/Logger/__tests__/Logger-test.js

2

package.json
{
"version": "0.24.1",
"version": "0.24.2",
"name": "metro-core",

@@ -4,0 +4,0 @@ "description": "🚇 Core files for Metro",

@@ -26,28 +26,28 @@ /**

/**
* Prints a banner with a border around it containing the given message. The
* following options are supported:
*
* type Options = {
* // A function to apply to each line of text to decorate it
* chalkFunction: (string: message) => string;
* // The total width (max line length) of the banner, including margin and
* // padding (default = 80)
* width: number;
* // How much leading space to prepend to each line (default = 0)
* marginLeft: number;
* // How much trailing space to append to each line (default = 0)
* marginRight: number;
* // Space between the top banner border and the text (default = 0)
* paddingTop: number;
* // Space between the bottom banner border and the text (default = 0)
* paddingBottom: number;
* // Space between the left banner border and the text (default = 2)
* paddingLeft: number;
* // Space between the right banner border and the text (default = 2)
* paddingRight: number;
* };
*
* @PrettierFixMe can't use comment-style flow syntax because prettier strips it
* https://github.com/prettier/prettier/issues/204
*/
* Prints a banner with a border around it containing the given message. The
* following options are supported:
*
* type Options = {
* // A function to apply to each line of text to decorate it
* chalkFunction: (string: message) => string;
* // The total width (max line length) of the banner, including margin and
* // padding (default = 80)
* width: number;
* // How much leading space to prepend to each line (default = 0)
* marginLeft: number;
* // How much trailing space to append to each line (default = 0)
* marginRight: number;
* // Space between the top banner border and the text (default = 0)
* paddingTop: number;
* // Space between the bottom banner border and the text (default = 0)
* paddingBottom: number;
* // Space between the left banner border and the text (default = 2)
* paddingLeft: number;
* // Space between the right banner border and the text (default = 2)
* paddingRight: number;
* };
*
* @PrettierFixMe can't use comment-style flow syntax because prettier strips it
* https://github.com/prettier/prettier/issues/204
*/
function formatBanner(message, options) {

@@ -63,5 +63,5 @@ options = options || {};

paddingLeft: 2,
paddingRight: 2 });
paddingRight: 2,
});
var width = options.width;

@@ -84,6 +84,6 @@ var marginLeft = options.marginLeft;

var bodyLines = _.flattenDeep([
arrayOf('', paddingTop),
body.split('\n'),
arrayOf('', paddingBottom)]).
map(function (line) {
arrayOf('', paddingTop),
body.split('\n'),
arrayOf('', paddingBottom),
]).map(function(line) {
var padding = spaces(Math.max(0, maxLineWidth - line.length));

@@ -94,17 +94,17 @@ return left + options.chalkFunction(line) + padding + right;

var horizontalBorderLine = repeatString(
HORIZONTAL_LINE,
width - marginLeft - marginRight - 2);
HORIZONTAL_LINE,
width - marginLeft - marginRight - 2,
);
var top =
spaces(marginLeft) +
TOP_LEFT +
horizontalBorderLine +
TOP_RIGHT +
spaces(marginRight);
spaces(marginLeft) +
TOP_LEFT +
horizontalBorderLine +
TOP_RIGHT +
spaces(marginRight);
var bottom =
spaces(marginLeft) +
BOTTOM_LEFT +
horizontalBorderLine +
BOTTOM_RIGHT +
spaces(marginRight);
spaces(marginLeft) +
BOTTOM_LEFT +
horizontalBorderLine +
BOTTOM_RIGHT +
spaces(marginRight);
return _.flattenDeep([top, bodyLines, bottom]).join('\n');

@@ -122,3 +122,3 @@ }

function arrayOf(value, number) {
return _.range(number).map(function () {
return _.range(number).map(function() {
return value;

@@ -128,2 +128,2 @@ });

module.exports = formatBanner;
module.exports = formatBanner;

@@ -9,3 +9,3 @@ /**

*
*
* @flow
* @format

@@ -18,2 +18,2 @@ */

module.exports = formatBanner;
module.exports = formatBanner;

@@ -9,3 +9,3 @@ /**

*
*
* @flow
* @format

@@ -24,2 +24,3 @@ */

Terminal,
formatBanner };
formatBanner,
};

@@ -17,11 +17,11 @@ /**

return {};
} };
},
};
};
mockColor.bold = function () {
mockColor.bold = function() {
return {};
};
mockColor.bgRed = function () {
mockColor.bgRed = function() {
return {};

@@ -41,2 +41,3 @@ };

gray: mockColor,
black: mockColor };
black: mockColor,
};

@@ -9,3 +9,3 @@ /**

*
*
* @flow
* @format

@@ -18,4 +18,4 @@ */

export type {LogEntry} from './Logger';
module.exports = Logger;
module.exports = Logger;

@@ -9,67 +9,67 @@ /**

*
*
* @flow
* @format
*/
'use strict';var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;};
'use strict';
const os = require('os');var _require =
const os = require('os');
require('events');const EventEmitter = _require.EventEmitter;
const {EventEmitter} = require('events');
const VERSION = require('../../package.json').version;
type ActionLogEntryData = {
action_name: string,
};
type ActionStartLogEntry = {
action_name?: string,
action_phase?: string,
log_entry_label: string,
log_session?: string,
start_timestamp?: [number, number],
};
export type LogEntry = {
action_name?: string,
action_phase?: string,
duration_ms?: number,
log_entry_label: string,
log_session?: string,
start_timestamp?: [number, number],
};
const log_session = `${os.hostname()}-${Date.now()}`;
const eventEmitter = new EventEmitter();
function on(event, handler) {
function on(event: string, handler: (logEntry: LogEntry) => void): void {
eventEmitter.on(event, handler);
}
function createEntry(data) {
const logEntry = typeof data === 'string' ? { log_entry_label: data } : data;
function createEntry(data: LogEntry | string): LogEntry {
const logEntry = typeof data === 'string' ? {log_entry_label: data} : data;
return _extends({},
logEntry, {
return {
...logEntry,
log_session,
metro_bundler_version: VERSION });
metro_bundler_version: VERSION,
};
}
function createActionStartEntry(data) {
const logEntry = typeof data === 'string' ? { action_name: data } : data;const
action_name = logEntry.action_name;
function createActionStartEntry(data: ActionLogEntryData | string): LogEntry {
const logEntry = typeof data === 'string' ? {action_name: data} : data;
const {action_name} = logEntry;
return createEntry(_extends({},
logEntry, {
return createEntry({
...logEntry,
action_name,
action_phase: 'start',
log_entry_label: action_name,
start_timestamp: process.hrtime() }));
start_timestamp: process.hrtime(),
});
}
function createActionEndEntry(logEntry) {const
action_name = logEntry.action_name,action_phase = logEntry.action_phase,start_timestamp = logEntry.start_timestamp;
function createActionEndEntry(logEntry: ActionStartLogEntry): LogEntry {
const {action_name, action_phase, start_timestamp} = logEntry;

@@ -83,12 +83,12 @@ if (action_phase !== 'start' || !Array.isArray(start_timestamp)) {

return createEntry(_extends({},
logEntry, {
return createEntry({
...logEntry,
action_name,
action_phase: 'end',
duration_ms,
log_entry_label: action_name }));
log_entry_label: action_name,
});
}
function log(logEntry) {
function log(logEntry: LogEntry): LogEntry {
eventEmitter.emit('log', logEntry);

@@ -103,2 +103,3 @@ return logEntry;

createActionEndEntry,
log };
log,
};

@@ -9,3 +9,3 @@ /**

*
*
* @flow
* @format

@@ -18,2 +18,2 @@ */

module.exports = Terminal;
module.exports = Terminal;

@@ -9,3 +9,3 @@ /**

*
*
* @flow
* @format

@@ -21,10 +21,10 @@ */

type UnderlyingStream = net$Socket | stream$Writable;
/**
* Clear some text that was previously printed on an interactive stream,
* without trailing newline character (so we have to move back to the
* beginning of the line).
*/
function clearStringBackwards(stream, str) {
* Clear some text that was previously printed on an interactive stream,
* without trailing newline character (so we have to move back to the
* beginning of the line).
*/
function clearStringBackwards(stream: tty.WriteStream, str: string): void {
readline.moveCursor(stream, -stream.columns, 0);

@@ -41,11 +41,11 @@ readline.clearLine(stream, 0);

/**
* Cut a string into an array of string of the specific maximum size. A newline
* ends a chunk immediately (it's not included in the "." RexExp operator), and
* is not included in the result.
* When counting we should ignore non-printable characters. In particular the
* ANSI escape sequences (regex: /\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?m/)
* (Not an exhaustive match, intended to match ANSI color escapes)
* https://en.wikipedia.org/wiki/ANSI_escape_code
*/
function chunkString(str, size) {
* Cut a string into an array of string of the specific maximum size. A newline
* ends a chunk immediately (it's not included in the "." RexExp operator), and
* is not included in the result.
* When counting we should ignore non-printable characters. In particular the
* ANSI escape sequences (regex: /\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?m/)
* (Not an exhaustive match, intended to match ANSI color escapes)
* https://en.wikipedia.org/wiki/ANSI_escape_code
*/
function chunkString(str: string, size: number): Array<string> {
const ANSI_COLOR = '\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?m';

@@ -57,10 +57,10 @@ const SKIP_ANSI = `(?:${ANSI_COLOR})*`;

/**
* Get the stream as a TTY if it effectively looks like a valid TTY.
*/
function getTTYStream(stream) {
* Get the stream as a TTY if it effectively looks like a valid TTY.
*/
function getTTYStream(stream: UnderlyingStream): ?tty.WriteStream {
if (
stream instanceof tty.WriteStream &&
stream.isTTY &&
stream.columns >= 1)
{
stream instanceof tty.WriteStream &&
stream.isTTY &&
stream.columns >= 1
) {
return stream;

@@ -72,37 +72,37 @@ }

/**
* We don't just print things to the console, sometimes we also want to show
* and update progress. This utility just ensures the output stays neat: no
* missing newlines, no mangled log lines.
*
* const terminal = Terminal.default;
* terminal.status('Updating... 38%');
* terminal.log('warning: Something happened.');
* terminal.status('Updating, done.');
* terminal.persistStatus();
*
* The final output:
*
* warning: Something happened.
* Updating, done.
*
* Without the status feature, we may get a mangled output:
*
* Updating... 38%warning: Something happened.
* Updating, done.
*
* This is meant to be user-readable and TTY-oriented. We use stdout by default
* because it's more about status information than diagnostics/errors (stderr).
*
* Do not add any higher-level functionality in this class such as "warning" and
* "error" printers, as it is not meant for formatting/reporting. It has the
* single responsibility of handling status messages.
*/
* We don't just print things to the console, sometimes we also want to show
* and update progress. This utility just ensures the output stays neat: no
* missing newlines, no mangled log lines.
*
* const terminal = Terminal.default;
* terminal.status('Updating... 38%');
* terminal.log('warning: Something happened.');
* terminal.status('Updating, done.');
* terminal.persistStatus();
*
* The final output:
*
* warning: Something happened.
* Updating, done.
*
* Without the status feature, we may get a mangled output:
*
* Updating... 38%warning: Something happened.
* Updating, done.
*
* This is meant to be user-readable and TTY-oriented. We use stdout by default
* because it's more about status information than diagnostics/errors (stderr).
*
* Do not add any higher-level functionality in this class such as "warning" and
* "error" printers, as it is not meant for formatting/reporting. It has the
* single responsibility of handling status messages.
*/
class Terminal {
_logLines: Array<string>;
_nextStatusStr: string;
_scheduleUpdate: () => void;
_statusStr: string;
_stream: UnderlyingStream;
constructor(stream) {
constructor(stream: UnderlyingStream) {
this._logLines = [];

@@ -116,9 +116,9 @@ this._nextStatusStr = '';

/**
* Clear and write the new status, logging in bulk in-between. Doing this in a
* throttled way (in a different tick than the calls to `log()` and
* `status()`) prevents us from repeatedly rewriting the status in case
* `terminal.log()` is called several times.
*/
_update() {const
_statusStr = this._statusStr,_stream = this._stream;
* Clear and write the new status, logging in bulk in-between. Doing this in a
* throttled way (in a different tick than the calls to `log()` and
* `status()`) prevents us from repeatedly rewriting the status in case
* `terminal.log()` is called several times.
*/
_update(): void {
const {_statusStr, _stream} = this;
const ttyStream = getTTYStream(_stream);

@@ -138,5 +138,5 @@ if (_statusStr === this._nextStatusStr && this._logLines.length === 0) {

this._nextStatusStr = chunkString(
this._nextStatusStr,
ttyStream.columns).
join('\n');
this._nextStatusStr,
ttyStream.columns,
).join('\n');
_stream.write(this._nextStatusStr);

@@ -148,11 +148,11 @@ }

/**
* Shows some text that is meant to be overriden later. Return the previous
* status that was shown and is no more. Calling `status()` with no argument
* removes the status altogether. The status is never shown in a
* non-interactive terminal: for example, if the output is redirected to a
* file, then we don't care too much about having a progress bar.
*/
status(format) {const
_nextStatusStr = this._nextStatusStr;for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {args[_key - 1] = arguments[_key];}
this._nextStatusStr = util.format.apply(util, [format].concat(args));
* Shows some text that is meant to be overriden later. Return the previous
* status that was shown and is no more. Calling `status()` with no argument
* removes the status altogether. The status is never shown in a
* non-interactive terminal: for example, if the output is redirected to a
* file, then we don't care too much about having a progress bar.
*/
status(format: string, ...args: Array<mixed>): string {
const {_nextStatusStr} = this;
this._nextStatusStr = util.format(format, ...args);
this._scheduleUpdate();

@@ -163,8 +163,8 @@ return _nextStatusStr;

/**
* Similar to `console.log`, except it moves the status/progress text out of
* the way correctly. In non-interactive terminals this is the same as
* `console.log`.
*/
log(format) {for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {args[_key2 - 1] = arguments[_key2];}
this._logLines.push(util.format.apply(util, [format].concat(args)));
* Similar to `console.log`, except it moves the status/progress text out of
* the way correctly. In non-interactive terminals this is the same as
* `console.log`.
*/
log(format: string, ...args: Array<mixed>): void {
this._logLines.push(util.format(format, ...args));
this._scheduleUpdate();

@@ -174,11 +174,11 @@ }

/**
* Log the current status and start from scratch. This is useful if the last
* status was the last one of a series of updates.
*/
persistStatus() {
* Log the current status and start from scratch. This is useful if the last
* status was the last one of a series of updates.
*/
persistStatus(): void {
this.log(this._nextStatusStr);
this._nextStatusStr = '';
}}
}
}
module.exports = Terminal;
module.exports = Terminal;
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