This commit is contained in:
cirroskais 2024-02-23 06:27:26 -05:00
parent 9a207639e8
commit 38dc426bde
No known key found for this signature in database
GPG key ID: 5FC73EBF2678E33D
8 changed files with 1121 additions and 496 deletions

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
dist
build
node_modules
data.json

View file

@ -1,15 +1,14 @@
{
"name": "modpack-client",
"version": "1.0.0",
"type": "module",
"bin": "src/index.js",
"main": "src/index.js",
"license": "MIT",
"scripts": {
"build:linux": "yarn nexe -i src/index.js -o build/modpack-client -t linux-x64-20.11.1 --remote https://oldcdn.madhouselabs.net/nexe",
"build:win": "yarn nexe -i src/index.js -o build/modpack-client.exe -t windows-x64-14.5.0"
},
"dependencies": {},
"devDependencies": {
"pkg": "^5.8.1"
},
"pkg": {
"targets": [ "latest-linux-x64", "latest-win-x64" ],
"outputPath": "dist"
"nexe": "^4.0.0-rc.4"
}
}

View file

@ -1,6 +1,68 @@
import { info, warn, error, success } from "./lib/logger.js";
const package = require("../package.json");
const logger = require("./lib/logger.js");
const args = require("./lib/args.js");
const storage = require("./lib/storage.js");
const server = require("./lib/server.js");
info("info log");
warn("warn log");
error("error log");
success("success log");
let config = {
host: "http://localhost:3000",
};
async function main() {
logger.info("Starting modpack-client", package.version);
if (args.get("host")) config.host = args.get("host");
else logger.warn("Host not specified, using assuming localhost");
logger.info("Using modpack-server host", config.host);
logger.info("Fetching modpack versions...");
const localVersion = storage.read().version;
const remoteVersion = await server.getVersion(config.host);
logger.success("Local:", localVersion);
logger.success("Remote:", remoteVersion);
if (remoteVersion <= localVersion) {
logger.info(
"Local is higher or equal to remote; there is nothing to do."
);
return process.exit(0);
}
logger.info("Fetching mods...");
const remoteMods = await server.getAllMods(config.host);
const localMods = storage.read().mods;
let additions = [];
for (let i = 0; i < remoteMods.length; i++) {
let remoteMod = remoteMods[i];
let found = false;
for (let ii = 0; ii < localMods.length; ii++) {
let localMod = localMods[ii];
if (remoteMod.name == localMod.name) found = true;
}
if (!found) additions.push(remoteMod);
}
let removals = [];
for (let i = 0; i < localMods.length; i++) {
let localMod = localMods[i];
let found = false;
for (let ii = 0; ii < remoteMods.length; ii++) {
let remoteMod = remoteMods[ii];
if (localMod.name == remoteMod.name) found = true;
}
if (!found) removals.push(localMod);
}
console.log("Additions", additions);
console.log("Removals", removals);
}
main();

10
src/lib/args.js Normal file
View file

@ -0,0 +1,10 @@
const args = new Map();
for (let i = 0; i < process.argv.length; i++) {
let arg = process.argv[i];
if (arg.substring(0, 2) !== "--") continue;
args.set(arg.substring(2), process.argv[i + 1]);
}
module.exports = args;

View file

@ -14,7 +14,7 @@ const colors = {
green: [32, 42],
};
export function info(...args) {
exports.info = (...args) => {
console.log(
colors.bg(colors.blue) +
colors.fg(colors.black) +
@ -23,9 +23,9 @@ export function info(...args) {
colors.fg(colors.white),
...args
);
}
};
export function warn(...args) {
exports.warn = (...args) => {
console.log(
colors.bg(colors.yellow) +
colors.fg(colors.black) +
@ -34,9 +34,9 @@ export function warn(...args) {
colors.fg(colors.white),
...args
);
}
};
export function error(...args) {
exports.error = (...args) => {
console.log(
colors.bg(colors.red) +
colors.fg(colors.black) +
@ -45,9 +45,9 @@ export function error(...args) {
colors.fg(colors.white),
...args
);
}
};
export function success(...args) {
exports.success = (...args) => {
console.log(
colors.bg(colors.green) +
colors.fg(colors.black) +
@ -56,4 +56,4 @@ export function success(...args) {
colors.fg(colors.white),
...args
);
}
};

9
src/lib/server.js Normal file
View file

@ -0,0 +1,9 @@
exports.getVersion = async (host) => {
const request = await fetch(host + "/");
return (await request.json()).version;
};
exports.getAllMods = async (host) => {
const request = await fetch(host + "/getallmods");
return await request.json();
};

37
src/lib/storage.js Normal file
View file

@ -0,0 +1,37 @@
const fs = require("fs");
const path = require("path");
const STORAGE_FILE = "data.json";
try {
fs.readFileSync(path.join(process.cwd(), STORAGE_FILE), {
encoding: "utf-8",
});
} catch (e) {
fs.writeFileSync(
path.join(process.cwd(), STORAGE_FILE),
JSON.stringify(
{
version: 0,
mods: [],
},
null,
4
)
);
}
exports.read = () => {
return JSON.parse(
fs.readFileSync(path.join(process.cwd(), STORAGE_FILE), {
encoding: "utf-8",
})
);
};
exports.write = (data) => {
return fs.writeFileSync(
path.join(process.cwd(), STORAGE_FILE),
JSON.stringify(data, null, 4)
);
};

1443
yarn.lock

File diff suppressed because it is too large Load diff