Add types field to package.json

Fixes #500
This commit is contained in:
Sindre Sorhus 2021-07-30 17:30:19 +02:00
parent 48d25d156a
commit 625a285772
9 changed files with 41 additions and 39 deletions

View file

@ -1,7 +1,7 @@
import chalk from '../source/index.js';
import convertColor from 'color-convert'; import convertColor from 'color-convert';
import updateLog from 'log-update'; import updateLog from 'log-update';
import delay from 'yoctodelay'; import delay from 'yoctodelay';
import chalk from '../source/index.js';
const ignoreChars = /[^!-~]/g; const ignoreChars = /[^!-~]/g;

View file

@ -16,7 +16,7 @@
}, },
"files": [ "files": [
"source", "source",
"index.d.ts" "!source/index.test-d.ts"
], ],
"keywords": [ "keywords": [
"color", "color",
@ -43,20 +43,22 @@
], ],
"dependencies": { "dependencies": {
"ansi-styles": "^6.1.0", "ansi-styles": "^6.1.0",
"supports-color": "^9.0.0" "supports-color": "^9.0.2"
}, },
"devDependencies": { "devDependencies": {
"ava": "^3.15.0", "ava": "^3.15.0",
"color-convert": "^2.0.1", "color-convert": "^2.0.1",
"coveralls": "^3.1.0", "coveralls": "^3.1.1",
"execa": "^5.0.0", "execa": "^5.1.1",
"log-update": "^4.0.0", "log-update": "^4.0.0",
"matcha": "^0.7.0", "matcha": "^0.7.0",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"tsd": "^0.14.0", "tsd": "^0.17.0",
"xo": "^0.39.1", "typescript": "^4.3.5",
"xo": "^0.42.0",
"yoctodelay": "^1.2.0" "yoctodelay": "^1.2.0"
}, },
"types": "./source/index.d.ts",
"xo": { "xo": {
"rules": { "rules": {
"unicorn/prefer-string-slice": "off", "unicorn/prefer-string-slice": "off",

View file

View file

@ -2,7 +2,7 @@ import ansiStyles from 'ansi-styles';
import supportsColor from 'supports-color'; import supportsColor from 'supports-color';
import { import {
stringReplaceAll, stringReplaceAll,
stringEncaseCRLFWithFirstIndex stringEncaseCRLFWithFirstIndex,
} from './util.js'; } from './util.js';
import template from './templates.js'; import template from './templates.js';
@ -18,7 +18,7 @@ const levelMapping = [
'ansi', 'ansi',
'ansi', 'ansi',
'ansi256', 'ansi256',
'ansi16m' 'ansi16m',
]; ];
const styles = Object.create(null); const styles = Object.create(null);
@ -66,7 +66,7 @@ for (const [styleName, style] of Object.entries(ansiStyles)) {
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[IS_EMPTY]);
Object.defineProperty(this, styleName, {value: builder}); Object.defineProperty(this, styleName, {value: builder});
return builder; return builder;
} },
}; };
} }
@ -75,7 +75,7 @@ styles.visible = {
const builder = createBuilder(this, this[STYLER], true); const builder = createBuilder(this, this[STYLER], true);
Object.defineProperty(this, 'visible', {value: builder}); Object.defineProperty(this, 'visible', {value: builder});
return builder; return builder;
} },
}; };
const getModelAnsi = (model, level, type, ...arguments_) => { const getModelAnsi = (model, level, type, ...arguments_) => {
@ -108,7 +108,7 @@ for (const model of usedModels) {
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]); const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
return createBuilder(this, styler, this[IS_EMPTY]); return createBuilder(this, styler, this[IS_EMPTY]);
}; };
} },
}; };
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1); const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
@ -119,7 +119,7 @@ for (const model of usedModels) {
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]); const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
return createBuilder(this, styler, this[IS_EMPTY]); return createBuilder(this, styler, this[IS_EMPTY]);
}; };
} },
}; };
} }
@ -132,8 +132,8 @@ const proto = Object.defineProperties(() => {}, {
}, },
set(level) { set(level) {
this[GENERATOR].level = level; this[GENERATOR].level = level;
} },
} },
}); });
const createStyler = (open, close, parent) => { const createStyler = (open, close, parent) => {
@ -152,7 +152,7 @@ const createStyler = (open, close, parent) => {
close, close,
openAll, openAll,
closeAll, closeAll,
parent parent,
}; };
}; };
@ -228,7 +228,7 @@ const chalkTag = (chalk, ...strings) => {
for (let i = 1; i < firstString.length; i++) { for (let i = 1; i < firstString.length; i++) {
parts.push( parts.push(
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'), String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
String(firstString.raw[i]) String(firstString.raw[i]),
); );
} }
@ -242,7 +242,7 @@ export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level :
export { export {
stdoutColor as supportsColor, stdoutColor as supportsColor,
stderrColor as supportsColorStderr stderrColor as supportsColorStderr,
}; };
export default chalk; export default chalk;

View file

@ -13,7 +13,7 @@ const ESCAPES = new Map([
['0', '\0'], ['0', '\0'],
['\\', '\\'], ['\\', '\\'],
['e', '\u001B'], ['e', '\u001B'],
['a', '\u0007'] ['a', '\u0007'],
]); ]);
function unescape(c) { function unescape(c) {

View file

@ -20,7 +20,7 @@ test('support automatic casting to string', t => {
t.is(chalk(123), '123'); t.is(chalk(123), '123');
t.is(chalk.bold(['foo', 'bar']), '\u001B[1mfoo,bar\u001B[22m'); t.is(chalk.bold(['foo', 'bar']), '\u001B[1mfoo,bar\u001B[22m');
t.is(chalk.green(98765), '\u001B[32m98765\u001B[39m'); t.is(chalk.green(98_765), '\u001B[32m98765\u001B[39m');
}); });
test('style string', t => { test('style string', t => {
@ -37,14 +37,14 @@ test('support applying multiple styles at once', t => {
test('support nesting styles', t => { test('support nesting styles', t => {
t.is( t.is(
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'), chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
'\u001B[31mfoo\u001B[4m\u001B[44mbar\u001B[49m\u001B[24m!\u001B[39m' '\u001B[31mfoo\u001B[4m\u001B[44mbar\u001B[49m\u001B[24m!\u001B[39m',
); );
}); });
test('support nesting styles of the same type (color, underline, bg)', t => { test('support nesting styles of the same type (color, underline, bg)', t => {
t.is( t.is(
chalk.red('a' + chalk.yellow('b' + chalk.green('c') + 'b') + 'c'), chalk.red('a' + chalk.yellow('b' + chalk.green('c') + 'b') + 'c'),
'\u001B[31ma\u001B[33mb\u001B[32mc\u001B[39m\u001B[31m\u001B[33mb\u001B[39m\u001B[31mc\u001B[39m' '\u001B[31ma\u001B[33mb\u001B[32mc\u001B[39m\u001B[31m\u001B[33mb\u001B[39m\u001B[31mc\u001B[39m',
); );
}); });

View file

@ -1,4 +1,4 @@
import {fileURLToPath} from 'url'; import {fileURLToPath} from 'node:url';
import test from 'ava'; import test from 'ava';
import execa from 'execa'; import execa from 'execa';
import chalk from '../source/index.js'; import chalk from '../source/index.js';

View file

@ -42,22 +42,22 @@ test('correctly perform nested template substitutions', t => {
instance.strikethrough.cyanBright.bgBlack('Works with ' + instance.reset.bold('numbers') + ' ' + instance.bold.red(1))); instance.strikethrough.cyanBright.bgBlack('Works with ' + instance.reset.bold('numbers') + ' ' + instance.bold.red(1)));
t.is(chalk.bold`Also works on the shared {bgBlue chalk} object`, t.is(chalk.bold`Also works on the shared {bgBlue chalk} object`,
'\u001B[1mAlso works on the shared \u001B[1m' + '\u001B[1mAlso works on the shared \u001B[1m'
'\u001B[44mchalk\u001B[49m\u001B[22m' + + '\u001B[44mchalk\u001B[49m\u001B[22m'
'\u001B[1m object\u001B[22m'); + '\u001B[1m object\u001B[22m');
}); });
test('correctly parse and evaluate color-convert functions', t => { test('correctly parse and evaluate color-convert functions', t => {
const instance = new Chalk({level: 3}); const instance = new Chalk({level: 3});
t.is(instance`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`, t.is(instance`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`,
'\u001B[1m\u001B[38;2;144;10;178m\u001B[7mHello, ' + '\u001B[1m\u001B[38;2;144;10;178m\u001B[7mHello, '
'\u001B[27m\u001B[39m\u001B[22m\u001B[1m' + + '\u001B[27m\u001B[39m\u001B[22m\u001B[1m'
'\u001B[38;2;144;10;178mthere!\u001B[39m\u001B[22m'); + '\u001B[38;2;144;10;178mthere!\u001B[39m\u001B[22m');
t.is(instance`{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}`, t.is(instance`{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}`,
'\u001B[1m\u001B[48;2;144;10;178m\u001B[7mHello, ' + '\u001B[1m\u001B[48;2;144;10;178m\u001B[7mHello, '
'\u001B[27m\u001B[49m\u001B[22m\u001B[1m' + + '\u001B[27m\u001B[49m\u001B[22m\u001B[1m'
'\u001B[48;2;144;10;178mthere!\u001B[49m\u001B[22m'); + '\u001B[48;2;144;10;178mthere!\u001B[49m\u001B[22m');
}); });
test('properly handle escapes', t => { test('properly handle escapes', t => {
@ -103,13 +103,13 @@ test('properly style multiline color blocks', t => {
} {underline } {underline
I hope you enjoy I hope you enjoy
}`, }`,
'\u001B[1m\u001B[22m\n' + '\u001B[1m\u001B[22m\n'
'\u001B[1m\t\t\tHello! This is a\u001B[22m\n' + + '\u001B[1m\t\t\tHello! This is a\u001B[22m\n'
'\u001B[1m\t\t\tmultiline block!\u001B[22m\n' + + '\u001B[1m\t\t\tmultiline block!\u001B[22m\n'
'\u001B[1m\t\t\t:)\u001B[22m\n' + + '\u001B[1m\t\t\t:)\u001B[22m\n'
'\u001B[1m\t\t\u001B[22m \u001B[4m\u001B[24m\n' + + '\u001B[1m\t\t\u001B[22m \u001B[4m\u001B[24m\n'
'\u001B[4m\t\t\tI hope you enjoy\u001B[24m\n' + + '\u001B[4m\t\t\tI hope you enjoy\u001B[24m\n'
'\u001B[4m\t\t\u001B[24m' + '\u001B[4m\t\t\u001B[24m',
); );
}); });