From b55239ebcd2b56b39e8eb3cb3eb371a82033c6fe Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Sun, 28 Jun 2020 02:22:40 +0900 Subject: [PATCH 1/7] ESM support via minimal npm script --- .gitignore | 1 + index.js | 130 ++++++++++++++------------------------------ package.json | 18 ++++-- test/FORCE_COLOR.sh | 2 +- test/NO_COLOR.sh | 2 +- test/index.js | 96 -------------------------------- 6 files changed, 59 insertions(+), 190 deletions(-) delete mode 100644 test/index.js diff --git a/.gitignore b/.gitignore index cd87926..6b38e29 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ coverage # Misc node_modules +*.cjs *.xml .DS_Store example 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..a9a6ea4 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,20 @@ "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", + "test": "nyc -r lcov 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 && npx dual-publish" }, "repository": { @@ -13,7 +23,7 @@ "url": "jorgebucaran/colorette" }, "files": [ - "index.js", + "index.*", "colorette.d.ts" ], "keywords": [ @@ -30,8 +40,8 @@ }, "homepage": "https://github.com/jorgebucaran/colorette", "devDependencies": { - "dual-publish": "^0.10.6", "nyc": "15.0.1", + "esm": "3.2.25", "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.js deleted file mode 100644 index 3eafb31..0000000 --- a/test/index.js +++ /dev/null @@ -1,96 +0,0 @@ -const c = require("..") -const equal = require("testmatrix").equal - -const EqualTest = (actual, expected) => ({ - name: actual, - assert: equal, - actual, - expected, -}) - -const ScriptTest = (name, cmd, script) => ({ - name, - assert: equal, - actual: (done) => { - const exec = require("child_process").exec - exec(`${cmd} ${__dirname}/${script}`, done) - }, - expected: null, -}) - -const StyleTest = (name, open, close) => - EqualTest(c[name](name), open + name + close) - -exports.default = { - "using styles": [ - ["reset", "\x1b[0m", "\x1b[0m"], - ["bold", "\x1b[1m", "\x1b[22m"], - ["dim", "\x1b[2m", "\x1b[22m"], - ["italic", "\x1b[3m", "\x1b[23m"], - ["underline", "\x1b[4m", "\x1b[24m"], - ["inverse", "\x1b[7m", "\x1b[27m"], - ["hidden", "\x1b[8m", "\x1b[28m"], - ["strikethrough", "\x1b[9m", "\x1b[29m"], - ["black", "\x1b[30m", "\x1b[39m"], - ["red", "\x1b[31m", "\x1b[39m"], - ["green", "\x1b[32m", "\x1b[39m"], - ["yellow", "\x1b[33m", "\x1b[39m"], - ["blue", "\x1b[34m", "\x1b[39m"], - ["magenta", "\x1b[35m", "\x1b[39m"], - ["cyan", "\x1b[36m", "\x1b[39m"], - ["white", "\x1b[37m", "\x1b[39m"], - ["gray", "\x1b[90m", "\x1b[39m"], - ["bgBlack", "\x1b[40m", "\x1b[49m"], - ["bgRed", "\x1b[41m", "\x1b[49m"], - ["bgGreen", "\x1b[42m", "\x1b[49m"], - ["bgYellow", "\x1b[43m", "\x1b[49m"], - ["bgBlue", "\x1b[44m", "\x1b[49m"], - ["bgMagenta", "\x1b[45m", "\x1b[49m"], - ["bgCyan", "\x1b[46m", "\x1b[49m"], - ["bgWhite", "\x1b[47m", "\x1b[49m"], - ["blackBright", "\x1b[90m", "\x1b[39m"], - ["redBright", "\x1b[91m", "\x1b[39m"], - ["greenBright", "\x1b[92m", "\x1b[39m"], - ["yellowBright", "\x1b[93m", "\x1b[39m"], - ["blueBright", "\x1b[94m", "\x1b[39m"], - ["magentaBright", "\x1b[95m", "\x1b[39m"], - ["cyanBright", "\x1b[96m", "\x1b[39m"], - ["whiteBright", "\x1b[97m", "\x1b[39m"], - ["bgBlackBright", "\x1b[100m", "\x1b[49m"], - ["bgRedBright", "\x1b[101m", "\x1b[49m"], - ["bgGreenBright", "\x1b[102m", "\x1b[49m"], - ["bgYellowBright", "\x1b[103m", "\x1b[49m"], - ["bgBlueBright", "\x1b[104m", "\x1b[49m"], - ["bgMagentaBright", "\x1b[105m", "\x1b[49m"], - ["bgCyanBright", "\x1b[106m", "\x1b[49m"], - ["bgWhiteBright", "\x1b[107m", "\x1b[49m"], - ].map((args) => StyleTest.apply({}, args)), - "nesting styles": [ - EqualTest( - c.bold(`BOLD ${c.red(`RED ${c.dim("DIM")} RED`)} BOLD`), - `\x1b[1mBOLD \x1b[31mRED \x1b[2mDIM\x1b[22m\x1b[1m RED\x1b[39m BOLD\x1b[22m` - ), - ], - "numbers & others": [new Date(), -1e10, -1, -0.1, 0, 0.1, 1, 1e10].map((n) => - EqualTest(c.red(n), `\x1b[31m${n}\x1b[39m`) - ), - "options.enabled can toggle color on/off": [ - { - assert: equal, - actual: (done) => { - c.options.enabled = false - done(c.bold(c.options.enabled)) - c.options.enabled = true - }, - expected: false, - }, - ], - "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"), - ], -} From aa8ce9823de6e3719b453e0e454e2ed69aac1f09 Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Sun, 28 Jun 2020 02:26:50 +0900 Subject: [PATCH 2/7] Use import syntax in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 ``` From 9021d101ababdc5ad31dae6e80686c51fa0c1195 Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Sun, 28 Jun 2020 02:31:21 +0900 Subject: [PATCH 3/7] Escape less --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a9a6ea4..2a8c3e3 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "types": "colorette.d.ts", "scripts": { "test": "nyc -r lcov testmatrix test/*.cjs", - "build": "node -e 'fs.writeFileSync(\"index.cjs\",fs.readFileSync(\"index.js\", \"utf8\").replace(/export const /g, \"exports.\"),\"utf8\")'", + "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 && npx dual-publish" }, "repository": { From aeb5150d1281cafd7c757d8932dca3fcd9803bc5 Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Sun, 5 Jul 2020 22:16:32 +0900 Subject: [PATCH 4/7] Don't ignore test/index .cjs until until new testmatrix is ready --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6b38e29..623fddc 100644 --- a/.gitignore +++ b/.gitignore @@ -13,7 +13,7 @@ coverage # Misc node_modules -*.cjs +/*.cjs *.xml .DS_Store example From 46bc0fc207a87485ca4047a1222f21a887cc6296 Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Sun, 5 Jul 2020 22:17:00 +0900 Subject: [PATCH 5/7] Use commonjs for tests until new testmatrix is ready --- test/index.cjs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 test/index.cjs diff --git a/test/index.cjs b/test/index.cjs new file mode 100644 index 0000000..67f3103 --- /dev/null +++ b/test/index.cjs @@ -0,0 +1,92 @@ +const c = require("..") +const { equal } = require("testmatrix") + +const EqualTest = (actual, expected) => ({ + name: actual, + assert: equal, + actual, + expected, +}) + +const ScriptTest = (name, cmd, script) => ({ + name, + assert: equal, + actual: (done) => { + const exec = require("child_process").exec + exec(`${cmd} ${__dirname}/${script}`, done) + }, + expected: null, +}) + +const StyleTest = (name, open, close) => + EqualTest(c[name](name), open + name + close) + +exports.default = { + "using styles": [ + ["reset", "\x1b[0m", "\x1b[0m"], + ["bold", "\x1b[1m", "\x1b[22m"], + ["dim", "\x1b[2m", "\x1b[22m"], + ["italic", "\x1b[3m", "\x1b[23m"], + ["underline", "\x1b[4m", "\x1b[24m"], + ["inverse", "\x1b[7m", "\x1b[27m"], + ["hidden", "\x1b[8m", "\x1b[28m"], + ["strikethrough", "\x1b[9m", "\x1b[29m"], + ["black", "\x1b[30m", "\x1b[39m"], + ["red", "\x1b[31m", "\x1b[39m"], + ["green", "\x1b[32m", "\x1b[39m"], + ["yellow", "\x1b[33m", "\x1b[39m"], + ["blue", "\x1b[34m", "\x1b[39m"], + ["magenta", "\x1b[35m", "\x1b[39m"], + ["cyan", "\x1b[36m", "\x1b[39m"], + ["white", "\x1b[37m", "\x1b[39m"], + ["gray", "\x1b[90m", "\x1b[39m"], + ["bgBlack", "\x1b[40m", "\x1b[49m"], + ["bgRed", "\x1b[41m", "\x1b[49m"], + ["bgGreen", "\x1b[42m", "\x1b[49m"], + ["bgYellow", "\x1b[43m", "\x1b[49m"], + ["bgBlue", "\x1b[44m", "\x1b[49m"], + ["bgMagenta", "\x1b[45m", "\x1b[49m"], + ["bgCyan", "\x1b[46m", "\x1b[49m"], + ["bgWhite", "\x1b[47m", "\x1b[49m"], + ["blackBright", "\x1b[90m", "\x1b[39m"], + ["redBright", "\x1b[91m", "\x1b[39m"], + ["greenBright", "\x1b[92m", "\x1b[39m"], + ["yellowBright", "\x1b[93m", "\x1b[39m"], + ["blueBright", "\x1b[94m", "\x1b[39m"], + ["magentaBright", "\x1b[95m", "\x1b[39m"], + ["cyanBright", "\x1b[96m", "\x1b[39m"], + ["whiteBright", "\x1b[97m", "\x1b[39m"], + ["bgBlackBright", "\x1b[100m", "\x1b[49m"], + ["bgRedBright", "\x1b[101m", "\x1b[49m"], + ["bgGreenBright", "\x1b[102m", "\x1b[49m"], + ["bgYellowBright", "\x1b[103m", "\x1b[49m"], + ["bgBlueBright", "\x1b[104m", "\x1b[49m"], + ["bgMagentaBright", "\x1b[105m", "\x1b[49m"], + ["bgCyanBright", "\x1b[106m", "\x1b[49m"], + ["bgWhiteBright", "\x1b[107m", "\x1b[49m"], + ].map((args) => StyleTest.apply({}, args)), + "nesting styles": [ + EqualTest( + c.bold(`BOLD ${c.red(`RED ${c.dim("DIM")} RED`)} BOLD`), + `\x1b[1mBOLD \x1b[31mRED \x1b[2mDIM\x1b[22m\x1b[1m RED\x1b[39m BOLD\x1b[22m` + ), + ], + "numbers & others": [new Date(), -1e10, -1, -0.1, 0, 0.1, 1, 1e10].map((n) => + EqualTest(c.red(n), `\x1b[31m${n}\x1b[39m`) + ), + "options.enabled can toggle color on/off": [ + { + assert: equal, + actual: (done) => { + c.options.enabled = false + done(c.bold(c.options.enabled)) + c.options.enabled = true + }, + expected: false, + }, + ], + "env variables": [ + ScriptTest("`FORCE_COLOR` in the env forces color", "sh", "FORCE_COLOR.sh"), + ScriptTest("`NO_COLOR` in the env disables color", "sh", "NO_COLOR.sh"), + ], +} From a7442715d92448c3c896cd826b87375fad8b83b2 Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Sun, 5 Jul 2020 22:17:37 +0900 Subject: [PATCH 6/7] Use more lightweight codecov reporter --- package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 2a8c3e3..8f81a60 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "types": "colorette.d.ts", "scripts": { - "test": "nyc -r lcov testmatrix test/*.cjs", + "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 && npx dual-publish" }, @@ -40,8 +40,7 @@ }, "homepage": "https://github.com/jorgebucaran/colorette", "devDependencies": { - "nyc": "15.0.1", - "esm": "3.2.25", + "c8": "7.2.0", "testmatrix": "0.1.2" } } From 42252cde8c1628afee0b4f5e548b251f6d87de52 Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Sun, 5 Jul 2020 22:18:26 +0900 Subject: [PATCH 7/7] Use npm publish --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8f81a60..4a19188 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "scripts": { "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 && npx dual-publish" + "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",