From d9d1649ec382a2984edd17bad19b004d12d01046 Mon Sep 17 00:00:00 2001 From: Ivan Tanev Date: Wed, 16 Oct 2024 11:51:07 +0300 Subject: [PATCH] Replace spawndamnit with tinyexec --- .changeset/spicy-hornets-beam.md | 5 +++++ packages/cli/package.json | 2 +- packages/cli/src/index.ts | 12 +++++++----- packages/cli/src/npm-tag.ts | 10 ++++++---- packages/cli/src/run.test.ts | 10 +++++----- packages/cli/src/run.ts | 23 +++++++++++++---------- packages/cli/src/utils.ts | 6 +++--- types/spawndamnit.d.ts | 10 ---------- yarn.lock | 5 +++++ 9 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 .changeset/spicy-hornets-beam.md delete mode 100644 types/spawndamnit.d.ts diff --git a/.changeset/spicy-hornets-beam.md b/.changeset/spicy-hornets-beam.md new file mode 100644 index 0000000..32fbcb6 --- /dev/null +++ b/.changeset/spicy-hornets-beam.md @@ -0,0 +1,5 @@ +--- +"@manypkg/cli": minor +--- + +Replace `spawndamnit` with `tinyexec` for fewer dependencies diff --git a/packages/cli/package.json b/packages/cli/package.json index 68b449b..98b708a 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -22,7 +22,7 @@ "picocolors": "^1.1.0", "sembear": "^0.5.0", "semver": "^6.3.0", - "spawndamnit": "^2.0.0", + "tinyexec": "^0.3.1", "validate-npm-package-name": "^5.0.1" }, "devDependencies": { diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 8d87cfe..ffc73b4 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -8,7 +8,7 @@ import { writePackage, install } from "./utils"; import { runCmd } from "./run"; import { upgradeDependency } from "./upgrade"; import { npmTagAll } from "./npm-tag"; -import spawn from "spawndamnit"; +import { exec } from "tinyexec"; import pLimit from "p-limit"; type RootPackage = Package & { @@ -93,11 +93,13 @@ async function execCmd(args: string[]) { await Promise.all( packages.map((pkg) => { return execLimit(async () => { - let { code } = await spawn(args[0], args.slice(1), { - cwd: pkg.dir, - stdio: "inherit", + const { exitCode } = await exec(args[0], args.slice(1), { + nodeOptions: { + cwd: pkg.dir, + stdio: "inherit", + }, }); - highestExitCode = Math.max(code, highestExitCode); + highestExitCode = Math.max(exitCode ?? 1, highestExitCode); }); }) ); diff --git a/packages/cli/src/npm-tag.ts b/packages/cli/src/npm-tag.ts index 3936649..84fb383 100644 --- a/packages/cli/src/npm-tag.ts +++ b/packages/cli/src/npm-tag.ts @@ -1,6 +1,6 @@ import { getPackages } from "@manypkg/get-packages"; import type { PackageJSON } from "@changesets/types"; -import spawn from "spawndamnit"; +import { exec } from "tinyexec"; import pLimit from "p-limit"; let npmLimit = pLimit(40); @@ -30,7 +30,7 @@ async function tagApackage( flags.push("--otp", otpCode); } - return await spawn( + return await exec( "npm", [ "dist-tag", @@ -40,8 +40,10 @@ async function tagApackage( ...flags, ], { - stdio: "inherit", - env: Object.assign({}, process.env, envOverride), + nodeOptions: { + stdio: "inherit", + env: envOverride, + }, } ); } diff --git a/packages/cli/src/run.test.ts b/packages/cli/src/run.test.ts index adf0870..d0f4778 100644 --- a/packages/cli/src/run.test.ts +++ b/packages/cli/src/run.test.ts @@ -1,6 +1,6 @@ import { runCmd } from "./run"; import fixturez from "fixturez"; -import spawn from "spawndamnit"; +import { exec } from "tinyexec"; import stripAnsi from "strip-ansi"; const f = fixturez(__dirname); @@ -22,16 +22,16 @@ describe("Run command", () => { // @ts-ignore ])( 'should execute "%s %s" and exit with %i', - async (arg0, arg1, exitCode) => { - const { stdout, stderr, code } = await spawn( + async (arg0, arg1, expectedExitCode) => { + const { exitCode, stdout, stderr } = await exec( require.resolve("../bin"), // @ts-ignore ["run", arg0, arg1], { - cwd: f.find("basic-with-scripts"), + nodeOptions: { cwd: f.find("basic-with-scripts") }, } ); - expect(code).toBe(exitCode); + expect(exitCode).toBe(expectedExitCode); expect(stripAnsi(stdout.toString())).toMatchSnapshot("stdout"); expect(stripAnsi(stderr.toString())).toMatchSnapshot("stderr"); } diff --git a/packages/cli/src/run.ts b/packages/cli/src/run.ts index bb56932..6584715 100644 --- a/packages/cli/src/run.ts +++ b/packages/cli/src/run.ts @@ -1,6 +1,5 @@ import { getPackages } from "@manypkg/get-packages"; -import path from "path"; -import spawn from "spawndamnit"; +import { exec } from "tinyexec"; import * as logger from "./logger"; import { ExitError } from "./errors"; @@ -12,11 +11,13 @@ export async function runCmd(args: string[], cwd: string) { }); if (exactMatchingPackage) { - const { code } = await spawn("yarn", args.slice(1), { - cwd: exactMatchingPackage.dir, - stdio: "inherit", + const { exitCode } = await exec("yarn", args.slice(1), { + nodeOptions: { + cwd: exactMatchingPackage.dir, + stdio: "inherit", + }, }); - throw new ExitError(code); + throw new ExitError(exitCode ?? 1); } const matchingPackages = packages.filter((pkg) => { @@ -39,10 +40,12 @@ export async function runCmd(args: string[], cwd: string) { logger.error("No matching packages found"); throw new ExitError(1); } else { - const { code } = await spawn("yarn", args.slice(1), { - cwd: matchingPackages[0].dir, - stdio: "inherit", + const { exitCode } = await exec("yarn", args.slice(1), { + nodeOptions: { + cwd: matchingPackages[0].dir, + stdio: "inherit", + }, }); - throw new ExitError(code); + throw new ExitError(exitCode ?? 1); } } diff --git a/packages/cli/src/utils.ts b/packages/cli/src/utils.ts index d136d8b..1cc9b7e 100644 --- a/packages/cli/src/utils.ts +++ b/packages/cli/src/utils.ts @@ -1,7 +1,7 @@ import fs from "node:fs/promises"; import { Package, Tool } from "@manypkg/get-packages"; import path from "path"; -import spawn from "spawndamnit"; +import { exec } from "tinyexec"; import detectIndent from "detect-indent"; export async function writePackage(pkg: Package) { @@ -24,13 +24,13 @@ export async function install(toolType: string, cwd: string) { yarn: "yarn", }; - await spawn( + await exec( cliRunners[toolType], toolType === "pnpm" ? ["install"] : toolType === "lerna" ? ["bootstrap", "--since", "HEAD"] : [], - { cwd, stdio: "inherit" } + { nodeOptions: { cwd, stdio: "inherit" } } ); } diff --git a/types/spawndamnit.d.ts b/types/spawndamnit.d.ts deleted file mode 100644 index 27ba0f2..0000000 --- a/types/spawndamnit.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -declare module "spawndamnit" { - import { SpawnOptions } from "child_process"; - import { EventEmitter } from "events"; - - export default function spawn( - cmd: string, - args: Array, - opts?: SpawnOptions - ): Promise<{ stdout: string; code: number; stderr: string }> & EventEmitter; -} diff --git a/yarn.lock b/yarn.lock index ea26824..4b1f06b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14199,6 +14199,11 @@ timsort@^0.3.0: resolved "https://registry.yarnpkg.com/timsort/-/timsort-0.3.0.tgz#405411a8e7e6339fe64db9a234de11dc31e02bd4" integrity sha1-QFQRqOfmM5/mTbmiNN4R3DHgK9Q= +tinyexec@^0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/tinyexec/-/tinyexec-0.3.1.tgz#0ab0daf93b43e2c211212396bdb836b468c97c98" + integrity sha512-WiCJLEECkO18gwqIp6+hJg0//p23HXp4S+gGtAKu3mI2F2/sXC4FvHvXvB0zJVVaTPhx1/tOwdbRsa1sOBIKqQ== + title-case@^2.1.0: version "2.1.1" resolved "https://registry.yarnpkg.com/title-case/-/title-case-2.1.1.tgz#3e127216da58d2bc5becf137ab91dae3a7cd8faa"