From 3cd849197d913dcf460d7708240af9a6fbb7047c Mon Sep 17 00:00:00 2001 From: Thomas Sauer Date: Sat, 1 Jul 2017 14:26:39 +0200 Subject: [PATCH 1/8] Added Typescript definition --- index.d.ts | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 81 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..774314a --- /dev/null +++ b/index.d.ts @@ -0,0 +1,80 @@ +// Type definitions for Chalk 2 +// Definitions by: Thomas Sauer + +export = chalk; + +declare function chalk(...text: (string | number)[]): string; +declare function chalk(text: TemplateStringsArray, ...placeholders: (string | number)[]): string; + +declare namespace chalk { + + interface ChalkConstructorOptions { + enabled?: boolean; + level?: number; + } + + export function constructor(options?: ChalkConstructorOptions): typeof chalk; + + export let enabled: boolean; + export let level: number; + export const supportsColor: boolean; + + export const hex: (color: string) => typeof chalk; + export const keyword: (color: string) => typeof chalk; + export const rgb: (r: number, g: number, b: number) => typeof chalk; + export const hsl: (h: number, s: number, l: number) => typeof chalk; + export const hsv: (h: number, s: number, v: number) => typeof chalk; + export const hwb: (h: number, w: number, b: number) => typeof chalk; + + export const bgHex: (color: string) => typeof chalk; + export const bgKeyword: (color: string) => typeof chalk; + export const bgRgb: (r: number, g: number, b: number) => typeof chalk; + export const bgHsl: (h: number, s: number, l: number) => typeof chalk; + export const bgHsv: (h: number, s: number, v: number) => typeof chalk; + export const bgHwb: (h: number, w: number, b: number) => typeof chalk; + + export const reset: typeof chalk; + export const bold: typeof chalk; + export const dim: typeof chalk; + export const italic: typeof chalk; + export const underline: typeof chalk; + export const inverse: typeof chalk; + export const hidden: typeof chalk; + export const strikethrough: typeof chalk; + + export const black: typeof chalk; + export const red: typeof chalk; + export const green: typeof chalk; + export const yellow: typeof chalk; + export const blue: typeof chalk; + export const magenta: typeof chalk; + export const cyan: typeof chalk; + export const white: typeof chalk; + export const gray: typeof chalk; + export const grey: typeof chalk; + export const blackBright: typeof chalk; + export const redBright: typeof chalk; + export const greenBright: typeof chalk; + export const yellowBright: typeof chalk; + export const blueBright: typeof chalk; + export const magentaBright: typeof chalk; + export const cyanBright: typeof chalk; + export const whiteBright: typeof chalk; + + export const bgBlack: typeof chalk; + export const bgRed: typeof chalk; + export const bgGreen: typeof chalk; + export const bgYellow: typeof chalk; + export const bgBlue: typeof chalk; + export const bgMagenta: typeof chalk; + export const bgCyan: typeof chalk; + export const bgWhite: typeof chalk; + export const bgBlackBright: typeof chalk; + export const bgRedBright: typeof chalk; + export const bgGreenBright: typeof chalk; + export const bgYellowBright: typeof chalk; + export const bgBlueBright: typeof chalk; + export const bgMagentaBright: typeof chalk; + export const bgCyanBright: typeof chalk; + export const bgWhiteBright: typeof chalk; +} diff --git a/package.json b/package.json index ac1f9b2..6e77c90 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "resolve-from": "^3.0.0", "xo": "*" }, + "types": "./index.d.ts", "xo": { "envs": [ "node", From f18decbd7fcc512765c4d059b4c2df91f5494f4f Mon Sep 17 00:00:00 2001 From: Thomas Sauer Date: Sun, 2 Jul 2017 14:32:45 +0200 Subject: [PATCH 2/8] Added test for Typescript definition --- package.json | 5 +++-- index.d.ts => types/index.d.ts | 0 types/test.ts | 31 +++++++++++++++++++++++++++++++ types/tsconfig.json | 9 +++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) rename index.d.ts => types/index.d.ts (100%) create mode 100644 types/test.ts create mode 100644 types/tsconfig.json diff --git a/package.json b/package.json index 6e77c90..099e266 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "node": ">=4" }, "scripts": { - "test": "xo && nyc mocha", + "test": "xo && tsc -p types && nyc mocha", "bench": "matcha benchmark.js", "coveralls": "nyc report --reporter=text-lcov | coveralls" }, @@ -51,9 +51,10 @@ "mocha": "*", "nyc": "^11.0.2", "resolve-from": "^3.0.0", + "typescript": "^2.4.1", "xo": "*" }, - "types": "./index.d.ts", + "types": "./types/index.d.ts", "xo": { "envs": [ "node", diff --git a/index.d.ts b/types/index.d.ts similarity index 100% rename from index.d.ts rename to types/index.d.ts diff --git a/types/test.ts b/types/test.ts new file mode 100644 index 0000000..3ced627 --- /dev/null +++ b/types/test.ts @@ -0,0 +1,31 @@ +import * as chalk from '../.'; + +chalk.underline('foo'); +chalk.red('foo'); +chalk.bgRed('foo'); + +chalk.red`foo`; +chalk.underline`foo`; +chalk`foo`; + +chalk.red.bgGreen.underline('foo'); +chalk.underline.red.bgGreen('foo'); + +chalk.grey('foo'); +chalk.red(0); + +const ctx = chalk.constructor({level: 1}); +ctx('foo'); +ctx.red('foo'); +ctx`foo`; + +chalk.enabled; +chalk.supportsColor; +chalk.level; + +chalk.keyword('orange').bgBlue('foo'); +chalk.hex('#123456').bgBlue('foo'); +chalk.rgb(1, 14, 9).bgBlue('foo'); +chalk.hsl(1, 14, 9).bgBlue('foo'); +chalk.hsv(1, 14, 9).bgBlue('foo'); +chalk.hwb(1, 14, 9).bgBlue('foo'); diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..af436cc --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noEmit": true, + "allowJs": true + } +} From 26b6a2afb590a45370e1b3fb42897a29f21e5da4 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 2 Jul 2017 14:52:52 +0200 Subject: [PATCH 3/8] Update test.ts --- types/test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/types/test.ts b/types/test.ts index 3ced627..cde09c3 100644 --- a/types/test.ts +++ b/types/test.ts @@ -1,9 +1,12 @@ -import * as chalk from '../.'; +import * as chalk from '..'; chalk.underline('foo'); chalk.red('foo'); chalk.bgRed('foo'); +const name = 'Josh'; +chalk`Hello {bold.red ${name}}`; + chalk.red`foo`; chalk.underline`foo`; chalk`foo`; From c2e3bf075694d5b46d0e67dc25ea6a93a0cfe912 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 2 Jul 2017 14:55:29 +0200 Subject: [PATCH 4/8] Update package.json --- package.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 099e266..403ebf6 100644 --- a/package.json +++ b/package.json @@ -8,13 +8,14 @@ "node": ">=4" }, "scripts": { - "test": "xo && tsc -p types && nyc mocha", + "test": "xo && tsc --project types && nyc mocha", "bench": "matcha benchmark.js", "coveralls": "nyc report --reporter=text-lcov | coveralls" }, "files": [ "index.js", - "templates.js" + "templates.js", + "types/index.d.ts" ], "keywords": [ "color", @@ -54,7 +55,7 @@ "typescript": "^2.4.1", "xo": "*" }, - "types": "./types/index.d.ts", + "types": "types/index.d.ts", "xo": { "envs": [ "node", From ecf8f86c7cc5459841b6eeb871b71b7c490fef06 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 2 Jul 2017 14:58:15 +0200 Subject: [PATCH 5/8] Update index.d.ts --- types/index.d.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 774314a..0aa3abc 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Chalk 2 +// Type definitions for Chalk // Definitions by: Thomas Sauer export = chalk; @@ -7,7 +7,6 @@ declare function chalk(...text: (string | number)[]): string; declare function chalk(text: TemplateStringsArray, ...placeholders: (string | number)[]): string; declare namespace chalk { - interface ChalkConstructorOptions { enabled?: boolean; level?: number; From 8046089bc6c7cca9a045702231145b56223484df Mon Sep 17 00:00:00 2001 From: Thomas Sauer Date: Sun, 2 Jul 2017 17:26:14 +0200 Subject: [PATCH 6/8] Updated typings --- types/index.d.ts | 18 +++++++++++++----- types/test.ts | 7 +++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 0aa3abc..6e0acc5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -3,19 +3,27 @@ export = chalk; -declare function chalk(...text: (string | number)[]): string; -declare function chalk(text: TemplateStringsArray, ...placeholders: (string | number)[]): string; +declare function chalk(...text: string[]): string; +declare function chalk(text: TemplateStringsArray, ...placeholders: string[]): string; declare namespace chalk { + + export enum ChalkLevel { + Disabled = 0, + Basic = 1, + Extended = 2, + TrueColor = 3 + } + interface ChalkConstructorOptions { enabled?: boolean; - level?: number; + level?: ChalkLevel; } export function constructor(options?: ChalkConstructorOptions): typeof chalk; export let enabled: boolean; - export let level: number; + export let level: ChalkLevel; export const supportsColor: boolean; export const hex: (color: string) => typeof chalk; @@ -50,7 +58,7 @@ declare namespace chalk { export const cyan: typeof chalk; export const white: typeof chalk; export const gray: typeof chalk; - export const grey: typeof chalk; + export const grey: typeof gray; export const blackBright: typeof chalk; export const redBright: typeof chalk; export const greenBright: typeof chalk; diff --git a/types/test.ts b/types/test.ts index cde09c3..c2f20b9 100644 --- a/types/test.ts +++ b/types/test.ts @@ -5,6 +5,7 @@ chalk.red('foo'); chalk.bgRed('foo'); const name = 'Josh'; +const number = 0; chalk`Hello {bold.red ${name}}`; chalk.red`foo`; @@ -15,13 +16,15 @@ chalk.red.bgGreen.underline('foo'); chalk.underline.red.bgGreen('foo'); chalk.grey('foo'); -chalk.red(0); -const ctx = chalk.constructor({level: 1}); +chalk.constructor({level: 1}); +const ctx = chalk.constructor({level: chalk.ChalkLevel.TrueColor }); ctx('foo'); ctx.red('foo'); ctx`foo`; +chalk.level === chalk.ChalkLevel.Extended; + chalk.enabled; chalk.supportsColor; chalk.level; From e35b732597de5be39ba20f62e3a35bc82632f7d1 Mon Sep 17 00:00:00 2001 From: Thomas Sauer Date: Mon, 3 Jul 2017 00:05:36 +0200 Subject: [PATCH 7/8] Updated TS interfaces --- types/index.d.ts | 12 ++++++------ types/test.ts | 5 ++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 6e0acc5..a711115 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -8,22 +8,22 @@ declare function chalk(text: TemplateStringsArray, ...placeholders: string[]): s declare namespace chalk { - export enum ChalkLevel { - Disabled = 0, + export enum Level { + None = 0, Basic = 1, Extended = 2, TrueColor = 3 } - interface ChalkConstructorOptions { + interface ConstructorOptions { enabled?: boolean; - level?: ChalkLevel; + level?: Level; } - export function constructor(options?: ChalkConstructorOptions): typeof chalk; + export function constructor(options?: ConstructorOptions): typeof chalk; export let enabled: boolean; - export let level: ChalkLevel; + export let level: Level; export const supportsColor: boolean; export const hex: (color: string) => typeof chalk; diff --git a/types/test.ts b/types/test.ts index c2f20b9..53a6df6 100644 --- a/types/test.ts +++ b/types/test.ts @@ -5,7 +5,6 @@ chalk.red('foo'); chalk.bgRed('foo'); const name = 'Josh'; -const number = 0; chalk`Hello {bold.red ${name}}`; chalk.red`foo`; @@ -18,12 +17,12 @@ chalk.underline.red.bgGreen('foo'); chalk.grey('foo'); chalk.constructor({level: 1}); -const ctx = chalk.constructor({level: chalk.ChalkLevel.TrueColor }); +const ctx = chalk.constructor({level: chalk.Level.TrueColor }); ctx('foo'); ctx.red('foo'); ctx`foo`; -chalk.level === chalk.ChalkLevel.Extended; +chalk.level === chalk.Level.Extended; chalk.enabled; chalk.supportsColor; From a4d6a513f11b39839d95c02a0c902bb80c0519f8 Mon Sep 17 00:00:00 2001 From: Thomas Sauer Date: Mon, 3 Jul 2017 00:27:16 +0200 Subject: [PATCH 8/8] Changed to default export in Typescript definition --- types/index.d.ts | 2 +- types/test.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index a711115..1b7079e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,7 +1,7 @@ // Type definitions for Chalk // Definitions by: Thomas Sauer -export = chalk; +export default chalk; declare function chalk(...text: string[]): string; declare function chalk(text: TemplateStringsArray, ...placeholders: string[]): string; diff --git a/types/test.ts b/types/test.ts index 53a6df6..d0e6aa4 100644 --- a/types/test.ts +++ b/types/test.ts @@ -1,4 +1,4 @@ -import * as chalk from '..'; +import chalk from '..'; chalk.underline('foo'); chalk.red('foo'); @@ -22,6 +22,10 @@ ctx('foo'); ctx.red('foo'); ctx`foo`; +chalk.enabled = true; +chalk.level = 1; +chalk.level = chalk.Level.Extended; + chalk.level === chalk.Level.Extended; chalk.enabled;