Require Node.js 8

This commit is contained in:
Sindre Sorhus 2019-03-12 20:11:31 +07:00
parent de2f4cd606
commit 3ef170b457
6 changed files with 56 additions and 54 deletions

View file

@ -2,6 +2,5 @@ language: node_js
node_js: node_js:
- '10' - '10'
- '8' - '8'
- '6'
after_success: after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'

View file

@ -3,36 +3,39 @@ const chalk = require('..');
const ignoreChars = /[^!-~]/g; const ignoreChars = /[^!-~]/g;
function rainbow(str, offset) { const delay = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
if (!str || str.length === 0) {
return str; function rainbow(string, offset) {
if (!string || string.length === 0) {
return string;
} }
const hueStep = 360 / str.replace(ignoreChars, '').length; const hueStep = 360 / string.replace(ignoreChars, '').length;
let hue = offset % 360; let hue = offset % 360;
const chars = []; const characters = [];
for (const c of str) { for (const character of string) {
if (c.match(ignoreChars)) { if (character.match(ignoreChars)) {
chars.push(c); characters.push(character);
} else { } else {
chars.push(chalk.hsl(hue, 100, 50)(c)); characters.push(chalk.hsl(hue, 100, 50)(character));
hue = (hue + hueStep) % 360; hue = (hue + hueStep) % 360;
} }
} }
return chars.join(''); return characters.join('');
} }
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); async function animateString(string) {
async function animateString(str) {
console.log(); console.log();
for (let i = 0; i < 360 * 5; i++) { for (let i = 0; i < 360 * 5; i++) {
console.log('\u001B[1F\u001B[G ', rainbow(str, i)); console.log('\u001B[1F\u001B[G', rainbow(string, i));
await sleep(2); // eslint-disable-line no-await-in-loop await delay(2); // eslint-disable-line no-await-in-loop
} }
} }
console.log(); (async () => {
animateString('We hope you enjoy the new version of Chalk 2! <3').then(() => console.log()); console.log();
await animateString('We hope you enjoy Chalk! <3');
console.log();
})();

2
index.d.ts vendored
View file

@ -271,6 +271,6 @@ export interface Chalk {
* Order doesn't matter, and later styles take precedent in case of a conflict. * Order doesn't matter, and later styles take precedent in case of a conflict.
* This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`. * This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
*/ */
declare const chalk: Chalk & { supportsColor: ColorSupport }; declare const chalk: Chalk & {supportsColor: ColorSupport};
export default chalk; export default chalk;

View file

@ -7,7 +7,12 @@ const template = require('./templates.js');
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm'); const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
// `supportsColor.level` → `ansiStyles.color[name]` mapping // `supportsColor.level` → `ansiStyles.color[name]` mapping
const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m']; const levelMapping = [
'ansi',
'ansi',
'ansi256',
'ansi16m'
];
// `color-convert` models to exclude from the Chalk API due to conflicts and such // `color-convert` models to exclude from the Chalk API due to conflicts and such
const skipModels = new Set(['gray']); const skipModels = new Set(['gray']);
@ -35,7 +40,7 @@ function chalkFactory(options) {
const chalk = {}; const chalk = {};
applyOptions(chalk, options); applyOptions(chalk, options);
chalk.template = (...args) => chalkTag(chalk.template, ...args); chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_);
Object.setPrototypeOf(chalk, Chalk.prototype); Object.setPrototypeOf(chalk, Chalk.prototype);
Object.setPrototypeOf(chalk.template, chalk); Object.setPrototypeOf(chalk.template, chalk);
@ -43,6 +48,7 @@ function chalkFactory(options) {
chalk.template.constructor = () => { chalk.template.constructor = () => {
throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.'); throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
}; };
chalk.template.Instance = ChalkClass; chalk.template.Instance = ChalkClass;
return chalk.template; return chalk.template;
@ -57,13 +63,12 @@ if (isSimpleWindowsTerm) {
ansiStyles.blue.open = '\u001B[94m'; ansiStyles.blue.open = '\u001B[94m';
} }
for (const key of Object.keys(ansiStyles)) { for (const [styleName, style] of Object.entries(ansiStyles)) {
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g'); style.closeRe = new RegExp(escapeStringRegexp(style.close), 'g');
styles[key] = { styles[styleName] = {
get() { get() {
const codes = ansiStyles[key]; return build.call(this, [...(this._styles || []), style], this._empty, styleName);
return build.call(this, [...(this._styles || []), codes], this._empty, key);
} }
}; };
} }
@ -106,8 +111,8 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
styles[bgModel] = { styles[bgModel] = {
get() { get() {
const {level} = this; const {level} = this;
return function (...args) { return function (...arguments_) {
const open = ansiStyles.bgColor[levelMapping[level]][model](...args); const open = ansiStyles.bgColor[levelMapping[level]][model](...arguments_);
const codes = { const codes = {
open, open,
close: ansiStyles.bgColor.close, close: ansiStyles.bgColor.close,
@ -122,7 +127,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
const proto = Object.defineProperties(() => {}, styles); const proto = Object.defineProperties(() => {}, styles);
function build(_styles, _empty, key) { function build(_styles, _empty, key) {
const builder = (...args) => applyStyle.call(builder, ...args); const builder = (...arguments_) => applyStyle.call(builder, ...arguments_);
builder._styles = _styles; builder._styles = _styles;
builder._empty = _empty; builder._empty = _empty;
@ -158,8 +163,8 @@ function build(_styles, _empty, key) {
return builder; return builder;
} }
function applyStyle(...args) { function applyStyle(...arguments_) {
let string = args.join(' '); let string = arguments_.join(' ');
if (!this.enabled || this.level <= 0 || !string) { if (!this.enabled || this.level <= 0 || !string) {
return this._empty ? '' : string; return this._empty ? '' : string;
@ -200,12 +205,12 @@ function chalkTag(chalk, ...strings) {
return strings.join(' '); return strings.join(' ');
} }
const args = strings.slice(1); const arguments_ = strings.slice(1);
const parts = [firstString.raw[0]]; const parts = [firstString.raw[0]];
for (let i = 1; i < firstString.length; i++) { for (let i = 1; i < firstString.length; i++) {
parts.push( parts.push(
String(args[i - 1]).replace(/[{}\\]/g, '\\$&'), String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
String(firstString.raw[i]) String(firstString.raw[i])
); );
} }

View file

@ -5,7 +5,7 @@
"license": "MIT", "license": "MIT",
"repository": "chalk/chalk", "repository": "chalk/chalk",
"engines": { "engines": {
"node": ">=6" "node": ">=8"
}, },
"scripts": { "scripts": {
"test": "xo && nyc ava && tsd-check && flow", "test": "xo && nyc ava && tsd-check && flow",
@ -43,20 +43,20 @@
"dependencies": { "dependencies": {
"ansi-styles": "^3.2.1", "ansi-styles": "^3.2.1",
"escape-string-regexp": "^1.0.5", "escape-string-regexp": "^1.0.5",
"supports-color": "^6.0.0" "supports-color": "^6.1.0"
}, },
"devDependencies": { "devDependencies": {
"@sindresorhus/tsconfig": "^0.1.1", "@sindresorhus/tsconfig": "^0.2.1",
"ava": "^1.0.1", "ava": "^1.3.1",
"coveralls": "^3.0.2", "coveralls": "^3.0.3",
"execa": "^1.0.0", "execa": "^1.0.0",
"flow-bin": "^0.89.0", "flow-bin": "^0.94.0",
"import-fresh": "^3.0.0", "import-fresh": "^3.0.0",
"matcha": "^0.7.0", "matcha": "^0.7.0",
"nyc": "^13.1.0", "nyc": "^13.3.0",
"resolve-from": "^4.0.0", "resolve-from": "^4.0.0",
"tsd-check": "^0.3.0", "tsd-check": "^0.3.0",
"xo": "^0.23.0" "xo": "^0.24.0"
}, },
"types": "index.d.ts", "types": "index.d.ts",
"xo": { "xo": {

View file

@ -25,9 +25,9 @@ function unescape(c) {
return ESCAPES.get(c) || c; return ESCAPES.get(c) || c;
} }
function parseArguments(name, args) { function parseArguments(name, arguments_) {
const results = []; const results = [];
const chunks = args.trim().split(/\s*,\s*/g); const chunks = arguments_.trim().split(/\s*,\s*/g);
let matches; let matches;
for (const chunk of chunks) { for (const chunk of chunks) {
@ -51,7 +51,7 @@ function parseStyle(style) {
let matches; let matches;
while ((matches = STYLE_REGEX.exec(style)) !== null) { while ((matches = STYLE_REGEX.exec(style)) !== null) {
const name = matches[1]; // eslint-disable-line prefer-destructuring const name = matches[1];
if (matches[2]) { if (matches[2]) {
const args = parseArguments(name, matches[2]); const args = parseArguments(name, matches[2]);
@ -74,9 +74,8 @@ function buildStyle(chalk, styles) {
} }
let current = chalk; let current = chalk;
// TODO: Use `Object.entries` when targeting Node.js 8 for (const [styleName, styles] of Object.entries(enabled)) {
for (const styleName of Object.keys(enabled)) { if (!Array.isArray(styles)) {
if (!Array.isArray(enabled[styleName])) {
continue; continue;
} }
@ -84,23 +83,19 @@ function buildStyle(chalk, styles) {
throw new Error(`Unknown Chalk style: ${styleName}`); throw new Error(`Unknown Chalk style: ${styleName}`);
} }
if (enabled[styleName].length > 0) { current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
current = current[styleName](...enabled[styleName]);
} else {
current = current[styleName];
}
} }
return current; return current;
} }
module.exports = (chalk, tmp) => { module.exports = (chalk, temporary) => {
const styles = []; const styles = [];
const chunks = []; const chunks = [];
let chunk = []; let chunk = [];
// eslint-disable-next-line max-params // eslint-disable-next-line max-params
tmp.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => { temporary.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => {
if (escapeCharacter) { if (escapeCharacter) {
chunk.push(unescape(escapeCharacter)); chunk.push(unescape(escapeCharacter));
} else if (style) { } else if (style) {