-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
41 lines (38 loc) · 1.31 KB
/
index.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
import { components } from "@octokit/openapi-types";
import { OctokitResponse } from "@octokit/types";
import { findUp } from "find-up-simple";
import { readFile } from "fs/promises";
import { Octokit } from "octokit";
import { Manifest } from "pacote";
export async function nextVersion(path?: string) {
try {
const manifestPath = await findUp("node_modules/next/package.json", {
cwd: path,
});
const manifest: Manifest = JSON.parse(
await readFile(manifestPath!, { encoding: "utf8" }),
);
return manifest.version;
} catch {
throw new Error("Next not found in node_modules");
}
}
export async function nextManifest(version?: string): Promise<Manifest> {
try {
const response = (await new Octokit().rest.repos.getContent({
owner: "vercel",
repo: "next.js",
path: "package.json",
ref: version ? `v${version}` : undefined,
})) as OctokitResponse<components["schemas"]["content-file"]>;
return JSON.parse(atob(response.data.content));
} catch {
throw new Error("Next download from GitHub failed");
}
}
export default async function reactVersion(path?: string) {
const next = await nextVersion(path);
const react = (await nextManifest(next)).devDependencies?.["react-builtin"];
if (react) return react.substring(react.indexOf("@") + 1);
else throw new Error(`React not pinned in Next ${next}`);
}