@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/src/api/lock.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { readJson, rootPath } from "../helpers.js"
|
|
4
|
+
|
|
5
|
+
export function lockFile(dir = rootPath) {
|
|
6
|
+
return path.join(dir, "spm.lock.json")
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function readLock(dir = rootPath) {
|
|
10
|
+
try {
|
|
11
|
+
const data = readJson(lockFile(dir))
|
|
12
|
+
if (!data.packages || typeof data.packages !== "object") data.packages = {}
|
|
13
|
+
return data
|
|
14
|
+
} catch {
|
|
15
|
+
return { packages: {} }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function writeLock(data, dir = rootPath) {
|
|
20
|
+
fs.writeFileSync(lockFile(dir), JSON.stringify(data, null, 4) + "\n", "utf8")
|
|
21
|
+
return data
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function setLockEntry(name, entry, dir = rootPath) {
|
|
25
|
+
const lock = readLock(dir)
|
|
26
|
+
lock.packages[name] = entry
|
|
27
|
+
return writeLock(lock, dir)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function removeLockEntry(name, dir = rootPath) {
|
|
31
|
+
const lock = readLock(dir)
|
|
32
|
+
if (name in lock.packages) {
|
|
33
|
+
delete lock.packages[name]
|
|
34
|
+
writeLock(lock, dir)
|
|
35
|
+
}
|
|
36
|
+
return lock
|
|
37
|
+
}
|
package/src/api/spm.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const API_URL = "https://spm.yurba.one"
|
|
2
|
+
|
|
3
|
+
async function request(endpoint, body) {
|
|
4
|
+
const res = await fetch(`${API_URL}${endpoint}`, body === undefined
|
|
5
|
+
? { method: "GET", headers: { "Content-Type": "application/json" } }
|
|
6
|
+
: { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) })
|
|
7
|
+
|
|
8
|
+
let data
|
|
9
|
+
try {
|
|
10
|
+
data = await res.json()
|
|
11
|
+
} catch {
|
|
12
|
+
return { success: false, content: `SPM registry returned HTTP ${res.status}` }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return { success: !!data.success, content: data.result }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function publishPackage({ token, name, version, github, description = "" }) {
|
|
19
|
+
return request("/publish", { github_token: token, name, description, version, github })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function getPackage({ name }) {
|
|
23
|
+
return request(`/install?name=${encodeURIComponent(name)}`)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function updatePackage({ token, name, description, version, github }) {
|
|
27
|
+
return request("/update", { github_token: token, name, description, version, github })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function removePackage({ token, name }) {
|
|
31
|
+
return request("/remove", { github_token: token, name })
|
|
32
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import keytar from "keytar";
|
|
2
|
+
|
|
3
|
+
const SERVICE = "slim-package-manager";
|
|
4
|
+
const ACCOUNT = "github";
|
|
5
|
+
|
|
6
|
+
export async function saveGitHubToken(token) {
|
|
7
|
+
if (!token) {
|
|
8
|
+
throw new Error("GitHub token is empty");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
await keytar.setPassword(
|
|
12
|
+
SERVICE,
|
|
13
|
+
ACCOUNT,
|
|
14
|
+
token
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function getGitHubToken() {
|
|
19
|
+
return await keytar.getPassword(
|
|
20
|
+
SERVICE,
|
|
21
|
+
ACCOUNT
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function deleteGitHubToken() {
|
|
26
|
+
return await keytar.deletePassword(
|
|
27
|
+
SERVICE,
|
|
28
|
+
ACCOUNT
|
|
29
|
+
);
|
|
30
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import { formatError, formatBold, formatItalic, formatSuccess, createFolder, createFile } from "./helpers.js"
|
|
5
|
+
import pkg from "../package.json" with { type: "json" };
|
|
6
|
+
import { log, isPackageExists, isValidPackageName, rootPath, deleteDirectory, loading, delay, projectPackagesDir } from "./helpers.js";
|
|
7
|
+
|
|
8
|
+
import fs from "node:fs"
|
|
9
|
+
import path from "node:path"
|
|
10
|
+
import { getSPM } from "./parsers/spm.js";
|
|
11
|
+
import { loginGitHub } from "./api/github_auth.js";
|
|
12
|
+
import { deleteGitHubToken, getGitHubToken, saveGitHubToken } from "./api/storage.js";
|
|
13
|
+
import { downloadGitHubRepo, getGitHubUser, packageIntegrity } from "./api/github_get.js";
|
|
14
|
+
|
|
15
|
+
import { getPackage, publishPackage, removePackage, updatePackage } from "./api/spm.js"
|
|
16
|
+
import { readLock, setLockEntry, removeLockEntry } from "./api/lock.js"
|
|
17
|
+
import { createGitHubRepo, createGitHubTag, deleteGitHubRepo, githubHeaders, publishToGitHub, updateGitHubRepo } from "./api/github_req.js";
|
|
18
|
+
|
|
19
|
+
import open from "open";
|
|
20
|
+
|
|
21
|
+
export const spm = new Command();
|
|
22
|
+
|
|
23
|
+
function spmlog(...args) {
|
|
24
|
+
console.log("[SPM]", ...args)
|
|
25
|
+
}
|
|
26
|
+
function errlog(...args) {
|
|
27
|
+
console.log(formatError(...args))
|
|
28
|
+
process.exitCode = 1
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const packagesPath = projectPackagesDir()
|
|
32
|
+
|
|
33
|
+
function packageDir(name) {
|
|
34
|
+
if (!isValidPackageName(name)) {
|
|
35
|
+
throw new Error(`"${name}" is not a valid package name: use letters, digits, ".", "_" and "-"`)
|
|
36
|
+
}
|
|
37
|
+
return path.join(packagesPath, name)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function installPackage(name, { version = null, latest = false } = {}) {
|
|
41
|
+
const target = packageDir(name)
|
|
42
|
+
const getPackageReq = await getPackage({ name: name })
|
|
43
|
+
|
|
44
|
+
if (!getPackageReq.success) {
|
|
45
|
+
return { success: false, msg: `SPM Install: ${getPackageReq.content}` }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const githubToken = await getGitHubToken()
|
|
49
|
+
const data = getPackageReq.content
|
|
50
|
+
const repo = data.repo
|
|
51
|
+
const resolvedVersion = version ?? data.latest
|
|
52
|
+
const entry = readLock().packages[name]
|
|
53
|
+
const locked = !latest && entry && entry.repo === repo && (version === null || entry.version === version) ? entry : null
|
|
54
|
+
const wanted = locked ? locked.version : resolvedVersion
|
|
55
|
+
|
|
56
|
+
console.log(`\nDownloading ${repo}${locked?.commit ? ` @ ${locked.commit.slice(0, 7)} (locked)` : ""}...`)
|
|
57
|
+
|
|
58
|
+
const result = await downloadGitHubRepo(githubToken, repo, target, wanted ?? null, locked, { fallbackToMain: !locked && version === null })
|
|
59
|
+
if (!result.success) return result
|
|
60
|
+
|
|
61
|
+
if (result.untagged) {
|
|
62
|
+
spmlog(`${repo} has no tag v${wanted}, installed the newest commit of main instead`)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (result.rejected?.length) {
|
|
66
|
+
spmlog(`Skipped ${result.rejected.length} unsafe entr${result.rejected.length === 1 ? "y" : "ies"}: ${result.rejected.slice(0, 5).join(", ")}`)
|
|
67
|
+
}
|
|
68
|
+
setLockEntry(name, { version: wanted, repo, commit: result.commit, integrity: result.integrity })
|
|
69
|
+
return { success: true, msg: `Package @${name}:${wanted} installed (${result.commit.slice(0, 7)})` }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function renderObject(obj, indent = 0) {
|
|
73
|
+
let output = "";
|
|
74
|
+
const prefix = " ".repeat(indent);
|
|
75
|
+
|
|
76
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
77
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
78
|
+
output += `\n${formatItalic(prefix)}${formatBold(key)}:`;
|
|
79
|
+
output += renderObject(value, indent + 2);
|
|
80
|
+
} else {
|
|
81
|
+
output += `\n${formatItalic(prefix)}${formatBold(key)}: ${value}`;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return output;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function renderBlock({ name, content }) {
|
|
89
|
+
function generateSepLines(len) {
|
|
90
|
+
let line = `-------------------------------------`
|
|
91
|
+
|
|
92
|
+
if (len) {
|
|
93
|
+
for (let i = 0; i < len; i++) {
|
|
94
|
+
line += "-"
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return line
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const nameLen = name.length
|
|
102
|
+
|
|
103
|
+
return `${formatSuccess(name)} ${generateSepLines()}` +
|
|
104
|
+
content +
|
|
105
|
+
`\n${generateSepLines(nameLen + 1)}`
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
spm
|
|
109
|
+
.name("spm")
|
|
110
|
+
.description(`Slim Package Manager (SPM ${pkg.version})`)
|
|
111
|
+
.version(pkg.version);
|
|
112
|
+
|
|
113
|
+
spm
|
|
114
|
+
.command("list")
|
|
115
|
+
.action(() => {
|
|
116
|
+
const targetDir = packagesPath
|
|
117
|
+
if (!fs.existsSync(targetDir)) {
|
|
118
|
+
spmlog("No packages installed (the packages/ directory does not exist)")
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const relativePaths = fs.readdirSync(targetDir, { withFileTypes: true })
|
|
123
|
+
.filter(entry => entry.isDirectory() && !/\.spm-(old-)?\d+-\d+$/.test(entry.name))
|
|
124
|
+
.map(entry => entry.name);
|
|
125
|
+
|
|
126
|
+
if (relativePaths.length === 0) {
|
|
127
|
+
spmlog("No packages installed")
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getPkgPath(pkg) {
|
|
132
|
+
const relative = path.relative(process.cwd(), path.join(targetDir, pkg))
|
|
133
|
+
return relative
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
spmlog(`List of all installed packages:\n${relativePaths.map(item => "- @" + item + ` (${getPkgPath(item)})`).join(",\n")}`)
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
spm
|
|
140
|
+
.command("lock")
|
|
141
|
+
.description("Show resolved package versions from spm.lock.json")
|
|
142
|
+
.action(() => {
|
|
143
|
+
const { packages } = readLock()
|
|
144
|
+
const names = Object.keys(packages)
|
|
145
|
+
|
|
146
|
+
if (names.length === 0) {
|
|
147
|
+
spmlog("No locked packages (spm.lock.json is empty or missing)")
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
spmlog(`Locked packages:\n${names.map(name => `- @${name}:${packages[name].version} (${packages[name].repo}${packages[name].commit ? ` @ ${packages[name].commit.slice(0, 7)}` : ""})`).join("\n")}`)
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
spm
|
|
155
|
+
.command("get")
|
|
156
|
+
.argument("<name>")
|
|
157
|
+
.action(async (name) => {
|
|
158
|
+
let msg = null;
|
|
159
|
+
|
|
160
|
+
if (isPackageExists(name)) {
|
|
161
|
+
const spmFileContent = await getSPM(name);
|
|
162
|
+
|
|
163
|
+
if (!spmFileContent.success) {
|
|
164
|
+
errlog(spmFileContent.content);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const SPMContent = spmFileContent.content;
|
|
169
|
+
let packageName = "@" + name
|
|
170
|
+
|
|
171
|
+
const infoFromRegistryReq = await getPackage({ name: name })
|
|
172
|
+
|
|
173
|
+
if(infoFromRegistryReq.success) {
|
|
174
|
+
packageName = `${packageName} (${infoFromRegistryReq.content.latest})`
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
msg = renderBlock({
|
|
178
|
+
name: packageName,
|
|
179
|
+
content: renderObject(SPMContent)
|
|
180
|
+
})
|
|
181
|
+
} else {
|
|
182
|
+
msg =
|
|
183
|
+
formatError(`@${name} not installed\n`) +
|
|
184
|
+
`Want to install? Try ${formatItalic(`${spm.name()} i ${name}`)}`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
spmlog(msg);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
spm
|
|
191
|
+
.command("rm")
|
|
192
|
+
.argument('<name>')
|
|
193
|
+
.option("--g", "Removes the project from the project registry and GitHub")
|
|
194
|
+
.action(async (name, opt) => {
|
|
195
|
+
await loading({
|
|
196
|
+
startMsg: `Removing @${name}...`,
|
|
197
|
+
callback: async ({ fail, ok }) => {
|
|
198
|
+
const isGlobally = opt.g == undefined ? false : true
|
|
199
|
+
|
|
200
|
+
if (isGlobally) {
|
|
201
|
+
const spmFileContent = await getSPM(name);
|
|
202
|
+
|
|
203
|
+
if (!spmFileContent.success) {
|
|
204
|
+
return fail("SPM Check:", spmFileContent.content);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const SPMContent = spmFileContent.content;
|
|
208
|
+
const githubRepo = SPMContent.github?.repo
|
|
209
|
+
|
|
210
|
+
if (!githubRepo) {
|
|
211
|
+
return fail("SPM Check: the .spm file has no @github repo")
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const githubToken = await getGitHubToken()
|
|
215
|
+
|
|
216
|
+
const removePackageRegReq = await removePackage({
|
|
217
|
+
token: githubToken,
|
|
218
|
+
name: name
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
if (!removePackageRegReq.success) {
|
|
222
|
+
return fail("SPM Registry remove:", removePackageRegReq.content)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const removeFromGithubReq = await deleteGitHubRepo(githubToken, githubRepo)
|
|
226
|
+
|
|
227
|
+
if (!removeFromGithubReq.success) {
|
|
228
|
+
return fail(`${githubRepo} failed to remove from Github`)
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
return ok(`${githubRepo} removed from Github and registry`)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
let res = false
|
|
236
|
+
try {
|
|
237
|
+
const dir = packageDir(name)
|
|
238
|
+
if (!fs.existsSync(dir)) {
|
|
239
|
+
if (name in readLock().packages) removeLockEntry(name)
|
|
240
|
+
return fail(`@${name} is not installed`)
|
|
241
|
+
}
|
|
242
|
+
res = await deleteDirectory(dir)
|
|
243
|
+
} catch (error) {
|
|
244
|
+
return fail(error.message)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (res) {
|
|
248
|
+
removeLockEntry(name)
|
|
249
|
+
return ok(`package @${name} successfully removed locally`)
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
return fail(`Failed to remove the @${name} package`)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
})
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
spm
|
|
259
|
+
.command("i")
|
|
260
|
+
.argument("[name]")
|
|
261
|
+
.description("Install a package, or every package in spm.lock.json when no name is given")
|
|
262
|
+
.option("--ver <version>", "Download a specific version")
|
|
263
|
+
.option("--latest", "Ignore the locked commit and take the newest one")
|
|
264
|
+
.action(async (name, opt) => {
|
|
265
|
+
const names = name ? [name] : Object.keys(readLock().packages)
|
|
266
|
+
if (names.length === 0) {
|
|
267
|
+
spmlog("Nothing to install: spm.lock.json is empty or missing")
|
|
268
|
+
return
|
|
269
|
+
}
|
|
270
|
+
for (const packageName of names) {
|
|
271
|
+
await loading({
|
|
272
|
+
startMsg: `Installing @${packageName}...`,
|
|
273
|
+
callback: async ({ fail, ok }) => {
|
|
274
|
+
try {
|
|
275
|
+
const result = await installPackage(packageName, { version: name ? opt.ver ?? null : null, latest: !!opt.latest })
|
|
276
|
+
return result.success ? ok(result.msg) : fail(result.msg)
|
|
277
|
+
} catch (error) {
|
|
278
|
+
return fail(error.message)
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
spm
|
|
286
|
+
.command("verify")
|
|
287
|
+
.description("Check that installed packages match spm.lock.json")
|
|
288
|
+
.action(() => {
|
|
289
|
+
const { packages } = readLock()
|
|
290
|
+
const names = Object.keys(packages)
|
|
291
|
+
if (names.length === 0) {
|
|
292
|
+
spmlog("No locked packages (spm.lock.json is empty or missing)")
|
|
293
|
+
return
|
|
294
|
+
}
|
|
295
|
+
let broken = 0
|
|
296
|
+
for (const name of names) {
|
|
297
|
+
const dir = packageDir(name)
|
|
298
|
+
const expected = packages[name].integrity
|
|
299
|
+
if (!fs.existsSync(dir)) {
|
|
300
|
+
broken++
|
|
301
|
+
errlog(`@${name}: not installed`)
|
|
302
|
+
} else if (!expected) {
|
|
303
|
+
spmlog(`@${name}: no integrity in spm.lock.json, reinstall it to record one`)
|
|
304
|
+
} else if (packageIntegrity(dir) !== expected) {
|
|
305
|
+
broken++
|
|
306
|
+
errlog(`@${name}: files differ from spm.lock.json`)
|
|
307
|
+
} else {
|
|
308
|
+
spmlog(`@${name}: ok`)
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (broken > 0) process.exitCode = 1
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
spm
|
|
315
|
+
.command("create")
|
|
316
|
+
.argument("<name>")
|
|
317
|
+
|
|
318
|
+
.option("--github", "Create repository on Github")
|
|
319
|
+
.option("--local", "Create minimum package template in packages")
|
|
320
|
+
|
|
321
|
+
.option("--github-repo <repoName>", "Fill in '@github/repo' in the configuration")
|
|
322
|
+
.option("--description <description>", "Fill in 'description' in the configuration")
|
|
323
|
+
.option("--ver <version>", "Fill in 'version' in the configuration")
|
|
324
|
+
|
|
325
|
+
.action(async (name, opt) => {
|
|
326
|
+
const defaultGithubRepo = opt.githubRepo == undefined ? "username/repo" : opt.githubRepo
|
|
327
|
+
const defaultDescription = opt.description == undefined ? "My first Slim package" : opt.description
|
|
328
|
+
const defaultVersion = opt.ver == undefined ? "1.0.0" : opt.ver
|
|
329
|
+
|
|
330
|
+
if (opt.local) {
|
|
331
|
+
const isExists = isPackageExists(name)
|
|
332
|
+
|
|
333
|
+
await loading({
|
|
334
|
+
startMsg: `Creating path for ${name}`,
|
|
335
|
+
callback: async ({ fail, ok }) => {
|
|
336
|
+
const packagePath = packageDir(name)
|
|
337
|
+
const unsafe = [defaultDescription, defaultVersion, defaultGithubRepo].find(value => /["\r\n]/.test(String(value)))
|
|
338
|
+
|
|
339
|
+
if (unsafe !== undefined) return fail(`Values cannot contain quotes or line breaks: ${unsafe}`)
|
|
340
|
+
if (isExists) return fail("Package is already exists")
|
|
341
|
+
else {
|
|
342
|
+
const createFolderReq = await createFolder(packagePath)
|
|
343
|
+
|
|
344
|
+
if (!createFolderReq.success) {
|
|
345
|
+
return fail(createFolderReq.content)
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
const createSPMReq = createFile(path.join(packagePath, ".spm"), `
|
|
349
|
+
name = "${name}"
|
|
350
|
+
description = "${defaultDescription}"
|
|
351
|
+
version = "${defaultVersion}"
|
|
352
|
+
|
|
353
|
+
@ github
|
|
354
|
+
repo = "${defaultGithubRepo}"
|
|
355
|
+
`.trim())
|
|
356
|
+
|
|
357
|
+
if (!createSPMReq.success) {
|
|
358
|
+
return fail(createSPMReq.content)
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
return ok("Local package created")
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
}
|
|
368
|
+
else if (opt.github || opt.githubRepo) {
|
|
369
|
+
const spmFileContent = await getSPM(name);
|
|
370
|
+
|
|
371
|
+
if (!spmFileContent.success) {
|
|
372
|
+
spmlog(formatError(spmFileContent.content));
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const SPMContent = spmFileContent.content;
|
|
377
|
+
const packageName = "@" + name;
|
|
378
|
+
const githubRepo = SPMContent.github?.repo
|
|
379
|
+
const organization = SPMContent.github?.organization === true
|
|
380
|
+
|
|
381
|
+
if (!githubRepo) {
|
|
382
|
+
errlog("The .spm file has no @github repo");
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
await loading({
|
|
387
|
+
startMsg: `Creating repository for ${packageName}`,
|
|
388
|
+
callback: async ({ fail, ok }) => {
|
|
389
|
+
const githubToken = await getGitHubToken();
|
|
390
|
+
|
|
391
|
+
const createRepoReq = await createGitHubRepo(githubToken, githubRepo, organization)
|
|
392
|
+
|
|
393
|
+
if (!createRepoReq.success) {
|
|
394
|
+
return fail(createRepoReq.msg)
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
return ok(createRepoReq.msg)
|
|
398
|
+
}
|
|
399
|
+
},
|
|
400
|
+
})
|
|
401
|
+
}
|
|
402
|
+
})
|
|
403
|
+
|
|
404
|
+
spm
|
|
405
|
+
.command("publish")
|
|
406
|
+
.argument('<name>')
|
|
407
|
+
.action(async (name) => {
|
|
408
|
+
if (isPackageExists(name)) {
|
|
409
|
+
const spmFileContent = await getSPM(name);
|
|
410
|
+
|
|
411
|
+
if (!spmFileContent.success) {
|
|
412
|
+
spmlog(formatError(spmFileContent.content));
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const SPMContent = spmFileContent.content;
|
|
417
|
+
const packageName = "@" + name;
|
|
418
|
+
|
|
419
|
+
const githubRepo = SPMContent.github?.repo
|
|
420
|
+
const version = SPMContent.version
|
|
421
|
+
const description = SPMContent.description
|
|
422
|
+
const packagePath = packageDir(name)
|
|
423
|
+
|
|
424
|
+
if (githubRepo) {
|
|
425
|
+
await loading({
|
|
426
|
+
startMsg: `Publishing ${packageName}`,
|
|
427
|
+
callback: async ({ fail, ok }) => {
|
|
428
|
+
const githubToken = await getGitHubToken()
|
|
429
|
+
|
|
430
|
+
const installationsReq = await fetch(
|
|
431
|
+
"https://api.github.com/user/installations",
|
|
432
|
+
{
|
|
433
|
+
headers: githubHeaders(githubToken),
|
|
434
|
+
},
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
const installations = await installationsReq.json()
|
|
438
|
+
|
|
439
|
+
if ("total_count" in installations) {
|
|
440
|
+
if (installations.total_count == 0) {
|
|
441
|
+
await open(
|
|
442
|
+
"https://github.com/apps/slim-package-manager/installations/new",
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
console.log("\n")
|
|
446
|
+
spmlog(formatError(`No application has been installed on the "${githubRepo}" repository\n`))
|
|
447
|
+
spmlog("Open:", formatBold.underline("https://github.com/apps/slim-package-manager/installations/new"))
|
|
448
|
+
spmlog(`Install the app, then run ${formatItalic(`${spm.name()} publish ${name}`)} again\n`)
|
|
449
|
+
|
|
450
|
+
return fail(`No application has been installed on the "${githubRepo}" repository`)
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
const githubPublishReq = await publishToGitHub({
|
|
454
|
+
token: githubToken,
|
|
455
|
+
repo: githubRepo,
|
|
456
|
+
contentPath: packagePath
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
if (!githubPublishReq.success) {
|
|
460
|
+
return fail("Github publish:", githubPublishReq.msg)
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
const isPackagesExists = (await getPackage({ name })).success
|
|
464
|
+
|
|
465
|
+
if (isPackagesExists) {
|
|
466
|
+
console.log("\nPackage is already exists... updating")
|
|
467
|
+
|
|
468
|
+
const updatePackageReq = await updatePackage({
|
|
469
|
+
token: githubToken,
|
|
470
|
+
name: name,
|
|
471
|
+
description: description,
|
|
472
|
+
version: version,
|
|
473
|
+
github: githubRepo
|
|
474
|
+
})
|
|
475
|
+
|
|
476
|
+
if (!updatePackageReq.success) {
|
|
477
|
+
return fail("Something went wrong while requesting a package update. Please try again later")
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
console.log("\nUpdating package on Github...")
|
|
481
|
+
|
|
482
|
+
const updateReq = await updateGitHubRepo({
|
|
483
|
+
token: githubToken,
|
|
484
|
+
repo: githubRepo,
|
|
485
|
+
packagePath: packagePath,
|
|
486
|
+
version: updatePackageReq.content.latest
|
|
487
|
+
})
|
|
488
|
+
|
|
489
|
+
if (!updateReq.success) {
|
|
490
|
+
return fail("Failed to push the new version to GitHub:", updateReq.msg)
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
const publishedVer = updateReq.version
|
|
494
|
+
return ok(`@${name} ${publishedVer} published on Github`)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
const publishReq = await publishPackage({
|
|
500
|
+
token: githubToken,
|
|
501
|
+
name: name,
|
|
502
|
+
version: version,
|
|
503
|
+
github: githubRepo,
|
|
504
|
+
description: description
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
if (!publishReq.success) {
|
|
508
|
+
return fail("SPM Registry publishing:", publishReq.content)
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
const tagReq = await createGitHubTag(githubToken, githubRepo, version, githubPublishReq.commit)
|
|
512
|
+
if (!tagReq.success) {
|
|
513
|
+
return fail(`${packageName} registered, but tagging v${version} on GitHub failed:`, tagReq.msg)
|
|
514
|
+
}
|
|
515
|
+
return ok(`${packageName} published and registred: ${name} v${version}`)
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
return fail("Installations:", installations.message)
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
})
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
errlog("No GitHub repository specified");
|
|
529
|
+
}
|
|
530
|
+
} else {
|
|
531
|
+
errlog(`No package founded: ${path.relative(process.cwd(), path.join(packagesPath, name)) || name}`);
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
spm
|
|
536
|
+
.command("login")
|
|
537
|
+
.description("Connect your GitHub account")
|
|
538
|
+
.option("--check", "Check if you logged in")
|
|
539
|
+
.action(async (opt) => {
|
|
540
|
+
if (opt.check) {
|
|
541
|
+
const token = await getGitHubToken()
|
|
542
|
+
|
|
543
|
+
if (token) {
|
|
544
|
+
try {
|
|
545
|
+
const user = await getGitHubUser(token)
|
|
546
|
+
|
|
547
|
+
spmlog(formatSuccess("Github connected"))
|
|
548
|
+
spmlog(`${formatBold("Account")}: ${user.name} (${user.html_url})`)
|
|
549
|
+
}
|
|
550
|
+
catch (e) {
|
|
551
|
+
spmlog(formatError(String(e)))
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
spmlog(formatError(`Github NOT connected. Use ${formatItalic.underline(spm.name() + " login")} to login via Github`))
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
await loading({
|
|
560
|
+
startMsg: `Waiting for GitHub authorization...`,
|
|
561
|
+
callback: async () => {
|
|
562
|
+
try {
|
|
563
|
+
const token = await loginGitHub();
|
|
564
|
+
await saveGitHubToken(token)
|
|
565
|
+
|
|
566
|
+
return {
|
|
567
|
+
success: true,
|
|
568
|
+
msg: "Github account connected"
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
catch (error) {
|
|
572
|
+
return {
|
|
573
|
+
success: false,
|
|
574
|
+
msg:`${error.message}`
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
})
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
spm
|
|
583
|
+
.command("logout")
|
|
584
|
+
.description("Disconnect GitHub account")
|
|
585
|
+
.action(async () => {
|
|
586
|
+
try {
|
|
587
|
+
await deleteGitHubToken();
|
|
588
|
+
|
|
589
|
+
spmlog(formatSuccess("GitHub account disconnected"));
|
|
590
|
+
} catch (error) {
|
|
591
|
+
spmlog(formatError(`✗ ${error.message}`));
|
|
592
|
+
process.exitCode = 1;
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
spm.parseAsync().catch(err => {
|
|
597
|
+
spmlog(formatError(err?.message ?? String(err)))
|
|
598
|
+
process.exitCode = 1
|
|
599
|
+
})
|