forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependency.ts
More file actions
67 lines (52 loc) · 2.34 KB
/
dependency.ts
File metadata and controls
67 lines (52 loc) · 2.34 KB
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
import axios from "axios";
import * as fs from 'fs';
import type * as npm from "@npm/types";
import packageJson from "../package.json";
import { execSync } from "child_process";
// packument stuff
const REGISTRY = 'https://registry.npmjs.org/';
const ignoredKeys = [ 'created', 'modified' ];
// script api modules
const MinecraftChannel: "stable" | "preview" = "preview";
// sapi version format. Undefined means stable (ie. 1.0.0)
const preReleaseChannel: undefined | "rc" | "internal" | "beta" = "beta";
export async function getVersion(module: string): Promise<string> {
const response = await axios.get(REGISTRY + module);
const packument = response.data as npm.Packument;
const { time } = packument;
for (const key of ignoredKeys) {
delete time[key];
};
const sorted = Object.keys(time).sort(function(a,b){
// Turn your strings into dates, and then subtract them
// to get a value that is either negative, positive, or zero.
return new Date(time[a]).getTime() - new Date(time[b]).getTime();
}).reverse();
const latestPreview = sorted.find((v) => {
if (module.startsWith("@minecraft/server")) return v.includes(MinecraftChannel) && v.includes('-' + preReleaseChannel);
else if (module.startsWith("@minecraft/vanilla")) return v.includes('-' + MinecraftChannel);
else return v;
});
if (!latestPreview) throw "Cannot fetch latest preview version of " + module;
console.log(`Latest preview version for ${module}: ${latestPreview}`);
return latestPreview;
};
async function main() {
// we have specified that packages in dependencies section must only have script api modules only,
// for other node modules, please save them as development.
for (const moduleName in packageJson.dependencies) {
const latestPreview = await getVersion(moduleName);
packageJson.dependencies[moduleName as (keyof typeof import("../package.json")["dependencies"])] = latestPreview;
}
fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
// for actual node modules, update them to the latest version
for (const moduleName in packageJson.devDependencies) {
const cmd = `npm install ${moduleName}@latest --save-dev`;
console.log(
"\n" + "======================================================",
"\n" + cmd,
"\n" + execSync(cmd).toString(),
);
}
};
main().catch(console.error);