Skip to content
Open
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
30 changes: 27 additions & 3 deletions packages/app/server/routes/publish.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default eventHandler(async (event) => {
"sb-comment": commentHeader,
"sb-compact": compactHeader,
"sb-bin": binHeader,
"sb-bin-packages": binPackagesHeader,
"sb-package-manager": packageManagerHeader,
"sb-only-templates": onlyTemplatesHeader,
"sb-comment-with-sha": commentWithShaHeader,
Expand All @@ -32,7 +33,10 @@ export default eventHandler(async (event) => {
const compact = compactHeader === "true";
const onlyTemplates = onlyTemplatesHeader === "true";
const comment: Comment = (commentHeader ?? "update") as Comment;
const bin = binHeader === "true";
const isBinPackage = parseBinPackagesHeader(
binPackagesHeader,
binHeader === "true",
);
const packageManager: PackageManager =
(packageManagerHeader as PackageManager) || "npm";
const commentWithSha = commentWithShaHeader === "true";
Expand Down Expand Up @@ -271,7 +275,7 @@ export default eventHandler(async (event) => {
workflowData,
compact,
packageManager,
bin,
isBinPackage,
commentWithDev,
),
},
Expand Down Expand Up @@ -328,7 +332,7 @@ export default eventHandler(async (event) => {
checkRunUrl,
packageManager,
commentWithSha || comment !== "update" ? "sha" : "ref",
bin,
isBinPackage,
commentWithDev,
);

Expand Down Expand Up @@ -408,6 +412,26 @@ export default eventHandler(async (event) => {
}
});

function parseBinPackagesHeader(
header: string | undefined,
legacyBinAll: boolean,
): (packageName: string) => boolean {
if (header === undefined) {
return legacyBinAll ? () => true : () => false;
}
let parsed: unknown;
try {
parsed = JSON.parse(header);
} catch {
return () => false;
}
if (!Array.isArray(parsed)) {
return () => false;
}
const set = new Set(parsed.filter((v): v is string => typeof v === "string"));
return (name) => set.has(name);
}

async function getPullRequestState(
installation: Awaited<ReturnType<typeof useOctokitInstallation>>,
workflowData: WorkflowData,
Expand Down
10 changes: 6 additions & 4 deletions packages/app/server/utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function generateCommitPublishMessage(
workflowData: WorkflowData,
compact: boolean,
packageManager: PackageManager,
bin: boolean,
isBin: (packageName: string) => boolean,
commentWithDev: boolean,
) {
const isMoreThanFour = isMoreThanFourPackages(packages, packageManager);
Expand All @@ -40,6 +40,7 @@ export function generateCommitPublishMessage(
workflowData,
compact,
);
const packageIsBin = isBin(packageName);
return packageManager
.split(",")
.map((pm) => {
Expand All @@ -51,7 +52,7 @@ export function generateCommitPublishMessage(

return `
\`\`\`
${bin ? binCommands[pm as PackageManager] : installCommands[pm as PackageManager]} ${descriptor}
${packageIsBin ? binCommands[pm as PackageManager] : installCommands[pm as PackageManager]} ${descriptor}
\`\`\`
`;
})
Expand Down Expand Up @@ -83,7 +84,7 @@ export function generatePullRequestPublishMessage(
checkRunUrl: string,
packageManager: PackageManager,
base: "sha" | "ref",
bin: boolean,
isBin: (packageName: string) => boolean,
commentWithDev: boolean,
) {
const isMoreThanFour = isMoreThanFourPackages(packages, packageManager);
Expand All @@ -96,6 +97,7 @@ export function generatePullRequestPublishMessage(
workflowData,
compact,
);
const packageIsBin = isBin(packageName);
return packageManager
.split(",")
.map((pm) => {
Expand All @@ -105,7 +107,7 @@ export function generatePullRequestPublishMessage(

return `
\`\`\`
${bin ? binCommands[pm as PackageManager] : installCommands[pm as PackageManager]} ${refUrl + (commentWithDev ? " -D" : "")}
${packageIsBin ? binCommands[pm as PackageManager] : installCommands[pm as PackageManager]} ${refUrl + (commentWithDev ? " -D" : "")}
\`\`\`
`;
})
Expand Down
32 changes: 30 additions & 2 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,17 @@ const main = defineCommand({

const isPeerDepsEnabled = !!args.peerDeps;
const isOnlyTemplates = !!args["only-templates"];
const isBinaryApplication = !!args.bin;
const binArg = args.bin as boolean | string | undefined;
const binAllPackages =
binArg === true ||
(typeof binArg === "string" && binArg.toLowerCase() === "true");
const binExplicitNames =
typeof binArg === "string" && !binAllPackages
? binArg
.split(",")
.map((s) => s.trim())
.filter(Boolean)
: [];
const isCommentWithSha = !!args.commentWithSha;
const isCommentWithDev = !!args.commentWithDev;
const comment: Comment = args.comment as Comment;
Expand Down Expand Up @@ -397,6 +407,23 @@ const main = defineCommand({
}
}

const knownPackageNames = new Set(
packageInfos.map((info) => info.packageName),
);
const binPackages = new Set<string>(
binAllPackages
? knownPackageNames
: binExplicitNames.filter((name) => {
if (knownPackageNames.has(name)) {
return true;
}
console.warn(
`--bin: '${name}' does not match any package being published (known: ${[...knownPackageNames].join(", ") || "(none)"}). Ignoring.`,
);
return false;
}),
);

if (isCompact) {
for (const { packageName } of packageInfos) {
try {
Expand Down Expand Up @@ -737,7 +764,8 @@ const main = defineCommand({
"sb-key": key,
"sb-shasums": JSON.stringify(shasums),
"sb-run-id": GITHUB_RUN_ID,
"sb-bin": `${isBinaryApplication}`,
"sb-bin": `${binPackages.size > 0 && binPackages.size === knownPackageNames.size}`,
"sb-bin-packages": JSON.stringify([...binPackages]),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we don't need the [...] here, we can just pass binPackages directly!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How? this is a set, and when we JSON.stringify it without the making it an array by [...], it sees an object with no serializable properties

https://github.com/stackblitz-labs/pkg.pr.new/pull/524/changes#diff-ce752ce36be970cbc0110dae521e82462b9ee7dbca55f45b51648c82998da18dR413

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

"sb-package-manager": selectedPackageManager.join(","),
"sb-only-templates": `${isOnlyTemplates}`,
"sb-comment-with-sha": `${isCommentWithSha}`,
Expand Down
Loading