Skip to content

Commit

Permalink
Improve tinyexec errors (withastro#12368)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Nov 4, 2024
1 parent ec3113d commit 493fe43
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-eyes-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improves error logs when executing commands
2 changes: 1 addition & 1 deletion benchmark/bench/cli-startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function benchmarkCommand(command, args, root) {

for (let i = 0; i < 10; i++) {
const start = performance.now();
await exec(command, args, { nodeOptions: { cwd: root } });
await exec(command, args, { nodeOptions: { cwd: root }, throwOnError: true });
durations.push(performance.now() - start);
}

Expand Down
1 change: 1 addition & 0 deletions benchmark/bench/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function run(projectDir, outputFile) {
ASTRO_TIMER_PATH: outputFilePath,
},
},
throwOnError: true,
});

console.log('Raw results written to', outputFilePath);
Expand Down
2 changes: 2 additions & 0 deletions benchmark/bench/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export async function run(projectDir, outputFile) {
cwd: root,
stdio: 'inherit',
},
throwOnError: true,
});

console.log('Previewing...');
Expand All @@ -33,6 +34,7 @@ export async function run(projectDir, outputFile) {
cwd: root,
stdio: 'inherit',
},
throwOnError: true,
});

console.log('Waiting for server ready...');
Expand Down
1 change: 1 addition & 0 deletions benchmark/bench/server-stress.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function run(projectDir, outputFile) {
cwd: root,
stdio: 'inherit',
},
throwOnError: true,
});

console.log('Previewing...');
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import ora from 'ora';
import preferredPM from 'preferred-pm';
import prompts from 'prompts';
import maxSatisfying from 'semver/ranges/max-satisfying.js';
import { exec } from 'tinyexec';
import {
loadTSConfig,
resolveConfig,
Expand All @@ -30,6 +29,7 @@ import { appendForwardSlash } from '../../core/path.js';
import { apply as applyPolyfill } from '../../core/polyfill.js';
import { ensureProcessNodeEnv, parseNpmName } from '../../core/util.js';
import { eventCliSession, telemetry } from '../../events/index.js';
import { exec } from '../exec.js';
import { type Flags, createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js';
import { fetchPackageJson, fetchPackageVersions } from '../install-package.js';

Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/cli/docs/open.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Result, exec } from 'tinyexec';
import type { Result } from 'tinyexec';
import { exec } from '../exec.js';

/**
* Credit: Azhar22
Expand Down
26 changes: 26 additions & 0 deletions packages/astro/src/cli/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NonZeroExitError, type Options, x } from 'tinyexec';

/**
* Improve tinyexec error logging and set `throwOnError` to `true` by default
*/
export function exec(command: string, args?: string[], options?: Partial<Options>) {
return x(command, args, {
throwOnError: true,
...options,
}).then(
(o) => o,
(e) => {
if (e instanceof NonZeroExitError) {
const fullCommand = args?.length
? `${command} ${args.map((a) => (a.includes(' ') ? `"${a}"` : a)).join(' ')}`
: command;
const message = `The command \`${fullCommand}\` exited with code ${e.exitCode}`;
const newError = new Error(message, e.cause ? { cause: e.cause } : undefined);
(newError as any).stderr = e.output?.stderr;
(newError as any).stdout = e.output?.stdout;
throw newError;
}
throw e;
},
);
}
2 changes: 1 addition & 1 deletion packages/astro/src/cli/install-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { bold, cyan, dim, magenta } from 'kleur/colors';
import ora from 'ora';
import preferredPM from 'preferred-pm';
import prompts from 'prompts';
import { exec } from 'tinyexec';
import whichPm from 'which-pm';
import type { Logger } from '../core/logger/core.js';
import { exec } from './exec.js';

const require = createRequire(import.meta.url);

Expand Down
1 change: 1 addition & 0 deletions scripts/smoke/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function run() {

await exec('pnpm', ['install'], {
nodeOptions: { cwd: fileURLToPath(rootDir), stdio: ['pipe', 'inherit', 'inherit'] },
throwOnError: true
});
}

Expand Down
5 changes: 4 additions & 1 deletion scripts/smoke/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ async function run() {

const directories = [...(await getChildDirectories(smokeDir)), ...(await getChildDirectories(exampleDir))];
/** @type {Partial<import('tinyexec').Options>} */
const execOptions = { nodeOptions: { cwd: fileURLToPath(rootDir), stdio: 'inherit' }};
const execOptions = {
nodeOptions: { cwd: fileURLToPath(rootDir), stdio: 'inherit' },
throwOnError: true,
};

console.log('🤖', 'Preparing', 'pnpm');

Expand Down

0 comments on commit 493fe43

Please sign in to comment.