@astrojs/cli-kit
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -15,1 +15,27 @@ /// <reference types="node" resolution-mode="require"/> | ||
}): Promise<void>; | ||
export interface Task { | ||
start: string; | ||
end: string; | ||
pending: string; | ||
onError?: (e: any) => void; | ||
while: (...args: any) => Promise<any>; | ||
} | ||
/** | ||
* Displays a spinner while executing a list of sequential tasks | ||
* Note that the tasks are not parallelized! A task is implicitly dependent on the tasks that preceed it. | ||
* | ||
* @param labels configures the start and end labels for the task queue | ||
* @param tasks is an array of tasks that will be displayed as a list | ||
* @param options can be used to the source of `stdin` and `stdout` | ||
*/ | ||
export declare function tasks({ start, end }: { | ||
start: string; | ||
end: string; | ||
}, t: Task[], { stdin, stdout }?: { | ||
stdin?: (NodeJS.ReadStream & { | ||
fd: 0; | ||
}) | undefined; | ||
stdout?: (NodeJS.WriteStream & { | ||
fd: 1; | ||
}) | undefined; | ||
}): Promise<void>; |
@@ -59,4 +59,5 @@ import readline from "node:readline"; | ||
stdin.setRawMode(true); | ||
stdout.write(cursor.hide + erase.lines(1)); | ||
stdout.write(cursor.hide + erase.lines(text.split('\n').length)); | ||
}; | ||
let refresh = () => { }; | ||
let done = false; | ||
@@ -78,3 +79,4 @@ const spinner = { | ||
let frame = frames[i]; | ||
logUpdate(`${frame} ${text}`); | ||
refresh = () => logUpdate(`${frame} ${text}`); | ||
refresh(); | ||
if (!done) | ||
@@ -86,2 +88,6 @@ await sleep(90); | ||
}, | ||
update(value) { | ||
text = value; | ||
refresh(); | ||
}, | ||
stop() { | ||
@@ -99,3 +105,3 @@ done = true; | ||
export async function spinner({ start, end, onError, while: update = () => sleep(100), }, { stdin = process.stdin, stdout = process.stdout } = {}) { | ||
const loading = await gradient(chalk.green(start), { stdin, stdout }); | ||
const loading = await gradient(start, { stdin, stdout }); | ||
const act = update(); | ||
@@ -117,1 +123,63 @@ const tooslow = Object.create(null); | ||
} | ||
const TASK_SUCCESS_FLASH = 750; | ||
const TASK_INDENT = 5; | ||
function formatTask(task, state) { | ||
switch (state) { | ||
case 'start': return `${" ".repeat(TASK_INDENT + 3)} ${chalk.cyan(`▶ ${task.start}`)}`; | ||
case 'pending': return `${" ".repeat(TASK_INDENT + 3)} ${chalk.dim(`□ ${task.pending}`)}`; | ||
case 'success': return `${" ".repeat(TASK_INDENT + 3)} ${chalk.green(`✔ ${task.end}`)}`; | ||
case 'end': return `${" ".repeat(TASK_INDENT + 3)} ${chalk.dim(`■ ${task.end}`)}`; | ||
} | ||
} | ||
/** | ||
* Displays a spinner while executing a list of sequential tasks | ||
* Note that the tasks are not parallelized! A task is implicitly dependent on the tasks that preceed it. | ||
* | ||
* @param labels configures the start and end labels for the task queue | ||
* @param tasks is an array of tasks that will be displayed as a list | ||
* @param options can be used to the source of `stdin` and `stdout` | ||
*/ | ||
export async function tasks({ start, end }, t, { stdin = process.stdin, stdout = process.stdout } = {}) { | ||
let text = Array.from({ length: t.length + 1 }, () => ''); | ||
text[0] = start; | ||
t.forEach((task, i) => { | ||
const state = i === 0 ? 'start' : 'pending'; | ||
text[i + 1] = formatTask(task, state); | ||
}); | ||
const loading = await gradient(text.join('\n'), { stdin, stdout }); | ||
const refresh = () => loading.update(text.join('\n')); | ||
let action; | ||
let i = 0; | ||
let timeouts = []; | ||
for (const task of t) { | ||
i++; | ||
text[i] = formatTask(task, 'start'); | ||
refresh(); | ||
action = task.while(); | ||
try { | ||
await action; | ||
text[i] = formatTask(task, 'success'); | ||
refresh(); | ||
const active = { i, task }; | ||
timeouts.push(setTimeout(() => { | ||
const { i, task } = active; | ||
text[i] = formatTask(task, 'end'); | ||
refresh(); | ||
}, TASK_SUCCESS_FLASH)); | ||
} | ||
catch (e) { | ||
loading.stop(); | ||
task.onError?.(e); | ||
} | ||
} | ||
for (const timeout of timeouts) { | ||
clearTimeout(timeout); | ||
} | ||
await sleep(TASK_SUCCESS_FLASH); | ||
loading.stop(); | ||
text[0] = `${" ".repeat(TASK_INDENT)} ${chalk.green("✔")} ${chalk.green(end)}`; | ||
t.forEach((task, i) => { | ||
text[i + 1] = formatTask(task, 'end'); | ||
}); | ||
console.log(text.join('\n')); | ||
} |
{ | ||
"name": "@astrojs/cli-kit", | ||
"type": "module", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "types": "./dist/index.d.ts", |
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
57313
1770