pkg-pr-new 0.0.38 → 0.0.40

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,7 +90,8 @@ 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
  },
@@ -113,16 +115,29 @@ const main = defineCommand({
113
115
  const formData = new FormData();
114
116
 
115
117
  const isCompact = !!args.compact;
116
- const isPnpm = !!args.pnpm;
118
+ let packMethod: PackMethod = "npm";
119
+
120
+ if (args.pnpm) {
121
+ packMethod = "pnpm";
122
+ } else if (args.yarn) {
123
+ packMethod = "yarn";
124
+ }
125
+
117
126
  const isPeerDepsEnabled = !!args.peerDeps;
118
127
  const isOnlyTemplates = !!args["only-templates"];
119
128
 
120
129
  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)) {
130
+ const selectedPackageManager = args.packageManager as
131
+ | "npm"
132
+ | "bun"
133
+ | "pnpm"
134
+ | "yarn";
135
+
136
+ if (
137
+ !["npm", "bun", "pnpm", "yarn"].includes(selectedPackageManager)
138
+ ) {
124
139
  console.error(
125
- `Unsupported package manager: ${selectedPackageManager}. Supported managers are npm, bun, pnpm, yarn.`
140
+ `Unsupported package manager: ${selectedPackageManager}. Supported managers are npm, bun, pnpm, yarn.`,
126
141
  );
127
142
  process.exit(1);
128
143
  }
@@ -183,11 +198,12 @@ const main = defineCommand({
183
198
  };
184
199
 
185
200
  for (const p of paths) {
186
- if (!(await hasPackageJson(p))) {
201
+ const pJsonPath = path.resolve(p, "package.json");
202
+ const pJson = await readPackageJson(pJsonPath);
203
+
204
+ if (!pJson) {
187
205
  continue;
188
206
  }
189
- const pJsonPath = path.resolve(p, "package.json");
190
- const pJson = await readPackageJSON(pJsonPath);
191
207
 
192
208
  if (!pJson.name) {
193
209
  throw new Error(`"name" field in ${pJsonPath} should be defined`);
@@ -223,14 +239,18 @@ const main = defineCommand({
223
239
  }
224
240
 
225
241
  for (const templateDir of templates) {
226
- if (!(await hasPackageJson(templateDir))) {
242
+ const pJsonPath = path.resolve(templateDir, "package.json");
243
+ const pJsonContents = await tryReadFile(pJsonPath);
244
+ const pJson = pJsonContents
245
+ ? parsePackageJson(pJsonContents)
246
+ : null;
247
+
248
+ if (!pJson || !pJsonContents) {
227
249
  console.warn(
228
250
  `skipping ${templateDir} because there's no package.json file`,
229
251
  );
230
252
  continue;
231
253
  }
232
- const pJsonPath = path.resolve(templateDir, "package.json");
233
- const pJson = await readPackageJSON(pJsonPath);
234
254
 
235
255
  if (!pJson.name) {
236
256
  throw new Error(`"name" field in ${pJsonPath} should be defined`);
@@ -238,7 +258,13 @@ const main = defineCommand({
238
258
 
239
259
  console.warn("preparing template:", pJson.name);
240
260
 
241
- const restore = await writeDeps(templateDir, deps, realDeps);
261
+ const restore = await writeDeps(
262
+ templateDir,
263
+ pJsonContents,
264
+ pJson,
265
+ deps,
266
+ realDeps,
267
+ );
242
268
 
243
269
  const gitignorePath = path.join(templateDir, ".gitignore");
244
270
  const ig = ignore().add("node_modules").add(".git");
@@ -301,31 +327,38 @@ const main = defineCommand({
301
327
  Awaited<ReturnType<typeof writeDeps>>
302
328
  >();
303
329
  for (const p of paths) {
304
- if (!(await hasPackageJson(p))) {
330
+ const pJsonPath = path.resolve(p, "package.json");
331
+ const pJsonContents = await tryReadFile(pJsonPath);
332
+ const pJson = pJsonContents
333
+ ? parsePackageJson(pJsonContents)
334
+ : null;
335
+
336
+ if (!pJson || !pJsonContents) {
305
337
  continue;
306
338
  }
307
- const pJsonPath = path.resolve(p, "package.json");
308
- const pJson = await readPackageJSON(pJsonPath);
309
339
 
310
340
  if (pJson.private) {
311
341
  continue;
312
342
  }
313
343
 
314
- restoreMap.set(p, await writeDeps(p, deps, realDeps));
344
+ restoreMap.set(
345
+ p,
346
+ await writeDeps(p, pJsonContents, pJson, deps, realDeps),
347
+ );
315
348
  }
316
349
 
317
350
  const shasums: Record<string, string> = {};
318
351
  for (const p of paths) {
319
- if (!(await hasPackageJson(p))) {
352
+ const pJsonPath = path.resolve(p, "package.json");
353
+ const pJson = await readPackageJson(pJsonPath);
354
+ if (!pJson) {
320
355
  console.warn(
321
356
  `skipping ${p} because there's no package.json file`,
322
357
  );
323
358
  continue;
324
359
  }
325
- const pJsonPath = path.resolve(p, "package.json");
326
- try {
327
- const pJson = await readPackageJSON(pJsonPath);
328
360
 
361
+ try {
329
362
  if (!pJson.name) {
330
363
  throw new Error(
331
364
  `"name" field in ${pJsonPath} should be defined`,
@@ -337,8 +370,9 @@ const main = defineCommand({
337
370
  }
338
371
 
339
372
  const { filename, shasum } = await resolveTarball(
340
- isPnpm ? "pnpm" : "npm",
373
+ packMethod,
341
374
  p,
375
+ pJson,
342
376
  );
343
377
 
344
378
  shasums[pJson.name] = shasum;
@@ -369,7 +403,7 @@ const main = defineCommand({
369
403
 
370
404
  // multipart uploading
371
405
  if (formDataPackagesSize > 1024 * 1024 * 99) {
372
- for (const [name, entry] of [...formData]) {
406
+ for (const [name, entry] of formData) {
373
407
  if (name.startsWith("package:")) {
374
408
  const file = entry as File;
375
409
  const chunkSize = 1024 * 1024 * 5;
@@ -388,11 +422,8 @@ const main = defineCommand({
388
422
  console.error(await createMultipartRes.text());
389
423
  continue;
390
424
  }
391
- const {
392
- key: uploadKey,
393
- id: uploadId,
394
- ...data
395
- } = await createMultipartRes.json();
425
+ const { key: uploadKey, id: uploadId } =
426
+ await createMultipartRes.json();
396
427
 
397
428
  interface R2UploadedPart {
398
429
  partNumber: number;
@@ -473,8 +504,11 @@ const main = defineCommand({
473
504
  .filter((k) => k.startsWith("package:"))
474
505
  .map((name, i) => {
475
506
  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")
507
+ const url = new URL(laterRes.urls[i]);
508
+ const publintUrl = new URL(
509
+ `/pkg.pr.new${url.pathname}`,
510
+ "https://publint.dev",
511
+ );
478
512
  return `${packageName}:
479
513
  - sha: ${shasums[packageName]}
480
514
  - publint: ${publintUrl}
@@ -498,7 +532,9 @@ const main = defineCommand({
498
532
  link: () => {
499
533
  return {
500
534
  meta: {},
501
- run: () => {},
535
+ run: () => {
536
+ // noop
537
+ },
502
538
  };
503
539
  },
504
540
  },
@@ -508,14 +544,23 @@ runMain(main)
508
544
  .then(() => process.exit(0))
509
545
  .catch(() => process.exit(1));
510
546
 
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`, {
547
+ type PackMethod = "npm" | "pnpm" | "yarn";
548
+
549
+ async function resolveTarball(pm: PackMethod, p: string, pJson: PackageJson) {
550
+ let cmd = `${pm} pack`;
551
+ let filename = `${pJson.name!.replace("/", "-")}-${pJson.version}.tgz`;
552
+ if (pm === "yarn") {
553
+ cmd += ` --filename ${filename}`;
554
+ }
555
+ const { stdout } = await ezSpawn.async(cmd, {
514
556
  stdio: "overlapped",
515
557
  cwd: p,
516
558
  });
517
559
  const lines = stdout.split("\n").filter(Boolean);
518
- const filename = lines[lines.length - 1].trim();
560
+
561
+ if (pm !== "yarn") {
562
+ filename = lines[lines.length - 1].trim();
563
+ }
519
564
 
520
565
  const shasum = createHash("sha1")
521
566
  .update(await fs.readFile(path.resolve(p, filename)))
@@ -526,13 +571,12 @@ async function resolveTarball(pm: "npm" | "pnpm", p: string) {
526
571
 
527
572
  async function writeDeps(
528
573
  p: string,
574
+ pJsonContents: string,
575
+ pJson: PackageJson,
529
576
  deps: Map<string, string>,
530
577
  realDeps: Map<string, string> | null,
531
578
  ) {
532
579
  const pJsonPath = path.resolve(p, "package.json");
533
- const content = await fs.readFile(pJsonPath, "utf-8");
534
-
535
- const pJson = await readPackageJSON(pJsonPath);
536
580
 
537
581
  hijackDeps(deps, pJson.dependencies);
538
582
  hijackDeps(deps, pJson.devDependencies);
@@ -544,7 +588,7 @@ async function writeDeps(
544
588
 
545
589
  await writePackageJSON(pJsonPath, pJson);
546
590
 
547
- return () => fs.writeFile(pJsonPath, content);
591
+ return () => fs.writeFile(pJsonPath, pJsonContents);
548
592
  }
549
593
 
550
594
  function hijackDeps(
@@ -600,11 +644,30 @@ ${instruction}`,
600
644
  }
601
645
  }
602
646
 
603
- async function hasPackageJson(p: string) {
647
+ async function tryReadFile(p: string) {
648
+ try {
649
+ return await fs.readFile(p, "utf8");
650
+ } catch {
651
+ return null;
652
+ }
653
+ }
654
+
655
+ async function readPackageJson(p: string) {
656
+ const contents = await tryReadFile(p);
657
+ if (contents === null) {
658
+ return null;
659
+ }
660
+ try {
661
+ return parsePackageJson(contents);
662
+ } catch {
663
+ return null;
664
+ }
665
+ }
666
+
667
+ function parsePackageJson(contents: string) {
604
668
  try {
605
- await fs.access(path.resolve(p, "package.json"), fs.constants.F_OK);
606
- return true;
669
+ return JSON.parse(contents) as PackageJson;
607
670
  } catch {
608
- return false;
671
+ return null;
609
672
  }
610
673
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pkg-pr-new",
3
- "version": "0.0.38",
3
+ "version": "0.0.40",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",