@astrojs/cli-kit
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -1,5 +0,12 @@ | ||
export declare const say: (messages?: string | string[], { clear, hat }?: { | ||
/// <reference types="node" /> | ||
export declare const say: (messages?: string | string[], { clear, hat, stdin, stdout }?: { | ||
clear?: boolean | undefined; | ||
hat?: string | undefined; | ||
stdin?: (NodeJS.ReadStream & { | ||
fd: 0; | ||
}) | undefined; | ||
stdout?: (NodeJS.WriteStream & { | ||
fd: 1; | ||
}) | undefined; | ||
}) => Promise<void>; | ||
export declare const label: (text: string, c?: import("chalk").ChalkInstance, t?: import("chalk").ChalkInstance) => string; |
import readline from 'node:readline'; | ||
import color from 'chalk'; | ||
import logUpdate from 'log-update'; | ||
import { createLogUpdate } from 'log-update'; | ||
import { random, randomBetween, sleep, useAscii } from '../utils/index.js'; | ||
import { action } from '../prompt/util/action.js'; | ||
import { strip } from '../prompt/util/clear.js'; | ||
export const say = async (messages = [], { clear = false, hat = '' } = {}) => { | ||
const rl = readline.createInterface({ input: process.stdin, escapeCodeTimeout: 50 }); | ||
readline.emitKeypressEvents(process.stdin, rl); | ||
export const say = async (messages = [], { clear = false, hat = '', stdin = process.stdin, stdout = process.stdout } = {}) => { | ||
const rl = readline.createInterface({ input: stdin, escapeCodeTimeout: 50 }); | ||
const logUpdate = createLogUpdate(stdout, { showCursor: false }); | ||
readline.emitKeypressEvents(stdin, rl); | ||
let i = 0; | ||
let cancelled = false; | ||
const done = async () => { | ||
process.stdin.off('keypress', done); | ||
if (process.stdin.isTTY) | ||
process.stdin.setRawMode(false); | ||
stdin.off('keypress', done); | ||
if (stdin.isTTY) | ||
stdin.setRawMode(false); | ||
rl.close(); | ||
@@ -28,7 +29,7 @@ cancelled = true; | ||
}; | ||
if (process.stdin.isTTY) | ||
process.stdin.setRawMode(true); | ||
process.stdin.on('keypress', (str, key) => { | ||
if (process.stdin.isTTY) | ||
process.stdin.setRawMode(true); | ||
if (stdin.isTTY) | ||
stdin.setRawMode(true); | ||
stdin.on('keypress', (str, key) => { | ||
if (stdin.isTTY) | ||
stdin.setRawMode(true); | ||
const k = action(key, true); | ||
@@ -84,9 +85,9 @@ if (k === 'abort') { | ||
} | ||
process.stdin.off('keypress', done); | ||
stdin.off('keypress', done); | ||
await sleep(100); | ||
done(); | ||
if (process.stdin.isTTY) | ||
process.stdin.setRawMode(false); | ||
process.stdin.removeAllListeners('keypress'); | ||
if (stdin.isTTY) | ||
stdin.setRawMode(false); | ||
stdin.removeAllListeners('keypress'); | ||
}; | ||
export const label = (text, c = color.bgHex('#883AE2'), t = color.whiteBright) => c(` ${t(text)} `); |
/** @type {import('../../types').default} */ | ||
export default function prompt<T extends Readonly<import("../../types").Prompt> | readonly import("../../types").Prompt[]>(questions?: T, { onSubmit, onCancel }?: import("../../types").PromptOptions | undefined): Promise<import("../../types").Answers<T>>; | ||
export default function prompt<T extends Readonly<import("../../types").Prompt> | readonly import("../../types").Prompt[]>(questions?: T, { onSubmit, onCancel, stdin, stdout }?: import("../../types").PromptOptions | undefined): Promise<import("../../types").Answers<T>>; |
@@ -5,3 +5,3 @@ import * as el from "./elements/index.js"; | ||
return new Promise((res, rej) => { | ||
const p = new el[type](args); | ||
const p = new el[type](args, opts); | ||
const onAbort = opts.onAbort || noop; | ||
@@ -23,3 +23,3 @@ const onSubmit = opts.onSubmit || noop; | ||
/** @type {import('../../types').default} */ | ||
export default async function prompt(questions = [], { onSubmit = noop, onCancel = () => process.exit(0) } = {}) { | ||
export default async function prompt(questions = [], { onSubmit = noop, onCancel = () => process.exit(0), stdin = process.stdin, stdout = process.stdout } = {}) { | ||
const answers = {}; | ||
@@ -32,3 +32,3 @@ questions = [].concat(questions); | ||
// Get the injected answer if there is one or prompt the user | ||
answer = await prompts[type](question); | ||
answer = await prompts[type](Object.assign({ stdin, stdout }, question)); | ||
answers[name] = answer; | ||
@@ -35,0 +35,0 @@ quit = await onSubmit(question, answer, answers); |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
export declare function spinner({ start, end, while: update }: { | ||
@@ -5,2 +6,9 @@ start: string; | ||
while: (...args: any) => Promise<any>; | ||
}, { stdin, stdout }?: { | ||
stdin?: (NodeJS.ReadStream & { | ||
fd: 0; | ||
}) | undefined; | ||
stdout?: (NodeJS.WriteStream & { | ||
fd: 1; | ||
}) | undefined; | ||
}): Promise<void>; |
@@ -45,14 +45,14 @@ import readline from 'node:readline'; | ||
*/ | ||
async function gradient(text) { | ||
async function gradient(text, { stdin = process.stdin, stdout = process.stdout } = {}) { | ||
let i = 0; | ||
const frames = getGradientAnimFrames(); | ||
let interval; | ||
const rl = readline.createInterface({ input: process.stdin, escapeCodeTimeout: 50 }); | ||
readline.emitKeypressEvents(process.stdin, rl); | ||
if (process.stdin.isTTY) | ||
process.stdin.setRawMode(true); | ||
const rl = readline.createInterface({ input: stdin, escapeCodeTimeout: 50 }); | ||
readline.emitKeypressEvents(stdin, rl); | ||
if (stdin.isTTY) | ||
stdin.setRawMode(true); | ||
const keypress = () => { | ||
if (process.stdin.isTTY) | ||
process.stdin.setRawMode(true); | ||
process.stdout.write(cursor.hide + erase.lines(2)); | ||
if (stdin.isTTY) | ||
stdin.setRawMode(true); | ||
stdout.write(cursor.hide + erase.lines(2)); | ||
}; | ||
@@ -62,4 +62,4 @@ let done = false; | ||
start() { | ||
process.stdout.write(cursor.hide); | ||
process.stdin.on('keypress', keypress); | ||
stdout.write(cursor.hide); | ||
stdin.on('keypress', keypress); | ||
logUpdate(`${frames[0]} ${text}`); | ||
@@ -85,3 +85,3 @@ const loop = async () => { | ||
done = true; | ||
process.stdin.removeListener('keypress', keypress); | ||
stdin.removeListener('keypress', keypress); | ||
clearInterval(interval); | ||
@@ -94,3 +94,3 @@ logUpdate.clear(); | ||
} | ||
export async function spinner({ start, end, while: update = () => sleep(100) }) { | ||
export async function spinner({ start, end, while: update = () => sleep(100) }, { stdin = process.stdin, stdout = process.stdout } = {}) { | ||
const act = update(); | ||
@@ -100,3 +100,3 @@ const tooslow = Object.create(null); | ||
if (result === tooslow) { | ||
const loading = await gradient(chalk.green(start)); | ||
const loading = await gradient(chalk.green(start), { stdin, stdout }); | ||
await act; | ||
@@ -106,3 +106,3 @@ loading.stop(); | ||
; | ||
console.log(`${' '.repeat(5)} ${chalk.green('✔')} ${chalk.green(end)}`); | ||
stdout.write(`\n${' '.repeat(5)} ${chalk.green('✔')} ${chalk.green(end)}`); | ||
} |
{ | ||
"name": "@astrojs/cli-kit", | ||
"type": "module", | ||
"version": "0.1.6", | ||
"license": "MIT", | ||
"types": "./dist/index.d.ts", | ||
"packageManager": "pnpm@7.11.0", | ||
"exports": { | ||
".": { | ||
"default": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"./utils": { | ||
"default": "./dist/utils/index.js", | ||
"types": "./dist/utils/index.d.ts" | ||
} | ||
"name": "@astrojs/cli-kit", | ||
"type": "module", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
"types": "./dist/index.d.ts", | ||
"packageManager": "pnpm@7.11.0", | ||
"exports": { | ||
".": { | ||
"default": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"files": [ | ||
"dist", | ||
"types.d.ts", | ||
"utils.d.ts" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/withastro/cli-kit.git" | ||
}, | ||
"scripts": { | ||
"build": "tsc -p ." | ||
}, | ||
"dependencies": { | ||
"chalk": "^5.0.1", | ||
"log-update": "^5.0.1", | ||
"sisteransi": "^1.0.5" | ||
}, | ||
"devDependencies": { | ||
"@changesets/cli": "^2.24.4", | ||
"@types/node": "^18.7.16", | ||
"typescript": "^4.8.3" | ||
}, | ||
"volta": { | ||
"node": "18.9.0", | ||
"npm": "8.19.1" | ||
"./utils": { | ||
"default": "./dist/utils/index.js", | ||
"types": "./dist/utils/index.d.ts" | ||
} | ||
} | ||
}, | ||
"files": [ | ||
"dist", | ||
"types.d.ts", | ||
"utils.d.ts" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/withastro/cli-kit.git" | ||
}, | ||
"dependencies": { | ||
"chalk": "^5.0.1", | ||
"log-update": "^5.0.1", | ||
"sisteransi": "^1.0.5" | ||
}, | ||
"devDependencies": { | ||
"@changesets/cli": "^2.24.4", | ||
"@types/node": "^18.7.16", | ||
"typescript": "^4.8.3" | ||
}, | ||
"volta": { | ||
"node": "18.9.0", | ||
"npm": "8.19.1" | ||
}, | ||
"scripts": { | ||
"build": "tsc -p ." | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52625
1614