Skip to content

Commit

Permalink
Fix(all): Remove dependency on @types/node (to reduce package size)
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Sep 28, 2024
1 parent 05867f4 commit c7b798d
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 34 deletions.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"devDependencies": {
"@eslint/js": "^9.11.1",
"@repo/tsconfig": "workspace:*",
"@types/node": "^22.7.4",
"@vitest/coverage-v8": "^2.1.1",
"@vitest/ui": "^2.1.1",
"eslint": "^9.11.1",
Expand Down
2 changes: 0 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@
"dependencies": {
"@inquirer/figures": "^1.0.6",
"@inquirer/type": "^2.0.0",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.7.4",
"@types/wrap-ansi": "^3.0.0",
"ansi-escapes": "^4.3.2",
"cli-width": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/create-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function createPrompt<Value, Config>(view: ViewFunction<Value, Config>) {
terminal: true,
input,
output,
}) as InquirerReadline;
}) as unknown as InquirerReadline;
const screen = new ScreenManager(rl);

const { promise, resolve, reject } = PromisePolyfill.withResolver<Value>();
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/lib/screen-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { CursorPos } from 'node:readline';
import stripAnsi from 'strip-ansi';
import ansiEscapes from 'ansi-escapes';
import { breakLines, readlineWidth } from './utils.js';
Expand All @@ -15,7 +14,10 @@ export default class ScreenManager {
// These variables are keeping information to allow correct prompt re-rendering
private height: number = 0;
private extraLinesUnderPrompt: number = 0;
private cursorPos: CursorPos;
private cursorPos: {
rows: number;
cols: number;
};

constructor(private readonly rl: InquirerReadline) {
this.rl = rl;
Expand Down
8 changes: 4 additions & 4 deletions packages/inquirer/inquirer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ describe('inquirer.prompt(...)', () => {
it("should close and create a new readline instances each time it's called", async () => {
const actualCreateInterface = readline.createInterface;
vi.spyOn(readline, 'createInterface').mockImplementation((opts) => {
const rl = actualCreateInterface(opts) as InquirerReadline;
const rl = actualCreateInterface(opts);
vi.spyOn(rl, 'close');
vi.spyOn(rl.output, 'end');
vi.spyOn((rl as unknown as InquirerReadline).output, 'end');
return rl;
});

Expand Down Expand Up @@ -266,9 +266,9 @@ describe('inquirer.prompt(...)', () => {
it('should close readline instance on rejected promise', async () => {
const actualCreateInterface = readline.createInterface;
vi.spyOn(readline, 'createInterface').mockImplementation((opts) => {
const rl = actualCreateInterface(opts) as InquirerReadline;
const rl = actualCreateInterface(opts);
vi.spyOn(rl, 'close');
vi.spyOn(rl.output, 'end');
vi.spyOn((rl as unknown as InquirerReadline).output, 'end');
return rl;
});

Expand Down
1 change: 0 additions & 1 deletion packages/inquirer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"@inquirer/core": "^9.2.1",
"@inquirer/prompts": "^6.0.1",
"@inquirer/type": "^2.0.0",
"@types/mute-stream": "^0.0.4",
"ansi-escapes": "^4.3.2",
"mute-stream": "^1.0.0",
"run-async": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/inquirer/src/ui/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export default class PromptsRunner<A extends Answers> {

const rl = readline.createInterface(
setupReadlineOptions(opt),
) as InquirerReadline;
) as unknown as InquirerReadline;

/**
* Handle the ^C exit
Expand Down
2 changes: 0 additions & 2 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@
},
"dependencies": {
"@inquirer/type": "^2.0.0",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.7.4",
"ansi-escapes": "^4.3.2",
"mute-stream": "^1.0.0",
"strip-ansi": "^6.0.1"
Expand Down
1 change: 1 addition & 0 deletions packages/type/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"tsc": "tshy"
},
"dependencies": {
"@types/mute-stream": "^0.0.4",
"mute-stream": "^1.0.0"
},
"devDependencies": {
Expand Down
19 changes: 17 additions & 2 deletions packages/type/src/inquirer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import * as readline from 'node:readline';
import MuteStream from 'mute-stream';

export type InquirerReadline = readline.ReadLine & {
/**
* `InquirerReadline` is a re-implementation of `readline.Interface` from Node.js.
* We're reimplementing it because of 3 reasons:
* 1. The `readline.Interface` API is not complete; it's missing for example `clearLine`.
* 2. The input/output streams are not generics, meaning they're inexact.
* 3. Since ReadLine isn't built-in Typescript Global NodeJS type, it'd force us to ship `@types/node` as a dependency to all users.
*/
export type InquirerReadline = {
output: MuteStream;
input: NodeJS.ReadableStream;
clearLine: (dir: 0 | 1 | -1) => void; // https://nodejs.org/api/readline.html#rlclearlinedir
getCursorPos: () => { rows: number; cols: number };
setPrompt: (prompt: string) => void;
line: string;
write: (data: string) => void;
on: (event: string, listener: (...args: unknown[]) => void) => void;
removeListener: (event: string, listener: (...args: unknown[]) => void) => void;
pause: () => void;
resume: () => void;
close: () => void;
};

export type Context = {
Expand Down
22 changes: 4 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,6 @@ __metadata:
"@inquirer/testing": "npm:^2.1.34"
"@inquirer/type": "npm:^2.0.0"
"@repo/tsconfig": "workspace:*"
"@types/mute-stream": "npm:^0.0.4"
"@types/node": "npm:^22.7.4"
"@types/wrap-ansi": "npm:^3.0.0"
ansi-escapes: "npm:^4.3.2"
cli-width: "npm:^4.1.0"
Expand Down Expand Up @@ -634,7 +632,6 @@ __metadata:
dependencies:
"@eslint/js": "npm:^9.11.1"
"@repo/tsconfig": "workspace:*"
"@types/node": "npm:^22.7.4"
"@vitest/coverage-v8": "npm:^2.1.1"
"@vitest/ui": "npm:^2.1.1"
eslint: "npm:^9.11.1"
Expand Down Expand Up @@ -694,8 +691,6 @@ __metadata:
"@arethetypeswrong/cli": "npm:^0.16.4"
"@inquirer/type": "npm:^2.0.0"
"@repo/tsconfig": "workspace:*"
"@types/mute-stream": "npm:^0.0.4"
"@types/node": "npm:^22.7.4"
ansi-escapes: "npm:^4.3.2"
mute-stream: "npm:^1.0.0"
strip-ansi: "npm:^6.0.1"
Expand All @@ -709,6 +704,7 @@ __metadata:
dependencies:
"@arethetypeswrong/cli": "npm:^0.16.4"
"@repo/tsconfig": "workspace:*"
"@types/mute-stream": "npm:^0.0.4"
mute-stream: "npm:^1.0.0"
tshy: "npm:^3.0.2"
languageName: unknown
Expand Down Expand Up @@ -1711,15 +1707,6 @@ __metadata:
linkType: hard

"@types/node@npm:*":
version: 22.5.4
resolution: "@types/node@npm:22.5.4"
dependencies:
undici-types: "npm:~6.19.2"
checksum: 10/d46e0abf437b36bdf89011287aa43873d68ea6f2521a11b5c9a033056fd0d07af36daf51439010e8d41c62c55d0b00e9b5e09ed00bb2617723f73f28a873903a
languageName: node
linkType: hard

"@types/node@npm:^22.7.4":
version: 22.7.4
resolution: "@types/node@npm:22.7.4"
dependencies:
Expand Down Expand Up @@ -4771,7 +4758,6 @@ __metadata:
"@inquirer/prompts": "npm:^6.0.1"
"@inquirer/type": "npm:^2.0.0"
"@repo/tsconfig": "workspace:*"
"@types/mute-stream": "npm:^0.0.4"
ansi-escapes: "npm:^4.3.2"
mute-stream: "npm:^1.0.0"
run-async: "npm:^3.0.0"
Expand Down Expand Up @@ -8678,9 +8664,9 @@ __metadata:
linkType: hard

"undici-types@npm:~6.19.2":
version: 6.19.6
resolution: "undici-types@npm:6.19.6"
checksum: 10/0ea9bc25762a86597d095b3772f6cec0bcabb796c339f7dfa2bd601c745a480289eb2939848dc285a56d4f94f50c475868160d8d6d3f54e823f1faf7ea9e9468
version: 6.19.8
resolution: "undici-types@npm:6.19.8"
checksum: 10/cf0b48ed4fc99baf56584afa91aaffa5010c268b8842f62e02f752df209e3dea138b372a60a963b3b2576ed932f32329ce7ddb9cb5f27a6c83040d8cd74b7a70
languageName: node
linkType: hard

Expand Down

0 comments on commit c7b798d

Please sign in to comment.