pkg-pr-new 0.0.39 → 0.0.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/environments.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/no-namespace */
1
2
  // https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
2
3
  declare global {
3
4
  namespace NodeJS {
package/index.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { defineCommand, runMain } from "citty";
1
+ /* eslint-disable unicorn/no-process-exit */
2
2
  import assert from "node:assert";
3
- import path from "path";
4
- import ezSpawn from "@jsdevtools/ez-spawn";
3
+ import path from "node:path";
5
4
  import { createHash } from "node:crypto";
5
+ import fsSync from "node:fs";
6
+ import fs from "node:fs/promises";
6
7
  import { hash } from "ohash";
7
- import fsSync from "fs";
8
- import fs from "fs/promises";
8
+ import ezSpawn from "@jsdevtools/ez-spawn";
9
+ import { defineCommand, runMain } from "citty";
9
10
  import { getPackageManifest, type PackageManifest } from "query-registry";
10
11
  import type { Comment } from "@pkg-pr-new/utils";
11
12
  import {
@@ -16,13 +17,13 @@ import {
16
17
  import { glob } from "tinyglobby";
17
18
  import ignore from "ignore";
18
19
  import "./environments";
19
- import pkg from "./package.json" with { type: "json" };
20
20
  import { isBinaryFile } from "isbinaryfile";
21
- import { readPackageJSON, writePackageJSON } from "pkg-types";
21
+ import { writePackageJSON, type PackageJson } from "pkg-types";
22
+ import pkg from "./package.json" with { type: "json" };
22
23
  import { createDefaultTemplate } from "./template";
23
24
 
24
25
  declare global {
25
- var API_URL: string;
26
+ const API_URL: string;
26
27
  }
27
28
 
28
29
  type OutputMetadata = {
@@ -89,10 +90,16 @@ const main = defineCommand({
89
90
  },
90
91
  packageManager: {
91
92
  type: "string",
92
- description: "Specify the package manager to use (npm, bun, pnpm, yarn)",
93
+ description:
94
+ "Specify the package manager to use (npm, bun, pnpm, yarn)",
93
95
  enum: ["npm", "bun", "pnpm", "yarn"],
94
96
  default: "npm",
95
97
  },
98
+ bin: {
99
+ type: "boolean",
100
+ description:
101
+ "Set to true if your package is a binary application and you would like to show an execute command instead of an install command.",
102
+ },
96
103
  },
97
104
  run: async ({ args }) => {
98
105
  const paths =
@@ -113,16 +120,30 @@ const main = defineCommand({
113
120
  const formData = new FormData();
114
121
 
115
122
  const isCompact = !!args.compact;
116
- const isPnpm = !!args.pnpm;
123
+ let packMethod: PackMethod = "npm";
124
+
125
+ if (args.pnpm) {
126
+ packMethod = "pnpm";
127
+ } else if (args.yarn) {
128
+ packMethod = "yarn";
129
+ }
130
+
117
131
  const isPeerDepsEnabled = !!args.peerDeps;
118
132
  const isOnlyTemplates = !!args["only-templates"];
119
133
 
134
+ const isBinaryApplication = !!args.binaryApplication;
120
135
  const comment: Comment = args.comment as Comment;
121
- const selectedPackageManager = args.packageManager as "npm" | "bun" | "pnpm" | "yarn";
122
-
123
- if (!["npm", "bun", "pnpm", "yarn"].includes(selectedPackageManager)) {
136
+ const selectedPackageManager = args.packageManager as
137
+ | "npm"
138
+ | "bun"
139
+ | "pnpm"
140
+ | "yarn";
141
+
142
+ if (
143
+ !["npm", "bun", "pnpm", "yarn"].includes(selectedPackageManager)
144
+ ) {
124
145
  console.error(
125
- `Unsupported package manager: ${selectedPackageManager}. Supported managers are npm, bun, pnpm, yarn.`
146
+ `Unsupported package manager: ${selectedPackageManager}. Supported managers are npm, bun, pnpm, yarn.`,
126
147
  );
127
148
  process.exit(1);
128
149
  }
@@ -183,11 +204,12 @@ const main = defineCommand({
183
204
  };
184
205
 
185
206
  for (const p of paths) {
186
- if (!(await hasPackageJson(p))) {
207
+ const pJsonPath = path.resolve(p, "package.json");
208
+ const pJson = await readPackageJson(pJsonPath);
209
+
210
+ if (!pJson) {
187
211
  continue;
188
212
  }
189
- const pJsonPath = path.resolve(p, "package.json");
190
- const pJson = await readPackageJSON(pJsonPath);
191
213
 
192
214
  if (!pJson.name) {
193
215
  throw new Error(`"name" field in ${pJsonPath} should be defined`);
@@ -223,14 +245,18 @@ const main = defineCommand({
223
245
  }
224
246
 
225
247
  for (const templateDir of templates) {
226
- if (!(await hasPackageJson(templateDir))) {
248
+ const pJsonPath = path.resolve(templateDir, "package.json");
249
+ const pJsonContents = await tryReadFile(pJsonPath);
250
+ const pJson = pJsonContents
251
+ ? parsePackageJson(pJsonContents)
252
+ : null;
253
+
254
+ if (!pJson || !pJsonContents) {
227
255
  console.warn(
228
256
  `skipping ${templateDir} because there's no package.json file`,
229
257
  );
230
258
  continue;
231
259
  }
232
- const pJsonPath = path.resolve(templateDir, "package.json");
233
- const pJson = await readPackageJSON(pJsonPath);
234
260
 
235
261
  if (!pJson.name) {
236
262
  throw new Error(`"name" field in ${pJsonPath} should be defined`);
@@ -238,7 +264,13 @@ const main = defineCommand({
238
264
 
239
265
  console.warn("preparing template:", pJson.name);
240
266
 
241
- const restore = await writeDeps(templateDir, deps, realDeps);
267
+ const restore = await writeDeps(
268
+ templateDir,
269
+ pJsonContents,
270
+ pJson,
271
+ deps,
272
+ realDeps,
273
+ );
242
274
 
243
275
  const gitignorePath = path.join(templateDir, ".gitignore");
244
276
  const ig = ignore().add("node_modules").add(".git");
@@ -301,31 +333,38 @@ const main = defineCommand({
301
333
  Awaited<ReturnType<typeof writeDeps>>
302
334
  >();
303
335
  for (const p of paths) {
304
- if (!(await hasPackageJson(p))) {
336
+ const pJsonPath = path.resolve(p, "package.json");
337
+ const pJsonContents = await tryReadFile(pJsonPath);
338
+ const pJson = pJsonContents
339
+ ? parsePackageJson(pJsonContents)
340
+ : null;
341
+
342
+ if (!pJson || !pJsonContents) {
305
343
  continue;
306
344
  }
307
- const pJsonPath = path.resolve(p, "package.json");
308
- const pJson = await readPackageJSON(pJsonPath);
309
345
 
310
346
  if (pJson.private) {
311
347
  continue;
312
348
  }
313
349
 
314
- restoreMap.set(p, await writeDeps(p, deps, realDeps));
350
+ restoreMap.set(
351
+ p,
352
+ await writeDeps(p, pJsonContents, pJson, deps, realDeps),
353
+ );
315
354
  }
316
355
 
317
356
  const shasums: Record<string, string> = {};
318
357
  for (const p of paths) {
319
- if (!(await hasPackageJson(p))) {
358
+ const pJsonPath = path.resolve(p, "package.json");
359
+ const pJson = await readPackageJson(pJsonPath);
360
+ if (!pJson) {
320
361
  console.warn(
321
362
  `skipping ${p} because there's no package.json file`,
322
363
  );
323
364
  continue;
324
365
  }
325
- const pJsonPath = path.resolve(p, "package.json");
326
- try {
327
- const pJson = await readPackageJSON(pJsonPath);
328
366
 
367
+ try {
329
368
  if (!pJson.name) {
330
369
  throw new Error(
331
370
  `"name" field in ${pJsonPath} should be defined`,
@@ -337,8 +376,9 @@ const main = defineCommand({
337
376
  }
338
377
 
339
378
  const { filename, shasum } = await resolveTarball(
340
- isPnpm ? "pnpm" : "npm",
379
+ packMethod,
341
380
  p,
381
+ pJson,
342
382
  );
343
383
 
344
384
  shasums[pJson.name] = shasum;
@@ -369,7 +409,7 @@ const main = defineCommand({
369
409
 
370
410
  // multipart uploading
371
411
  if (formDataPackagesSize > 1024 * 1024 * 99) {
372
- for (const [name, entry] of [...formData]) {
412
+ for (const [name, entry] of formData) {
373
413
  if (name.startsWith("package:")) {
374
414
  const file = entry as File;
375
415
  const chunkSize = 1024 * 1024 * 5;
@@ -388,11 +428,8 @@ const main = defineCommand({
388
428
  console.error(await createMultipartRes.text());
389
429
  continue;
390
430
  }
391
- const {
392
- key: uploadKey,
393
- id: uploadId,
394
- ...data
395
- } = await createMultipartRes.json();
431
+ const { key: uploadKey, id: uploadId } =
432
+ await createMultipartRes.json();
396
433
 
397
434
  interface R2UploadedPart {
398
435
  partNumber: number;
@@ -454,6 +491,7 @@ const main = defineCommand({
454
491
  "sb-key": key,
455
492
  "sb-shasums": JSON.stringify(shasums),
456
493
  "sb-run-id": GITHUB_RUN_ID,
494
+ "sb-bin": `${isBinaryApplication}`,
457
495
  "sb-package-manager": selectedPackageManager,
458
496
  "sb-only-templates": `${isOnlyTemplates}`,
459
497
  },
@@ -473,8 +511,11 @@ const main = defineCommand({
473
511
  .filter((k) => k.startsWith("package:"))
474
512
  .map((name, i) => {
475
513
  const packageName = name.slice("package:".length);
476
- const url = new URL(laterRes.urls[i])
477
- const publintUrl = new URL(`/pkg.pr.new${url.pathname}`, "https://publint.dev")
514
+ const url = new URL(laterRes.urls[i]);
515
+ const publintUrl = new URL(
516
+ `/pkg.pr.new${url.pathname}`,
517
+ "https://publint.dev",
518
+ );
478
519
  return `${packageName}:
479
520
  - sha: ${shasums[packageName]}
480
521
  - publint: ${publintUrl}
@@ -498,7 +539,9 @@ const main = defineCommand({
498
539
  link: () => {
499
540
  return {
500
541
  meta: {},
501
- run: () => {},
542
+ run: () => {
543
+ // noop
544
+ },
502
545
  };
503
546
  },
504
547
  },
@@ -508,14 +551,23 @@ runMain(main)
508
551
  .then(() => process.exit(0))
509
552
  .catch(() => process.exit(1));
510
553
 
511
- // TODO: we'll add support for yarn if users hit issues with npm
512
- async function resolveTarball(pm: "npm" | "pnpm", p: string) {
513
- const { stdout } = await ezSpawn.async(`${pm} pack`, {
554
+ type PackMethod = "npm" | "pnpm" | "yarn";
555
+
556
+ async function resolveTarball(pm: PackMethod, p: string, pJson: PackageJson) {
557
+ let cmd = `${pm} pack`;
558
+ let filename = `${pJson.name!.replace("/", "-")}-${pJson.version}.tgz`;
559
+ if (pm === "yarn") {
560
+ cmd += ` --filename ${filename}`;
561
+ }
562
+ const { stdout } = await ezSpawn.async(cmd, {
514
563
  stdio: "overlapped",
515
564
  cwd: p,
516
565
  });
517
566
  const lines = stdout.split("\n").filter(Boolean);
518
- const filename = lines[lines.length - 1].trim();
567
+
568
+ if (pm !== "yarn") {
569
+ filename = lines[lines.length - 1].trim();
570
+ }
519
571
 
520
572
  const shasum = createHash("sha1")
521
573
  .update(await fs.readFile(path.resolve(p, filename)))
@@ -526,13 +578,12 @@ async function resolveTarball(pm: "npm" | "pnpm", p: string) {
526
578
 
527
579
  async function writeDeps(
528
580
  p: string,
581
+ pJsonContents: string,
582
+ pJson: PackageJson,
529
583
  deps: Map<string, string>,
530
584
  realDeps: Map<string, string> | null,
531
585
  ) {
532
586
  const pJsonPath = path.resolve(p, "package.json");
533
- const content = await fs.readFile(pJsonPath, "utf-8");
534
-
535
- const pJson = await readPackageJSON(pJsonPath);
536
587
 
537
588
  hijackDeps(deps, pJson.dependencies);
538
589
  hijackDeps(deps, pJson.devDependencies);
@@ -544,7 +595,7 @@ async function writeDeps(
544
595
 
545
596
  await writePackageJSON(pJsonPath, pJson);
546
597
 
547
- return () => fs.writeFile(pJsonPath, content);
598
+ return () => fs.writeFile(pJsonPath, pJsonContents);
548
599
  }
549
600
 
550
601
  function hijackDeps(
@@ -600,11 +651,30 @@ ${instruction}`,
600
651
  }
601
652
  }
602
653
 
603
- async function hasPackageJson(p: string) {
654
+ async function tryReadFile(p: string) {
655
+ try {
656
+ return await fs.readFile(p, "utf8");
657
+ } catch {
658
+ return null;
659
+ }
660
+ }
661
+
662
+ async function readPackageJson(p: string) {
663
+ const contents = await tryReadFile(p);
664
+ if (contents === null) {
665
+ return null;
666
+ }
667
+ try {
668
+ return parsePackageJson(contents);
669
+ } catch {
670
+ return null;
671
+ }
672
+ }
673
+
674
+ function parsePackageJson(contents: string) {
604
675
  try {
605
- await fs.access(path.resolve(p, "package.json"), fs.constants.F_OK);
606
- return true;
676
+ return JSON.parse(contents) as PackageJson;
607
677
  } catch {
608
- return false;
678
+ return null;
609
679
  }
610
680
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pkg-pr-new",
3
- "version": "0.0.39",
3
+ "version": "0.0.41",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",