0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Use tinyexec (#11845)

This commit is contained in:
Bjorn Lu 2024-08-28 22:52:49 +08:00 committed by GitHub
parent 6272e6cec0
commit 440a4be0a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 87 additions and 59 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Replaces `execa` with `tinyexec` internally

View file

@ -1,5 +1,5 @@
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { execaCommand } from 'execa'; import { exec } from 'tinyexec';
import { markdownTable } from 'markdown-table'; import { markdownTable } from 'markdown-table';
import { astroBin, calculateStat } from './_util.js'; import { astroBin, calculateStat } from './_util.js';
@ -14,11 +14,11 @@ export async function run(projectDir, outputFile) {
const root = fileURLToPath(projectDir); const root = fileURLToPath(projectDir);
console.log('Benchmarking `astro --help`...'); console.log('Benchmarking `astro --help`...');
const helpStat = await benchmarkCommand(`node ${astroBin} --help`, root); const helpStat = await benchmarkCommand('node', [astroBin, '--help'], root);
console.log('Done'); console.log('Done');
console.log('Benchmarking `astro info`...'); console.log('Benchmarking `astro preferences list`...');
const infoStat = await benchmarkCommand(`node ${astroBin} info`, root); const infoStat = await benchmarkCommand('node', [astroBin, 'preferences', 'list'], root);
console.log('Done'); console.log('Done');
console.log('Result preview:'); console.log('Result preview:');
@ -35,16 +35,17 @@ export async function run(projectDir, outputFile) {
/** /**
* @param {string} command * @param {string} command
* @param {string[]} args
* @param {string} root * @param {string} root
* @returns {Promise<import('./_util.js').Stat>} * @returns {Promise<import('./_util.js').Stat>}
*/ */
async function benchmarkCommand(command, root) { async function benchmarkCommand(command, args, root) {
/** @type {number[]} */ /** @type {number[]} */
const durations = []; const durations = [];
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
const start = performance.now(); const start = performance.now();
await execaCommand(command, { cwd: root }); await exec(command, args, { nodeOptions: { cwd: root } });
durations.push(performance.now() - start); durations.push(performance.now() - start);
} }

View file

@ -1,4 +1,4 @@
import { execaCommand } from 'execa'; import { exec } from 'tinyexec';
import { markdownTable } from 'markdown-table'; import { markdownTable } from 'markdown-table';
import fs from 'node:fs/promises'; import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
@ -18,11 +18,13 @@ export async function run(projectDir, outputFile) {
const outputFilePath = fileURLToPath(outputFile); const outputFilePath = fileURLToPath(outputFile);
console.log('Building and benchmarking...'); console.log('Building and benchmarking...');
await execaCommand(`node --expose-gc --max_old_space_size=10000 ${astroBin} build --silent`, { await exec('node', ['--expose-gc', '--max_old_space_size=10000', astroBin, 'build'], {
cwd: root, nodeOptions: {
stdio: 'inherit', cwd: root,
env: { stdio: 'inherit',
ASTRO_TIMER_PATH: outputFilePath, env: {
ASTRO_TIMER_PATH: outputFilePath,
},
}, },
}); });

View file

@ -1,4 +1,4 @@
import { execaCommand } from 'execa'; import { exec } from 'tinyexec';
import { markdownTable } from 'markdown-table'; import { markdownTable } from 'markdown-table';
import fs from 'node:fs/promises'; import fs from 'node:fs/promises';
import http from 'node:http'; import http from 'node:http';
@ -20,15 +20,19 @@ export async function run(projectDir, outputFile) {
const root = fileURLToPath(projectDir); const root = fileURLToPath(projectDir);
console.log('Building...'); console.log('Building...');
await execaCommand(`${astroBin} build`, { await exec(astroBin, ['build'], {
cwd: root, nodeOptions: {
stdio: 'inherit', cwd: root,
stdio: 'inherit',
},
}); });
console.log('Previewing...'); console.log('Previewing...');
const previewProcess = execaCommand(`${astroBin} preview --port ${port}`, { const previewProcess = exec(astroBin, ['preview', '--port', port], {
cwd: root, nodeOptions: {
stdio: 'inherit', cwd: root,
stdio: 'inherit',
},
}); });
console.log('Waiting for server ready...'); console.log('Waiting for server ready...');

View file

@ -1,5 +1,5 @@
import autocannon from 'autocannon'; import autocannon from 'autocannon';
import { execaCommand } from 'execa'; import { exec } from 'tinyexec';
import { markdownTable } from 'markdown-table'; import { markdownTable } from 'markdown-table';
import fs from 'node:fs/promises'; import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
@ -19,9 +19,11 @@ export async function run(projectDir, outputFile) {
const root = fileURLToPath(projectDir); const root = fileURLToPath(projectDir);
console.log('Building...'); console.log('Building...');
await execaCommand(`${astroBin} build`, { await exec(astroBin, ['build'], {
cwd: root, nodeOptions: {
stdio: 'inherit', cwd: root,
stdio: 'inherit',
},
}); });
console.log('Previewing...'); console.log('Previewing...');

View file

@ -12,11 +12,11 @@
"@benchmark/timer": "workspace:*", "@benchmark/timer": "workspace:*",
"astro": "workspace:*", "astro": "workspace:*",
"autocannon": "^7.15.0", "autocannon": "^7.15.0",
"execa": "^8.0.1",
"markdown-table": "^3.0.3", "markdown-table": "^3.0.3",
"mri": "^1.2.0", "mri": "^1.2.0",
"port-authority": "^2.0.1", "port-authority": "^2.0.1",
"pretty-bytes": "^6.1.1", "pretty-bytes": "^6.1.1",
"sharp": "^0.33.3" "sharp": "^0.33.3",
"tinyexec": "^0.3.0"
} }
} }

View file

@ -151,7 +151,6 @@
"es-module-lexer": "^1.5.4", "es-module-lexer": "^1.5.4",
"esbuild": "^0.21.5", "esbuild": "^0.21.5",
"estree-walker": "^3.0.3", "estree-walker": "^3.0.3",
"execa": "^8.0.1",
"fast-glob": "^3.3.2", "fast-glob": "^3.3.2",
"flattie": "^1.1.1", "flattie": "^1.1.1",
"github-slugger": "^2.0.0", "github-slugger": "^2.0.0",
@ -176,6 +175,7 @@
"shiki": "^1.14.1", "shiki": "^1.14.1",
"string-width": "^7.2.0", "string-width": "^7.2.0",
"strip-ansi": "^7.1.0", "strip-ansi": "^7.1.0",
"tinyexec": "^0.3.0",
"tsconfck": "^3.1.1", "tsconfck": "^3.1.1",
"unist-util-visit": "^5.0.0", "unist-util-visit": "^5.0.0",
"vfile": "^6.0.3", "vfile": "^6.0.3",
@ -214,6 +214,7 @@
"astro-scripts": "workspace:*", "astro-scripts": "workspace:*",
"cheerio": "1.0.0", "cheerio": "1.0.0",
"eol": "^0.9.1", "eol": "^0.9.1",
"execa": "^8.0.1",
"expect-type": "^0.20.0", "expect-type": "^0.20.0",
"mdast-util-mdx": "^3.0.0", "mdast-util-mdx": "^3.0.0",
"mdast-util-mdx-jsx": "^3.1.3", "mdast-util-mdx-jsx": "^3.1.3",

View file

@ -3,12 +3,12 @@ import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url'; import { fileURLToPath, pathToFileURL } from 'node:url';
import boxen from 'boxen'; import boxen from 'boxen';
import { diffWords } from 'diff'; import { diffWords } from 'diff';
import { execa } from 'execa';
import { bold, cyan, dim, green, magenta, red, yellow } from 'kleur/colors'; import { bold, cyan, dim, green, magenta, red, yellow } 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 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,
@ -659,7 +659,7 @@ async function tryToInstallIntegrations({
if (await askToContinue({ flags })) { if (await askToContinue({ flags })) {
const spinner = ora('Installing dependencies...').start(); const spinner = ora('Installing dependencies...').start();
try { try {
await execa( await exec(
installCommand.pm, installCommand.pm,
[ [
installCommand.command, installCommand.command,
@ -668,10 +668,12 @@ async function tryToInstallIntegrations({
...installCommand.dependencies, ...installCommand.dependencies,
], ],
{ {
cwd, nodeOptions: {
// reset NODE_ENV to ensure install command run in dev mode cwd,
env: { NODE_ENV: undefined }, // reset NODE_ENV to ensure install command run in dev mode
}, env: { NODE_ENV: undefined },
},
}
); );
spinner.succeed(); spinner.succeed();
return UpdateResult.updated; return UpdateResult.updated;

View file

@ -1,5 +1,4 @@
import type { ExecaChildProcess } from 'execa'; import { type Result, exec } from 'tinyexec';
import { execa } from 'execa';
/** /**
* Credit: Azhar22 * Credit: Azhar22
@ -26,7 +25,7 @@ const getPlatformSpecificCommand = (): [string] | [string, string[]] => {
} }
}; };
export async function openInBrowser(url: string): Promise<ExecaChildProcess> { export async function openInBrowser(url: string): Promise<Result> {
const [command, args = []] = getPlatformSpecificCommand(); const [command, args = []] = getPlatformSpecificCommand();
return execa(command, [...args, encodeURI(url)]); return exec(command, [...args, encodeURI(url)]);
} }

View file

@ -1,11 +1,11 @@
import { createRequire } from 'node:module'; import { createRequire } from 'node:module';
import boxen from 'boxen'; import boxen from 'boxen';
import ci from 'ci-info'; import ci from 'ci-info';
import { execa } from 'execa';
import { bold, cyan, dim, magenta } from 'kleur/colors'; 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';
@ -141,10 +141,10 @@ async function installPackage(
if (Boolean(response)) { if (Boolean(response)) {
const spinner = ora('Installing dependencies...').start(); const spinner = ora('Installing dependencies...').start();
try { try {
await execa( await exec(
installCommand.pm, installCommand.pm,
[installCommand.command, ...installCommand.flags, ...installCommand.dependencies], [installCommand.command, ...installCommand.flags, ...installCommand.dependencies],
{ cwd: cwd }, { nodeOptions: { cwd: cwd } }
); );
spinner.succeed(); spinner.succeed();
@ -203,8 +203,8 @@ async function getRegistry(): Promise<string> {
const fallback = 'https://registry.npmjs.org'; const fallback = 'https://registry.npmjs.org';
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm'; const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
try { try {
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']); const { stdout } = await exec(packageManager, ['config', 'get', 'registry']);
_registry = stdout?.trim()?.replace(/\/$/, '') || fallback; _registry = stdout.trim()?.replace(/\/$/, '') || fallback;
// Detect cases where the shell command returned a non-URL (e.g. a warning) // Detect cases where the shell command returned a non-URL (e.g. a warning)
if (!new URL(_registry).host) _registry = fallback; if (!new URL(_registry).host) _registry = fallback;
} catch { } catch {

View file

@ -78,9 +78,6 @@ importers:
autocannon: autocannon:
specifier: ^7.15.0 specifier: ^7.15.0
version: 7.15.0 version: 7.15.0
execa:
specifier: ^8.0.1
version: 8.0.1
markdown-table: markdown-table:
specifier: ^3.0.3 specifier: ^3.0.3
version: 3.0.3 version: 3.0.3
@ -96,6 +93,9 @@ importers:
sharp: sharp:
specifier: ^0.33.3 specifier: ^0.33.3
version: 0.33.3 version: 0.33.3
tinyexec:
specifier: ^0.3.0
version: 0.3.0
benchmark/packages/timer: benchmark/packages/timer:
dependencies: dependencies:
@ -642,9 +642,6 @@ importers:
estree-walker: estree-walker:
specifier: ^3.0.3 specifier: ^3.0.3
version: 3.0.3 version: 3.0.3
execa:
specifier: ^8.0.1
version: 8.0.1
fast-glob: fast-glob:
specifier: ^3.3.2 specifier: ^3.3.2
version: 3.3.2 version: 3.3.2
@ -717,6 +714,9 @@ importers:
strip-ansi: strip-ansi:
specifier: ^7.1.0 specifier: ^7.1.0
version: 7.1.0 version: 7.1.0
tinyexec:
specifier: ^0.3.0
version: 0.3.0
tsconfck: tsconfck:
specifier: ^3.1.1 specifier: ^3.1.1
version: 3.1.1(typescript@5.5.4) version: 3.1.1(typescript@5.5.4)
@ -821,6 +821,9 @@ importers:
eol: eol:
specifier: ^0.9.1 specifier: ^0.9.1
version: 0.9.1 version: 0.9.1
execa:
specifier: ^8.0.1
version: 8.0.1
expect-type: expect-type:
specifier: ^0.20.0 specifier: ^0.20.0
version: 0.20.0 version: 0.20.0
@ -6007,9 +6010,6 @@ importers:
esbuild-plugin-copy: esbuild-plugin-copy:
specifier: ^2.1.1 specifier: ^2.1.1
version: 2.1.1(esbuild@0.21.5) version: 2.1.1(esbuild@0.21.5)
execa:
specifier: ^8.0.1
version: 8.0.1
fast-glob: fast-glob:
specifier: ^3.3.2 specifier: ^3.3.2
version: 3.3.2 version: 3.3.2
@ -6019,6 +6019,9 @@ importers:
p-limit: p-limit:
specifier: ^6.1.0 specifier: ^6.1.0
version: 6.1.0 version: 6.1.0
tinyexec:
specifier: ^0.3.0
version: 0.3.0
tsconfck: tsconfck:
specifier: ^3.1.1 specifier: ^3.1.1
version: 3.1.1(typescript@5.5.4) version: 3.1.1(typescript@5.5.4)
@ -11020,6 +11023,9 @@ packages:
tinybench@2.8.0: tinybench@2.8.0:
resolution: {integrity: sha512-1/eK7zUnIklz4JUUlL+658n58XO2hHLQfSk1Zf2LKieUjxidN16eKFEoDEfjHc3ohofSSqK3X5yO6VGb6iW8Lw==} resolution: {integrity: sha512-1/eK7zUnIklz4JUUlL+658n58XO2hHLQfSk1Zf2LKieUjxidN16eKFEoDEfjHc3ohofSSqK3X5yO6VGb6iW8Lw==}
tinyexec@0.3.0:
resolution: {integrity: sha512-tVGE0mVJPGb0chKhqmsoosjsS+qUnJVGJpZgsHYQcGoPlG3B51R3PouqTgEGH2Dc9jjFyOqOpix6ZHNMXp1FZg==}
tinypool@1.0.0: tinypool@1.0.0:
resolution: {integrity: sha512-KIKExllK7jp3uvrNtvRBYBWBOAXSX8ZvoaD8T+7KB/QHIuoJW3Pmr60zucywjAlMb5TeXUkcs/MWeWLu0qvuAQ==} resolution: {integrity: sha512-KIKExllK7jp3uvrNtvRBYBWBOAXSX8ZvoaD8T+7KB/QHIuoJW3Pmr60zucywjAlMb5TeXUkcs/MWeWLu0qvuAQ==}
engines: {node: ^18.0.0 || >=20.0.0} engines: {node: ^18.0.0 || >=20.0.0}
@ -17560,6 +17566,8 @@ snapshots:
tinybench@2.8.0: {} tinybench@2.8.0: {}
tinyexec@0.3.0: {}
tinypool@1.0.0: {} tinypool@1.0.0: {}
tinyrainbow@1.2.0: {} tinyrainbow@1.2.0: {}

View file

@ -10,10 +10,10 @@
"dependencies": { "dependencies": {
"esbuild": "^0.21.5", "esbuild": "^0.21.5",
"esbuild-plugin-copy": "^2.1.1", "esbuild-plugin-copy": "^2.1.1",
"execa": "^8.0.1",
"fast-glob": "^3.3.2", "fast-glob": "^3.3.2",
"kleur": "^4.1.5", "kleur": "^4.1.5",
"p-limit": "^6.1.0", "p-limit": "^6.1.0",
"tinyexec": "^0.3.0",
"tsconfck": "^3.1.1" "tsconfck": "^3.1.1"
} }
} }

View file

@ -2,7 +2,7 @@
// @ts-check // @ts-check
import { execa } from 'execa'; import { exec } from 'tinyexec';
import { promises as fs } from 'node:fs'; import { promises as fs } from 'node:fs';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
@ -36,7 +36,9 @@ async function run() {
console.log('🤖', 'Resetting', 'pnpm'); console.log('🤖', 'Resetting', 'pnpm');
await execa('pnpm', ['install'], { cwd: fileURLToPath(rootDir), stdout: 'inherit', stderr: 'inherit' }); await exec('pnpm', ['install'], {
nodeOptions: { cwd: fileURLToPath(rootDir), stdio: ['pipe', 'inherit', 'inherit'] },
});
} }
/* Functionality /* Functionality

View file

@ -2,7 +2,7 @@
// @ts-check // @ts-check
import { execa } from 'execa'; import { exec } from 'tinyexec';
import { promises as fs } from 'node:fs'; import { promises as fs } from 'node:fs';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
@ -32,10 +32,12 @@ async function run() {
console.log(''); console.log('');
const directories = [...(await getChildDirectories(smokeDir)), ...(await getChildDirectories(exampleDir))]; const directories = [...(await getChildDirectories(smokeDir)), ...(await getChildDirectories(exampleDir))];
/** @type {Partial<import('tinyexec').Options>} */
const execOptions = { nodeOptions: { cwd: fileURLToPath(rootDir), stdio: 'inherit' }};
console.log('🤖', 'Preparing', 'pnpm'); console.log('🤖', 'Preparing', 'pnpm');
await execa('pnpm', ['install', '--frozen-lockfile=false'], { cwd: fileURLToPath(rootDir), stdio: 'inherit' }); await exec('pnpm', ['install', '--frozen-lockfile=false'], execOptions);
for (const directory of directories) { for (const directory of directories) {
const name = directory.pathname.split('/').at(-1) ?? ""; const name = directory.pathname.split('/').at(-1) ?? "";
@ -43,9 +45,9 @@ async function run() {
console.log('🤖', 'Testing', name); console.log('🤖', 'Testing', name);
try { try {
await execa('pnpm', ['install', '--ignore-scripts', '--frozen-lockfile=false'].filter(x => x), { cwd: fileURLToPath(directory), stdio: 'inherit' }); await exec('pnpm', ['install', '--ignore-scripts', '--frozen-lockfile=false'], execOptions);
await execa('pnpm', ['astro', 'telemetry', 'disable']); await exec('pnpm', ['astro', 'telemetry', 'disable']);
await execa('pnpm', ['run', 'build'], { cwd: fileURLToPath(directory), stdio: 'inherit' }); await exec('pnpm', ['run', 'build'], execOptions);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
process.exit(1); process.exit(1);