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

feat: yarn pack #329

Merged
merged 17 commits into from
Feb 21, 2025
1 change: 0 additions & 1 deletion .eslintcache

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ To customize which package manager is reflected in the comments, use the `--pack

For repositories with many packages, comments might get too long. In that case, you can use `--only-templates` to only show templates.

pkg.pr.new uses `npm pack --json` under the hood, in case you face issues, you can also use the `--pnpm` flag so it starts using `pnpm pack`. This is not necessary in most cases.
pkg.pr.new uses `npm pack --json` under the hood, in case you face issues, you can also use the `--pnpm` or `--yarn` flag so it starts using `pnpm pack` or `yarn pack`. This is not necessary in most cases.

<img width="100%" src="https://github.com/stackblitz-labs/pkg.pr.new/assets/37929992/2fc03b94-ebae-4c47-a271-03a4ad5d2449" />

Expand Down
29 changes: 23 additions & 6 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ const main = defineCommand({
const formData = new FormData();

const isCompact = !!args.compact;
const isPnpm = !!args.pnpm;
let packMethod: PackMethod = "npm";

if (args.pnpm) {
packMethod = "pnpm";
} else if (args.yarn) {
packMethod = "yarn";
}

const isPeerDepsEnabled = !!args.peerDeps;
const isOnlyTemplates = !!args["only-templates"];

Expand Down Expand Up @@ -363,8 +370,9 @@ const main = defineCommand({
}

const { filename, shasum } = await resolveTarball(
isPnpm ? "pnpm" : "npm",
packMethod,
p,
pJson,
);

shasums[pJson.name] = shasum;
Expand Down Expand Up @@ -536,14 +544,23 @@ runMain(main)
.then(() => process.exit(0))
.catch(() => process.exit(1));

// TODO: we'll add support for yarn if users hit issues with npm
async function resolveTarball(pm: "npm" | "pnpm", p: string) {
const { stdout } = await ezSpawn.async(`${pm} pack`, {
type PackMethod = "npm" | "pnpm" | "yarn";

async function resolveTarball(pm: PackMethod, p: string, pJson: PackageJson) {
let cmd = `${pm} pack`;
let filename = `${pJson.name!.replace("/", "-")}-${pJson.version}.tgz`;
if (pm === "yarn") {
cmd += ` --filename ${filename}`;
}
const { stdout } = await ezSpawn.async(cmd, {
stdio: "overlapped",
cwd: p,
});
const lines = stdout.split("\n").filter(Boolean);
const filename = lines[lines.length - 1].trim();

if (pm !== "yarn") {
filename = lines[lines.length - 1].trim();
}

const shasum = createHash("sha1")
.update(await fs.readFile(path.resolve(p, filename)))
Expand Down