Skip to content

Commit

Permalink
Use tinyexec (#11845)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Aug 28, 2024
1 parent 6272e6c commit 440a4be
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-beans-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Replaces `execa` with `tinyexec` internally
13 changes: 7 additions & 6 deletions benchmark/bench/cli-startup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fileURLToPath } from 'node:url';
import { execaCommand } from 'execa';
import { exec } from 'tinyexec';
import { markdownTable } from 'markdown-table';
import { astroBin, calculateStat } from './_util.js';

Expand All @@ -14,11 +14,11 @@ export async function run(projectDir, outputFile) {
const root = fileURLToPath(projectDir);

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('Benchmarking `astro info`...');
const infoStat = await benchmarkCommand(`node ${astroBin} info`, root);
console.log('Benchmarking `astro preferences list`...');
const infoStat = await benchmarkCommand('node', [astroBin, 'preferences', 'list'], root);
console.log('Done');

console.log('Result preview:');
Expand All @@ -35,16 +35,17 @@ export async function run(projectDir, outputFile) {

/**
* @param {string} command
* @param {string[]} args
* @param {string} root
* @returns {Promise<import('./_util.js').Stat>}
*/
async function benchmarkCommand(command, root) {
async function benchmarkCommand(command, args, root) {
/** @type {number[]} */
const durations = [];

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

Expand Down
14 changes: 8 additions & 6 deletions benchmark/bench/memory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execaCommand } from 'execa';
import { exec } from 'tinyexec';
import { markdownTable } from 'markdown-table';
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
Expand All @@ -18,11 +18,13 @@ export async function run(projectDir, outputFile) {
const outputFilePath = fileURLToPath(outputFile);

console.log('Building and benchmarking...');
await execaCommand(`node --expose-gc --max_old_space_size=10000 ${astroBin} build --silent`, {
cwd: root,
stdio: 'inherit',
env: {
ASTRO_TIMER_PATH: outputFilePath,
await exec('node', ['--expose-gc', '--max_old_space_size=10000', astroBin, 'build'], {
nodeOptions: {
cwd: root,
stdio: 'inherit',
env: {
ASTRO_TIMER_PATH: outputFilePath,
},
},
});

Expand Down
18 changes: 11 additions & 7 deletions benchmark/bench/render.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execaCommand } from 'execa';
import { exec } from 'tinyexec';
import { markdownTable } from 'markdown-table';
import fs from 'node:fs/promises';
import http from 'node:http';
Expand All @@ -20,15 +20,19 @@ export async function run(projectDir, outputFile) {
const root = fileURLToPath(projectDir);

console.log('Building...');
await execaCommand(`${astroBin} build`, {
cwd: root,
stdio: 'inherit',
await exec(astroBin, ['build'], {
nodeOptions: {
cwd: root,
stdio: 'inherit',
},
});

console.log('Previewing...');
const previewProcess = execaCommand(`${astroBin} preview --port ${port}`, {
cwd: root,
stdio: 'inherit',
const previewProcess = exec(astroBin, ['preview', '--port', port], {
nodeOptions: {
cwd: root,
stdio: 'inherit',
},
});

console.log('Waiting for server ready...');
Expand Down
10 changes: 6 additions & 4 deletions benchmark/bench/server-stress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import autocannon from 'autocannon';
import { execaCommand } from 'execa';
import { exec } from 'tinyexec';
import { markdownTable } from 'markdown-table';
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
Expand All @@ -19,9 +19,11 @@ export async function run(projectDir, outputFile) {
const root = fileURLToPath(projectDir);

console.log('Building...');
await execaCommand(`${astroBin} build`, {
cwd: root,
stdio: 'inherit',
await exec(astroBin, ['build'], {
nodeOptions: {
cwd: root,
stdio: 'inherit',
},
});

console.log('Previewing...');
Expand Down
4 changes: 2 additions & 2 deletions benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"@benchmark/timer": "workspace:*",
"astro": "workspace:*",
"autocannon": "^7.15.0",
"execa": "^8.0.1",
"markdown-table": "^3.0.3",
"mri": "^1.2.0",
"port-authority": "^2.0.1",
"pretty-bytes": "^6.1.1",
"sharp": "^0.33.3"
"sharp": "^0.33.3",
"tinyexec": "^0.3.0"
}
}
3 changes: 2 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
"es-module-lexer": "^1.5.4",
"esbuild": "^0.21.5",
"estree-walker": "^3.0.3",
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"flattie": "^1.1.1",
"github-slugger": "^2.0.0",
Expand All @@ -176,6 +175,7 @@
"shiki": "^1.14.1",
"string-width": "^7.2.0",
"strip-ansi": "^7.1.0",
"tinyexec": "^0.3.0",
"tsconfck": "^3.1.1",
"unist-util-visit": "^5.0.0",
"vfile": "^6.0.3",
Expand Down Expand Up @@ -214,6 +214,7 @@
"astro-scripts": "workspace:*",
"cheerio": "1.0.0",
"eol": "^0.9.1",
"execa": "^8.0.1",
"expect-type": "^0.20.0",
"mdast-util-mdx": "^3.0.0",
"mdast-util-mdx-jsx": "^3.1.3",
Expand Down
14 changes: 8 additions & 6 deletions packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import boxen from 'boxen';
import { diffWords } from 'diff';
import { execa } from 'execa';
import { bold, cyan, dim, green, magenta, red, yellow } from 'kleur/colors';
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 Down Expand Up @@ -659,7 +659,7 @@ async function tryToInstallIntegrations({
if (await askToContinue({ flags })) {
const spinner = ora('Installing dependencies...').start();
try {
await execa(
await exec(
installCommand.pm,
[
installCommand.command,
Expand All @@ -668,10 +668,12 @@ async function tryToInstallIntegrations({
...installCommand.dependencies,
],
{
cwd,
// reset NODE_ENV to ensure install command run in dev mode
env: { NODE_ENV: undefined },
},
nodeOptions: {
cwd,
// reset NODE_ENV to ensure install command run in dev mode
env: { NODE_ENV: undefined },
},
}
);
spinner.succeed();
return UpdateResult.updated;
Expand Down
7 changes: 3 additions & 4 deletions packages/astro/src/cli/docs/open.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ExecaChildProcess } from 'execa';
import { execa } from 'execa';
import { type Result, exec } from 'tinyexec';

/**
* Credit: Azhar22
Expand All @@ -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();
return execa(command, [...args, encodeURI(url)]);
return exec(command, [...args, encodeURI(url)]);
}
10 changes: 5 additions & 5 deletions packages/astro/src/cli/install-package.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createRequire } from 'node:module';
import boxen from 'boxen';
import ci from 'ci-info';
import { execa } from 'execa';
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';

Expand Down Expand Up @@ -141,10 +141,10 @@ async function installPackage(
if (Boolean(response)) {
const spinner = ora('Installing dependencies...').start();
try {
await execa(
await exec(
installCommand.pm,
[installCommand.command, ...installCommand.flags, ...installCommand.dependencies],
{ cwd: cwd },
{ nodeOptions: { cwd: cwd } }
);
spinner.succeed();

Expand Down Expand Up @@ -203,8 +203,8 @@ async function getRegistry(): Promise<string> {
const fallback = 'https://registry.npmjs.org';
const packageManager = (await preferredPM(process.cwd()))?.name || 'npm';
try {
const { stdout } = await execa(packageManager, ['config', 'get', 'registry']);
_registry = stdout?.trim()?.replace(/\/$/, '') || fallback;
const { stdout } = await exec(packageManager, ['config', 'get', 'registry']);
_registry = stdout.trim()?.replace(/\/$/, '') || fallback;
// Detect cases where the shell command returned a non-URL (e.g. a warning)
if (!new URL(_registry).host) _registry = fallback;
} catch {
Expand Down
26 changes: 17 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"dependencies": {
"esbuild": "^0.21.5",
"esbuild-plugin-copy": "^2.1.1",
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"kleur": "^4.1.5",
"p-limit": "^6.1.0",
"tinyexec": "^0.3.0",
"tsconfck": "^3.1.1"
}
}
6 changes: 4 additions & 2 deletions scripts/smoke/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// @ts-check

import { execa } from 'execa';
import { exec } from 'tinyexec';
import { promises as fs } from 'node:fs';
import { fileURLToPath } from 'node:url';

Expand Down Expand Up @@ -36,7 +36,9 @@ async function run() {

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
Expand Down
Loading

0 comments on commit 440a4be

Please sign in to comment.