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 Oct 6, 2024
1 parent b0a84ab commit c12ab13
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 36 deletions.
Binary file not shown.
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +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",
"mute-stream": "^2.0.0",
Expand All @@ -91,6 +88,9 @@
"@arethetypeswrong/cli": "^0.16.4",
"@inquirer/testing": "^2.1.34",
"@repo/tsconfig": "workspace:*",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.7.4",
"@types/wrap-ansi": "^3.0.0",
"tshy": "^3.0.2"
},
"engines": {
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
1 change: 1 addition & 0 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
},
"devDependencies": {
"@repo/tsconfig": "workspace:*",
"@types/node": "^22.7.4",
"tshy": "^3.0.2"
},
"engines": {
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
2 changes: 1 addition & 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": "^2.0.0",
"run-async": "^3.0.0",
Expand All @@ -86,6 +85,7 @@
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.4",
"@repo/tsconfig": "workspace:*",
"@types/mute-stream": "^0.0.4",
"tshy": "^3.0.2"
},
"engines": {
Expand Down
4 changes: 1 addition & 3 deletions packages/inquirer/src/run-async.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

type ExtractPromise<T> = T extends Promise<infer U> ? U : T;

declare module 'run-async' {
const runAsync: <F extends (...args: any[]) => any>(
func: F,
) => (...args: Parameters<F>) => Promise<ExtractPromise<ReturnType<F>>>;
) => (...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>;

export default runAsync;
}
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
4 changes: 2 additions & 2 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@
},
"dependencies": {
"@inquirer/type": "^2.0.0",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.7.4",
"ansi-escapes": "^4.3.2",
"mute-stream": "^2.0.0",
"strip-ansi": "^6.0.1"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.4",
"@repo/tsconfig": "workspace:*",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.7.4",
"tshy": "^3.0.2"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions packages/type/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@
"attw": "attw --pack",
"tsc": "tshy"
},
"dependencies": {
"mute-stream": "^2.0.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.4",
"@repo/tsconfig": "workspace:*",
"tshy": "^3.0.2"
},
"peerDependencies": {
"@types/node": ">=18"
},
"engines": {
"node": ">=18"
},
Expand Down
25 changes: 20 additions & 5 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 & {
output: MuteStream;
/// <reference types="node" />
import { Duplex } from 'node:stream';
/**
* `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: Duplex & { mute: () => void; unmute: () => void };
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
15 changes: 4 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ __metadata:
"@inquirer/figures": "npm:^1.0.6"
"@inquirer/prompts": "npm:^6.0.1"
"@repo/tsconfig": "workspace:*"
"@types/node": "npm:^22.7.4"
tshy: "npm:^3.0.2"
yoctocolors-cjs: "npm:^2.1.2"
bin:
Expand Down Expand Up @@ -711,8 +712,9 @@ __metadata:
dependencies:
"@arethetypeswrong/cli": "npm:^0.16.4"
"@repo/tsconfig": "workspace:*"
mute-stream: "npm:^2.0.0"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": "*"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -1712,16 +1714,7 @@ __metadata:
languageName: node
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":
"@types/node@npm:*, @types/node@npm:^22.7.4":
version: 22.7.4
resolution: "@types/node@npm:22.7.4"
dependencies:
Expand Down

0 comments on commit c12ab13

Please sign in to comment.