From 80f5db43e656fb0e0755268c608f04ceca2a4577 Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Sun, 25 Feb 2024 11:10:05 +0100 Subject: [PATCH] Support named exports in TypeScript projects 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. --- .github/workflows/testing.yaml | 1 + .gitignore | 1 + package.json | 7 +++--- picocolors.d.ts | 39 +++++++++++++++++++++++++++++++--- tests/types.mts | 25 ++++++++++++++++++++++ 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 tests/types.mts diff --git a/.github/workflows/testing.yaml b/.github/workflows/testing.yaml index e7faefa..872b4f5 100644 --- a/.github/workflows/testing.yaml +++ b/.github/workflows/testing.yaml @@ -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 diff --git a/.gitignore b/.gitignore index f0b2b54..1c11785 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules picocolors.mjs +tests/types.mjs diff --git a/package.json b/package.json index 4d6d9d5..82fdf2d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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, diff --git a/picocolors.d.ts b/picocolors.d.ts index 94e146a..ab19c8f 100644 --- a/picocolors.d.ts +++ b/picocolors.d.ts @@ -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 diff --git a/tests/types.mts b/tests/types.mts new file mode 100644 index 0000000..285b6c8 --- /dev/null +++ b/tests/types.mts @@ -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 + } +}