From 8ea07bdbb4d07ccfa4d61072e5bfd3d1415c8f70 Mon Sep 17 00:00:00 2001 From: pietru Date: Thu, 20 Feb 2025 13:44:22 +0100 Subject: [PATCH] update publish script --- publish-dev.mjs | 89 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 24 deletions(-) diff --git a/publish-dev.mjs b/publish-dev.mjs index 32b1725..62b708d 100644 --- a/publish-dev.mjs +++ b/publish-dev.mjs @@ -1,6 +1,32 @@ import { readFile } from "node:fs/promises"; -const CONFIG_FILE = "publish.config.json"; +// Load key value pairs from any .env like file +// const props = await getProps("gradle.properties"); + +// Either a path to a JSON file or an object +// const CONFIG = "publish.config.json"; +const gradleProps = getProps("gradle.properties"); +const CONFIG = { + "title": process.env.VER_TITTLE, + "version": "${gradleProps.version}", + "featured": false, + "releaseChannel": "dev", + "loaders": ["quilt"], + "gameVersions": { + "file": "src/main/resources/quilt.mod.json", + "key": "quilt_loader.depends.1.versions" + }, + "files": { + "primary": "build/libs/CookieServerUtils-${gradleProps.version}.jar" + }, + "repoApi": "", + "gitReleaseUrl": "/releases/latest", + "crmm": { + "authToken": process.env.TOKEN, + "projectId": "cookie-server-utils" + } +}; + const CRMM_API_URL = "https://api.crmm.tech/api"; const includeGameVersionTypes = [ "release", @@ -58,27 +84,7 @@ async function prepareMultipartFormData(data) { async function loadConfigData() { console.log("Loading config..."); - //const config = await getConfig(); - const config = { - "title": process.env.VER_TITTLE, - "version": "1.0.6", - "featured": false, - "releaseChannel": "dev", - "loaders": ["quilt"], - "gameVersions": { - "file": "src/main/resources/quilt.mod.json", - "key": "quilt_loader.depends.1.versions" - }, - "files": { - "primary": "build/libs/CookieServerUtils-1.0.6.jar" - }, - "repoApi": "", - "gitReleaseUrl": "/releases/latest", - "crmm": { - "authToken": process.env.TOKEN, - "projectId": "cookie-server-utils" - } - } + const config = await getConfig(); // Version const version = config.version; @@ -168,7 +174,8 @@ async function getGameVersions(source) { `Failed to get game versions from source file using key '${gameVersionSourceKey}'`, ); - if (typeof gameVersions_raw === "string") gameVersions_raw = [gameVersions_raw]; + if (typeof gameVersions_raw === "string") + gameVersions_raw = [gameVersions_raw]; } const parsedGameVersions = new Set(); @@ -204,7 +211,11 @@ async function getVersionChangelog(url) { } async function getConfig() { - const file = await getFileContents(CONFIG_FILE, "utf-8"); + if (typeof CONFIG !== "string") { + return CONFIG; + } + + const file = await getFileContents(CONFIG, "utf-8"); const json = JSON.parse(file); return json; @@ -302,4 +313,34 @@ function getObjValue(obj, keys) { } return curr; +} + +async function getProps(filePath) { + const file = await getFileContents(filePath, "utf-8"); + if (!file) return undefined; + + // Parse the .env file + const props = {}; + + const lines = file.split("\n"); + for (const line of lines) { + if (line.startsWith("#") || !line.includes("=")) continue; + + let [key, value] = line.split("="); + if (!key?.length || !value?.length) continue; + + key = key.trim(); + value = value.trim(); + + if ( + value.startsWith('"') && value.endsWith('"') || + value.startsWith("'") && value.endsWith("'") + ) { + value = value.slice(1, -1); + } + + props[key] = value; + } + + return props; } \ No newline at end of file