@slim-lang/spm 0.0.0-stage → 1.0.0
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/README.md +42 -3
- package/package.json +38 -6
- package/src/api/github_auth.js +89 -0
- package/src/api/github_get.js +244 -0
- package/src/api/github_req.js +281 -0
- package/src/api/lock.js +37 -0
- package/src/api/spm.js +32 -0
- package/src/api/storage.js +30 -0
- package/src/cli.js +599 -0
- package/src/helpers.js +157 -0
- package/src/parsers/spm.js +186 -0
package/README.md
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# slim-spm
|
|
2
|
+
|
|
3
|
+
The package manager of [Slim](https://github.com/cdmtn/slim-lang): installs packages from GitHub into a project, locks their versions and publishes your own.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```console
|
|
8
|
+
npm i -g @slim-lang/spm
|
|
9
|
+
spm --version
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Requires Node.js 22.12+ and Git. The compiler (`@slim-lang/core`) is a separate package and doesn't need spm.
|
|
13
|
+
|
|
14
|
+
## Quick example
|
|
15
|
+
|
|
16
|
+
```console
|
|
17
|
+
spm i std
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
use { positive } from @std/types/numerical
|
|
22
|
+
use { int } from @std/converters
|
|
23
|
+
|
|
24
|
+
let pages: positive = int("12")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`spm i std` downloads the `std` package into `packages/std` and records the exact commit in `spm.lock.json`.
|
|
28
|
+
|
|
29
|
+
## Documentation
|
|
30
|
+
|
|
31
|
+
The full guide is in [`docs/`](docs/index.md):
|
|
32
|
+
|
|
33
|
+
- [Getting started](docs/01-start.md): installing spm and a package, what happens on the way
|
|
34
|
+
- [Commands](docs/02-commands.md): every command and option
|
|
35
|
+
- [Versions and the lockfile](docs/03-versions.md): `spm.lock.json`, `verify`, `--latest`
|
|
36
|
+
- [Publishing](docs/04-publishing.md): the `.spm` file, `create`, `publish`, the registry
|
|
37
|
+
|
|
38
|
+
## Tests
|
|
39
|
+
|
|
40
|
+
```console
|
|
41
|
+
npm test
|
|
42
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@slim-lang/spm",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Slim Package Manager: installs, locks and publishes Slim packages",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/cdmtn/slim-spm.git"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=22.12.0"
|
|
12
|
+
},
|
|
13
|
+
"bin": {
|
|
14
|
+
"spm": "./src/cli.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "node --test test/*.test.mjs",
|
|
22
|
+
"release:patch": "npm version patch && git push --follow-tags && npm publish",
|
|
23
|
+
"release:minor": "npm version minor && git push --follow-tags && npm publish",
|
|
24
|
+
"release:major": "npm version major && git push --follow-tags && npm publish"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"slim",
|
|
28
|
+
"package-manager"
|
|
29
|
+
],
|
|
30
|
+
"license": "ISC",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"chalk": "^6.0.0",
|
|
33
|
+
"commander": "^15.0.0",
|
|
34
|
+
"keytar": "^7.9.0",
|
|
35
|
+
"open": "^11.0.1",
|
|
36
|
+
"tar": "^7.5.22"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { formatBold, formatItalic } from "../helpers.js";
|
|
2
|
+
|
|
3
|
+
const CLIENT_ID = "Iv23lijjcVMtJwfS18dj";
|
|
4
|
+
|
|
5
|
+
const DEVICE_URL = "https://github.com/login/device/code";
|
|
6
|
+
const TOKEN_URL = "https://github.com/login/oauth/access_token";
|
|
7
|
+
|
|
8
|
+
export async function loginGitHub() {
|
|
9
|
+
if (!CLIENT_ID) {
|
|
10
|
+
throw new Error("GITHUB_CLIENT_ID is not configured");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const response = await fetch(DEVICE_URL, {
|
|
14
|
+
method: "POST",
|
|
15
|
+
headers: {
|
|
16
|
+
"Accept": "application/json",
|
|
17
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
18
|
+
},
|
|
19
|
+
body: new URLSearchParams({
|
|
20
|
+
client_id: CLIENT_ID
|
|
21
|
+
})
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (!response.ok) {
|
|
25
|
+
throw new Error("Failed to start GitHub authorization");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const data = await response.json();
|
|
29
|
+
|
|
30
|
+
console.log("\n");
|
|
31
|
+
console.log("Open:", formatItalic.underline(data.verification_uri));
|
|
32
|
+
console.log(`Code: ${formatBold(data.user_code)}`);
|
|
33
|
+
console.log("");
|
|
34
|
+
console.log("Open the link and paste the code into the fields you see. Then, follow the steps");
|
|
35
|
+
|
|
36
|
+
let interval = data.interval || 5;
|
|
37
|
+
|
|
38
|
+
while (true) {
|
|
39
|
+
await sleep(interval * 1000);
|
|
40
|
+
|
|
41
|
+
const tokenResponse = await fetch(TOKEN_URL, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
headers: {
|
|
44
|
+
"Accept": "application/json",
|
|
45
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
46
|
+
},
|
|
47
|
+
body: new URLSearchParams({
|
|
48
|
+
client_id: CLIENT_ID,
|
|
49
|
+
device_code: data.device_code,
|
|
50
|
+
grant_type:
|
|
51
|
+
"urn:ietf:params:oauth:grant-type:device_code"
|
|
52
|
+
})
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const tokenData = await tokenResponse.json();
|
|
56
|
+
|
|
57
|
+
if (tokenData.access_token) {
|
|
58
|
+
return tokenData.access_token;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (tokenData.error === "authorization_pending") {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (tokenData.error === "slow_down") {
|
|
66
|
+
interval += 5;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (tokenData.error === "access_denied") {
|
|
71
|
+
throw new Error("GitHub authorization denied");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
tokenData.error === "expired_token"
|
|
76
|
+
) {
|
|
77
|
+
throw new Error("GitHub authorization expired");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
throw new Error(
|
|
81
|
+
tokenData.error_description ||
|
|
82
|
+
"GitHub authorization failed"
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function sleep(ms) {
|
|
88
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
89
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { GITHUB_API, githubHeaders } from "./github_req.js";
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import { pipeline } from "node:stream/promises";
|
|
7
|
+
import { createWriteStream } from "node:fs";
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import * as tar from 'tar'
|
|
10
|
+
|
|
11
|
+
export async function getGitHubUser(token) {
|
|
12
|
+
const response = await fetch(
|
|
13
|
+
`${GITHUB_API}/user`,
|
|
14
|
+
{
|
|
15
|
+
headers: githubHeaders(token)
|
|
16
|
+
}
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
if (!response.ok) {
|
|
20
|
+
throw new Error("GitHub token is invalid");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return response.json();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function getGitHubRepo(token, repo) {
|
|
27
|
+
const response = await fetch(
|
|
28
|
+
`${GITHUB_API}/repos/${repo}`,
|
|
29
|
+
{
|
|
30
|
+
headers: githubHeaders(token)
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`GitHub repository "${repo}" not found`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return response.json();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function getPermission(token, repo, username) {
|
|
44
|
+
const response = await fetch(
|
|
45
|
+
`${GITHUB_API}/repos/${repo}/collaborators/${username}/permission`,
|
|
46
|
+
{
|
|
47
|
+
headers: githubHeaders(token)
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
"Unable to verify repository permissions"
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return response.json();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const MAX_PACKAGE_BYTES = 200 * 1024 * 1024
|
|
61
|
+
const MAX_PACKAGE_FILES = 20000
|
|
62
|
+
const SAFE_ENTRY_TYPES = new Set(["File", "OldFile", "ContiguousFile", "Directory"])
|
|
63
|
+
|
|
64
|
+
function isInside(root, target) {
|
|
65
|
+
const relative = path.relative(root, target)
|
|
66
|
+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function extractTarGz(file, destination) {
|
|
70
|
+
const root = path.resolve(destination)
|
|
71
|
+
const rejected = []
|
|
72
|
+
let bytes = 0
|
|
73
|
+
let files = 0
|
|
74
|
+
|
|
75
|
+
await tar.x({
|
|
76
|
+
file,
|
|
77
|
+
cwd: root,
|
|
78
|
+
strip: 1,
|
|
79
|
+
strict: true,
|
|
80
|
+
preservePaths: false,
|
|
81
|
+
filter: (entryPath, entry) => {
|
|
82
|
+
const parts = entryPath.split(/[\\/]+/).slice(1)
|
|
83
|
+
const target = path.resolve(root, ...parts)
|
|
84
|
+
if (!SAFE_ENTRY_TYPES.has(entry.type)) {
|
|
85
|
+
rejected.push(`${entryPath} (${entry.type})`)
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
88
|
+
if (parts.includes("..") || path.isAbsolute(entryPath) || !isInside(root, target)) {
|
|
89
|
+
rejected.push(entryPath)
|
|
90
|
+
return false
|
|
91
|
+
}
|
|
92
|
+
bytes += entry.size ?? 0
|
|
93
|
+
files++
|
|
94
|
+
return bytes <= MAX_PACKAGE_BYTES && files <= MAX_PACKAGE_FILES
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
if (bytes > MAX_PACKAGE_BYTES || files > MAX_PACKAGE_FILES) {
|
|
99
|
+
throw new Error(`The package is too large (${files} files, ${Math.round(bytes / 1024 / 1024)} MB)`)
|
|
100
|
+
}
|
|
101
|
+
return rejected
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function listFiles(dir, base = dir, found = []) {
|
|
105
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
106
|
+
const full = path.join(dir, entry.name)
|
|
107
|
+
if (entry.isDirectory()) listFiles(full, base, found)
|
|
108
|
+
else if (entry.isFile()) found.push(path.relative(base, full).split(path.sep).join("/"))
|
|
109
|
+
}
|
|
110
|
+
return found
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function packageIntegrity(dir) {
|
|
114
|
+
const total = createHash("sha256")
|
|
115
|
+
for (const file of listFiles(dir).sort()) {
|
|
116
|
+
const content = createHash("sha256").update(fs.readFileSync(path.join(dir, file))).digest("hex")
|
|
117
|
+
total.update(`${file}\0${content}\n`)
|
|
118
|
+
}
|
|
119
|
+
return `sha256-${total.digest("base64")}`
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function resolveCommit(token, owner, repo, ref) {
|
|
123
|
+
const response = await fetch(
|
|
124
|
+
`${GITHUB_API}/repos/${owner}/${repo}/commits/${encodeURIComponent(ref)}`,
|
|
125
|
+
{
|
|
126
|
+
headers: githubHeaders(token)
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
if (!response.ok) {
|
|
131
|
+
const data = await response.json().catch(() => null);
|
|
132
|
+
const failure = new Error(data?.message ? `${ref}: ${data.message}` : `Can't find ${ref} in ${owner}/${repo}`);
|
|
133
|
+
failure.missing = response.status === 404 || response.status === 422;
|
|
134
|
+
throw failure;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const data = await response.json();
|
|
138
|
+
if (typeof data?.sha !== "string" || !/^[0-9a-f]{40}$/.test(data.sha)) throw new Error(`GitHub returned no commit for ${ref}`);
|
|
139
|
+
return data.sha;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function downloadGitHubRepo(
|
|
143
|
+
token,
|
|
144
|
+
repository,
|
|
145
|
+
destination,
|
|
146
|
+
version = null,
|
|
147
|
+
locked = null,
|
|
148
|
+
{ fallbackToMain = false } = {}
|
|
149
|
+
) {
|
|
150
|
+
const match = repository.match(
|
|
151
|
+
/^([a-zA-Z0-9-]+)\/([a-zA-Z0-9._-]+)$/
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
if (!match) {
|
|
155
|
+
return {
|
|
156
|
+
success: false,
|
|
157
|
+
msg: "Repository must have format user/repo"
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const [, owner, repo] = match;
|
|
162
|
+
const target = path.resolve(destination);
|
|
163
|
+
const staging = `${target}.spm-${process.pid}-${Date.now()}`;
|
|
164
|
+
const tempFile = path.join(
|
|
165
|
+
os.tmpdir(),
|
|
166
|
+
`spm-${owner}-${repo}-${Date.now()}.tar.gz`
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
let untagged = false;
|
|
171
|
+
let commit = locked?.commit && /^[0-9a-f]{40}$/.test(locked.commit) ? locked.commit : null;
|
|
172
|
+
if (!commit && version) {
|
|
173
|
+
try {
|
|
174
|
+
commit = await resolveCommit(token, owner, repo, `v${version}`);
|
|
175
|
+
}
|
|
176
|
+
catch (tagError) {
|
|
177
|
+
if (!tagError.missing || !fallbackToMain) throw tagError;
|
|
178
|
+
untagged = true;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (!commit) commit = await resolveCommit(token, owner, repo, "main");
|
|
182
|
+
|
|
183
|
+
const response = await fetch(
|
|
184
|
+
`${GITHUB_API}/repos/${owner}/${repo}/tarball/${commit}`,
|
|
185
|
+
{
|
|
186
|
+
headers: githubHeaders(token)
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
if (!response.ok) {
|
|
191
|
+
const data = await response.json().catch(() => null);
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
success: false,
|
|
195
|
+
msg: data?.message || `GitHub API error: ${response.status}`
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
await pipeline(response.body, createWriteStream(tempFile));
|
|
200
|
+
await fs.promises.mkdir(staging, { recursive: true });
|
|
201
|
+
const rejected = await extractTarGz(tempFile, staging);
|
|
202
|
+
const integrity = packageIntegrity(staging);
|
|
203
|
+
|
|
204
|
+
if (locked?.integrity && locked.integrity !== integrity) {
|
|
205
|
+
return {
|
|
206
|
+
success: false,
|
|
207
|
+
msg: `Integrity check failed for ${repository}@${commit.slice(0, 7)}: expected ${locked.integrity}, got ${integrity}. The package changed since it was locked; remove it from spm.lock.json to accept the new contents`
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const backup = fs.existsSync(target) ? `${target}.spm-old-${process.pid}-${Date.now()}` : null;
|
|
212
|
+
if (backup) await fs.promises.rename(target, backup);
|
|
213
|
+
try {
|
|
214
|
+
await fs.promises.rename(staging, target);
|
|
215
|
+
}
|
|
216
|
+
catch (swapError) {
|
|
217
|
+
if (backup) await fs.promises.rename(backup, target).catch(() => {});
|
|
218
|
+
throw swapError;
|
|
219
|
+
}
|
|
220
|
+
if (backup) await fs.promises.rm(backup, { recursive: true, force: true }).catch(() => {});
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
success: true,
|
|
224
|
+
msg: `Repository ${repository} downloaded`,
|
|
225
|
+
path: target,
|
|
226
|
+
commit,
|
|
227
|
+
integrity,
|
|
228
|
+
rejected,
|
|
229
|
+
untagged
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
return {
|
|
234
|
+
success: false,
|
|
235
|
+
msg: error instanceof Error
|
|
236
|
+
? error.message
|
|
237
|
+
: String(error)
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
finally {
|
|
241
|
+
await fs.promises.rm(tempFile, { force: true });
|
|
242
|
+
await fs.promises.rm(staging, { recursive: true, force: true });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export const GITHUB_API = "https://api.github.com";
|
|
5
|
+
export const API_VERSION = "2026-03-10";
|
|
6
|
+
|
|
7
|
+
export function githubHeaders(token, extra = {}) {
|
|
8
|
+
return {
|
|
9
|
+
Accept: "application/vnd.github+json",
|
|
10
|
+
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
11
|
+
"X-GitHub-Api-Version": API_VERSION,
|
|
12
|
+
...extra
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function githubRequest(token, url, options = {}) {
|
|
17
|
+
const response = await fetch(`${GITHUB_API}${url}`, {
|
|
18
|
+
...options,
|
|
19
|
+
headers: githubHeaders(token, {
|
|
20
|
+
"Content-Type": "application/json",
|
|
21
|
+
...options.headers
|
|
22
|
+
})
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const data = response.status === 204 ? null : await response.json().catch(() => null);
|
|
26
|
+
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new Error(data?.message || `GitHub API error: ${response.status}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return data;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function getFiles(dir, base = dir) {
|
|
35
|
+
const files = [];
|
|
36
|
+
|
|
37
|
+
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
|
|
38
|
+
const fullPath = path.join(dir, entry.name);
|
|
39
|
+
|
|
40
|
+
if (entry.isDirectory()) files.push(...await getFiles(fullPath, base));
|
|
41
|
+
else if (entry.isFile()) files.push({ path: path.relative(base, fullPath).replaceAll("\\", "/"), fullPath });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return files;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function commitDirectory({ token, repo, dir, message, branch = "main" }) {
|
|
48
|
+
const files = await getFiles(dir);
|
|
49
|
+
if (files.length === 0) throw new Error("Package directory is empty");
|
|
50
|
+
|
|
51
|
+
const reference = await githubRequest(token, `/repos/${repo}/git/ref/heads/${branch}`);
|
|
52
|
+
const parentSha = reference.object.sha;
|
|
53
|
+
const parentCommit = await githubRequest(token, `/repos/${repo}/git/commits/${parentSha}`);
|
|
54
|
+
|
|
55
|
+
const tree = [];
|
|
56
|
+
for (const file of files) {
|
|
57
|
+
const blob = await githubRequest(token, `/repos/${repo}/git/blobs`, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
body: JSON.stringify({ content: await fs.readFile(file.fullPath, "base64"), encoding: "base64" })
|
|
60
|
+
});
|
|
61
|
+
tree.push({ path: file.path, mode: "100644", type: "blob", sha: blob.sha });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const newTree = await githubRequest(token, `/repos/${repo}/git/trees`, {
|
|
65
|
+
method: "POST",
|
|
66
|
+
body: JSON.stringify({ base_tree: parentCommit.tree.sha, tree })
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const commit = await githubRequest(token, `/repos/${repo}/git/commits`, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
body: JSON.stringify({ message, tree: newTree.sha, parents: [parentSha] })
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
await githubRequest(token, `/repos/${repo}/git/refs/heads/${branch}`, {
|
|
75
|
+
method: "PATCH",
|
|
76
|
+
body: JSON.stringify({ sha: commit.sha })
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return { commit: commit.sha, tree: newTree.sha };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function publishToGitHub({ token, repo, contentPath, branch = "main" }) {
|
|
83
|
+
if (!/^[^/]+\/[^/]+$/.test(repo)) {
|
|
84
|
+
return { success: false, msg: `Invalid GitHub repository: ${repo}` };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
const repository = await githubRequest(token, `/repos/${repo}`);
|
|
89
|
+
const { commit } = await commitDirectory({ token, repo, dir: contentPath, message: "Publish package", branch });
|
|
90
|
+
|
|
91
|
+
return { success: true, msg: `Published ${repo}`, repo: repository.full_name, commit };
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
return { success: false, msg: error instanceof Error ? error.message : String(error) };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function createGitHubTag(token, repo, version, commit) {
|
|
99
|
+
try {
|
|
100
|
+
await githubRequest(token, `/repos/${repo}/git/refs`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
body: JSON.stringify({ ref: `refs/tags/v${version}`, sha: commit })
|
|
103
|
+
});
|
|
104
|
+
return { success: true, tag: `v${version}` };
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
return { success: false, msg: error instanceof Error ? error.message : String(error) };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function updateGitHubRepo({ token, repo, packagePath, version, message = `Release ${version}` }) {
|
|
112
|
+
try {
|
|
113
|
+
const { commit, tree } = await commitDirectory({ token, repo, dir: packagePath, message });
|
|
114
|
+
|
|
115
|
+
await githubRequest(token, `/repos/${repo}/git/refs`, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
body: JSON.stringify({ ref: `refs/tags/v${version}`, sha: commit })
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return { success: true, commit, tree, version, tag: `v${version}` };
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
return { success: false, msg: error instanceof Error ? error.message : String(error) };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function createGitHubRepo(token, repository, organization = false) {
|
|
128
|
+
const match = repository.match(
|
|
129
|
+
/^([a-zA-Z0-9-]+)\/([a-zA-Z0-9._-]+)$/
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
if (!match) {
|
|
133
|
+
return {
|
|
134
|
+
success: false,
|
|
135
|
+
msg: "Repository must have format user/repo"
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const [, owner, repo] = match;
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
const check = await fetch(
|
|
143
|
+
`${GITHUB_API}/repos/${owner}/${repo}`,
|
|
144
|
+
{
|
|
145
|
+
headers: githubHeaders(token)
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
if (check.ok) {
|
|
150
|
+
return {
|
|
151
|
+
success: false,
|
|
152
|
+
msg: `Repository ${repository} already exists`
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (check.status !== 404) {
|
|
157
|
+
const data = await check.json();
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
success: false,
|
|
161
|
+
msg: data.message || `GitHub API error: ${check.status}`
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!organization) {
|
|
166
|
+
const userResponse = await fetch(
|
|
167
|
+
`${GITHUB_API}/user`,
|
|
168
|
+
{
|
|
169
|
+
headers: githubHeaders(token)
|
|
170
|
+
}
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
if (!userResponse.ok) {
|
|
174
|
+
const data = await userResponse.json();
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
success: false,
|
|
178
|
+
msg: data.message || "Failed to get GitHub user"
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const user = await userResponse.json();
|
|
183
|
+
|
|
184
|
+
if (user.login.toLowerCase() !== owner.toLowerCase()) {
|
|
185
|
+
return {
|
|
186
|
+
success: false,
|
|
187
|
+
msg: `You cannot create ${repository}. Your GitHub account is ${user.login}`
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const createUrl = organization
|
|
193
|
+
? `${GITHUB_API}/orgs/${owner}/repos`
|
|
194
|
+
: `${GITHUB_API}/user/repos`;
|
|
195
|
+
|
|
196
|
+
const response = await fetch(
|
|
197
|
+
createUrl,
|
|
198
|
+
{
|
|
199
|
+
method: "POST",
|
|
200
|
+
headers: githubHeaders(token, {
|
|
201
|
+
"Content-Type": "application/json"
|
|
202
|
+
}),
|
|
203
|
+
body: JSON.stringify({
|
|
204
|
+
name: repo,
|
|
205
|
+
private: false,
|
|
206
|
+
auto_init: true
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const data = await response.json();
|
|
212
|
+
|
|
213
|
+
if (!response.ok) {
|
|
214
|
+
return {
|
|
215
|
+
success: false,
|
|
216
|
+
msg: data.message || `GitHub API error: ${response.status}`
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
success: true,
|
|
222
|
+
msg: `Repository ${repository} created`,
|
|
223
|
+
repo: data
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
return {
|
|
228
|
+
success: false,
|
|
229
|
+
msg: error instanceof Error
|
|
230
|
+
? error.message
|
|
231
|
+
: String(error)
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export async function deleteGitHubRepo(token, repository) {
|
|
237
|
+
const match = repository.match(
|
|
238
|
+
/^([a-zA-Z0-9-]+)\/([a-zA-Z0-9._-]+)$/
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
if (!match) {
|
|
242
|
+
return {
|
|
243
|
+
success: false,
|
|
244
|
+
msg: "Repository must have format user/repo"
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const [, owner, repo] = match;
|
|
249
|
+
|
|
250
|
+
try {
|
|
251
|
+
const response = await fetch(
|
|
252
|
+
`${GITHUB_API}/repos/${owner}/${repo}`,
|
|
253
|
+
{
|
|
254
|
+
method: "DELETE",
|
|
255
|
+
headers: githubHeaders(token)
|
|
256
|
+
}
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
if (response.status === 204) {
|
|
260
|
+
return {
|
|
261
|
+
success: true,
|
|
262
|
+
msg: `Repository ${repository} deleted`
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const data = await response.json().catch(() => null);
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
success: false,
|
|
270
|
+
msg: data?.message || `GitHub API error: ${response.status}`
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
return {
|
|
275
|
+
success: false,
|
|
276
|
+
msg: error instanceof Error
|
|
277
|
+
? error.message
|
|
278
|
+
: String(error)
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
}
|