mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Improve tinyexec errors (#12368)
This commit is contained in:
parent
ec3113d25a
commit
493fe43cd3
11 changed files with 45 additions and 5 deletions
5
.changeset/many-eyes-call.md
Normal file
5
.changeset/many-eyes-call.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Improves error logs when executing commands
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ export async function run(projectDir, outputFile) {
|
|||
ASTRO_TIMER_PATH: outputFilePath,
|
||||
},
|
||||
},
|
||||
throwOnError: true,
|
||||
});
|
||||
|
||||
console.log('Raw results written to', outputFilePath);
|
||||
|
|
|
@ -25,6 +25,7 @@ export async function run(projectDir, outputFile) {
|
|||
cwd: root,
|
||||
stdio: 'inherit',
|
||||
},
|
||||
throwOnError: true,
|
||||
});
|
||||
|
||||
console.log('Previewing...');
|
||||
|
@ -33,6 +34,7 @@ export async function run(projectDir, outputFile) {
|
|||
cwd: root,
|
||||
stdio: 'inherit',
|
||||
},
|
||||
throwOnError: true,
|
||||
});
|
||||
|
||||
console.log('Waiting for server ready...');
|
||||
|
|
|
@ -24,6 +24,7 @@ export async function run(projectDir, outputFile) {
|
|||
cwd: root,
|
||||
stdio: 'inherit',
|
||||
},
|
||||
throwOnError: true,
|
||||
});
|
||||
|
||||
console.log('Previewing...');
|
||||
|
|
|
@ -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,
|
||||
|
@ -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';
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { type Result, exec } from 'tinyexec';
|
||||
import type { Result } from 'tinyexec';
|
||||
import { exec } from '../exec.js';
|
||||
|
||||
/**
|
||||
* Credit: Azhar22
|
||||
|
|
26
packages/astro/src/cli/exec.ts
Normal file
26
packages/astro/src/cli/exec.ts
Normal file
|
@ -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;
|
||||
},
|
||||
);
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ async function run() {
|
|||
|
||||
await exec('pnpm', ['install'], {
|
||||
nodeOptions: { cwd: fileURLToPath(rootDir), stdio: ['pipe', 'inherit', 'inherit'] },
|
||||
throwOnError: true
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
||||
|
|
Loading…
Reference in a new issue