Skip to content

Commit 98628d9

Browse files
BendingBendersindresorhus
authored andcommittedApr 28, 2019
Use CommonJS-compatible export in TypeScript definition (#344)
1 parent 7b9211b commit 98628d9

File tree

5 files changed

+285
-275
lines changed

5 files changed

+285
-275
lines changed
 

‎.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: node_js
22
node_js:
3+
- '12'
34
- '10'
45
- '8'
56
after_success:

‎index.d.ts

+268-257
Original file line numberDiff line numberDiff line change
@@ -1,276 +1,287 @@
1-
export const enum Level {
1+
declare const enum LevelEnum {
22
/**
3-
* All colors disabled.
4-
*/
3+
All colors disabled.
4+
*/
55
None = 0,
66

77
/**
8-
* Basic 16 colors support.
9-
*/
8+
Basic 16 colors support.
9+
*/
1010
Basic = 1,
1111

1212
/**
13-
* ANSI 256 colors support.
14-
*/
13+
ANSI 256 colors support.
14+
*/
1515
Ansi256 = 2,
1616

1717
/**
18-
* Truecolor 16 million colors support.
19-
*/
18+
Truecolor 16 million colors support.
19+
*/
2020
TrueColor = 3
2121
}
2222

23-
export interface Options {
23+
declare namespace chalk {
24+
type Level = LevelEnum;
25+
26+
interface Options {
27+
/**
28+
Enable or disable Chalk.
29+
30+
@default true
31+
*/
32+
enabled?: boolean;
33+
34+
/**
35+
Specify the color support for Chalk.
36+
By default, color support is automatically detected based on the environment.
37+
*/
38+
level?: Level;
39+
}
40+
41+
interface Instance {
42+
/**
43+
Return a new Chalk instance.
44+
*/
45+
new (options?: Options): Chalk;
46+
}
47+
2448
/**
25-
* Enable or disable Chalk.
26-
*
27-
* @default true
28-
*/
29-
enabled?: boolean;
49+
Detect whether the terminal supports color.
50+
*/
51+
interface ColorSupport {
52+
/**
53+
The color level used by Chalk.
54+
*/
55+
level: Level;
56+
57+
/**
58+
Return whether Chalk supports basic 16 colors.
59+
*/
60+
hasBasic: boolean;
61+
62+
/**
63+
Return whether Chalk supports ANSI 256 colors.
64+
*/
65+
has256: boolean;
3066

31-
/**
32-
* Specify the color support for Chalk.
33-
* By default, color support is automatically detected based on the environment.
34-
*/
35-
level?: Level;
36-
}
37-
38-
export interface Instance {
39-
/**
40-
* Return a new Chalk instance.
41-
*/
42-
new (options?: Options): Chalk;
43-
}
44-
45-
/**
46-
* Detect whether the terminal supports color.
47-
*/
48-
export interface ColorSupport {
49-
/**
50-
* The color level used by Chalk.
51-
*/
52-
level: Level;
53-
54-
/**
55-
* Return whether Chalk supports basic 16 colors.
56-
*/
57-
hasBasic: boolean;
58-
59-
/**
60-
* Return whether Chalk supports ANSI 256 colors.
61-
*/
62-
has256: boolean;
63-
64-
/**
65-
* Return whether Chalk supports Truecolor 16 million colors.
66-
*/
67-
has16m: boolean;
68-
}
69-
70-
export interface Chalk {
71-
(...text: unknown[]): string;
72-
73-
(text: TemplateStringsArray, ...placeholders: unknown[]): string;
74-
75-
/**
76-
* Return a new Chalk instance.
77-
*/
78-
Instance: Instance;
79-
80-
/**
81-
* Enable or disable Chalk.
82-
*
83-
* @default true
84-
*/
85-
enabled: boolean;
86-
87-
/**
88-
* The color support for Chalk.
89-
* By default, color support is automatically detected based on the environment.
90-
*/
91-
level: Level;
92-
93-
/**
94-
* Use HEX value to set text color.
95-
*
96-
* @param color - Hexadecimal value representing the desired color.
97-
*
98-
* @example
99-
*
100-
* import chalk from 'chalk';
101-
*
102-
* chalk.hex('#DEADED');
103-
*/
104-
hex(color: string): this;
105-
106-
/**
107-
* Use keyword color value to set text color.
108-
*
109-
* @param color - Keyword value representing the desired color.
110-
*
111-
* @example
112-
*
113-
* import chalk from 'chalk';
114-
*
115-
* chalk.keyword('orange');
116-
*/
117-
keyword(color: string): this;
118-
119-
/**
120-
* Use RGB values to set text color.
121-
*/
122-
rgb(red: number, green: number, blue: number): this;
123-
124-
/**
125-
* Use HSL values to set text color.
126-
*/
127-
hsl(hue: number, saturation: number, lightness: number): this;
128-
129-
/**
130-
* Use HSV values to set text color.
131-
*/
132-
hsv(hue: number, saturation: number, value: number): this;
133-
134-
/**
135-
* Use HWB values to set text color.
136-
*/
137-
hwb(hue: number, whiteness: number, blackness: number): this;
138-
139-
/**
140-
* Use HEX value to set background color.
141-
*
142-
* @param color - Hexadecimal value representing the desired color.
143-
*
144-
* @example
145-
*
146-
* import chalk from 'chalk';
147-
*
148-
* chalk.bgHex('#DEADED');
149-
*/
150-
bgHex(color: string): this;
151-
152-
/**
153-
* Use keyword color value to set background color.
154-
*
155-
* @param color - Keyword value representing the desired color.
156-
*
157-
* @example
158-
*
159-
* import chalk from 'chalk';
160-
*
161-
* chalk.bgKeyword('orange');
162-
*/
163-
bgKeyword(color: string): this;
164-
165-
/**
166-
* Use RGB values to set background color.
167-
*/
168-
bgRgb(red: number, green: number, blue: number): this;
169-
170-
/**
171-
* Use HSL values to set background color.
172-
*/
173-
bgHsl(hue: number, saturation: number, lightness: number): this;
174-
175-
/**
176-
* Use HSV values to set background color.
177-
*/
178-
bgHsv(hue: number, saturation: number, value: number): this;
179-
180-
/**
181-
* Use HWB values to set background color.
182-
*/
183-
bgHwb(hue: number, whiteness: number, blackness: number): this;
184-
185-
/**
186-
* Modifier: Resets the current color chain.
187-
*/
188-
readonly reset: this;
189-
190-
/**
191-
* Modifier: Make text bold.
192-
*/
193-
readonly bold: this;
194-
195-
/**
196-
* Modifier: Emitting only a small amount of light.
197-
*/
198-
readonly dim: this;
199-
200-
/**
201-
* Modifier: Make text italic. (Not widely supported)
202-
*/
203-
readonly italic: this;
204-
205-
/**
206-
* Modifier: Make text underline. (Not widely supported)
207-
*/
208-
readonly underline: this;
209-
210-
/**
211-
* Modifier: Inverse background and foreground colors.
212-
*/
213-
readonly inverse: this;
214-
215-
/**
216-
* Modifier: Prints the text, but makes it invisible.
217-
*/
218-
readonly hidden: this;
219-
220-
/**
221-
* Modifier: Puts a horizontal line through the center of the text. (Not widely supported)
222-
*/
223-
readonly strikethrough: this;
67+
/**
68+
Return whether Chalk supports Truecolor 16 million colors.
69+
*/
70+
has16m: boolean;
71+
}
22472

225-
/**
226-
* Modifier: Prints the text only when Chalk is enabled.
227-
* Can be useful for things that are purely cosmetic.
228-
*/
229-
readonly visible: this;
230-
231-
readonly black: this;
232-
readonly red: this;
233-
readonly green: this;
234-
readonly yellow: this;
235-
readonly blue: this;
236-
readonly magenta: this;
237-
readonly cyan: this;
238-
readonly white: this;
239-
readonly gray: this;
240-
readonly grey: this;
241-
readonly blackBright: this;
242-
readonly redBright: this;
243-
readonly greenBright: this;
244-
readonly yellowBright: this;
245-
readonly blueBright: this;
246-
readonly magentaBright: this;
247-
readonly cyanBright: this;
248-
readonly whiteBright: this;
249-
250-
readonly bgBlack: this;
251-
readonly bgRed: this;
252-
readonly bgGreen: this;
253-
readonly bgYellow: this;
254-
readonly bgBlue: this;
255-
readonly bgMagenta: this;
256-
readonly bgCyan: this;
257-
readonly bgWhite: this;
258-
readonly bgBlackBright: this;
259-
readonly bgRedBright: this;
260-
readonly bgGreenBright: this;
261-
readonly bgYellowBright: this;
262-
readonly bgBlueBright: this;
263-
readonly bgMagentaBright: this;
264-
readonly bgCyanBright: this;
265-
readonly bgWhiteBright: this;
73+
interface Chalk {
74+
(...text: unknown[]): string;
75+
76+
(text: TemplateStringsArray, ...placeholders: unknown[]): string;
77+
78+
/**
79+
Return a new Chalk instance.
80+
*/
81+
Instance: Instance;
82+
83+
/**
84+
Enable or disable Chalk.
85+
86+
@default true
87+
*/
88+
enabled: boolean;
89+
90+
/**
91+
The color support for Chalk.
92+
By default, color support is automatically detected based on the environment.
93+
*/
94+
level: Level;
95+
96+
/**
97+
Use HEX value to set text color.
98+
99+
@param color - Hexadecimal value representing the desired color.
100+
101+
@example
102+
```
103+
import chalk = require('chalk');
104+
105+
chalk.hex('#DEADED');
106+
```
107+
*/
108+
hex(color: string): this;
109+
110+
/**
111+
Use keyword color value to set text color.
112+
113+
@param color - Keyword value representing the desired color.
114+
115+
@example
116+
```
117+
import chalk = require('chalk');
118+
119+
chalk.keyword('orange');
120+
```
121+
*/
122+
keyword(color: string): this;
123+
124+
/**
125+
Use RGB values to set text color.
126+
*/
127+
rgb(red: number, green: number, blue: number): this;
128+
129+
/**
130+
Use HSL values to set text color.
131+
*/
132+
hsl(hue: number, saturation: number, lightness: number): this;
133+
134+
/**
135+
Use HSV values to set text color.
136+
*/
137+
hsv(hue: number, saturation: number, value: number): this;
138+
139+
/**
140+
Use HWB values to set text color.
141+
*/
142+
hwb(hue: number, whiteness: number, blackness: number): this;
143+
144+
/**
145+
Use HEX value to set background color.
146+
147+
@param color - Hexadecimal value representing the desired color.
148+
149+
@example
150+
```
151+
import chalk = require('chalk');
152+
153+
chalk.bgHex('#DEADED');
154+
```
155+
*/
156+
bgHex(color: string): this;
157+
158+
/**
159+
Use keyword color value to set background color.
160+
161+
@param color - Keyword value representing the desired color.
162+
163+
@example
164+
```
165+
import chalk = require('chalk');
166+
167+
chalk.bgKeyword('orange');
168+
```
169+
*/
170+
bgKeyword(color: string): this;
171+
172+
/**
173+
Use RGB values to set background color.
174+
*/
175+
bgRgb(red: number, green: number, blue: number): this;
176+
177+
/**
178+
Use HSL values to set background color.
179+
*/
180+
bgHsl(hue: number, saturation: number, lightness: number): this;
181+
182+
/**
183+
Use HSV values to set background color.
184+
*/
185+
bgHsv(hue: number, saturation: number, value: number): this;
186+
187+
/**
188+
Use HWB values to set background color.
189+
*/
190+
bgHwb(hue: number, whiteness: number, blackness: number): this;
191+
192+
/**
193+
Modifier: Resets the current color chain.
194+
*/
195+
readonly reset: this;
196+
197+
/**
198+
Modifier: Make text bold.
199+
*/
200+
readonly bold: this;
201+
202+
/**
203+
Modifier: Emitting only a small amount of light.
204+
*/
205+
readonly dim: this;
206+
207+
/**
208+
Modifier: Make text italic. (Not widely supported)
209+
*/
210+
readonly italic: this;
211+
212+
/**
213+
Modifier: Make text underline. (Not widely supported)
214+
*/
215+
readonly underline: this;
216+
217+
/**
218+
Modifier: Inverse background and foreground colors.
219+
*/
220+
readonly inverse: this;
221+
222+
/**
223+
Modifier: Prints the text, but makes it invisible.
224+
*/
225+
readonly hidden: this;
226+
227+
/**
228+
Modifier: Puts a horizontal line through the center of the text. (Not widely supported)
229+
*/
230+
readonly strikethrough: this;
231+
232+
/**
233+
Modifier: Prints the text only when Chalk is enabled.
234+
Can be useful for things that are purely cosmetic.
235+
*/
236+
readonly visible: this;
237+
238+
readonly black: this;
239+
readonly red: this;
240+
readonly green: this;
241+
readonly yellow: this;
242+
readonly blue: this;
243+
readonly magenta: this;
244+
readonly cyan: this;
245+
readonly white: this;
246+
readonly gray: this;
247+
readonly grey: this;
248+
readonly blackBright: this;
249+
readonly redBright: this;
250+
readonly greenBright: this;
251+
readonly yellowBright: this;
252+
readonly blueBright: this;
253+
readonly magentaBright: this;
254+
readonly cyanBright: this;
255+
readonly whiteBright: this;
256+
257+
readonly bgBlack: this;
258+
readonly bgRed: this;
259+
readonly bgGreen: this;
260+
readonly bgYellow: this;
261+
readonly bgBlue: this;
262+
readonly bgMagenta: this;
263+
readonly bgCyan: this;
264+
readonly bgWhite: this;
265+
readonly bgBlackBright: this;
266+
readonly bgRedBright: this;
267+
readonly bgGreenBright: this;
268+
readonly bgYellowBright: this;
269+
readonly bgBlueBright: this;
270+
readonly bgMagentaBright: this;
271+
readonly bgCyanBright: this;
272+
readonly bgWhiteBright: this;
273+
}
266274
}
267275

268276
/**
269-
* Main Chalk object that allows to chain styles together.
270-
* Call the last one as a method with a string argument.
271-
* Order doesn't matter, and later styles take precedent in case of a conflict.
272-
* This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
273-
*/
274-
declare const chalk: Chalk & {supportsColor: ColorSupport};
275-
276-
export default chalk;
277+
Main Chalk object that allows to chain styles together.
278+
Call the last one as a method with a string argument.
279+
Order doesn't matter, and later styles take precedent in case of a conflict.
280+
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
281+
*/
282+
declare const chalk: chalk.Chalk & {
283+
supportsColor: chalk.ColorSupport;
284+
Level: typeof LevelEnum;
285+
};
286+
287+
export = chalk;

‎index.js

-1
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,3 @@ Object.defineProperties(Chalk.prototype, styles);
199199

200200
module.exports = Chalk(); // eslint-disable-line new-cap
201201
module.exports.supportsColor = stdoutColor;
202-
module.exports.default = module.exports; // For TypeScript

‎index.test-d.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import {expectType} from 'tsd-check';
2-
import chalk, {Level, Chalk, ColorSupport} from '.';
1+
import {expectType} from 'tsd';
2+
import chalk = require('.');
33

44
// - Helpers -
5-
type colorReturn = Chalk & {supportsColor: ColorSupport};
5+
type colorReturn = chalk.Chalk & {supportsColor: chalk.ColorSupport};
66

77
// - Level -
8-
expectType<number>(Level.None);
9-
expectType<number>(Level.Basic);
10-
expectType<number>(Level.Ansi256);
11-
expectType<number>(Level.TrueColor);
8+
expectType<number>(chalk.Level.None);
9+
expectType<number>(chalk.Level.Basic);
10+
expectType<number>(chalk.Level.Ansi256);
11+
expectType<number>(chalk.Level.TrueColor);
1212

1313
// - supportsColor -
1414
expectType<boolean>(chalk.supportsColor.hasBasic);
@@ -17,11 +17,11 @@ expectType<boolean>(chalk.supportsColor.has16m);
1717

1818
// - Chalk -
1919
// -- Instance --
20-
expectType<Chalk>(new chalk.Instance({level: 1}));
20+
expectType<chalk.Chalk>(new chalk.Instance({level: 1}));
2121

2222
// -- Properties --
2323
expectType<boolean>(chalk.enabled);
24-
expectType<Level>(chalk.level);
24+
expectType<chalk.Level>(chalk.level);
2525

2626
// -- Template literal --
2727
expectType<string>(chalk``);

‎package.json

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"node": ">=8"
99
},
1010
"scripts": {
11-
"test": "xo && nyc ava && tsd-check && flow",
11+
"test": "xo && nyc ava && tsd && flow",
1212
"bench": "matcha benchmark.js"
1313
},
1414
"files": [
@@ -46,19 +46,18 @@
4646
"supports-color": "^6.1.0"
4747
},
4848
"devDependencies": {
49-
"@sindresorhus/tsconfig": "^0.2.1",
50-
"ava": "^1.3.1",
49+
"@sindresorhus/tsconfig": "^0.3.0",
50+
"ava": "^1.4.1",
5151
"coveralls": "^3.0.3",
5252
"execa": "^1.0.0",
53-
"flow-bin": "^0.94.0",
53+
"flow-bin": "^0.98.0",
5454
"import-fresh": "^3.0.0",
5555
"matcha": "^0.7.0",
56-
"nyc": "^13.3.0",
57-
"resolve-from": "^4.0.0",
58-
"tsd-check": "^0.3.0",
56+
"nyc": "^14.0.0",
57+
"resolve-from": "^5.0.0",
58+
"tsd": "^0.7.2",
5959
"xo": "^0.24.0"
6060
},
61-
"types": "index.d.ts",
6261
"xo": {
6362
"ignores": [
6463
"test/_flow.js"

0 commit comments

Comments
 (0)
Please sign in to comment.