Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dependencies-replacement] Replace spawndamnit with tinyexec (8 fewer dependencies) #224

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-hornets-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@manypkg/cli": minor
---

Replace `spawndamnit` with `tinyexec` for fewer dependencies
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
12 changes: 7 additions & 5 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to treat a nullish exitCode as a failure here?

Copy link
Contributor Author

@VanTanev VanTanev Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per node:child_proccess.spawn(), exitCode can be nullish in 2 cases:

  • The process failed to spawn.
  • The process was externally killed.

For our purpose, both of these mean that the underlying process never executed successfully, so we should treat a missing exit code as an error.

});
})
);
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/npm-tag.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -30,7 +30,7 @@ async function tagApackage(
flags.push("--otp", otpCode);
}

return await spawn(
return await exec(
"npm",
[
"dist-tag",
Expand All @@ -40,8 +40,10 @@ async function tagApackage(
...flags,
],
{
stdio: "inherit",
env: Object.assign({}, process.env, envOverride),
nodeOptions: {
stdio: "inherit",
env: envOverride,
},
}
);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/run.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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");
}
Expand Down
23 changes: 13 additions & 10 deletions packages/cli/src/run.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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) => {
Expand All @@ -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);
}
}
6 changes: 3 additions & 3 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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" } }
);
}
10 changes: 0 additions & 10 deletions types/spawndamnit.d.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

This file was deleted.

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading