node_modules/chalk/source/index.js in immosquare-cleaner-0.1.42 vs node_modules/chalk/source/index.js in immosquare-cleaner-0.1.43

- old
+ new

@@ -1,24 +1,21 @@ -import ansiStyles from '#ansi-styles'; -import supportsColor from '#supports-color'; -import { // eslint-disable-line import/order +'use strict'; +const ansiStyles = require('ansi-styles'); +const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color'); +const { stringReplaceAll, - stringEncaseCRLFWithFirstIndex, -} from './utilities.js'; + stringEncaseCRLFWithFirstIndex +} = require('./util'); -const {stdout: stdoutColor, stderr: stderrColor} = supportsColor; +const {isArray} = Array; -const GENERATOR = Symbol('GENERATOR'); -const STYLER = Symbol('STYLER'); -const IS_EMPTY = Symbol('IS_EMPTY'); - // `supportsColor.level` → `ansiStyles.color[name]` mapping const levelMapping = [ 'ansi', 'ansi', 'ansi256', - 'ansi16m', + 'ansi16m' ]; const styles = Object.create(null); const applyOptions = (object, options = {}) => { @@ -29,106 +26,95 @@ // Detect level if not set manually const colorLevel = stdoutColor ? stdoutColor.level : 0; object.level = options.level === undefined ? colorLevel : options.level; }; -export class Chalk { +class ChalkClass { constructor(options) { // eslint-disable-next-line no-constructor-return return chalkFactory(options); } } const chalkFactory = options => { - const chalk = (...strings) => strings.join(' '); + const chalk = {}; applyOptions(chalk, options); - Object.setPrototypeOf(chalk, createChalk.prototype); + chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_); - return chalk; + Object.setPrototypeOf(chalk, Chalk.prototype); + Object.setPrototypeOf(chalk.template, chalk); + + chalk.template.constructor = () => { + throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.'); + }; + + chalk.template.Instance = ChalkClass; + + return chalk.template; }; -function createChalk(options) { +function Chalk(options) { return chalkFactory(options); } -Object.setPrototypeOf(createChalk.prototype, Function.prototype); - for (const [styleName, style] of Object.entries(ansiStyles)) { styles[styleName] = { get() { - const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]); + const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty); Object.defineProperty(this, styleName, {value: builder}); return builder; - }, + } }; } styles.visible = { get() { - const builder = createBuilder(this, this[STYLER], true); + const builder = createBuilder(this, this._styler, true); Object.defineProperty(this, 'visible', {value: builder}); return builder; - }, -}; - -const getModelAnsi = (model, level, type, ...arguments_) => { - if (model === 'rgb') { - if (level === 'ansi16m') { - return ansiStyles[type].ansi16m(...arguments_); - } - - if (level === 'ansi256') { - return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_)); - } - - return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_)); } - - if (model === 'hex') { - return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_)); - } - - return ansiStyles[type][model](...arguments_); }; -const usedModels = ['rgb', 'hex', 'ansi256']; +const usedModels = ['rgb', 'hex', 'keyword', 'hsl', 'hsv', 'hwb', 'ansi', 'ansi256']; for (const model of usedModels) { styles[model] = { get() { const {level} = this; return function (...arguments_) { - const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]); - return createBuilder(this, styler, this[IS_EMPTY]); + const styler = createStyler(ansiStyles.color[levelMapping[level]][model](...arguments_), ansiStyles.color.close, this._styler); + return createBuilder(this, styler, this._isEmpty); }; - }, + } }; +} +for (const model of usedModels) { const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1); styles[bgModel] = { get() { const {level} = this; return function (...arguments_) { - const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]); - return createBuilder(this, styler, this[IS_EMPTY]); + const styler = createStyler(ansiStyles.bgColor[levelMapping[level]][model](...arguments_), ansiStyles.bgColor.close, this._styler); + return createBuilder(this, styler, this._isEmpty); }; - }, + } }; } const proto = Object.defineProperties(() => {}, { ...styles, level: { enumerable: true, get() { - return this[GENERATOR].level; + return this._generator.level; }, set(level) { - this[GENERATOR].level = level; - }, - }, + this._generator.level = level; + } + } }); const createStyler = (open, close, parent) => { let openAll; let closeAll; @@ -143,43 +129,50 @@ return { open, close, openAll, closeAll, - parent, + parent }; }; const createBuilder = (self, _styler, _isEmpty) => { - // Single argument is hot path, implicit coercion is faster than anything - // eslint-disable-next-line no-implicit-coercion - const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' ')); + const builder = (...arguments_) => { + if (isArray(arguments_[0]) && isArray(arguments_[0].raw)) { + // Called as a template literal, for example: chalk.red`2 + 3 = {bold ${2+3}}` + return applyStyle(builder, chalkTag(builder, ...arguments_)); + } + // Single argument is hot path, implicit coercion is faster than anything + // eslint-disable-next-line no-implicit-coercion + return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' ')); + }; + // We alter the prototype because we must return a function, but there is // no way to create a function with a different prototype Object.setPrototypeOf(builder, proto); - builder[GENERATOR] = self; - builder[STYLER] = _styler; - builder[IS_EMPTY] = _isEmpty; + builder._generator = self; + builder._styler = _styler; + builder._isEmpty = _isEmpty; return builder; }; const applyStyle = (self, string) => { if (self.level <= 0 || !string) { - return self[IS_EMPTY] ? '' : string; + return self._isEmpty ? '' : string; } - let styler = self[STYLER]; + let styler = self._styler; if (styler === undefined) { return string; } const {openAll, closeAll} = styler; - if (string.includes('\u001B')) { + if (string.indexOf('\u001B') !== -1) { while (styler !== undefined) { // Replace any instances already present with a re-opening code // otherwise only the part of the string until said closing code // will be colored, and the rest will simply be 'plain'. string = stringReplaceAll(string, styler.close, styler.open); @@ -197,29 +190,40 @@ } return openAll + string + closeAll; }; -Object.defineProperties(createChalk.prototype, styles); +let template; +const chalkTag = (chalk, ...strings) => { + const [firstString] = strings; -const chalk = createChalk(); -export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0}); + if (!isArray(firstString) || !isArray(firstString.raw)) { + // If chalk() was called by itself or with a string, + // return the string itself as a string. + return strings.join(' '); + } -export { - modifierNames, - foregroundColorNames, - backgroundColorNames, - colorNames, + const arguments_ = strings.slice(1); + const parts = [firstString.raw[0]]; - // TODO: Remove these aliases in the next major version - modifierNames as modifiers, - foregroundColorNames as foregroundColors, - backgroundColorNames as backgroundColors, - colorNames as colors, -} from './vendor/ansi-styles/index.js'; + for (let i = 1; i < firstString.length; i++) { + parts.push( + String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'), + String(firstString.raw[i]) + ); + } -export { - stdoutColor as supportsColor, - stderrColor as supportsColorStderr, + if (template === undefined) { + template = require('./templates'); + } + + return template(chalk, parts.join('')); }; -export default chalk; +Object.defineProperties(Chalk.prototype, styles); + +const chalk = Chalk(); // eslint-disable-line new-cap +chalk.supportsColor = stdoutColor; +chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap +chalk.stderr.supportsColor = stderrColor; + +module.exports = chalk;