From 369aa38ae4bcdfe5d51413d648b8d192b1dc6abc Mon Sep 17 00:00:00 2001 From: biodiscus Date: Fri, 3 Nov 2023 11:57:28 +0100 Subject: [PATCH] release v2.0.0 --- .editorconfig | 2 +- .github/workflows/test.yml | 4 +- CHANGELOG.md | 16 ++++ README.md | 57 +++++++------ bench/index.js | 23 +++--- bench/package.json | 1 + examples/ansi256.js | 4 +- examples/ansis-logo.js | 5 +- examples/ansis-styles-demo.js | 19 +++-- examples/index.js | 7 +- package.json | 19 ++--- package/index.js | 10 --- package/package.json | 10 +-- rollup.config.js | 49 +++--------- src/ansi-codes.js | 5 +- src/colors.cjs | 74 ----------------- src/colors.d.ts | 69 ---------------- src/colors.mjs | 72 ----------------- src/index.d.ts | 77 +++++++++++++++++- src/index.js | 145 +++++++++++++++++----------------- src/utils.js | 4 +- test/index.test.js | 99 ++++++++++------------- test/package/cjs/index.js | 21 ++++- test/package/esm/index.js | 21 ++++- test/package/ts/index.js | 3 +- test/package/ts/index.ts | 17 ++-- 26 files changed, 344 insertions(+), 489 deletions(-) delete mode 100644 package/index.js delete mode 100644 src/colors.cjs delete mode 100644 src/colors.d.ts delete mode 100644 src/colors.mjs diff --git a/.editorconfig b/.editorconfig index 03eb465..3ee9b90 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,6 +9,6 @@ trim_trailing_whitespace = true indent_size = 2 tab_width = 2 -[{*.htm,*.html,*.pug,*.md}] +[{*.htm,*.html,*.md}] indent_size = 2 tab_width = 2 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f70859b..38700cf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,8 +14,8 @@ jobs: integration: strategy: matrix: - os: [ubuntu-latest] - node-version: [12, 14, 16, 18, 20] + os: [ ubuntu-latest ] + node-version: [ 14, 16, 18, 20 ] runs-on: ${{ matrix.os }} steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index 2508a68..5970855 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Change log +## 2.0.0 (2023-11-03) + +- feat: add supports the Deno +- feat: add supports the Next.js `edge` runtime +- feat(CHANGE): add named import for `ansis`:\ + NEW named import: `import { red } from 'ansis'`.\ + If you use `TypeScript` and your IDE show the error: `TS2307: Cannot find module ansis/colors`,\ + then you should use the new syntax, + update you code: `import { red } from 'ansis/colors'` --> `import { red } from 'ansis'`. +- feat(DEPRECATE): OLD named import `import { red } from 'ansis/colors'` is deprecated, use the NEW named import +- feat(DEPRECATE): instead of the `ansi` use `ansi256` or alias `fg` +- feat(DEPRECATE): instead of the `bgAnsi` use `bgAnsi256` or alias `bg` +- feat: optimize named export +- feat: reduce the size of dist/ directory +- chore: update dev dependencies, new jest requires node.js >= 14 + ## 1.6.0-beta.0 (2023-11-01) - feat: add supports the Deno diff --git a/README.md b/README.md index acafbc3..e2f5df6 100644 --- a/README.md +++ b/README.md @@ -32,17 +32,18 @@ and [benchmarks](https://github.com/webdiscus/ansis#benchmark) of most popular N [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/edit/stackblitz-starters-gs2gve?file=index.js) -## Highlights +## đź’ˇ Highlights - supports both **ESM** and **CommonJS** - supports **Deno**, **Next.JS** runtime -- up to **x3.5 faster** than **chalk**, [see benchmarks](#benchmark) -- dist code is only **5 KB** incl. named import for ESM and CommonJS +- up to **x3 faster** than **chalk**, [see benchmarks](#benchmark) +- dist code is only **5 KB** incl. named import of all styles - [standard API](#base-colors) like **chalk** -- default and [named import](#named-import) `import { red, bold, rgb } from 'ansis/colors'` -- [chained syntax](#chained-syntax) `red.bold('text')` +- default import `import ansis from 'ansis'`, usage `ansis.red('error')` +- [named import](#named-import) `import { red } from 'ansis'`, usage ``` red('error') ``` +- [chained syntax](#chained-syntax) `red.bold.underline('text')` - [nested **template strings**](#nested-syntax) ``` red`R ${green`G`} R` ``` -- [ANSI 256 colors](#256-colors) and [Truecolor](#truecolor) (**RGB**, **HEX**) +- [ANSI 256 colors](#256-colors) and [Truecolor](#truecolor) (**RGB**, **HEX**) ``` rgb(224, 17, 95)`Ruby` ```, ``` hex('#96C')`Amethyst` ``` - [extending of base colors](#extend-colors) with named **truecolors** - [ANSI codes](#escape-codes) as `open` and `close` property for each style ``` `Hello ${red.open}World${red.close}!` ``` @@ -53,6 +54,11 @@ and [benchmarks](https://github.com/webdiscus/ansis#benchmark) of most popular N - **TypeScript** friendly - zero dependencies +## âť“Question / Feature Request / Bug + +If you have discovered a bug or have a feature suggestion, feel free to create +an [issue](https://github.com/webdiscus/ansis/issues) on GitHub. + ## Install @@ -63,21 +69,20 @@ npm install ansis ## Usage -You can import module and named colors with ESM or CommonJS syntax. +You can import default module or named colors with ESM or CommonJS syntax. ```js -// ESM +// ESM default import import ansis from 'ansis'; -import { red, green, blue } from 'ansis/colors'; +// ESM named import +import { red, green, blue } from 'ansis'; -// CommonJS +// CommonJS default import const ansis = require('ansis'); -const { red, green, blue } = require('ansis/colors'); +// CommonJS named import +const { red, green, blue } = require('ansis'); -// default import -console.log(ansis.green(`Success!`)); - -// named import +console.log(ansis.green('Success!')); console.log(green('Success!')); // template string @@ -94,7 +99,7 @@ console.log(red`The ${blue.underline`file.js`} not found!`); Basic example `Hello World!`: ```js -import { red, black, inverse, reset } from 'ansis/colors'; +import { red, black, inverse, reset } from 'ansis'; console.log(green`Hello ${inverse`ANSI`} World! ${black.bgYellow`Warning:`} ${cyan`/path/to/file.js`} ${red`not found!`}`); @@ -122,7 +127,7 @@ You can import named colors, styles and functions. All imported colors and style ```js // named import -import { red, hex, italic } from 'ansis/colors'; +import { red, hex, italic } from 'ansis'; red.bold('text'); ``` @@ -137,7 +142,7 @@ The `template literals` allow you to make a complex template more readable and s The `function syntax` can be used to colorize a variable. ```js -import { red } from 'ansis/colors'; +import { red } from 'ansis'; let message = 'error'; @@ -153,7 +158,7 @@ red`text ${message} text`; All colors, styles and functions are chainable. Each color or style can be combined in any order. ```js -import { red, bold, italic, hex } from 'ansis/colors'; +import { red, bold, italic, hex } from 'ansis'; red.bold`text`; hex('#FF75D1').bgCyan.bold`text`; @@ -171,7 +176,7 @@ None of the other libraries (chalk, kleur, colorette, colors.js etc.) support ne Nested template strings: ```js -import { red, green } from 'ansis/colors'; +import { red, green } from 'ansis'; red`red ${green`green`} red`; ``` @@ -179,7 +184,7 @@ red`red ${green`green`} red`; Deep nested chained styles: ```js -import { red, green, cyan, magenta, yellow, italic, underline } from 'ansis/colors'; +import { red, green, cyan, magenta, yellow, italic, underline } from 'ansis'; red(`red ${italic(`red italic ${underline(`red italic underline`)}`)} red`); @@ -201,7 +206,7 @@ Output:\ Multiline nested template strings: ```js -import { red, green, hex, visible, inverse } from 'ansis/colors'; +import { red, green, hex, visible, inverse } from 'ansis'; // defined a truecolor as the constant const orange = hex('#FFAB40'); @@ -324,7 +329,7 @@ Background function: `bgAnsi256(code)` has short alias `bg(code)` See [ANSI color codes](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit). ```js -import { bold, ansi256, fg, bgAnsi256, bg } from 'ansis/colors'; +import { bold, ansi256, fg, bgAnsi256, bg } from 'ansis'; // foreground color ansi256(96)`Bright Cyan`; @@ -355,7 +360,7 @@ Foreground function: `hex()` `rgb()`\ Background function: `bgHex()` `bgRgb()` ```js -import { bold, hex, rgb, bgHex, bgRgb } from 'ansis/colors'; +import { bold, hex, rgb, bgHex, bgRgb } from 'ansis'; // foreground color hex('#E0115F').bold`bold Ruby`; @@ -379,7 +384,7 @@ You can use the [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_co and `close` properties for each style. ```js -import { red, bold } from 'ansis/colors'; +import { red, bold } from 'ansis'; // each style has `open` and `close` properties console.log(`Hello ${red.open}ANSI${red.close} World!`); @@ -412,7 +417,7 @@ The variable `string` will contain the pure string without ANSI codes. Supports correct style break at the `end of line`. ```js -import { bgGreen } from 'ansis/colors'; +import { bgGreen } from 'ansis'; console.log(bgGreen`\nAnsis\nNew Line\nNext New Line\n`); ``` diff --git a/bench/index.js b/bench/index.js index d0dc8d7..9f31503 100644 --- a/bench/index.js +++ b/bench/index.js @@ -1,7 +1,7 @@ // // ATTENTION !!! ACHTUNG !!! MEGA ULTRA IMPORTANT !!! WICHTIG !!! // -// For a correct measures, DO NOT use the same function instance inside the added benchmark: +// For the correct measures, DO NOT use the same function instance inside the added benchmark: // bench('Benchmark') // .add('bench1', () => anyFixture(arg1)) // <== only first measure of `anyFixture()` will be correct // .add('bench2', () => anyFixture(arg2)) // <== second and next measures of same function will be WRONG! @@ -38,12 +38,11 @@ import colorCli from 'colors-cli/lib/color-safe.js'; import kleur from 'kleur'; import * as kleurColors from 'kleur/colors'; import picocolors from 'picocolors'; -import { Ansis } from '../src/index.js'; -import { green, red, yellow, hex } from '../src/colors.mjs'; +import { Ansis, green, red, yellow, hex } from 'ansis'; const log = console.log; -// create new instance of Ansis for correct measure in benchmark +// create a new instance of Ansis for correct measure in benchmark const ansis = new Ansis(); // All vendor libraries to be tested @@ -76,6 +75,8 @@ const baseColors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan' let fixture = []; +log(hex('#F88').inverse.bold` -= Benchmark =- `); + //showSupportOfDeepNestedStyling(); //showSupportOfDeepNestedChainedStyling(); //showSupportOfBreakStyleAtNewLine(); @@ -229,11 +230,11 @@ bench('Template literals'). add('ansis', () => red`red ${yellow`yellow ${green`green`} yellow`} red`). run(); -function coloretteBench (c) { +function coloretteBench(c) { return c.red(`${c.bold(`${c.cyan(`${c.yellow('yellow')}cyan`)}`)}red`); } -function nestedFixture (c) { +function nestedFixture(c) { return c.red( `a red ${c.white('white')} red ${c.red('red')} red ${c.cyan('cyan')} red ${c.black('black')} red ${c.red( 'red', @@ -257,7 +258,7 @@ function nestedFixture (c) { ); } -function deepNestedFixture (c) { +function deepNestedFixture(c) { return c.green( `green ${c.cyan( `cyan ${c.red( @@ -271,7 +272,7 @@ function deepNestedFixture (c) { ); } -function complexNestedFixture (c) { +function complexNestedFixture(c) { return c.red( `red ${c.yellow('yellow')} red ${c.italic.cyan('italic cyan')} red ${c.underline.green( `underline green ${c.yellow('underline yellow')} underline green`, @@ -279,7 +280,7 @@ function complexNestedFixture (c) { ); } -function showSupportOfDeepNestedStyling () { +function showSupportOfDeepNestedStyling() { log('colors.js: ', deepNestedFixture(colorsJs)); log('colorette: ', deepNestedFixture(colorette)); log('picocolors: ', deepNestedFixture(picocolors)); @@ -292,12 +293,12 @@ function showSupportOfDeepNestedStyling () { log('ansis: ', deepNestedFixture(ansis)); } -function showSupportOfDeepNestedChainedStyling () { +function showSupportOfDeepNestedChainedStyling() { log('chalk: ', complexNestedFixture(chalk)); log('ansis: ', complexNestedFixture(ansis)); } -function showSupportOfBreakStyleAtNewLine () { +function showSupportOfBreakStyleAtNewLine() { log('colors.js: ', colorsJs.bgGreen(`\nAnsis\nNEW LINE\nNEXT NEW LINE\n`)); // OK log('colorette: ', colorette.bgGreen(`\nAnsis\nNEW LINE\nNEXT NEW LINE\n`)); // (not supported) log('picocolors: ', picocolors.bgGreen(`\nAnsis\nNEW LINE\nNEXT NEW LINE\n`)); // (not supported) diff --git a/bench/package.json b/bench/package.json index 5eb7738..1560017 100644 --- a/bench/package.json +++ b/bench/package.json @@ -8,6 +8,7 @@ }, "dependencies": { "ansi-colors": "4.1.1", + "ansis": "file:../dist", "benchmark": "2.1.4", "chalk": "5.0.0", "cli-color": "2.0.1", diff --git a/examples/ansi256.js b/examples/ansi256.js index da09cb2..2af6fd2 100755 --- a/examples/ansi256.js +++ b/examples/ansi256.js @@ -1,4 +1,4 @@ -import { fg, bg } from '../src/colors.mjs'; +import { fg, bg } from '../src/index.js'; let out; let n; @@ -66,4 +66,4 @@ for (let i = 232; i <= 255; i++) { } //console.log(out); -export { out as ansi256Table } +export { out as ansi256Table }; diff --git a/examples/ansis-logo.js b/examples/ansis-logo.js index c44a597..e3ca1b0 100644 --- a/examples/ansis-logo.js +++ b/examples/ansis-logo.js @@ -17,7 +17,7 @@ const colorizeASCII = ({ ascii, data }, paddingLeft = 5) => { for (let charWidthIdx = 0; charWidthIdx < width; charWidthIdx++) { char = ascii[i++]; code = Array.isArray(codes) ? codes[row] : codes + row; - out += ansis.ansi(code)(char); + out += ansis.ansi256(code)(char); } } row++; @@ -45,7 +45,6 @@ const logo = { ], }; - -let out = colorizeASCII(logo, 5) +let out = colorizeASCII(logo, 5); export { out as ansisLogo }; diff --git a/examples/ansis-styles-demo.js b/examples/ansis-styles-demo.js index f34ec6f..4cc1407 100644 --- a/examples/ansis-styles-demo.js +++ b/examples/ansis-styles-demo.js @@ -15,13 +15,18 @@ import { underline, white, whiteBright, yellow, yellowBright, -} from '../src/colors.mjs'; +} from '../src/index.js'; -const out = `${bold`bold`} ${dim`dim`} ${italic`italic`} ${underline`underline`} ${strikethrough`strikethrough`} ${inverse`inverse`} ${bold.italic.underline.strike`bold italic underline strike`}` + '\n' + - `${red`red`} ${green`green`} ${yellow`yellow`} ${blue`blue`} ${magenta`magenta`} ${cyan`cyan`} ${white`white`} ${gray`gray`} ${bold.yellow`bold yellow`} ${dim.cyan`dim cyan`} ${red.italic`italic red`} ` + '\n' + - `${black.bgRed`bgRed`} ${black.bgGreen`bgGreen`} ${black.bgYellow`bgYellow`} ${bgBlue`bgBlue`} ${black.bgMagenta`bgMagenta`} ${black.bgCyan`bgCyan`} ${black.bgWhite`bgWhite`} ${black.bgRedBright`bgRedBright`} ${white.bgRed.bold.italic` CocaCola `}` + '\n' + - `${greenBright`greenBright`} ${yellowBright`yellowBright`} ${blueBright`blueBright`} ${magentaBright`magentaBright`} ${cyanBright`cyanBright`} ${whiteBright`whiteBright`} ${greenBright`A`}${magentaBright`N`}${yellowBright`S`}${redBright`I`}` + '\n' + - `${black.bgGreenBright`bgGreenBright`} ${black.bgYellowBright`bgYellowBright`} ${bgBlueBright`bgBlueBright`} ${black.bgMagentaBright`bgMagentaBright`} ${black.bgCyanBright`bgCyanBright`} ${magentaBright.bgGreenBright`C`}${greenBright.bgMagentaBright`O`}${redBright.bgYellowBright`L`}${yellowBright.bgRedBright`O`}${redBright.bgCyanBright`R`}${yellowBright.bgBlueBright`S`}` + '\n' + +const out = `${bold`bold`} ${dim`dim`} ${italic`italic`} ${underline`underline`} ${strikethrough`strikethrough`} ${inverse`inverse`} ${bold.italic.underline.strike`bold italic underline strike`}` + + '\n' + + `${red`red`} ${green`green`} ${yellow`yellow`} ${blue`blue`} ${magenta`magenta`} ${cyan`cyan`} ${white`white`} ${gray`gray`} ${bold.yellow`bold yellow`} ${dim.cyan`dim cyan`} ${red.italic`italic red`} ` + + '\n' + + `${black.bgRed`bgRed`} ${black.bgGreen`bgGreen`} ${black.bgYellow`bgYellow`} ${bgBlue`bgBlue`} ${black.bgMagenta`bgMagenta`} ${black.bgCyan`bgCyan`} ${black.bgWhite`bgWhite`} ${black.bgRedBright`bgRedBright`} ${white.bgRed.bold.italic` CocaCola `}` + + '\n' + + `${greenBright`greenBright`} ${yellowBright`yellowBright`} ${blueBright`blueBright`} ${magentaBright`magentaBright`} ${cyanBright`cyanBright`} ${whiteBright`whiteBright`} ${greenBright`A`}${magentaBright`N`}${yellowBright`S`}${redBright`I`}` + + '\n' + + `${black.bgGreenBright`bgGreenBright`} ${black.bgYellowBright`bgYellowBright`} ${bgBlueBright`bgBlueBright`} ${black.bgMagentaBright`bgMagentaBright`} ${black.bgCyanBright`bgCyanBright`} ${magentaBright.bgGreenBright`C`}${greenBright.bgMagentaBright`O`}${redBright.bgYellowBright`L`}${yellowBright.bgRedBright`O`}${redBright.bgCyanBright`R`}${yellowBright.bgBlueBright`S`}` + + '\n' + [ '#d93611', '#d97511', @@ -52,7 +57,7 @@ const out = `${bold`bold`} ${dim`dim`} ${italic`italic`} ${underline`underline`} ' 141 ', ' 98 ', ' 92 ', - ].reduce((out, code) => out + black.bgAnsi(parseInt(code, 10))(code), '') + + ].reduce((out, code) => out + black.bgAnsi256(parseInt(code, 10))(code), '') + '\n '; export { out as ansisStylesDemo }; diff --git a/examples/index.js b/examples/index.js index 5d68784..327dd29 100755 --- a/examples/index.js +++ b/examples/index.js @@ -2,9 +2,8 @@ import chalk from 'chalk'; -import ansis, { Ansis } from '../src/index.js'; -import { - red, +import ansis, { + Ansis, red, green, blue, cyan, @@ -17,7 +16,7 @@ import { inverse, visible, hex, -} from '../src/colors.mjs'; +} from '../src/index.js'; import { ansi256Table } from './ansi256.js'; import { ansisLogo } from './ansis-logo.js'; diff --git a/package.json b/package.json index 20e2727..d307bba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ansis", - "version": "1.6.0-beta.0", + "version": "2.0.0", "description": "Colorize text in terminal or console output with ANSI colors & styles", "keywords": [ "ansi", @@ -59,14 +59,12 @@ "types": "./src/index.d.ts", "exports": { ".": { - "types": "./src/index.d.ts", - "import": "./src/index.js", - "require": "./src/index.js" + "import": "./index.js", + "require": "./index.js" }, "./colors": { - "types": "./colors.d.ts", - "import": "./colors.mjs", - "require": "./colors.cjs" + "import": "./index.js", + "require": "./index.js" } }, "scripts": { @@ -87,13 +85,12 @@ "devDependencies": { "@babel/core": "^7.23.2", "@babel/preset-env": "^7.23.2", + "@rollup/plugin-terser": "^0.4.4", "@types/jest": "^29.5.7", "jest": "^29.7.0", "prettier": "^3.0.3", - "rollup": "^2.79.0", - "rollup-plugin-cleanup": "^3.2.1", + "rollup": "^4.2.0", "rollup-plugin-copy": "^3.5.0", - "rollup-plugin-dts": "^4.2.3", - "rollup-plugin-terser": "^7.0.2" + "rollup-plugin-dts": "^6.1.0" } } diff --git a/package/index.js b/package/index.js deleted file mode 100644 index 8bad743..0000000 --- a/package/index.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -// This is the generic module loader for both ESM and CommonJS. -// Note: -// - the file will be coped to ./dist directory as index.js via rollup. -// - the `./dist/colors.js` file will be auto generated via rollup at `npm publish`. - -const colors = require('./colors.js'); -module.exports = colors.default; -module.exports.Ansis = colors.Ansis; diff --git a/package/package.json b/package/package.json index 50e2001..7dbf434 100644 --- a/package/package.json +++ b/package/package.json @@ -1,6 +1,6 @@ { "name": "ansis", - "version": "1.6.0-beta.0", + "version": "2.0.0", "description": "Colorize text in terminal or console output with ANSI colors & styles", "keywords": [ "ansi", @@ -54,14 +54,12 @@ "types": "./index.d.ts", "exports": { ".": { - "types": "./index.d.ts", "import": "./index.js", "require": "./index.js" }, "./colors": { - "types": "./colors.d.ts", - "import": "./colors.js", - "require": "./colors.js" + "import": "./index.js", + "require": "./index.js" } }, "engines": { @@ -70,8 +68,6 @@ "files": [ "index.d.ts", "index.js", - "colors.d.ts", - "colors.js", "package.json", "LICENSE", "README.md" diff --git a/rollup.config.js b/rollup.config.js index 3472096..44cf18e 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,7 +1,6 @@ +import terser from '@rollup/plugin-terser'; import copy from 'rollup-plugin-copy'; -import cleanup from 'rollup-plugin-cleanup'; import dts from 'rollup-plugin-dts'; -import { terser } from 'rollup-plugin-terser'; // last ECMA version compatible with node.js 12 const ecma = 2019; @@ -13,8 +12,7 @@ export default [ { intro: '/* Auto generated by rollup.\nUse `npm run build` to create new version. */', exports: 'named', - // note: this bundle needs the wrapper `./dist/index.js` to properly export defaults - file: './dist/colors.js', + file: './dist/index.js', format: 'cjs', }, ], @@ -27,7 +25,13 @@ export default [ }, toplevel: true, }), - + copy({ + targets: [ + { src: 'package/package.json', dest: 'dist/' }, + { src: 'README.md', dest: 'dist/' }, + { src: 'LICENSE', dest: 'dist/' }, + ], + }), ], }, @@ -43,39 +47,4 @@ export default [ dts(), ], }, - - { - input: 'src/colors.d.ts', - output: [ - { - file: 'dist/colors.d.ts', - format: 'es', - }, - ], - plugins: [ - dts(), - ], - }, - - { - input: 'package/index.js', - output: [ - { - file: 'dist/index.js', - format: 'cjs', - }, - ], - plugins: [ - copy({ - targets: [ - { src: 'package/package.json', dest: 'dist/' }, - { src: 'README.md', dest: 'dist/' }, - { src: 'LICENSE', dest: 'dist/' }, - ], - }), - cleanup({ - extensions: ['js', 'ts', 'mjs', 'cjs'], - }), - ], - }, ]; diff --git a/src/ansi-codes.js b/src/ansi-codes.js index 060d8e6..5cfacac 100644 --- a/src/ansi-codes.js +++ b/src/ansi-codes.js @@ -27,8 +27,9 @@ export const isSupported = (mockThis) => { // Deno: if interactive permission is not granted, do nothing, no colors } - const hasForceColor = 'FORCE_COLOR' in env; - const forceColorValue = env.FORCE_COLOR; + const FORCE_COLOR = 'FORCE_COLOR'; + const hasForceColor = FORCE_COLOR in env; + const forceColorValue = env[FORCE_COLOR]; const forceColor = forceColorValue === 'true' || parseInt(forceColorValue, 10) > 0; const isForceDisabled = 'NO_COLOR' in env diff --git a/src/colors.cjs b/src/colors.cjs deleted file mode 100644 index 5db7af5..0000000 --- a/src/colors.cjs +++ /dev/null @@ -1,74 +0,0 @@ -'use strict'; - -/** @type {Ansis} ansis */ -const ansis = require('./bundle').default; - -module.exports = { - ansi256: ansis.ansi256, - ansi: ansis.ansi, - fg: ansis.fg, - bgAnsi256: ansis.bgAnsi256, - bgAnsi: ansis.bgAnsi, - bg: ansis.bg, - rgb: ansis.rgb, - bgRgb: ansis.bgRgb, - hex: ansis.hex, - bgHex: ansis.bgHex, - - // misc - reset: ansis.reset, - inverse: ansis.inverse, - hidden: ansis.hidden, - visible: ansis.visible, - - // styles - bold: ansis.bold, - dim: ansis.dim, - faint: ansis.faint, - italic: ansis.italic, - underline: ansis.underline, - doubleUnderline: ansis.doubleUnderline, - strikethrough: ansis.strikethrough, - strike: ansis.strike, - frame: ansis.frame, - encircle: ansis.encircle, - overline: ansis.overline, - - // foreground colors - black: ansis.black, - red: ansis.red, - green: ansis.green, - yellow: ansis.yellow, - blue: ansis.blue, - magenta: ansis.magenta, - cyan: ansis.cyan, - white: ansis.white, - gray: ansis.gray, - grey: ansis.grey, - blackBright: ansis.blackBright, - redBright: ansis.redBright, - greenBright: ansis.greenBright, - yellowBright: ansis.yellowBright, - blueBright: ansis.blueBright, - magentaBright: ansis.magentaBright, - cyanBright: ansis.cyanBright, - whiteBright: ansis.whiteBright, - - // background colors - bgBlack: ansis.bgBlack, - bgRed: ansis.bgRed, - bgGreen: ansis.bgGreen, - bgYellow: ansis.bgYellow, - bgBlue: ansis.bgBlue, - bgMagenta: ansis.bgMagenta, - bgCyan: ansis.bgCyan, - bgWhite: ansis.bgWhite, - bgBlackBright: ansis.bgBlackBright, - bgRedBright: ansis.bgRedBright, - bgGreenBright: ansis.bgGreenBright, - bgYellowBright: ansis.bgYellowBright, - bgBlueBright: ansis.bgBlueBright, - bgMagentaBright: ansis.bgMagentaBright, - bgCyanBright: ansis.bgCyanBright, - bgWhiteBright: ansis.bgWhiteBright, -}; diff --git a/src/colors.d.ts b/src/colors.d.ts deleted file mode 100644 index 737b7be..0000000 --- a/src/colors.d.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { Ansis } from './index'; - -export declare function ansi256(code: number): Ansis; -export declare function ansi(code: number): Ansis; -export declare function fg(code: number): Ansis; -export declare function bgAnsi256(code: number): Ansis; -export declare function bgAnsi(code: number): Ansis; -export declare function bg(code: number): Ansis; -export declare function rgb(red: number, green: number, blue: number): Ansis; -export declare function bgRgb(red: number, green: number, blue: number): Ansis; -export declare function hex(code: string): Ansis; -export declare function bgHex(code: string): Ansis; - -// misc -export declare const reset: Ansis; -export declare const inverse: Ansis; -export declare const hidden: Ansis; -export declare const visible: Ansis; - -// styles -export declare const bold: Ansis; -export declare const dim: Ansis; -export declare const faint: Ansis; -export declare const italic: Ansis; -export declare const underline: Ansis; -export declare const doubleUnderline: Ansis; -export declare const strikethrough: Ansis; -export declare const strike: Ansis; -export declare const frame: Ansis; -export declare const encircle: Ansis; -export declare const overline: Ansis; - -// foreground colors -export declare const black: Ansis; -export declare const red: Ansis; -export declare const green: Ansis; -export declare const yellow: Ansis; -export declare const blue: Ansis; -export declare const magenta: Ansis; -export declare const cyan: Ansis; -export declare const white: Ansis; -export declare const gray: Ansis; -export declare const grey: Ansis; -export declare const blackBright: Ansis; -export declare const redBright: Ansis; -export declare const greenBright: Ansis; -export declare const yellowBright: Ansis; -export declare const blueBright: Ansis; -export declare const magentaBright: Ansis; -export declare const cyanBright: Ansis; -export declare const whiteBright: Ansis; - -// background colors -export declare const bgBlack: Ansis; -export declare const bgRed: Ansis; -export declare const bgGreen: Ansis; -export declare const bgYellow: Ansis; -export declare const bgBlue: Ansis; -export declare const bgMagenta: Ansis; -export declare const bgCyan: Ansis; -export declare const bgWhite: Ansis; -export declare const bgBlackBright: Ansis; -export declare const bgRedBright: Ansis; -export declare const bgGreenBright: Ansis; -export declare const bgYellowBright: Ansis; -export declare const bgBlueBright: Ansis; -export declare const bgMagentaBright: Ansis; -export declare const bgCyanBright: Ansis; -export declare const bgWhiteBright: Ansis; diff --git a/src/colors.mjs b/src/colors.mjs deleted file mode 100644 index b3c1f46..0000000 --- a/src/colors.mjs +++ /dev/null @@ -1,72 +0,0 @@ -'use strict'; - -/** @type {Ansis} ansis */ -import ansis from './index.js'; - -export const ansi256 = ansis.ansi256; -export const ansi = ansis.ansi; -export const fg = ansis.fg; -export const bgAnsi256 = ansis.bgAnsi256; -export const bgAnsi = ansis.bgAnsi; -export const bg = ansis.bg; -export const rgb = ansis.rgb; -export const bgRgb = ansis.bgRgb; -export const hex = ansis.hex; -export const bgHex = ansis.bgHex; - -// misc -export const reset = ansis.reset; -export const inverse = ansis.inverse; -export const hidden = ansis.hidden; -export const visible = ansis.visible; - -// styles -export const bold = ansis.bold; -export const dim = ansis.dim; -export const faint = ansis.faint; -export const italic = ansis.italic; -export const underline = ansis.underline; -export const doubleUnderline = ansis.doubleUnderline; -export const strikethrough = ansis.strikethrough; -export const strike = ansis.strike; -export const frame = ansis.frame; -export const encircle = ansis.encircle; -export const overline = ansis.overline; - -// foreground colors -export const black = ansis.black; -export const red = ansis.red; -export const green = ansis.green; -export const yellow = ansis.yellow; -export const blue = ansis.blue; -export const magenta = ansis.magenta; -export const cyan = ansis.cyan; -export const white = ansis.white; -export const gray = ansis.gray; -export const grey = ansis.grey; -export const blackBright = ansis.blackBright; -export const redBright = ansis.redBright; -export const greenBright = ansis.greenBright; -export const yellowBright = ansis.yellowBright; -export const blueBright = ansis.blueBright; -export const magentaBright = ansis.magentaBright; -export const cyanBright = ansis.cyanBright; -export const whiteBright = ansis.whiteBright; - -// background colors -export const bgBlack = ansis.bgBlack; -export const bgRed = ansis.bgRed; -export const bgGreen = ansis.bgGreen; -export const bgYellow = ansis.bgYellow; -export const bgBlue = ansis.bgBlue; -export const bgMagenta = ansis.bgMagenta; -export const bgCyan = ansis.bgCyan; -export const bgWhite = ansis.bgWhite; -export const bgBlackBright = ansis.bgBlackBright; -export const bgRedBright = ansis.bgRedBright; -export const bgGreenBright = ansis.bgGreenBright; -export const bgYellowBright = ansis.bgYellowBright; -export const bgBlueBright = ansis.bgBlueBright; -export const bgMagentaBright = ansis.bgMagentaBright; -export const bgCyanBright = ansis.bgCyanBright; -export const bgWhiteBright = ansis.bgWhiteBright; diff --git a/src/index.d.ts b/src/index.d.ts index e768b60..144d304 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,4 +1,4 @@ -type ColorExtend = Record +type ColorExtend = Record interface Ansis { /** @@ -203,7 +203,7 @@ interface Ansis { } declare const ansis: Ansis; -declare const Ansis: new (str: string) => Ansis; +declare const Ansis: new () => Ansis; /** * Base ANSI Styles @@ -271,3 +271,76 @@ type StringUnion = T | (B & Record); type AnsiColorsExtend = StringUnion; export { AnsiColors, AnsiColorsExtend, AnsiStyles, Ansis, ansis as default }; + +export declare function ansi256(code: number): Ansis; + +export declare function fg(code: number): Ansis; + +export declare function bgAnsi256(code: number): Ansis; + +export declare function bg(code: number): Ansis; + +export declare function rgb(red: number, green: number, blue: number): Ansis; + +export declare function bgRgb(red: number, green: number, blue: number): Ansis; + +export declare function hex(code: string): Ansis; + +export declare function bgHex(code: string): Ansis; + +// misc +export declare const reset: Ansis; +export declare const inverse: Ansis; +export declare const hidden: Ansis; +export declare const visible: Ansis; + +// styles +export declare const bold: Ansis; +export declare const dim: Ansis; +export declare const faint: Ansis; +export declare const italic: Ansis; +export declare const underline: Ansis; +export declare const doubleUnderline: Ansis; +export declare const strikethrough: Ansis; +export declare const strike: Ansis; +export declare const frame: Ansis; +export declare const encircle: Ansis; +export declare const overline: Ansis; + +// foreground colors +export declare const black: Ansis; +export declare const red: Ansis; +export declare const green: Ansis; +export declare const yellow: Ansis; +export declare const blue: Ansis; +export declare const magenta: Ansis; +export declare const cyan: Ansis; +export declare const white: Ansis; +export declare const gray: Ansis; +export declare const grey: Ansis; +export declare const blackBright: Ansis; +export declare const redBright: Ansis; +export declare const greenBright: Ansis; +export declare const yellowBright: Ansis; +export declare const blueBright: Ansis; +export declare const magentaBright: Ansis; +export declare const cyanBright: Ansis; +export declare const whiteBright: Ansis; + +// background colors +export declare const bgBlack: Ansis; +export declare const bgRed: Ansis; +export declare const bgGreen: Ansis; +export declare const bgYellow: Ansis; +export declare const bgBlue: Ansis; +export declare const bgMagenta: Ansis; +export declare const bgCyan: Ansis; +export declare const bgWhite: Ansis; +export declare const bgBlackBright: Ansis; +export declare const bgRedBright: Ansis; +export declare const bgGreenBright: Ansis; +export declare const bgYellowBright: Ansis; +export declare const bgBlueBright: Ansis; +export declare const bgMagentaBright: Ansis; +export declare const bgCyanBright: Ansis; +export declare const bgWhiteBright: Ansis; diff --git a/src/index.js b/src/index.js index c3e4fa6..e9e7fc7 100644 --- a/src/index.js +++ b/src/index.js @@ -7,11 +7,9 @@ import { baseStyles, fnAnsi256, fnBgAnsi256, fnRgb, fnBgRgb } from './ansi-codes * @property {string} close * @property {string?} openStack * @property {string?} closeStack - * @property {null | AnsisProps} parent + * @property {null | AnsisProps} props */ -//Object.defineProperty(exports, '__esModule', { value: !0 }); - const { defineProperty, defineProperties, setPrototypeOf } = Object; const stripANSIRegEx = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; @@ -84,7 +82,7 @@ const createStyle = ({ props }, { open, close }) => { } setPrototypeOf(style, stylePrototype); - style.props = { open, close, openStack, closeStack, parent: props }; + style.props = { open, close, openStack, closeStack, props: props }; style.open = openStack; style.close = closeStack; @@ -108,7 +106,7 @@ const wrap = (strings, values, props) => { if (~string.indexOf('\x1b')) { while (props !== undefined) { string = strReplaceAll(string, props.close, props.open); - props = props.parent; + props = props.props; } } @@ -156,70 +154,73 @@ const ansis = new Ansis(); export { Ansis, ansis as default }; -export const ansi256 = ansis.ansi256; -export const ansi = ansis.ansi; -export const fg = ansis.fg; -export const bgAnsi256 = ansis.bgAnsi256; -export const bgAnsi = ansis.bgAnsi; -export const bg = ansis.bg; -export const rgb = ansis.rgb; -export const bgRgb = ansis.bgRgb; -export const hex = ansis.hex; -export const bgHex = ansis.bgHex; - -// misc -export const reset = ansis.reset; -export const inverse = ansis.inverse; -export const hidden = ansis.hidden; -export const visible = ansis.visible; - -// styles -export const bold = ansis.bold; -export const dim = ansis.dim; -export const faint = ansis.faint; -export const italic = ansis.italic; -export const underline = ansis.underline; -export const doubleUnderline = ansis.doubleUnderline; -export const strikethrough = ansis.strikethrough; -export const strike = ansis.strike; -export const frame = ansis.frame; -export const encircle = ansis.encircle; -export const overline = ansis.overline; - -// foreground colors -export const black = ansis.black; -export const red = ansis.red; -export const green = ansis.green; -export const yellow = ansis.yellow; -export const blue = ansis.blue; -export const magenta = ansis.magenta; -export const cyan = ansis.cyan; -export const white = ansis.white; -export const gray = ansis.gray; -export const grey = ansis.grey; -export const blackBright = ansis.blackBright; -export const redBright = ansis.redBright; -export const greenBright = ansis.greenBright; -export const yellowBright = ansis.yellowBright; -export const blueBright = ansis.blueBright; -export const magentaBright = ansis.magentaBright; -export const cyanBright = ansis.cyanBright; -export const whiteBright = ansis.whiteBright; - -// background colors -export const bgBlack = ansis.bgBlack; -export const bgRed = ansis.bgRed; -export const bgGreen = ansis.bgGreen; -export const bgYellow = ansis.bgYellow; -export const bgBlue = ansis.bgBlue; -export const bgMagenta = ansis.bgMagenta; -export const bgCyan = ansis.bgCyan; -export const bgWhite = ansis.bgWhite; -export const bgBlackBright = ansis.bgBlackBright; -export const bgRedBright = ansis.bgRedBright; -export const bgGreenBright = ansis.bgGreenBright; -export const bgYellowBright = ansis.bgYellowBright; -export const bgBlueBright = ansis.bgBlueBright; -export const bgMagentaBright = ansis.bgMagentaBright; -export const bgCyanBright = ansis.bgCyanBright; -export const bgWhiteBright = ansis.bgWhiteBright; +export const { + // color functions + ansi256, + ansi, + fg, + bgAnsi256, + bgAnsi, + bg, + rgb, + bgRgb, + hex, + bgHex, + + // misc + reset, + inverse, + hidden, + visible, + + // styles + bold, + dim, + faint, + italic, + underline, + doubleUnderline, + strikethrough, + strike, + frame, + encircle, + overline, + + // foreground colors + black, + red, + green, + yellow, + blue, + magenta, + cyan, + white, + gray, + grey, + blackBright, + redBright, + greenBright, + yellowBright, + blueBright, + magentaBright, + cyanBright, + whiteBright, + + // background colors + bgBlack, + bgRed, + bgGreen, + bgYellow, + bgBlue, + bgMagenta, + bgCyan, + bgWhite, + bgBlackBright, + bgRedBright, + bgGreenBright, + bgYellowBright, + bgBlueBright, + bgMagentaBright, + bgCyanBright, + bgWhiteBright, +} = ansis; diff --git a/src/utils.js b/src/utils.js index 4b72325..ae292ad 100644 --- a/src/utils.js +++ b/src/utils.js @@ -70,8 +70,8 @@ export const strReplaceAll = (str, searchValue, replaceValue) => { /** * The style must be break at the end of the line and continued on a new line. * TODO: compare with str.replace(/(\r*\n)/g, props.closeStack + '$1' + props.openStack); - * - compatibility in windows - * - performance + * - check compatibility in windows + * - check performance * - delete this function if replace() is faster * * @param {string} str The string containing linebreaks. diff --git a/test/index.test.js b/test/index.test.js index a60630d..0e5c02a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,11 +1,10 @@ -import {execSync} from 'child_process'; +import { execSync } from 'child_process'; import path from 'path'; // test distributed version -import ansis, {Ansis} from '../src/index.js'; -import {hexToRgb, clamp} from '../src/utils.js'; -import {isSupported} from '../src/ansi-codes.js'; -import {green, red, yellow} from '../src/colors.mjs'; +import { hexToRgb, clamp } from '../src/utils.js'; +import { isSupported } from '../src/ansi-codes.js'; +import ansis, { Ansis, green, red, yellow } from '../src/index.js'; const TEST_PATH = path.resolve('./test/'); @@ -223,13 +222,6 @@ describe('style tests', () => { done(); }); - test(`ansis.ansi(97)`, (done) => { - const received = ansis.ansi(97)('foo'); - const expected = '\x1b[38;5;97mfoo\x1b[39m'; - expect(esc(received)).toEqual(esc(expected)); - done(); - }); - test(`ansis.bgAnsi256(97)`, (done) => { const received = ansis.bgAnsi256(97)('foo'); const expected = '\x1b[48;5;97mfoo\x1b[49m'; @@ -237,13 +229,6 @@ describe('style tests', () => { done(); }); - test(`ansis.bgAnsi(97)`, (done) => { - const received = ansis.bgAnsi(97)('foo'); - const expected = '\x1b[48;5;97mfoo\x1b[49m'; - expect(esc(received)).toEqual(esc(expected)); - done(); - }); - test(`ansis.green('\nHello\nNew line\nNext new line.\n')`, (done) => { const received = ansis.green('\nHello\nNew line\nNext new line.\n'); const expected = `\x1b[32m\x1b[39m @@ -380,7 +365,7 @@ describe('template literals tests', () => { describe('extend base colors tests', () => { test('imported ansis`', (done) => { - ansis.extend({orange: '#FFAB40'}); + ansis.extend({ orange: '#FFAB40' }); const received = ansis.orange.bold('text'); const expected = '\x1b[38;2;255;171;64m\x1b[1mtext\x1b[22m\x1b[39m'; @@ -390,7 +375,7 @@ describe('extend base colors tests', () => { test('new ansis`', (done) => { const ansis = new Ansis(); - ansis.extend({orange: '#FFAB40'}); + ansis.extend({ orange: '#FFAB40' }); // test the order bold > orange const received = ansis.bold.orange('text'); @@ -434,10 +419,10 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {TERM: 'xterm'}, + env: { TERM: 'xterm' }, argv: [], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); const expected = true; @@ -463,7 +448,7 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {CI: 'GITLAB_CI'}, + env: { CI: 'GITLAB_CI' }, argv: [], }, @@ -476,10 +461,10 @@ describe('Node.JS isSupported', () => { test(`no colors, unsupported terminal`, (done) => { const received = isSupported({ process: { - env: {TERM: 'dumb'}, + env: { TERM: 'dumb' }, argv: [], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); @@ -491,7 +476,7 @@ describe('Node.JS isSupported', () => { test(`no colors, simulate output in file > log.txt`, (done) => { const received = isSupported({ process: { - env: {TERM: 'xterm'}, + env: { TERM: 'xterm' }, argv: [], }, @@ -533,10 +518,10 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {TERM: 'dumb'}, + env: { TERM: 'dumb' }, argv: ['--color=true'], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); @@ -549,10 +534,10 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {TERM: 'dumb'}, + env: { TERM: 'dumb' }, argv: ['-color=true'], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); @@ -565,10 +550,10 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {TERM: 'xterm'}, + env: { TERM: 'xterm' }, argv: ['--color=false'], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); @@ -581,10 +566,10 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {NO_COLOR: '1', TERM: 'xterm'}, + env: { NO_COLOR: '1', TERM: 'xterm' }, argv: [], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); @@ -597,10 +582,10 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {FORCE_COLOR: '0', TERM: 'xterm'}, + env: { FORCE_COLOR: '0', TERM: 'xterm' }, argv: [], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); @@ -613,10 +598,10 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {FORCE_COLOR: 'false', TERM: 'xterm'}, + env: { FORCE_COLOR: 'false', TERM: 'xterm' }, argv: [], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); @@ -629,7 +614,7 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {FORCE_COLOR: '1'}, + env: { FORCE_COLOR: '1' }, argv: [], }, @@ -643,7 +628,7 @@ describe('Node.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {FORCE_COLOR: 'true'}, + env: { FORCE_COLOR: 'true' }, argv: [], }, @@ -661,7 +646,7 @@ describe('Deno isSupported', () => { const received = isSupported({ Deno: { env: { - toObject: () => ({TERM: 'xterm-256color'}), + toObject: () => ({ TERM: 'xterm-256color' }), }, args: [], build: { @@ -680,7 +665,7 @@ describe('Deno isSupported', () => { const received = isSupported({ Deno: { env: { - toObject: () => ({TERM: ''}), + toObject: () => ({ TERM: '' }), }, args: [], build: { @@ -699,7 +684,7 @@ describe('Deno isSupported', () => { const received = isSupported({ Deno: { env: { - toObject: () => ({FORCE_COLOR: 1}), + toObject: () => ({ FORCE_COLOR: 1 }), }, args: [], build: { @@ -740,7 +725,7 @@ describe('Next.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {NEXT_RUNTIME: 'experimental-edge', TERM: 'xterm-256color'}, + env: { NEXT_RUNTIME: 'experimental-edge', TERM: 'xterm-256color' }, argv: [], }, @@ -754,7 +739,7 @@ describe('Next.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {NEXT_RUNTIME: 'edge', TERM: 'xterm-256color'}, + env: { NEXT_RUNTIME: 'edge', TERM: 'xterm-256color' }, argv: [], }, @@ -768,10 +753,10 @@ describe('Next.JS isSupported', () => { const received = isSupported({ process: { platform: 'linux', - env: {NEXT_RUNTIME: 'nodejs', TERM: 'xterm-256color'}, + env: { NEXT_RUNTIME: 'nodejs', TERM: 'xterm-256color' }, argv: [], - stdout: {isTTY: true}, - stderr: {isTTY: true}, + stdout: { isTTY: true }, + stderr: { isTTY: true }, }, }); diff --git a/test/package/cjs/index.js b/test/package/cjs/index.js index 9307cca..c947c78 100644 --- a/test/package/cjs/index.js +++ b/test/package/cjs/index.js @@ -1,9 +1,24 @@ const ansis = require('ansis'); -const { red, yellow, green } = require('ansis/colors'); + +// test new named import +const { Ansis, red, yellow, green } = require('ansis'); + +// test old named import +const { magenta, cyan, blue } = require('ansis/colors'); const log = console.log; -log(ansis.green.inverse('CommonJS')); +const ansis2 = new Ansis(); + +log(ansis.green.inverse(' CommonJS ')); +log(ansis2.bgCyanBright('const ansis = new Ansis();')); + +log(`--- NEW named import: ${yellow`const { red, yellow, green } = require('ansis');`}`); log(red('red')); log(green.bold('green bold')); -log(yellow.italic`yellow italic`); \ No newline at end of file +log(yellow.italic`yellow italic`); + +log(`--- OLD named import: ${yellow`const { magenta, cyan, blue } = require('ansis/colors');`}`); +log(magenta`magenta`); +log(cyan.bold`cyan bold`); +log(blue.italic`blue italic`); \ No newline at end of file diff --git a/test/package/esm/index.js b/test/package/esm/index.js index 1821bdb..0be9710 100644 --- a/test/package/esm/index.js +++ b/test/package/esm/index.js @@ -1,9 +1,22 @@ -import ansis from 'ansis'; -import { red, yellow, green } from 'ansis/colors'; +// test new named import +import ansis, { Ansis, red, yellow, green } from 'ansis'; + +// test old named import +import { magenta, cyan, blue } from 'ansis/colors'; const log = console.log; -log(ansis.green.inverse('ESM')); +const ansis2 = new Ansis(); + +log(ansis.green.inverse(' ESM ')); +log(ansis2.bgCyanBright('const ansis = new Ansis();')); + +log(`--- NEW named import: ${yellow`import { red, yellow, green } from 'ansis';`}`); log(red('red')); log(green.bold('green bold')); -log(yellow.italic`yellow italic`); \ No newline at end of file +log(yellow.italic`yellow italic`); + +log(`--- OLD named import: ${yellow`import { magenta, cyan, blue } from 'ansis/colors';`}`); +log(magenta`magenta`); +log(cyan.bold`cyan bold`); +log(blue.italic`blue italic`); \ No newline at end of file diff --git a/test/package/ts/index.js b/test/package/ts/index.js index 0ca089a..0ea34eb 100644 --- a/test/package/ts/index.js +++ b/test/package/ts/index.js @@ -1,5 +1,4 @@ -import ansis from 'ansis'; -import { red, green, blue, yellow, magenta } from 'ansis/colors'; +import ansis, { red, green, blue, yellow, magenta } from 'ansis'; const log = console.log; const pink = ansis.hex('#FF75D1'); diff --git a/test/package/ts/index.ts b/test/package/ts/index.ts index cecaaeb..90e6371 100644 --- a/test/package/ts/index.ts +++ b/test/package/ts/index.ts @@ -1,18 +1,23 @@ -import ansis, {AnsiColorsExtend} from 'ansis'; -import {red, green, blue, yellow, magenta} from 'ansis/colors'; +// NEW named import, >= v1.6.0 +import ansis, { Ansis, AnsiColorsExtend, red, green, blue, yellow, magenta } from 'ansis'; const log = console.log; const pink = ansis.hex('#FF75D1'); +// create new instance +const ansis2 = new Ansis(); +log(ansis2.cyan('new instance')); + // Extend base colors ansis.extend({ - pink: '#FF75D1', - orange: '#FFAB40', + pink: '#FF75D1', + orange: '#FFAB40', }); +// `AnsiColorsExtend` is extendable type for TS to add a custom color const write = (style: AnsiColorsExtend<'pink' | 'orange'>, message: string) => { - console.log(ansis[style](message)); -} + console.log(ansis[style](message)); +}; write('red', 'message'); // default style OK write('pink', 'message'); // extended style OK