0
Fork 0
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:
Bjorn Lu 2024-11-04 22:49:33 +08:00 committed by GitHub
parent ec3113d25a
commit 493fe43cd3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 45 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Improves error logs when executing commands

View file

@ -45,7 +45,7 @@ async function benchmarkCommand(command, args, root) {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
const start = performance.now(); 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); durations.push(performance.now() - start);
} }

View file

@ -26,6 +26,7 @@ export async function run(projectDir, outputFile) {
ASTRO_TIMER_PATH: outputFilePath, ASTRO_TIMER_PATH: outputFilePath,
}, },
}, },
throwOnError: true,
}); });
console.log('Raw results written to', outputFilePath); console.log('Raw results written to', outputFilePath);

View file

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

View file

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

View file

@ -10,7 +10,6 @@ import ora from 'ora';
import preferredPM from 'preferred-pm'; import preferredPM from 'preferred-pm';
import prompts from 'prompts'; import prompts from 'prompts';
import maxSatisfying from 'semver/ranges/max-satisfying.js'; import maxSatisfying from 'semver/ranges/max-satisfying.js';
import { exec } from 'tinyexec';
import { import {
loadTSConfig, loadTSConfig,
resolveConfig, resolveConfig,
@ -30,6 +29,7 @@ import { appendForwardSlash } from '../../core/path.js';
import { apply as applyPolyfill } from '../../core/polyfill.js'; import { apply as applyPolyfill } from '../../core/polyfill.js';
import { ensureProcessNodeEnv, parseNpmName } from '../../core/util.js'; import { ensureProcessNodeEnv, parseNpmName } from '../../core/util.js';
import { eventCliSession, telemetry } from '../../events/index.js'; import { eventCliSession, telemetry } from '../../events/index.js';
import { exec } from '../exec.js';
import { type Flags, createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js'; import { type Flags, createLoggerFromFlags, flagsToAstroInlineConfig } from '../flags.js';
import { fetchPackageJson, fetchPackageVersions } from '../install-package.js'; import { fetchPackageJson, fetchPackageVersions } from '../install-package.js';

View file

@ -1,4 +1,5 @@
import { type Result, exec } from 'tinyexec'; import type { Result } from 'tinyexec';
import { exec } from '../exec.js';
/** /**
* Credit: Azhar22 * Credit: Azhar22

View 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;
},
);
}

View file

@ -5,9 +5,9 @@ import { bold, cyan, dim, magenta } from 'kleur/colors';
import ora from 'ora'; import ora from 'ora';
import preferredPM from 'preferred-pm'; import preferredPM from 'preferred-pm';
import prompts from 'prompts'; import prompts from 'prompts';
import { exec } from 'tinyexec';
import whichPm from 'which-pm'; import whichPm from 'which-pm';
import type { Logger } from '../core/logger/core.js'; import type { Logger } from '../core/logger/core.js';
import { exec } from './exec.js';
const require = createRequire(import.meta.url); const require = createRequire(import.meta.url);

View file

@ -38,6 +38,7 @@ async function run() {
await exec('pnpm', ['install'], { await exec('pnpm', ['install'], {
nodeOptions: { cwd: fileURLToPath(rootDir), stdio: ['pipe', 'inherit', 'inherit'] }, nodeOptions: { cwd: fileURLToPath(rootDir), stdio: ['pipe', 'inherit', 'inherit'] },
throwOnError: true
}); });
} }

View file

@ -33,7 +33,10 @@ async function run() {
const directories = [...(await getChildDirectories(smokeDir)), ...(await getChildDirectories(exampleDir))]; const directories = [...(await getChildDirectories(smokeDir)), ...(await getChildDirectories(exampleDir))];
/** @type {Partial<import('tinyexec').Options>} */ /** @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'); console.log('🤖', 'Preparing', 'pnpm');