pkg-pr-new 0.0.2

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.
@@ -0,0 +1,39 @@
1
+ // https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
2
+ declare global {
3
+ namespace NodeJS {
4
+ interface ProcessEnv {
5
+ // Always set to true when GitHub Actions is running the workflow. You can use this variable to differentiate when tests are being run locally or by GitHub Actions.
6
+ GITHUB_ACTIONS?: "true";
7
+ // The short ref name of the branch or tag that triggered the workflow run. This value matches the branch or tag name shown on GitHub. For example, feature-branch-1.
8
+ // For pull requests, the format is <pr_number>/merge.
9
+ GITHUB_REF_NAME: string;
10
+ // The name of the base ref or target branch of the pull request in a workflow run. This is only set when the event that triggers a workflow run is either pull_request or pull_request_target. For example, main.
11
+ GITHUB_BASE_REF: string;
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
+ GITHUB_HEAD_REF: string;
14
+ // The URL of the GitHub server. For example: https://github.com.
15
+ GITHUB_SERVER_URL: string
16
+ // The owner and repository name. For example, octocat/Hello-World.
17
+ GITHUB_REPOSITORY: string;
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
+ GITHUB_SHA: string;
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
22
+ // The name of the person or app that initiated the workflow. For example, octocat.
23
+ GITHUB_ACTOR: string;
24
+ // The path to the file on the runner that contains the full event webhook payload. For example, /github/workflow/event.json.
25
+ GITHUB_EVENT_PATH: string;
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
+ GITHUB_RUN_ID: string;
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
30
+ }
31
+ }
32
+ }
33
+
34
+ // npm i https://pkg.sb.dev/dai-shi/waku/feature-a/54a65813/waku # stackblitz
35
+ // npm i https://pkg.sb.dev/dai-shi/waku/feature-a/waku # stackblitz
36
+ // npm i https://pkg.sb.dev/{GITHUB_REPOSITORY}/{GITHUB_REF_NAME}/{GITHUB_SHA}/{published-package} # stackblitz
37
+ // npm i https://pkg.sb.dev/54a6581354a65813 # short-form stackblitz
38
+
39
+ export {};
package/index.ts ADDED
@@ -0,0 +1,124 @@
1
+ import { defineCommand, runMain } from "citty";
2
+ import assert from "node:assert";
3
+ import path from "path";
4
+ import ezSpawn from "@jsdevtools/ez-spawn";
5
+ // import { createRequire } from "module";
6
+ import { hash } from "ohash";
7
+ import fs from "fs/promises";
8
+ import { Octokit } from "@octokit/action";
9
+ import { pathToFileURL } from "node:url";
10
+ 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
+ );
18
+
19
+ declare global {
20
+ var API_URL: string;
21
+ }
22
+
23
+ const publishUrl = new URL("/publish", API_URL);
24
+
25
+ if (!process.env.TEST && process.env.GITHUB_ACTIONS !== "true") {
26
+ console.error("Continuous Releases are only available in Github Actions.");
27
+ process.exit(1);
28
+ }
29
+ const octokit = new Octokit();
30
+
31
+ const {
32
+ GITHUB_SERVER_URL,
33
+ GITHUB_REPOSITORY,
34
+ GITHUB_RUN_ID,
35
+ GITHUB_RUN_ATTEMPT,
36
+ GITHUB_ACTOR_ID,
37
+ GITHUB_REF_NAME,
38
+ GITHUB_SHA,
39
+ } = process.env;
40
+
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
+ const [owner, repo] = GITHUB_REPOSITORY.split("/");
49
+
50
+ const commit = await octokit.git.getCommit({
51
+ owner,
52
+ repo,
53
+ commit_sha: GITHUB_SHA,
54
+ });
55
+
56
+ const commitTimestamp = Date.parse(commit.data.committer.date);
57
+
58
+ // Note: If you need to use a workflow run's URL from within a job, you can combine these variables: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
59
+ const url = `${GITHUB_SERVER_URL}/${owner}/${repo}/actions/runs/${GITHUB_RUN_ID}`;
60
+
61
+ const metadata = {
62
+ url,
63
+ attempt: Number(GITHUB_RUN_ATTEMPT),
64
+ actor: Number(GITHUB_ACTOR_ID),
65
+ };
66
+
67
+ const key = hash(metadata);
68
+
69
+ const main = defineCommand({
70
+ meta: {
71
+ version,
72
+ name: "stackblitz",
73
+ description: "A CLI for pkg.pr.new (Continuous Releases)",
74
+ },
75
+ subCommands: {
76
+ publish: () => {
77
+ return {
78
+ meta: {},
79
+ run: async () => {
80
+ await ezSpawn.async("npm pack", { stdio: "inherit" });
81
+
82
+ const file = await fs.readFile(`${name}-${version}.tgz`);
83
+ console.log('headers', {
84
+ headers: {
85
+ "sb-key": key,
86
+ "sb-package-name": name,
87
+ "sb-package-version": version,
88
+ "sb-commit-timestamp": commitTimestamp.toString(),
89
+ },
90
+ })
91
+
92
+ const res = await fetch(publishUrl, {
93
+ method: "POST",
94
+ headers: {
95
+ "sb-key": key,
96
+ "sb-package-name": name,
97
+ "sb-package-version": version,
98
+ "sb-commit-timestamp": commitTimestamp.toString(),
99
+ },
100
+ body: file,
101
+ });
102
+ const laterRes = res.clone();
103
+ assert.equal(
104
+ res.status,
105
+ 200,
106
+ `publishing failed: ${await res.text()}`,
107
+ );
108
+
109
+ console.log(
110
+ `⚡️ Your npm package is published: \`npm i ${(await laterRes.json()).url}\``,
111
+ );
112
+ },
113
+ };
114
+ },
115
+ link: () => {
116
+ return {
117
+ meta: {},
118
+ run: () => {},
119
+ };
120
+ },
121
+ },
122
+ });
123
+
124
+ runMain(main);
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "pkg-pr-new",
3
+ "version": "0.0.2",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "bin": {
8
+ "pkg-pr-new": "bin/cli.js"
9
+ },
10
+ "scripts": {
11
+ "dev": "tsup --watch",
12
+ "build": "tsup",
13
+ "build:publish": "API_URL=https://pkg.pr.new/ tsup"
14
+ },
15
+ "keywords": [],
16
+ "author": "",
17
+ "license": "ISC",
18
+ "dependencies": {
19
+ "@jsdevtools/ez-spawn": "^3.0.4",
20
+ "@octokit/action": "^6.0.7"
21
+ },
22
+ "devDependencies": {
23
+ "citty": "^0.1.6",
24
+ "tsup": "^8.0.2"
25
+ }
26
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ // https://nitro.unjs.io/guide/typescript
2
+ {
3
+ "compilerOptions": {
4
+ "strict": true,
5
+ "skipLibCheck": true
6
+ },
7
+ "extends": "../backend/.nitro/types/tsconfig.json"
8
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["index.ts"],
5
+ format: "esm",
6
+ minify: false,
7
+ splitting: false,
8
+ sourcemap: "inline",
9
+ define: {
10
+ API_URL: JSON.stringify(process.env.API_URL ?? 'https://localhost:3000')
11
+ },
12
+ clean: true,
13
+ bundle: true,
14
+ dts: false,
15
+ });