Skip to content

Commit 61c62ec

Browse files
authored
feat: display package manager output during dependency installs (#305)
1 parent 9da497f commit 61c62ec

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

.changeset/fluffy-readers-attend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
feat: display package manager output during dependency installs

packages/clack-prompts/index.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,52 @@ function buildBox(message = '', title = '', dimmed = true) {
603603

604604
export const note = (message = '', title = ''): void => buildBox(message, title, true);
605605
export const box = (message = '', title = ''): void => buildBox(message, title, false);
606+
export const taskLog = (title: string) => {
607+
const BAR = color.dim(S_BAR);
608+
const ACTIVE = color.green(S_STEP_SUBMIT);
609+
const SUCCESS = color.green(S_SUCCESS);
610+
const ERROR = color.red(S_ERROR);
611+
612+
// heading
613+
process.stdout.write(`${BAR}\n`);
614+
process.stdout.write(`${ACTIVE} ${title}\n`);
615+
616+
let output = '';
617+
618+
// clears previous output
619+
const clear = (buffer = 0): void => {
620+
if (!output) return;
621+
const lines = output.split('\n').length + buffer;
622+
process.stdout.write(erase.lines(lines + 1));
623+
};
624+
625+
// logs the output
626+
const print = (): void => {
627+
const lines = output.split('\n');
628+
for (const line of lines) {
629+
const msg = color.dim(`${BAR} ${line}\n`);
630+
process.stdout.write(msg);
631+
}
632+
};
633+
634+
return {
635+
set text(data: string) {
636+
clear();
637+
output += data;
638+
print();
639+
},
640+
fail(message: string): void {
641+
clear(1); // includes clearing the `title`
642+
process.stdout.write(`${ERROR} ${message}\n`);
643+
// log the output on failure
644+
print();
645+
},
646+
success(message: string): void {
647+
clear(1); // includes clearing the `title`
648+
process.stdout.write(`${SUCCESS} ${message}\n`);
649+
}
650+
};
651+
};
606652

607653
export const cancel = (message = ''): void => {
608654
process.stdout.write(`${color.gray(S_BAR_END)} ${color.red(message)}\n\n`);

packages/cli/utils/package-manager.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import process from 'node:process';
2-
import { exec, NonZeroExitError } from 'tinyexec';
2+
import { exec } from 'tinyexec';
33
import * as p from '@sveltejs/clack-prompts';
44
import {
55
AGENTS,
@@ -32,22 +32,29 @@ export async function packageManagerPrompt(cwd: string): Promise<AgentName | und
3232
}
3333

3434
export async function installDependencies(agent: AgentName, cwd: string): Promise<void> {
35-
const spinner = p.spinner();
36-
spinner.start('Installing dependencies...');
35+
const task = p.taskLog(`Installing dependencies with ${agent}...`);
36+
3737
try {
3838
const { command, args } = constructCommand(COMMANDS[agent].install, [])!;
39-
await exec(command, args, { nodeOptions: { cwd }, throwOnError: true });
39+
const proc = exec(command, args, {
40+
nodeOptions: { cwd, stdio: 'pipe' },
41+
throwOnError: true
42+
});
4043

41-
spinner.stop('Successfully installed dependencies');
42-
} catch (error) {
43-
spinner.stop('Failed to install dependencies', 2);
44+
proc.process?.stdout?.on('data', (data) => {
45+
task.text = data;
46+
});
47+
proc.process?.stderr?.on('data', (data) => {
48+
task.text = data;
49+
});
4450

45-
if (error instanceof NonZeroExitError) {
46-
const stderr = error.output?.stderr;
47-
if (stderr) p.log.error(stderr);
48-
}
51+
await proc;
4952

50-
throw error;
53+
task.success('Successfully installed dependencies');
54+
} catch {
55+
task.fail('Failed to install dependencies');
56+
p.cancel('Operation failed.');
57+
process.exit(2);
5158
}
5259
}
5360

0 commit comments

Comments
 (0)