pkg-pr-new 0.0.8 → 0.0.9

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/index.ts CHANGED
@@ -6,21 +6,26 @@ import { createHash } from "node:crypto";
6
6
  import { hash } from "ohash";
7
7
  import fsSync from "fs";
8
8
  import fs from "fs/promises";
9
- import { Octokit } from "@octokit/action";
10
- import { getPackageManifest } from "query-registry";
11
- import { extractOwnerAndRepo, extractRepository } from "@pkg-pr-new/utils";
9
+ import { getPackageManifest, type PackageManifest } from "query-registry";
10
+ import type { Comment } from "@pkg-pr-new/utils";
11
+ import {
12
+ abbreviateCommitHash,
13
+ extractOwnerAndRepo,
14
+ extractRepository,
15
+ } from "@pkg-pr-new/utils";
12
16
  import fg from "fast-glob";
13
17
  import ignore from "ignore";
14
18
  import "./environments";
15
19
  import pkg from "./package.json" with { type: "json" };
16
20
  import { isBinaryFile } from "isbinaryfile";
17
21
  import { readPackageJSON, writePackageJSON } from "pkg-types";
22
+ import { createDefaultTemplate } from "./template";
18
23
 
19
24
  declare global {
20
25
  var API_URL: string;
21
26
  }
22
27
 
23
- const apiUrl = process.env.API_URL ?? API_URL
28
+ const apiUrl = process.env.API_URL ?? API_URL;
24
29
  const publishUrl = new URL("/publish", apiUrl);
25
30
 
26
31
  const main = defineCommand({
@@ -47,6 +52,11 @@ const main = defineCommand({
47
52
  description:
48
53
  "generate stackblitz templates out of directories in the current repo with the new built packages",
49
54
  },
55
+ comment: {
56
+ type: "string", // "off", "create", "update" (default)
57
+ 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)`,
58
+ default: "update",
59
+ },
50
60
  },
51
61
  run: async ({ args }) => {
52
62
  const paths = (args._.length ? args._ : ["."])
@@ -56,7 +66,7 @@ const main = defineCommand({
56
66
  const templates = (
57
67
  typeof args.template === "string"
58
68
  ? [args.template]
59
- : ([...(args.template ?? [])] as string[])
69
+ : ([...(args.template || [])] as string[])
60
70
  )
61
71
  .flatMap((p) => (fg.isDynamicPattern(p) ? fg.sync(p) : p))
62
72
  .map((p) => path.resolve(p));
@@ -66,6 +76,8 @@ const main = defineCommand({
66
76
  const isCompact = !!args.compact;
67
77
  const isPnpm = !!args.pnpm;
68
78
 
79
+ const comment: Comment = args.comment as Comment;
80
+
69
81
  if (!process.env.TEST && process.env.GITHUB_ACTIONS !== "true") {
70
82
  console.error(
71
83
  "Continuous Releases are only available in Github Actions.",
@@ -107,6 +119,7 @@ const main = defineCommand({
107
119
  }
108
120
 
109
121
  const { sha } = await checkResponse.json();
122
+ const abbreviatedSha = abbreviateCommitHash(sha);
110
123
 
111
124
  const deps: Map<string, string> = new Map();
112
125
 
@@ -125,7 +138,7 @@ const main = defineCommand({
125
138
  deps.set(
126
139
  pJson.name,
127
140
  new URL(
128
- `/${owner}/${repo}/${pJson.name}@${sha}`,
141
+ `/${owner}/${repo}/${pJson.name}@${abbreviatedSha}`,
129
142
  apiUrl,
130
143
  ).href,
131
144
  );
@@ -174,6 +187,21 @@ const main = defineCommand({
174
187
  await restore();
175
188
  }
176
189
 
190
+ const noDefaultTemplate = args.template === false;
191
+
192
+ if (!templates.length && !noDefaultTemplate) {
193
+ const project = createDefaultTemplate(
194
+ Object.fromEntries(deps.entries()),
195
+ );
196
+
197
+ for (const filePath of Object.keys(project)) {
198
+ formData.append(
199
+ `template:default:${encodeURIComponent(filePath)}`,
200
+ project[filePath],
201
+ );
202
+ }
203
+ }
204
+
177
205
  const restoreMap = new Map<
178
206
  string,
179
207
  Awaited<ReturnType<typeof writeDeps>>
@@ -216,6 +244,7 @@ const main = defineCommand({
216
244
  const res = await fetch(publishUrl, {
217
245
  method: "POST",
218
246
  headers: {
247
+ "sb-comment": comment,
219
248
  "sb-compact": `${isCompact}`,
220
249
  "sb-key": key,
221
250
  "sb-shasums": JSON.stringify(shasums),
@@ -256,30 +285,18 @@ runMain(main);
256
285
 
257
286
  // TODO: we'll add support for yarn if users hit issues with npm
258
287
  async function resolveTarball(pm: "npm" | "pnpm", p: string) {
259
- if (pm === "npm") {
260
- const { stdout } = await ezSpawn.async("npm pack --json", {
261
- stdio: "overlapped",
262
- cwd: p,
263
- });
264
-
265
- const { filename, shasum }: { filename: string; shasum: string } =
266
- JSON.parse(stdout)[0];
267
-
268
- return { filename, shasum };
269
- } else if (pm === "pnpm") {
270
- const { stdout } = await ezSpawn.async("pnpm pack", {
271
- stdio: "overlapped",
272
- cwd: p,
273
- });
274
- const filename = stdout.trim();
275
-
276
- const shasum = createHash("sha1")
277
- .update(await fs.readFile(path.resolve(p, filename)))
278
- .digest("hex");
279
-
280
- return { filename, shasum };
281
- }
282
- throw new Error("Could not resolve package manager");
288
+ const { stdout } = await ezSpawn.async(`${pm} pack`, {
289
+ stdio: "overlapped",
290
+ cwd: p,
291
+ });
292
+ const lines = stdout.split("\n").filter(Boolean);
293
+ const filename = lines[lines.length - 1].trim();
294
+
295
+ const shasum = createHash("sha1")
296
+ .update(await fs.readFile(path.resolve(p, filename)))
297
+ .digest("hex");
298
+
299
+ return { filename, shasum };
283
300
  }
284
301
 
285
302
  async function writeDeps(p: string, deps: Map<string, string>) {
@@ -311,23 +328,33 @@ function hijackDeps(
311
328
  }
312
329
 
313
330
  async function verifyCompactMode(packageName: string) {
314
- const error = new Error(
315
- `pkg-pr-new cannot resolve ${packageName} from npm. --compact flag depends on the package being available in npm.
316
- Make sure to have your package on npm first or configure the 'repository' field in your package.json properly.`,
317
- );
331
+ let manifest: PackageManifest;
332
+
318
333
  try {
319
- const manifest = await getPackageManifest(packageName);
334
+ manifest = await getPackageManifest(packageName);
335
+ } catch {
336
+ throw new Error(
337
+ `pkg-pr-new cannot resolve ${packageName} from npm. --compact flag depends on the package being available in npm.
338
+ Make sure to have your package on npm first.`,
339
+ );
340
+ }
320
341
 
321
- const repository = extractRepository(manifest);
322
- if (!repository) {
323
- throw error;
324
- }
342
+ const instruction = `Make sure to configure the 'repository' / 'repository.url' field in its package.json properly.
343
+ See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#repository for details.`;
325
344
 
326
- const match = extractOwnerAndRepo(repository);
327
- if (!match) {
328
- throw error;
329
- }
330
- } catch {
331
- throw error;
345
+ const repository = extractRepository(manifest);
346
+ if (!repository) {
347
+ throw new Error(
348
+ `pkg-pr-new cannot extract the repository link from the ${packageName} manifest. --compact flag requires the link to be present.
349
+ ${instruction}`,
350
+ );
351
+ }
352
+
353
+ const match = extractOwnerAndRepo(repository);
354
+ if (!match) {
355
+ throw new Error(
356
+ `pkg-pr-new cannot extract the owner and repo names from the ${packageName} repository link: ${repository}. --compact flag requires these names.
357
+ ${instruction}`,
358
+ );
332
359
  }
333
360
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pkg-pr-new",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -22,7 +22,7 @@
22
22
  "ignore": "^5.3.1",
23
23
  "isbinaryfile": "^5.0.2",
24
24
  "pkg-types": "^1.1.1",
25
- "query-registry": "^3.0.0"
25
+ "query-registry": "^3.0.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@pkg-pr-new/utils": "workspace:^",
package/template.ts ADDED
@@ -0,0 +1,54 @@
1
+ export const createDefaultTemplate = (
2
+ dependencies: Record<string, string>,
3
+ ) => ({
4
+ "index.js": "",
5
+ "README.md": `
6
+ # Default Template
7
+
8
+ This is a template that leverages the experimental templates feature in the \`pkg.pr.new\` tool.
9
+
10
+ ## Overview
11
+
12
+ Templates are particularly useful for creating live, interactive examples of your packages, which can be very beneficial for both development and documentation purposes.
13
+
14
+ As a user, you can check the package.json file and see the new generated packages! You can just copy those and put them in your package.json or install them with your favorite package manager.
15
+
16
+ ${Object.values(dependencies)
17
+ .map(
18
+ (url) => `
19
+ \`\`\`sh
20
+ npm i ${url}
21
+ \`\`\`
22
+ `,
23
+ )
24
+ .join("")}
25
+
26
+ ## Usage
27
+
28
+ To use this feature as a maintainer, you can run the following command:
29
+
30
+ \`\`\`sh
31
+ npx pkg-pr-new publish ./packages/A --template ./examples/*
32
+ \`\`\`
33
+
34
+ ## Benefits
35
+
36
+ - Interactive Demos: Automatically create live demos that users can interact with directly in their browser.
37
+ - Enhanced Testing: Quickly spin up environments to test your package in different scenarios.
38
+ - Improved Sharing: Easily share working examples of your package with collaborators or users without needing them to set up their environment.
39
+ `,
40
+ "package.json": JSON.stringify(
41
+ {
42
+ name: "default",
43
+ version: "1.0.0",
44
+ description: "generated by pkg.pr.new",
45
+ main: "index.js",
46
+ dependencies,
47
+ keywords: [],
48
+ author: "pkg.pr.new",
49
+ license: "ISC",
50
+ },
51
+ null,
52
+ 2,
53
+ ),
54
+ });
Binary file