Skip to content

Commit

Permalink
ESM support via minimal npm script (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebucaran committed Jul 5, 2020
1 parent 42d3f6c commit 0ac1a15
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -13,6 +13,7 @@ coverage

# Misc
node_modules
/*.cjs
*.xml
.DS_Store
example
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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
```
Expand Down
130 changes: 42 additions & 88 deletions index.js
@@ -1,5 +1,3 @@
"use strict"

let enabled =
!("NO_COLOR" in process.env) &&
("FORCE_COLOR" in process.env ||
Expand Down Expand Up @@ -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)
21 changes: 15 additions & 6 deletions package.json
Expand Up @@ -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": [
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion 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
10 changes: 3 additions & 7 deletions test/index.js → 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,
Expand Down Expand Up @@ -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"),
],
}

0 comments on commit 0ac1a15

Please sign in to comment.