forked from stackblitz-labs/pkg.pr.new
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
724 lines (635 loc) · 22.5 KB
/
index.ts
File metadata and controls
724 lines (635 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/* eslint-disable unicorn/no-process-exit */
import assert from "node:assert";
import path from "node:path";
import { createHash } from "node:crypto";
import fsSync from "node:fs";
import fs from "node:fs/promises";
import { hash } from "ohash";
import ezSpawn from "@jsdevtools/ez-spawn";
import { defineCommand, runMain } from "citty";
import { getPackageManifest, type PackageManifest } from "query-registry";
import type { Comment } from "@pkg-pr-new/utils";
import {
abbreviateCommitHash,
extractOwnerAndRepo,
extractRepository,
} from "@pkg-pr-new/utils";
import { glob } from "tinyglobby";
import ignore from "ignore";
import "./environments";
import { isBinaryFile } from "isbinaryfile";
import { writePackageJSON, type PackageJson } from "pkg-types";
import pkg from "./package.json" with { type: "json" };
import { createDefaultTemplate } from "./template";
import * as core from "@actions/core";
declare global {
const API_URL: string;
}
type OutputMetadata = {
packages: {
name: string;
url: string;
shasum: string;
}[];
templates: {
name: string;
url: string;
}[];
};
const apiUrl = process.env.API_URL ?? API_URL;
const publishUrl = new URL("/publish", apiUrl);
const createMultipart = new URL("/multipart/create", apiUrl);
const uploadMultipart = new URL("/multipart/upload", apiUrl);
const completeMultipart = new URL("/multipart/complete", apiUrl);
const main = defineCommand({
meta: {
version: pkg.version,
name: "stackblitz",
description: "A CLI for pkg.pr.new (Continuous Releases)",
},
subCommands: {
publish: () => {
return {
args: {
compact: {
type: "boolean",
description:
"compact urls. The shortest form of urls like pkg.pr.new/tinybench@a832a55)",
},
peerDeps: {
type: "boolean",
description:
"handle peerDependencies by setting the workspace version instead of what has been set in the peerDeps itself. --peerDeps not being true would leave peerDependencies to the package manager itself (npm, pnpm)",
default: false,
},
pnpm: {
type: "boolean",
description: "use `pnpm pack` instead of `npm pack --json`",
},
yarn: {
type: "boolean",
description: "use `yarn pack` instead of `npm pack --json`",
},
bun: {
type: "boolean",
description: "use `bun pm pack` instead of `npm pack --json`",
},
template: {
type: "string",
description:
"generate stackblitz templates out of directories in the current repo with the new built packages",
},
comment: {
type: "string", // "off", "create", "update" (default)
description: `"off" for no comments (silent mode). "create" for comment on each publish. "update" for one comment across the pull request with edits on each publish (default)`,
default: "update",
},
"only-templates": {
type: "boolean",
description: `generate only stackblitz templates`,
default: false,
},
json: {
type: "mixed",
description: `Save metadata to a JSON file. If true, log the output for piping. If a string, save the output to the specified file path.`,
},
packageManager: {
type: "string",
description:
"Specify the package manager to use (npm, bun, pnpm, yarn)",
enum: ["npm", "bun", "pnpm", "yarn"],
default: "npm",
},
bin: {
type: "boolean",
description:
"Set to true if your package is a binary application and you would like to show an execute command instead of an install command.",
},
},
run: async ({ args }) => {
const paths =
args._.length > 0
? await glob(args._, {
expandDirectories: false,
onlyDirectories: true,
absolute: true,
})
: [process.cwd()];
const templates = await glob(args.template || [], {
expandDirectories: false,
onlyDirectories: true,
absolute: true,
});
const formData = new FormData();
const isCompact = !!args.compact;
let packMethod: PackMethod = "npm";
if (args.pnpm) {
packMethod = "pnpm";
} else if (args.yarn) {
packMethod = "yarn";
} else if (args.bun) {
packMethod = "bun";
}
const isPeerDepsEnabled = !!args.peerDeps;
const isOnlyTemplates = !!args["only-templates"];
const isBinaryApplication = !!args.bin;
const comment: Comment = args.comment as Comment;
const selectedPackageManager = (args.packageManager as string)
.split(",")
.filter((s) => s.trim()) as Array<"npm" | "bun" | "pnpm" | "yarn">;
const packageManagers = ["npm", "bun", "pnpm", "yarn"];
if (!selectedPackageManager.length) {
console.error(
`Unsupported package manager: ${args.packageManager}. Supported managers are npm, bun, pnpm, yarn.`,
);
process.exit(1);
}
for (let i = 0; i < packageManagers.length; i++) {
if (!packageManagers.includes(packageManagers[i])) {
console.error(
`Unsupported package manager: ${packageManagers[i]}. Supported managers are npm, bun, pnpm, yarn.`,
);
process.exit(1);
}
}
if (!process.env.TEST && process.env.GITHUB_ACTIONS !== "true") {
console.error(
"Continuous Releases are only available in GitHub Actions.",
);
process.exit(1);
}
const {
GITHUB_REPOSITORY,
GITHUB_RUN_ID,
GITHUB_RUN_ATTEMPT,
GITHUB_ACTOR_ID,
GITHUB_OUTPUT,
} = process.env;
const [owner, repo] = GITHUB_REPOSITORY.split("/");
const metadata = {
owner,
repo,
run: Number(GITHUB_RUN_ID),
attempt: Number(GITHUB_RUN_ATTEMPT),
actor: Number(GITHUB_ACTOR_ID),
};
const key = hash(metadata);
const checkResponse = await fetch(new URL("/check", apiUrl), {
method: "POST",
body: JSON.stringify({
owner,
repo,
key,
}),
});
if (!checkResponse.ok) {
console.error(await checkResponse.text());
process.exit(1);
}
const { sha } = await checkResponse.json();
const formattedSha = isCompact ? abbreviateCommitHash(sha) : sha;
const deps: Map<string, string> = new Map(); // pkg.pr.new versions of the package
const realDeps: Map<string, string> | null = isPeerDepsEnabled
? new Map()
: null; // real versions of the package, useful for peerDependencies
const printJson = typeof args.json === "boolean";
const saveJson = typeof args.json === "string";
const jsonFilePath = saveJson ? args.json : "";
const outputMetadata: OutputMetadata = {
packages: [],
templates: [],
};
for (const p of paths) {
const pJsonPath = path.resolve(p, "package.json");
const pJson = await readPackageJson(pJsonPath);
if (!pJson) {
continue;
}
if (!pJson.name) {
throw new Error(`"name" field in ${pJsonPath} should be defined`);
}
if (pJson.private) {
continue;
}
if (isCompact) {
await verifyCompactMode(pJson.name);
}
const longDepUrl = new URL(
`/${owner}/${repo}/${pJson.name}@${formattedSha}`,
apiUrl,
).href;
deps.set(pJson.name, longDepUrl);
realDeps?.set(pJson.name, pJson.version ?? longDepUrl);
const controller = new AbortController();
const resource = await fetch(longDepUrl, {
signal: controller.signal,
});
if (resource.ok) {
console.warn(
`${pJson.name}@${formattedSha} was already published on ${longDepUrl}`,
);
}
controller.abort();
const jsonUrl = isCompact
? new URL(`/${pJson.name}@${formattedSha}`, apiUrl).href
: longDepUrl;
// Collect package metadata
outputMetadata.packages.push({
name: pJson.name,
url: jsonUrl,
shasum: "", // will be filled later
});
}
for (const templateDir of templates) {
const pJsonPath = path.resolve(templateDir, "package.json");
const pJsonContents = await tryReadFile(pJsonPath);
const pJson = pJsonContents
? parsePackageJson(pJsonContents)
: null;
if (!pJson || !pJsonContents) {
console.warn(
`skipping ${templateDir} because there's no package.json file`,
);
continue;
}
if (!pJson.name) {
throw new Error(`"name" field in ${pJsonPath} should be defined`);
}
console.warn("preparing template:", pJson.name);
const restore = await writeDeps(
templateDir,
pJsonContents,
pJson,
deps,
realDeps,
);
const gitignorePath = path.join(templateDir, ".gitignore");
const ig = ignore().add("node_modules").add(".git");
if (fsSync.existsSync(gitignorePath)) {
const gitignoreContent = await fs.readFile(gitignorePath, "utf8");
ig.add(gitignoreContent);
}
const files = await glob(["**/*"], {
cwd: templateDir,
dot: true,
onlyFiles: true,
ignore: ["**/node_modules", ".git"], // always ignore node_modules and .git
});
const filteredFiles = files.filter((file) => !ig.ignores(file));
for (const filePath of filteredFiles) {
const file = await fs.readFile(path.join(templateDir, filePath));
const isBinary = await isBinaryFile(file);
const blob = new Blob([file.buffer], {
type: "application/octet-stream",
});
formData.append(
`template:${pJson.name}:${encodeURIComponent(filePath)}`,
isBinary ? blob : await blob.text(),
);
}
await restore();
// Collect template metadata
const templateUrl = new URL(
`/${owner}/${repo}/template/${pJson.name}`,
apiUrl,
).href;
outputMetadata.templates.push({
name: pJson.name,
url: templateUrl,
});
}
const noDefaultTemplate = args.template === false;
if (!noDefaultTemplate && templates.length === 0) {
const project = createDefaultTemplate(
Object.fromEntries(deps.entries()),
);
for (const filePath of Object.keys(project)) {
formData.append(
`template:default:${encodeURIComponent(filePath)}`,
project[filePath],
);
}
}
const restoreMap = new Map<
string,
Awaited<ReturnType<typeof writeDeps>>
>();
for (const p of paths) {
const pJsonPath = path.resolve(p, "package.json");
const pJsonContents = await tryReadFile(pJsonPath);
const pJson = pJsonContents
? parsePackageJson(pJsonContents)
: null;
if (!pJson || !pJsonContents) {
continue;
}
if (pJson.private) {
continue;
}
restoreMap.set(
p,
await writeDeps(p, pJsonContents, pJson, deps, realDeps),
);
}
const shasums: Record<string, string> = {};
for (const p of paths) {
const pJsonPath = path.resolve(p, "package.json");
const pJson = await readPackageJson(pJsonPath);
if (!pJson) {
console.warn(
`skipping ${p} because there's no package.json file`,
);
continue;
}
try {
if (!pJson.name) {
throw new Error(
`"name" field in ${pJsonPath} should be defined`,
);
}
if (pJson.private) {
console.warn(`skipping ${p} because the package is private`);
continue;
}
const { filename, shasum } = await resolveTarball(
packMethod,
p,
pJson,
);
shasums[pJson.name] = shasum;
const outputPkg = outputMetadata.packages.find(
(p) => p.name === pJson.name,
)!;
outputPkg.shasum = shasum;
const filePath = path.resolve(p, filename);
const buffer = await fs.readFile(filePath);
const blob = new Blob([buffer], {
type: "application/octet-stream",
});
formData.append(`package:${pJson.name}`, blob, filename);
await fs.rm(filePath);
} finally {
await restoreMap.get(p)?.();
}
}
const formDataPackagesSize = [...formData.entries()].reduce(
(prev, [_, entry]) => prev + getFormEntrySize(entry),
0,
);
// multipart uploading
if (formDataPackagesSize > 1024 * 1024 * 99) {
for (const [name, entry] of formData) {
if (name.startsWith("package:")) {
const file = entry as File;
const chunkSize = 1024 * 1024 * 5;
if (file.size <= chunkSize) {
continue;
}
const totalChunks = Math.ceil(file.size / chunkSize);
const createMultipartRes = await fetch(createMultipart, {
method: "POST",
headers: {
"sb-key": key,
"sb-name": name.slice("package:".length),
},
});
if (!createMultipartRes.ok) {
console.error(await createMultipartRes.text());
continue;
}
const { key: uploadKey, id: uploadId } =
await createMultipartRes.json();
interface R2UploadedPart {
partNumber: number;
etag: string;
}
const uploadedParts: R2UploadedPart[] = [];
for (let i = 0; i < totalChunks; i++) {
const start = i * chunkSize;
const end = Math.min(file.size, start + chunkSize);
const chunk = file.slice(start, end);
const uploadMultipartRes = await fetch(uploadMultipart, {
method: "PUT",
headers: {
key: uploadKey,
id: uploadId,
"part-number": `${i + 1}`,
},
body: chunk,
});
if (!uploadMultipartRes.ok) {
console.error(
`Error uploading part ${i + 1}: ${await uploadMultipartRes.text()}`,
);
break;
}
const { part } = await uploadMultipartRes.json();
uploadedParts.push(part);
}
const completeMultipartRes = await fetch(completeMultipart, {
method: "POST",
headers: {
key: uploadKey,
id: uploadId,
"uploaded-parts": JSON.stringify(uploadedParts),
},
});
if (!completeMultipartRes.ok) {
console.error(
`Error completing ${key}: ${await completeMultipartRes.text()}`,
);
break;
}
const { key: completionKey } =
await completeMultipartRes.json();
formData.set(name, `object:${completionKey}`);
}
}
}
const res = await fetch(publishUrl, {
method: "POST",
headers: {
"sb-comment": comment,
"sb-compact": `${isCompact}`,
"sb-key": key,
"sb-shasums": JSON.stringify(shasums),
"sb-run-id": GITHUB_RUN_ID,
"sb-bin": `${isBinaryApplication}`,
"sb-package-manager": selectedPackageManager[0],
"sb-only-templates": `${isOnlyTemplates}`,
},
body: formData,
});
const laterRes = await res.clone().json();
assert.equal(
res.status,
200,
`publishing failed: ${await res.text()}`,
);
const debug = laterRes.debug;
core.startGroup("🔍 Info");
core.notice(JSON.stringify(debug, null, 2));
core.endGroup();
console.warn("\n");
console.warn("⚡️ Your npm packages are published.\n");
const packageLogs = [...formData.keys()]
.filter((k) => k.startsWith("package:"))
.map((name, i) => {
const packageName = name.slice("package:".length);
const url = new URL(laterRes.urls[i]);
const publintUrl = new URL(
`/pkg.pr.new${url.pathname}`,
"https://publint.dev",
);
return `${packageName}:
- sha: ${shasums[packageName]}
- publint: ${publintUrl}
- npm: npm i ${url}`;
})
.join("\n\n");
console.warn(packageLogs);
const output = JSON.stringify(outputMetadata, null, 2);
if (printJson) {
console.log(output); // Log output for piping
}
if (saveJson) {
await fs.writeFile(jsonFilePath, output);
console.warn(`metadata written to ${jsonFilePath}`);
}
await fs.appendFile(GITHUB_OUTPUT, `sha=${formattedSha}\n`, "utf8");
await fs.appendFile(
GITHUB_OUTPUT,
`urls=${outputMetadata.packages.map((pkg) => pkg.url).join(" ")}\n`,
"utf8",
);
await fs.appendFile(
GITHUB_OUTPUT,
`packages=${outputMetadata.packages.map((pkg) => `${pkg.name}@${pkg.url}`).join(" ")}\n`,
"utf8",
);
},
};
},
link: () => {
return {
meta: {},
run: () => {
// noop
},
};
},
},
});
runMain(main)
.then(() => process.exit(0))
.catch(() => process.exit(1));
type PackMethod = "npm" | "pnpm" | "yarn" | "bun";
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}`;
} else if (pm === "bun") {
cmd = `bun pm pack --filename ${filename}`;
}
const { stdout } = await ezSpawn.async(cmd, {
stdio: "overlapped",
cwd: p,
});
const lines = stdout.split("\n").filter(Boolean);
if (pm !== "yarn" && pm !== "bun") {
filename = lines[lines.length - 1].trim();
}
const shasum = createHash("sha1")
.update(await fs.readFile(path.resolve(p, filename)))
.digest("hex");
return { filename, shasum };
}
async function writeDeps(
p: string,
pJsonContents: string,
pJson: PackageJson,
deps: Map<string, string>,
realDeps: Map<string, string> | null,
) {
const pJsonPath = path.resolve(p, "package.json");
hijackDeps(deps, pJson.dependencies);
hijackDeps(deps, pJson.devDependencies);
hijackDeps(deps, pJson.optionalDependencies);
if (realDeps) {
hijackDeps(realDeps, pJson.peerDependencies);
}
await writePackageJSON(pJsonPath, pJson);
return () => fs.writeFile(pJsonPath, pJsonContents);
}
function hijackDeps(
newDeps: Map<string, string>,
oldDeps?: Record<string, string>,
) {
if (!oldDeps) {
return;
}
for (const [newDep, url] of newDeps) {
if (newDep in oldDeps) {
oldDeps[newDep] = url;
}
}
}
function getFormEntrySize(entry: FormDataEntryValue) {
if (typeof entry === "string") {
return entry.length;
}
return entry.size;
}
async function verifyCompactMode(packageName: string) {
let manifest: PackageManifest;
try {
manifest = await getPackageManifest(packageName);
} catch {
throw new Error(
`pkg-pr-new cannot resolve ${packageName} from npm. --compact flag depends on the package being available in npm.
Make sure to have your package on npm first.`,
);
}
const instruction = `Make sure to configure the 'repository' / 'repository.url' field in its package.json properly.
See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#repository for details.`;
const repository = extractRepository(manifest);
if (!repository) {
throw new Error(
`pkg-pr-new cannot extract the repository link from the ${packageName} manifest. --compact flag requires the link to be present.
${instruction}`,
);
}
const match = extractOwnerAndRepo(repository);
if (!match) {
throw new Error(
`pkg-pr-new cannot extract the owner and repo names from the ${packageName} repository link: ${repository}. --compact flag requires these names.
${instruction}`,
);
}
}
async function tryReadFile(p: string) {
try {
return await fs.readFile(p, "utf8");
} catch {
return null;
}
}
async function readPackageJson(p: string) {
const contents = await tryReadFile(p);
if (contents === null) {
return null;
}
try {
return parsePackageJson(contents);
} catch {
return null;
}
}
function parsePackageJson(contents: string) {
try {
return JSON.parse(contents) as PackageJson;
} catch {
return null;
}
}