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

refactor: use OpenAlly configs #122

Merged
merged 1 commit into from
Aug 14, 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
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

10 changes: 0 additions & 10 deletions .eslintrc

This file was deleted.

6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ small and all contributions are valued.

This guide explains the process for contributing to the TopCli project's.

## [Code of Conduct](https://github.com/TopCli/Governance/blob/master/CODE_OF_CONDUCT.md)
## [Code of Conduct](https://github.com/TopCli/Governance/blob/main/CODE_OF_CONDUCT.md)

The TopCli project has a
[Code of Conduct](https://github.com/TopCli/Governance/blob/master/CODE_OF_CONDUCT.md)
[Code of Conduct](https://github.com/TopCli/Governance/blob/main/CODE_OF_CONDUCT.md)
that *all* contributors are expected to follow. This code describes the
*minimum* behavior expectations for all contributors.

See [details on our policy on Code of Conduct](https://github.com/TopCli/Governance/blob/master/COC_POLICY.md).
See [details on our policy on Code of Conduct](https://github.com/TopCli/Governance/blob/main/COC_POLICY.md).

<a id="developers-certificate-of-origin"></a>
## Developer's Certificate of Origin 1.1
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2023-2024 Pierre Demailly
Copyright 2023-2024 TopCli

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
![version](https://img.shields.io/badge/dynamic/json.svg?style=for-the-badge&url=https://raw.githubusercontent.com/TopCli/prompts/main/package.json&query=$.version&label=Version)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg?style=for-the-badge)](https://github.com/TopCli/prompts/commit-activity)
[![isc](https://img.shields.io/badge/License-ISC-blue.svg?style=for-the-badge)](https://github.com/TopCli/prompts/blob/main/LICENSE)
[![scorecard](https://api.securityscorecards.dev/projects/github.com/TopCli/prompts/badge?style=for-the-badge)](https://ossf.github.io/scorecard-visualizer/#/projects/github.com/TopCli/prompts)
![build](https://img.shields.io/github/actions/workflow/status/TopCli/prompts/node.js.yml?style=for-the-badge)

<img src="./public/topcli.gif" alt="demo">
Expand Down Expand Up @@ -229,6 +230,12 @@ export interface ConfirmOptions extends SharedOptions {
}
```

## Contributing

Please read [CONTRIBUTING.md](./CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

Open an issue if you want to provide feedback such as bug reports or enchancements.

## Contributors

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
Expand Down
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { typescriptConfig } from "@openally/config.eslint";

export default typescriptConfig();
11 changes: 7 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function select(message: string, options: Omit<SelectOptions, "mess
return selectPrompt.select();
}

export async function confirm(message: string, options: Omit<ConfirmOptions, "message">) {
export async function confirm(message: string, options: Omit<ConfirmOptions, "message"> = {}) {
const confirmPrompt = new prompts.ConfirmPrompt({ ...options, message });

return confirmPrompt.confirm();
Expand All @@ -31,14 +31,17 @@ export async function multiselect(message: string, options: Omit<MultiselectOpti
return multiselectPrompt.multiselect();
}

export {
required,
export type {
PromptValidator,
PromptAgent,
AbstractPromptOptions,
QuestionOptions,
ConfirmOptions,
Choice,
MultiselectOptions,
SelectOptions
};

export {
required,
PromptAgent
};
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
"license": "ISC",
"type": "module",
"devDependencies": {
"@nodesecure/eslint-config": "^1.9.0",
"@types/node": "^22.0.0",
"@openally/config.eslint": "^1.0.0",
"@openally/config.typescript": "^1.0.3",
"@types/node": "^22.3.0",
"c8": "^10.1.2",
"esmock": "^2.6.6",
"esmock": "^2.6.7",
"glob": "^11.0.0",
"tsup": "^8.1.0",
"tsx": "^4.16.2",
"typescript": "^5.5.3"
"tsup": "^8.2.4",
"tsx": "^4.17.0",
"typescript": "^5.5.4"
},
"dependencies": {
"@topcli/wcwidth": "^1.0.1"
Expand Down
2 changes: 1 addition & 1 deletion src/prompts/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Stdin = NodeJS.ReadStream & {

type Stdout = NodeJS.WriteStream & {
fd: 1;
}
};

export interface AbstractPromptOptions {
stdin?: Stdin;
Expand Down
5 changes: 3 additions & 2 deletions src/prompts/confirm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { styleText } from "node:util";
import wcwidth from "@topcli/wcwidth";

// Import Internal Dependencies
import { AbstractPrompt, AbstractPromptOptions } from "./abstract.js";
import { AbstractPrompt, type AbstractPromptOptions } from "./abstract.js";
import { stripAnsi } from "../utils.js";
import { SYMBOLS } from "../constants.js";

Expand Down Expand Up @@ -73,7 +73,7 @@ export class ConfirmPrompt extends AbstractPrompt<boolean> {
});
}

#onKeypress(resolve: (value: unknown) => void, value: never, key: Key) {
#onKeypress(resolve: (value: unknown) => void, value: any, key: Key) {
this.stdout.moveCursor(
-this.stdout.columns,
-Math.floor(wcwidth(stripAnsi(this.#getQuestionQuery())) / this.stdout.columns)
Expand Down Expand Up @@ -127,6 +127,7 @@ export class ConfirmPrompt extends AbstractPrompt<boolean> {
}

confirm(): Promise<boolean> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async(resolve, reject) => {
const answer = this.agent.nextAnswers.shift();
if (answer !== undefined) {
Expand Down
27 changes: 15 additions & 12 deletions src/prompts/multiselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { styleText } from "node:util";
import wcwidth from "@topcli/wcwidth";

// Import Internal Dependencies
import { AbstractPrompt, AbstractPromptOptions } from "./abstract.js";
import { AbstractPrompt, type AbstractPromptOptions } from "./abstract.js";
import { stripAnsi } from "../utils.js";
import { SYMBOLS } from "../constants.js";
import { isValid, PromptValidator, resultError } from "../validators.js";
import { Choice } from "../types.js";
import { isValid, type PromptValidator, resultError } from "../validators.js";
import { type Choice } from "../types.js";

// CONSTANTS
const kRequiredChoiceProperties = ["label", "value"];
Expand All @@ -25,9 +25,11 @@ export interface MultiselectOptions extends AbstractPromptOptions {
showHint?: boolean;
}

type VoidFn = () => void;

export class MultiselectPrompt extends AbstractPrompt<string | string[]> {
#boundExitEvent = () => void 0;
#boundKeyPressEvent = () => void 0;
#boundExitEvent: VoidFn = () => void 0;
#boundKeyPressEvent: VoidFn = () => void 0;
#validators: PromptValidator[];
#showHint: boolean;

Expand Down Expand Up @@ -222,8 +224,9 @@ export class MultiselectPrompt extends AbstractPrompt<string | string[]> {
render();
}
else if (key.ctrl && key.name === "a") {
// eslint-disable-next-line max-len
this.selectedIndexes = this.selectedIndexes.size === this.filteredChoices.length ? new Set() : new Set(this.filteredChoices.map((_, index) => index));
this.selectedIndexes = this.selectedIndexes.size === this.filteredChoices.length ?
new Set() :
new Set(this.filteredChoices.map((_, index) => index));
render();
}
else if (key.name === "right") {
Expand Down Expand Up @@ -306,10 +309,10 @@ export class MultiselectPrompt extends AbstractPrompt<string | string[]> {

const render = (
options: {
initialRender?: boolean;
clearRender?: boolean;
error?: string
} = {}
initialRender?: boolean;
clearRender?: boolean;
error?: string;
} = {}
) => {
const {
initialRender = false,
Expand Down Expand Up @@ -366,7 +369,7 @@ export class MultiselectPrompt extends AbstractPrompt<string | string[]> {

#showQuestion(error: string | null = null) {
let hint = this.#showHint ? styleText("gray",
// eslint-disable-next-line max-len
// eslint-disable-next-line @stylistic/max-len
`(Press ${styleText("bold", "<Ctrl+A>")} to toggle all, ${styleText("bold", "<Left/Right>")} to toggle, ${styleText("bold", "<Return>")} to submit)`
) : "";
if (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/prompts/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { styleText } from "node:util";
import wcwidth from "@topcli/wcwidth";

// Import Internal Dependencies
import { AbstractPrompt, AbstractPromptOptions } from "./abstract.js";
import { AbstractPrompt, type AbstractPromptOptions } from "./abstract.js";
import { stripAnsi } from "../utils.js";
import { SYMBOLS } from "../constants.js";
import { isValid, PromptValidator, resultError } from "../validators.js";
import { isValid, type PromptValidator, resultError } from "../validators.js";

export interface QuestionOptions extends AbstractPromptOptions {
defaultValue?: string;
Expand Down Expand Up @@ -101,6 +101,7 @@ export class QuestionPrompt extends AbstractPrompt<string> {
}

question(): Promise<string> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async(resolve, reject) => {
this.answer = this.agent.nextAnswers.shift();
if (this.answer !== undefined) {
Expand All @@ -116,7 +117,6 @@ export class QuestionPrompt extends AbstractPrompt<string> {
reject(error);
});


this.answer = await this.#question();

if (this.answer === "" && this.defaultValue) {
Expand Down
20 changes: 11 additions & 9 deletions src/prompts/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { styleText } from "node:util";
import wcwidth from "@topcli/wcwidth";

// Import Internal Dependencies
import { AbstractPrompt, AbstractPromptOptions } from "./abstract.js";
import { AbstractPrompt, type AbstractPromptOptions } from "./abstract.js";
import { stripAnsi } from "../utils.js";
import { SYMBOLS } from "../constants.js";
import { isValid, PromptValidator, resultError } from "../validators.js";
import { Choice } from "../types.js";
import { isValid, type PromptValidator, resultError } from "../validators.js";
import { type Choice } from "../types.js";

// CONSTANTS
const kRequiredChoiceProperties = ["label", "value"];
Expand All @@ -24,9 +24,11 @@ export interface SelectOptions extends AbstractPromptOptions {
caseSensitive?: boolean;
}

type VoidFn = () => void;

export class SelectPrompt extends AbstractPrompt<string> {
#boundExitEvent = () => void 0;
#boundKeyPressEvent = () => void 0;
#boundExitEvent: VoidFn = () => void 0;
#boundKeyPressEvent: VoidFn = () => void 0;
#validators: PromptValidator[];
activeIndex = 0;
questionMessage: string;
Expand Down Expand Up @@ -257,10 +259,10 @@ export class SelectPrompt extends AbstractPrompt<string> {

const render = (
options: {
initialRender?: boolean;
clearRender?: boolean;
error?: string
} = {}
initialRender?: boolean;
clearRender?: boolean;
error?: string;
} = {}
) => {
const {
initialRender = false,
Expand Down
20 changes: 10 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const kAnsiRegex = ansiRegex();
*/
export function ansiRegex({ onlyFirst = false } = {}) {
const pattern = [
// eslint-disable-next-line max-len
// eslint-disable-next-line @stylistic/max-len
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))"
].join("|");
Expand All @@ -31,13 +31,13 @@ export function isUnicodeSupported() {

// Windows Terminal
return Boolean(process.env.WT_SESSION)
// Terminus (<0.2.27)
|| Boolean(process.env.TERMINUS_SUBLIME)
// ConEmu and cmder
|| process.env.ConEmuTask === "{cmd::Cmder}"
|| process.env.TERM_PROGRAM === "Terminus-Sublime"
|| process.env.TERM_PROGRAM === "vscode"
|| process.env.TERM === "xterm-256color"
|| process.env.TERM === "alacritty"
|| process.env.TERMINAL_EMULATOR === "JetBrains-JediTerm";
// Terminus (<0.2.27)
|| Boolean(process.env.TERMINUS_SUBLIME)
// ConEmu and cmder
|| process.env.ConEmuTask === "{cmd::Cmder}"
|| process.env.TERM_PROGRAM === "Terminus-Sublime"
|| process.env.TERM_PROGRAM === "vscode"
|| process.env.TERM === "xterm-256color"
|| process.env.TERM === "alacritty"
|| process.env.TERMINAL_EMULATOR === "JetBrains-JediTerm";
}
4 changes: 2 additions & 2 deletions src/validators.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export type ValidResponseObject = {
isValid?: true;
}
};
export type InvalidResponseObject = {
isValid: false;
error: string;
}
};
export type ValidationResponseObject = ValidResponseObject | InvalidResponseObject;
export type ValidationResponse = InvalidResponse | ValidResponse;
export type InvalidResponse = string | InvalidResponseObject;
Expand Down
10 changes: 5 additions & 5 deletions test/helpers/testing-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export type TestingPromptOptions = Partial<QuestionOptions &
SelectOptions &
MultiselectOptions &
ConfirmOptions & {
input: any;
inputs: any[];
}> & {
onStdoutWrite: (value: string) => void;
}
input: any;
inputs: any[];
}> & {
onStdoutWrite: (value: string) => void;
};

export class TestingPrompt {
static async QuestionPrompt(message: string, options: TestingPromptOptions) {
Expand Down
3 changes: 1 addition & 2 deletions test/multi-select-prompt.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-lines */
/* eslint-disable max-len */

// Import Node.js Dependencies
import assert from "node:assert";
import { describe, it } from "node:test";
Expand Down Expand Up @@ -62,7 +62,6 @@ describe("MultiselectPrompt", () => {
});
});


it("should throw AbortError", async() => {
const { stdin, stdout } = mockProcess();

Expand Down
2 changes: 0 additions & 2 deletions test/select-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ describe("SelectPrompt", () => {
});
});


it("When press <return>, it should select the first choice.", async() => {
const message = "Choose between foo & bar";
const options = {
Expand Down Expand Up @@ -574,7 +573,6 @@ describe("SelectPrompt", () => {
assert.equal(input, "bar");
});


it("autocomplete filters should be case sensitive", async() => {
const logs: string[] = [];
const message = "Choose between foo, bar or baz";
Expand Down
Loading