diff --git a/.gitignore b/.gitignore index cd87926..623fddc 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ coverage # Misc node_modules +/*.cjs *.xml .DS_Store example diff --git a/README.md b/README.md index 684e589..dde8475 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ npm i colorette Import the [styles](#styles) you need. [Here](#supported-styles)'s the list of styles you can use. ```js -const { red, blue, bold } = require("colorette") +import { red, blue, bold } from "colorette" ``` Wrap your strings in one or more styles to produce the finish you're looking for. @@ -87,7 +87,7 @@ red("Red Alert") //=> \u001b[31mRed Alert\u001b[39m Color will be enabled if your terminal supports it, `FORCE_COLOR` is defined in [`process.env`](https://nodejs.org/dist/latest-v8.x/docs/api/process.html#process_process_env) and if `NO_COLOR` isn't, but you can always override it if you want. ```js -const { options } = require("colorette") +import { options } from "colorette" options.enabled = false ``` diff --git a/index.js b/index.js index 7fd570b..d3f7713 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,3 @@ -"use strict" - let enabled = !("NO_COLOR" in process.env) && ("FORCE_COLOR" in process.env || @@ -27,93 +25,49 @@ const init = (open, close) => { ) } -const options = Object.defineProperty({}, "enabled", { +export const options = Object.defineProperty({}, "enabled", { get: () => enabled, set: (value) => (enabled = value), }) -const reset = init(0, 0) -const bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m") -const dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m") -const italic = init(3, 23) -const underline = init(4, 24) -const inverse = init(7, 27) -const hidden = init(8, 28) -const strikethrough = init(9, 29) -const black = init(30, 39) -const red = init(31, 39) -const green = init(32, 39) -const yellow = init(33, 39) -const blue = init(34, 39) -const magenta = init(35, 39) -const cyan = init(36, 39) -const white = init(37, 39) -const gray = init(90, 39) -const bgBlack = init(40, 49) -const bgRed = init(41, 49) -const bgGreen = init(42, 49) -const bgYellow = init(43, 49) -const bgBlue = init(44, 49) -const bgMagenta = init(45, 49) -const bgCyan = init(46, 49) -const bgWhite = init(47, 49) -const blackBright = init(90, 39) -const redBright = init(91, 39) -const greenBright = init(92, 39) -const yellowBright = init(93, 39) -const blueBright = init(94, 39) -const magentaBright = init(95, 39) -const cyanBright = init(96, 39) -const whiteBright = init(97, 39) -const bgBlackBright = init(100, 49) -const bgRedBright = init(101, 49) -const bgGreenBright = init(102, 49) -const bgYellowBright = init(103, 49) -const bgBlueBright = init(104, 49) -const bgMagentaBright = init(105, 49) -const bgCyanBright = init(106, 49) -const bgWhiteBright = init(107, 49) -module.exports = { - options, - reset, - bold, - dim, - italic, - underline, - inverse, - hidden, - strikethrough, - black, - red, - green, - yellow, - blue, - magenta, - cyan, - white, - gray, - bgBlack, - bgRed, - bgGreen, - bgYellow, - bgBlue, - bgMagenta, - bgCyan, - bgWhite, - blackBright, - redBright, - greenBright, - yellowBright, - blueBright, - magentaBright, - cyanBright, - whiteBright, - bgBlackBright, - bgRedBright, - bgGreenBright, - bgYellowBright, - bgBlueBright, - bgMagentaBright, - bgCyanBright, - bgWhiteBright, -} +export const reset = init(0, 0) +export const bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m") +export const dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m") +export const italic = init(3, 23) +export const underline = init(4, 24) +export const inverse = init(7, 27) +export const hidden = init(8, 28) +export const strikethrough = init(9, 29) +export const black = init(30, 39) +export const red = init(31, 39) +export const green = init(32, 39) +export const yellow = init(33, 39) +export const blue = init(34, 39) +export const magenta = init(35, 39) +export const cyan = init(36, 39) +export const white = init(37, 39) +export const gray = init(90, 39) +export const bgBlack = init(40, 49) +export const bgRed = init(41, 49) +export const bgGreen = init(42, 49) +export const bgYellow = init(43, 49) +export const bgBlue = init(44, 49) +export const bgMagenta = init(45, 49) +export const bgCyan = init(46, 49) +export const bgWhite = init(47, 49) +export const blackBright = init(90, 39) +export const redBright = init(91, 39) +export const greenBright = init(92, 39) +export const yellowBright = init(93, 39) +export const blueBright = init(94, 39) +export const magentaBright = init(95, 39) +export const cyanBright = init(96, 39) +export const whiteBright = init(97, 39) +export const bgBlackBright = init(100, 49) +export const bgRedBright = init(101, 49) +export const bgGreenBright = init(102, 49) +export const bgYellowBright = init(103, 49) +export const bgBlueBright = init(104, 49) +export const bgMagentaBright = init(105, 49) +export const bgCyanBright = init(106, 49) +export const bgWhiteBright = init(107, 49) diff --git a/package.json b/package.json index 29cc1ed..4a19188 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,28 @@ "name": "colorette", "version": "1.2.0", "description": "Color your terminal using pure idiomatic JavaScript.", - "main": "index.js", + "main": "index.cjs", + "type": "module", + "module": "index.js", + "exports": { + "./package.json": "./package.json", + ".": { + "require": "./index.cjs", + "import": "./index.js" + } + }, "types": "colorette.d.ts", "scripts": { - "test": "nyc -r lcov testmatrix test/index.js", - "release": "v=$npm_package_version; git commit -am $v && git tag -s $v -m $v && git push && git push --tags && npx dual-publish" + "test": "c8 testmatrix test/*.cjs", + "build": "node -e \"fs.writeFileSync('index.cjs',fs.readFileSync('index.js','utf8').replace(/export const /g,'exports.'),'utf8')\"", + "release": "v=$npm_package_version; git commit -am $v && git tag -s $v -m $v && git push && git push --tags && npm publish" }, "repository": { "type": "git", "url": "jorgebucaran/colorette" }, "files": [ - "index.js", + "index.*", "colorette.d.ts" ], "keywords": [ @@ -30,8 +40,7 @@ }, "homepage": "https://github.com/jorgebucaran/colorette", "devDependencies": { - "dual-publish": "^0.10.6", - "nyc": "15.0.1", + "c8": "7.2.0", "testmatrix": "0.1.2" } } diff --git a/test/FORCE_COLOR.sh b/test/FORCE_COLOR.sh index 6a250dc..0db01fc 100644 --- a/test/FORCE_COLOR.sh +++ b/test/FORCE_COLOR.sh @@ -1,3 +1,3 @@ #!/bin/sh -[ "$(echo `FORCE_COLOR= node -e 'console.log(require(".").blue("ok"))' | strings`)" = "[34mok [39m" ] || exit 1 +[ "$(echo `FORCE_COLOR= node -e 'console.log(require(".").blue("hello"))' | strings`)" = "[34mhello [39m" ] || exit 1 \ No newline at end of file diff --git a/test/NO_COLOR.sh b/test/NO_COLOR.sh index ff3fd40..cc349d4 100755 --- a/test/NO_COLOR.sh +++ b/test/NO_COLOR.sh @@ -1,3 +1,3 @@ #!/bin/sh -[ `NO_COLOR= node -e 'console.log(require(".").blue("ok"))'` = "ok" ] || exit 1 +[ `NO_COLOR= node -e 'console.log(require(".").blue("hello"))'` = "hello" ] || exit 1 \ No newline at end of file diff --git a/test/index.js b/test/index.cjs similarity index 92% rename from test/index.js rename to test/index.cjs index 3eafb31..67f3103 100644 --- a/test/index.js +++ b/test/index.cjs @@ -1,5 +1,5 @@ const c = require("..") -const equal = require("testmatrix").equal +const { equal } = require("testmatrix") const EqualTest = (actual, expected) => ({ name: actual, @@ -86,11 +86,7 @@ exports.default = { }, ], "env variables": [ - ScriptTest( - "`FORCE_COLOR=` forces color even through a shell pipeline in a TTY", - "sh", - "FORCE_COLOR.sh" - ), - ScriptTest("`NO_COLOR=` disables color", "sh", "NO_COLOR.sh"), + ScriptTest("`FORCE_COLOR` in the env forces color", "sh", "FORCE_COLOR.sh"), + ScriptTest("`NO_COLOR` in the env disables color", "sh", "NO_COLOR.sh"), ], }