Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(all): Remove dependency on @types/node (to reduce package size) #1569

Merged
merged 3 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jobs:
yarn workspace @inquirer/core pack --out /tmp/artifacts/inquirer-core.tgz
yarn workspace @inquirer/prompts pack --out /tmp/artifacts/inquirer-prompts.tgz
yarn workspace @inquirer/figures pack --out /tmp/artifacts/inquirer-figures.tgz
yarn workspace @inquirer/type pack --out /tmp/artifacts/inquirer-type.tgz
yarn workspace @repo/tsconfig pack --out /tmp/artifacts/tsconfig.tgz
- name: Extract @inquirer/demo to an isolated project & build
run: |
Expand All @@ -141,10 +142,12 @@ jobs:
cp -r packages/demo $temp_dir
cp .yarnrc.yml $temp_dir/demo
cd $temp_dir/demo
yarn set version stable
yarn add --dev @repo/tsconfig@file:/tmp/artifacts/tsconfig.tgz
yarn add @inquirer/core@file:/tmp/artifacts/inquirer-core.tgz
yarn add @inquirer/prompts@file:/tmp/artifacts/inquirer-prompts.tgz
yarn add @inquirer/figures@file:/tmp/artifacts/inquirer-figures.tgz
yarn set resolution @inquirer/type@npm:* file:/tmp/artifacts/inquirer-type.tgz
yarn install
yarn tsc

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/checkbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/figures": "^1.0.6",
"@inquirer/type": "^2.0.0",
"@inquirer/type": "2.0.0",
"ansi-escapes": "^4.3.2",
"yoctocolors-cjs": "^2.1.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/confirm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/type": "^2.0.0"
"@inquirer/type": "2.0.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.4",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@
},
"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",
"@inquirer/type": "2.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
2 changes: 1 addition & 1 deletion packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/type": "^2.0.0",
"@inquirer/type": "2.0.0",
"external-editor": "^3.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/expand/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/type": "^2.0.0",
"@inquirer/type": "2.0.0",
"yoctocolors-cjs": "^2.1.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/type": "^2.0.0"
"@inquirer/type": "2.0.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.4",
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
4 changes: 2 additions & 2 deletions packages/inquirer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/prompts": "^6.0.1",
"@inquirer/type": "^2.0.0",
"@types/mute-stream": "^0.0.4",
"@inquirer/type": "2.0.0",
"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
2 changes: 1 addition & 1 deletion packages/number/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/type": "^2.0.0"
"@inquirer/type": "2.0.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/password/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/type": "^2.0.0",
"@inquirer/type": "2.0.0",
"ansi-escapes": "^4.3.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/prompts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.4",
"@inquirer/type": "^2.0.0",
"@inquirer/type": "2.0.0",
"@repo/tsconfig": "workspace:*",
"tshy": "^3.0.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rawlist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/type": "^2.0.0",
"@inquirer/type": "2.0.0",
"yoctocolors-cjs": "^2.1.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/figures": "^1.0.6",
"@inquirer/type": "^2.0.0",
"@inquirer/type": "2.0.0",
"yoctocolors-cjs": "^2.1.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"dependencies": {
"@inquirer/core": "^9.2.1",
"@inquirer/figures": "^1.0.6",
"@inquirer/type": "^2.0.0",
"@inquirer/type": "2.0.0",
"ansi-escapes": "^4.3.2",
"yoctocolors-cjs": "^2.1.2"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@
"tsc": "tshy"
},
"dependencies": {
"@inquirer/type": "^2.0.0",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.7.4",
"@inquirer/type": "2.0.0",
"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
Loading
Loading