-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil.ts
99 lines (84 loc) · 2.67 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import fs from "node:fs";
import path from "node:path";
import semver from "semver";
import type { NpmRelease } from "./sample/npm-release.js";
/**
* Checks if the given directory contains a NodeCG installation.
* @param pathToCheck
*/
export function pathContainsNodeCG(pathToCheck: string): boolean {
const pjsonPath = path.join(pathToCheck, "package.json");
try {
const pjson = JSON.parse(fs.readFileSync(pjsonPath, "utf-8"));
return pjson.name.toLowerCase() === "nodecg";
} catch {
return false;
}
}
/**
* Gets the nearest NodeCG installation folder. First looks in process.cwd(), then looks
* in every parent folder until reaching the root. Throws an error if no NodeCG installation
* could be found.
*/
export function getNodeCGPath() {
let curr = process.cwd();
do {
if (pathContainsNodeCG(curr)) {
return curr;
}
const nextCurr = path.resolve(curr, "..");
if (nextCurr === curr) {
throw new Error(
"NodeCG installation could not be found in this directory or any parent directory.",
);
}
curr = nextCurr;
} while (fs.lstatSync(curr).isDirectory());
throw new Error(
"NodeCG installation could not be found in this directory or any parent directory.",
);
}
/**
* Checks if the given directory is a NodeCG bundle.
*/
export function isBundleFolder(pathToCheck: string) {
const pjsonPath = path.join(pathToCheck, "package.json");
if (fs.existsSync(pjsonPath)) {
const pjson = JSON.parse(fs.readFileSync(pjsonPath, "utf8"));
return typeof pjson.nodecg === "object";
}
return false;
}
/**
* Gets the currently-installed NodeCG version string, in the format "vX.Y.Z"
*/
export function getCurrentNodeCGVersion(): string {
const nodecgPath = getNodeCGPath();
return JSON.parse(fs.readFileSync(`${nodecgPath}/package.json`, "utf8"))
.version;
}
/**
* Gets the latest NodeCG release information from npm, including tarball download link.
*/
export async function getNodeCGRelease(target: string): Promise<NpmRelease> {
const targetVersion = semver.coerce(target)?.version;
if (!targetVersion) {
throw new Error(`Failed to determine target NodeCG version`);
}
const res = await fetch(`http://registry.npmjs.org/nodecg/${targetVersion}`);
if (res.status !== 200) {
throw new Error(
`Failed to fetch NodeCG release information from npm, status code ${res.status}`,
);
}
return res.json() as Promise<NpmRelease>;
}
export async function getLatestCLIRelease(): Promise<NpmRelease> {
const res = await fetch("http://registry.npmjs.org/nodecg-cli/latest");
if (res.status !== 200) {
throw new Error(
`Failed to fetch NodeCG release information from npm, status code ${res.status}`,
);
}
return res.json() as Promise<NpmRelease>;
}