pkg-pr-new 0.0.2 → 0.0.4
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/dist/index.js +8475 -923
- package/environments.ts +3 -3
- package/index.ts +113 -34
- package/package.json +3 -1
- package/tsup.config.ts +1 -1
package/environments.ts
CHANGED
|
@@ -12,13 +12,13 @@ declare global {
|
|
|
12
12
|
// The head ref or source branch of the pull request in a workflow run. This property is only set when the event that triggers a workflow run is either pull_request or pull_request_target. For example, feature-branch-1.
|
|
13
13
|
GITHUB_HEAD_REF: string;
|
|
14
14
|
// The URL of the GitHub server. For example: https://github.com.
|
|
15
|
-
GITHUB_SERVER_URL: string
|
|
15
|
+
GITHUB_SERVER_URL: string;
|
|
16
16
|
// The owner and repository name. For example, octocat/Hello-World.
|
|
17
17
|
GITHUB_REPOSITORY: string;
|
|
18
18
|
// The commit SHA that triggered the workflow. The value of this commit SHA depends on the event that triggered the workflow. For more information, see "Events that trigger workflows." For example, ffac537e6cbbf934b08745a378932722df287a53.
|
|
19
19
|
GITHUB_SHA: string;
|
|
20
20
|
// The account ID of the person or app that triggered the initial workflow run. For example, 1234567. Note that this is different from the actor username.
|
|
21
|
-
GITHUB_ACTOR_ID: string
|
|
21
|
+
GITHUB_ACTOR_ID: string;
|
|
22
22
|
// The name of the person or app that initiated the workflow. For example, octocat.
|
|
23
23
|
GITHUB_ACTOR: string;
|
|
24
24
|
// The path to the file on the runner that contains the full event webhook payload. For example, /github/workflow/event.json.
|
|
@@ -26,7 +26,7 @@ declare global {
|
|
|
26
26
|
// A unique number for each workflow run within a repository. This number does not change if you re-run the workflow run. For example, 1658821493.
|
|
27
27
|
GITHUB_RUN_ID: string;
|
|
28
28
|
// A unique number for each attempt of a particular workflow run in a repository. This number begins at 1 for the workflow run's first attempt, and increments with each re-run. For example, 3.
|
|
29
|
-
GITHUB_RUN_ATTEMPT: string
|
|
29
|
+
GITHUB_RUN_ATTEMPT: string;
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
}
|
package/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineCommand, runMain } from "citty";
|
|
1
|
+
import { defineCommand, runMain, parseArgs } from "citty";
|
|
2
2
|
import assert from "node:assert";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import ezSpawn from "@jsdevtools/ez-spawn";
|
|
@@ -7,14 +7,10 @@ import { hash } from "ohash";
|
|
|
7
7
|
import fs from "fs/promises";
|
|
8
8
|
import { Octokit } from "@octokit/action";
|
|
9
9
|
import { pathToFileURL } from "node:url";
|
|
10
|
+
import { getPackageManifest } from "query-registry";
|
|
11
|
+
import { extractOwnerAndRepo, extractRepository } from "@pkg-pr-new/utils";
|
|
10
12
|
import "./environments";
|
|
11
|
-
|
|
12
|
-
const {
|
|
13
|
-
default: { name, version },
|
|
14
|
-
} = await import(
|
|
15
|
-
pathToFileURL(path.resolve(process.cwd(), "package.json")).href,
|
|
16
|
-
{ with: { type: "json" } }
|
|
17
|
-
);
|
|
13
|
+
import pkg from "./package.json" with { type: "json" };
|
|
18
14
|
|
|
19
15
|
declare global {
|
|
20
16
|
var API_URL: string;
|
|
@@ -34,17 +30,9 @@ const {
|
|
|
34
30
|
GITHUB_RUN_ID,
|
|
35
31
|
GITHUB_RUN_ATTEMPT,
|
|
36
32
|
GITHUB_ACTOR_ID,
|
|
37
|
-
GITHUB_REF_NAME,
|
|
38
33
|
GITHUB_SHA,
|
|
39
34
|
} = process.env;
|
|
40
35
|
|
|
41
|
-
let ref = GITHUB_REF_NAME.split("/merge")[0];
|
|
42
|
-
const isPullRequest = GITHUB_REF_NAME.endsWith("/merge");
|
|
43
|
-
|
|
44
|
-
if (isPullRequest) {
|
|
45
|
-
ref = "pr-" + ref;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
36
|
const [owner, repo] = GITHUB_REPOSITORY.split("/");
|
|
49
37
|
|
|
50
38
|
const commit = await octokit.git.getCommit({
|
|
@@ -68,38 +56,84 @@ const key = hash(metadata);
|
|
|
68
56
|
|
|
69
57
|
const main = defineCommand({
|
|
70
58
|
meta: {
|
|
71
|
-
version,
|
|
59
|
+
version: pkg.version,
|
|
72
60
|
name: "stackblitz",
|
|
73
61
|
description: "A CLI for pkg.pr.new (Continuous Releases)",
|
|
74
62
|
},
|
|
75
63
|
subCommands: {
|
|
76
64
|
publish: () => {
|
|
77
65
|
return {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
66
|
+
args: {
|
|
67
|
+
compact: {
|
|
68
|
+
type: "boolean",
|
|
69
|
+
description:
|
|
70
|
+
"compact urls. The shortest form of urls like pkg.pr.new/tinybench@a832a55)",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
run: async ({ args }) => {
|
|
74
|
+
const compact = !!args.compact;
|
|
81
75
|
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
76
|
+
const paths = (args._.length ? args._ : ["."]).map((p) =>
|
|
77
|
+
path.resolve(p),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const deps: Map<string, string> = new Map();
|
|
81
|
+
const pJsonContent: Map<string, string> = new Map();
|
|
82
|
+
for (const p of paths) {
|
|
83
|
+
const pJsonPath = path.resolve(p, "package.json");
|
|
84
|
+
const { name } = await importPackageJson(pJsonPath);
|
|
85
|
+
|
|
86
|
+
if (compact) {
|
|
87
|
+
await verifyCompactMode(name);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
deps.set(
|
|
91
|
+
name,
|
|
92
|
+
new URL(
|
|
93
|
+
`/${owner}/${repo}/${name}@${GITHUB_SHA.substring(0, 7)}`,
|
|
94
|
+
API_URL,
|
|
95
|
+
).href,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
for (const p of paths) {
|
|
99
|
+
const pJsonPath = path.resolve(p, "package.json");
|
|
100
|
+
const content = await fs.readFile(pJsonPath, "utf-8");
|
|
101
|
+
pJsonContent.set(pJsonPath, content);
|
|
102
|
+
|
|
103
|
+
const pJson = await importPackageJson(pJsonPath);
|
|
104
|
+
hijackDeps(deps, pJson.dependencies);
|
|
105
|
+
hijackDeps(deps, pJson.devDependencies);
|
|
106
|
+
await fs.writeFile(pJsonPath, JSON.stringify(pJson));
|
|
107
|
+
}
|
|
108
|
+
const formData = new FormData();
|
|
109
|
+
for (const p of paths) {
|
|
110
|
+
const pJsonPath = path.resolve(p, "package.json");
|
|
111
|
+
try {
|
|
112
|
+
const { name, version } = await importPackageJson(pJsonPath);
|
|
113
|
+
await ezSpawn.async("npm pack", { stdio: "inherit", cwd: p });
|
|
114
|
+
|
|
115
|
+
const fileName = `${name}-${version}.tgz`;
|
|
116
|
+
const file = await fs.readFile(path.resolve(p, fileName));
|
|
117
|
+
|
|
118
|
+
const blob = new Blob([file], {
|
|
119
|
+
type: "application/octet-stream",
|
|
120
|
+
});
|
|
121
|
+
formData.append(name, blob, fileName);
|
|
122
|
+
} finally {
|
|
123
|
+
await fs.writeFile(pJsonPath, pJsonContent.get(pJsonPath)!);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
91
126
|
|
|
92
127
|
const res = await fetch(publishUrl, {
|
|
93
128
|
method: "POST",
|
|
94
129
|
headers: {
|
|
130
|
+
"sb-compact": `${compact}`,
|
|
95
131
|
"sb-key": key,
|
|
96
|
-
"sb-package-name": name,
|
|
97
|
-
"sb-package-version": version,
|
|
98
132
|
"sb-commit-timestamp": commitTimestamp.toString(),
|
|
99
133
|
},
|
|
100
|
-
body:
|
|
134
|
+
body: formData,
|
|
101
135
|
});
|
|
102
|
-
const laterRes = res.clone();
|
|
136
|
+
const laterRes = await res.clone().json();
|
|
103
137
|
assert.equal(
|
|
104
138
|
res.status,
|
|
105
139
|
200,
|
|
@@ -107,7 +141,8 @@ const main = defineCommand({
|
|
|
107
141
|
);
|
|
108
142
|
|
|
109
143
|
console.log(
|
|
110
|
-
`⚡️ Your npm
|
|
144
|
+
`⚡️ Your npm packages are published.
|
|
145
|
+
${[...formData.keys()].map((name, i) => `${name}: \`npm i ${laterRes.urls[i]}\``).join("\n")}`,
|
|
111
146
|
);
|
|
112
147
|
},
|
|
113
148
|
};
|
|
@@ -122,3 +157,47 @@ const main = defineCommand({
|
|
|
122
157
|
});
|
|
123
158
|
|
|
124
159
|
runMain(main);
|
|
160
|
+
|
|
161
|
+
async function importPackageJson(p: string): Promise<Record<string, any>> {
|
|
162
|
+
const { default: obj } = await import(pathToFileURL(p).href, {
|
|
163
|
+
with: { type: "json" },
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
return obj;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function hijackDeps(
|
|
170
|
+
newDeps: Map<string, string>,
|
|
171
|
+
oldDeps?: Record<string, string>,
|
|
172
|
+
) {
|
|
173
|
+
if (!oldDeps) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
for (const [newDep, url] of newDeps) {
|
|
177
|
+
if (newDep in oldDeps) {
|
|
178
|
+
oldDeps[newDep] = url;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function verifyCompactMode(packageName: string) {
|
|
184
|
+
const error = new Error(
|
|
185
|
+
`pkg-pr-new cannot resolve ${packageName} from npm. --compact flag depends on the package being available in npm.
|
|
186
|
+
Make sure to have your package on npm first or configure the 'repository' field in your package.json properly.`,
|
|
187
|
+
);
|
|
188
|
+
try {
|
|
189
|
+
const manifest = await getPackageManifest(packageName);
|
|
190
|
+
|
|
191
|
+
const repository = extractRepository(manifest);
|
|
192
|
+
if (!repository) {
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const match = extractOwnerAndRepo(repository);
|
|
197
|
+
if (!match) {
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
} catch {
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pkg-pr-new",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"@octokit/action": "^6.0.7"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
+
"@pkg-pr-new/utils": "workspace:^",
|
|
23
24
|
"citty": "^0.1.6",
|
|
25
|
+
"query-registry": "^3.0.0",
|
|
24
26
|
"tsup": "^8.0.2"
|
|
25
27
|
}
|
|
26
28
|
}
|
package/tsup.config.ts
CHANGED
|
@@ -7,7 +7,7 @@ export default defineConfig({
|
|
|
7
7
|
splitting: false,
|
|
8
8
|
sourcemap: "inline",
|
|
9
9
|
define: {
|
|
10
|
-
API_URL: JSON.stringify(process.env.API_URL ??
|
|
10
|
+
API_URL: JSON.stringify(process.env.API_URL ?? "https://localhost:3000"),
|
|
11
11
|
},
|
|
12
12
|
clean: true,
|
|
13
13
|
bundle: true,
|