Skip to content

Commit

Permalink
Support named exports in TypeScript projects
Browse files Browse the repository at this point in the history
Export single named exports from `picocolors.d.ts`. Test
the functionality by `test/types.mts`.

Include both `*.ts` files in the package.

Include the test in the GitHub workflow.
  • Loading branch information
prantlf committed Feb 25, 2024
1 parent 8c8095e commit 80f5db4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- run: npm run build
- run: node tests/test.js
- run: node tests/esm.mjs
- run: node tests/types.mjs
legacy:
name: Node v${{ matrix.node-version }} (Legacy)
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
picocolors.mjs
tests/types.mjs
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
"sideEffects": false,
"description": "The tiniest and the fastest library for terminal output formatting with ANSI colors",
"scripts": {
"build": "node build-esm.mjs",
"build": "node build-esm.mjs && tsc --module esnext tests/types.mts",
"test": "node tests/test.js"
},
"files": [
"picocolors.*",
"types.ts"
"*.ts"
],
"keywords": [
"terminal",
Expand All @@ -43,7 +43,8 @@
"colorette": "^2.0.20",
"kleur": "^4.1.5",
"nanocolors": "^0.2.13",
"prettier": "^2.8.8"
"prettier": "^2.8.8",
"typescript": "^5.3.3"
},
"prettier": {
"printWidth": 100,
Expand Down
39 changes: 36 additions & 3 deletions picocolors.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import { Colors } from "./types"
import { Colors, Formatter } from "./types"

declare const picocolors: Colors & { createColors: (enabled?: boolean) => Colors }
export { Colors }

export = picocolors
declare type Generator = (enabled?: boolean) => Colors

declare const picocolors: Colors & { createColors: Generator }

export default picocolors

export let isColorSupported: boolean
export let reset: Formatter
export let bold: Formatter
export let dim: Formatter
export let italic: Formatter
export let underline: Formatter
export let inverse: Formatter
export let hidden: Formatter
export let strikethrough: Formatter
export let black: Formatter
export let red: Formatter
export let green: Formatter
export let yellow: Formatter
export let blue: Formatter
export let magenta: Formatter
export let cyan: Formatter
export let white: Formatter
export let gray: Formatter
export let bgBlack: Formatter
export let bgRed: Formatter
export let bgGreen: Formatter
export let bgYellow: Formatter
export let bgBlue: Formatter
export let bgMagenta: Formatter
export let bgCyan: Formatter
export let bgWhite: Formatter

export let createColors: Generator
25 changes: 25 additions & 0 deletions tests/types.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pc from "../picocolors.mjs"
import { bold, createColors, Colors } from "../picocolors.mjs"

test("default export", () => {
let _result: string = pc.bold("test")
})

test("named exports", () => {
let _result: string = bold("test")
})

test("factory function", () => {
let _result1: Colors = pc.createColors()
let _result2: Colors = createColors()
})

function test(name: string, fn: () => void) : void {
try {
fn()
console.log(pc.green("✓ " + name))
} catch (error) {
console.log(pc.red("✗ " + name))
throw error
}
}

0 comments on commit 80f5db4

Please sign in to comment.