Require Node.js 12 and move to ESM

This commit is contained in:
Sindre Sorhus 2021-04-16 15:23:29 +07:00
parent 4dab5e1fb6
commit fa16f4ec37
22 changed files with 362 additions and 412 deletions

View file

@ -1,11 +1,12 @@
'use strict';
const ansiStyles = require('ansi-styles');
const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
const {
import ansiStyles from 'ansi-styles';
import supportsColor from 'supports-color';
import {
stringReplaceAll,
stringEncaseCRLFWithFirstIndex
} = require('./util');
} from './util.js';
import template from './templates.js';
const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
const {isArray} = Array;
// `supportsColor.level` → `ansiStyles.color[name]` mapping
@ -168,7 +169,7 @@ const applyStyle = (self, string) => {
}
const {openAll, closeAll} = styler;
if (string.indexOf('\u001B') !== -1) {
if (string.includes('\u001B')) {
while (styler !== undefined) {
// Replace any instances already present with a re-opening code
// otherwise only the part of the string until said closing code
@ -190,7 +191,6 @@ const applyStyle = (self, string) => {
return openAll + string + closeAll;
};
let template;
const chalkTag = (chalk, ...strings) => {
const [firstString] = strings;
@ -210,10 +210,6 @@ const chalkTag = (chalk, ...strings) => {
);
}
if (template === undefined) {
template = require('./templates');
}
return template(chalk, parts.join(''));
};
@ -224,4 +220,4 @@ chalk.supportsColor = stdoutColor;
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
chalk.stderr.supportsColor = stderrColor;
module.exports = chalk;
export default chalk;

View file

@ -1,4 +1,3 @@
'use strict';
const TEMPLATE_REGEX = /(?:\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
@ -62,7 +61,7 @@ function parseStyle(style) {
if (matches[2]) {
const args = parseArguments(name, matches[2]);
results.push([name].concat(args));
results.push([name, ...args]);
} else {
results.push([name]);
}
@ -96,7 +95,7 @@ function buildStyle(chalk, styles) {
return current;
}
module.exports = (chalk, temporary) => {
export default function template(chalk, temporary) {
const styles = [];
const chunks = [];
let chunk = [];
@ -126,9 +125,9 @@ module.exports = (chalk, temporary) => {
chunks.push(chunk.join(''));
if (styles.length > 0) {
const errMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
throw new Error(errMessage);
const errorMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
throw new Error(errorMessage);
}
return chunks.join('');
};
}

View file

@ -1,6 +1,4 @@
'use strict';
const stringReplaceAll = (string, substring, replacer) => {
export function stringReplaceAll(string, substring, replacer) {
let index = string.indexOf(substring);
if (index === -1) {
return string;
@ -15,11 +13,11 @@ const stringReplaceAll = (string, substring, replacer) => {
index = string.indexOf(substring, endIndex);
} while (index !== -1);
returnValue += string.substr(endIndex);
returnValue += string.slice(endIndex);
return returnValue;
};
}
const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => {
export function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
let endIndex = 0;
let returnValue = '';
do {
@ -29,11 +27,6 @@ const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => {
index = string.indexOf('\n', endIndex);
} while (index !== -1);
returnValue += string.substr(endIndex);
returnValue += string.slice(endIndex);
return returnValue;
};
module.exports = {
stringReplaceAll,
stringEncaseCRLFWithFirstIndex
};
}