diff --git a/config.d.ts b/config.d.ts index 8ed1ab4..65c10d1 100644 --- a/config.d.ts +++ b/config.d.ts @@ -1,4 +1,4 @@ -import { BindingItem, Variable } from "./src/types/properties/value.ts" +import { Variable } from "./src/types/properties/value.ts" export interface RetBindingValue { generate_bindings?: Array<{ source_property_name: string; target_property_name: string }> @@ -13,6 +13,7 @@ export interface Config { autoEnable?: boolean gdkUserId?: string fixInventoryItemRenderer?: boolean + buildFolder?: string } packinfo?: { name?: string diff --git a/package.json b/package.json index acfb461..4e83b8a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asajs", - "version": "4.0.7-indev", + "version": "4.0.9", "description": "Create your Minecraft JSON-UI resource packs using JavaScript", "keywords": [ "Minecraft", diff --git a/src/compilers/Configuration.ts b/src/compilers/Configuration.ts index ea74ec8..8cd79ee 100644 --- a/src/compilers/Configuration.ts +++ b/src/compilers/Configuration.ts @@ -1,6 +1,6 @@ import fs from "fs" import path from "path" -// @ts-ignore +import jsonc from "jsonc-parser" import { Config, RetBindingValue } from "../../config.js" import { createRequire } from "module" @@ -10,7 +10,11 @@ for (const arg of process.argv) { if (arg.startsWith("--")) options[arg.slice(2)] = true } -export const isTestMode = !fs.existsSync("node_modules/asajs") +export let isTestMode = false +try { + const { name } = jsonc.parse(fs.readFileSync(path.resolve("package.json"), "utf-8")) + if (name === "asajs") isTestMode = true +} catch (error) {} if (!fs.existsSync("asajs.config.js")) { if (isTestMode) { @@ -55,6 +59,7 @@ export const config: Config = createRequire(import.meta.url)(path.resolve(proces export const isBuildMode = options["build"] ?? config.compiler?.enabled ?? false export const isLinkMode = options["link"] ?? config.compiler?.autoImport ?? false export const unLinked = options["unlink"] ?? !(config.compiler?.autoImport ?? true) +export const buildFolder = config.compiler?.buildFolder || "build" export const bindingFuntions = config.binding_functions diff --git a/src/compilers/PreCompile.ts b/src/compilers/PreCompile.ts index 48c5694..6dccbe0 100644 --- a/src/compilers/PreCompile.ts +++ b/src/compilers/PreCompile.ts @@ -19,12 +19,14 @@ Array.prototype.lastItem = function () { } const now = performance.now() -type LogType = "INFO" +type LogType = "INFO" | "WARN" function TypeHighlight(type: LogType) { switch (type) { case "INFO": return `\x1b[32mINFO\x1b[0m` + case "WARN": + return `\x1b[33mWARN\x1b[0m` default: return type satisfies never } diff --git a/src/compilers/ui/buildcache.ts b/src/compilers/ui/buildcache.ts index 97b1f5b..3216234 100644 --- a/src/compilers/ui/buildcache.ts +++ b/src/compilers/ui/buildcache.ts @@ -1,4 +1,5 @@ import fs from "fs/promises" +import { buildFolder } from "../Configuration.js" export class BuildCache { private static queue: Promise = Promise.resolve() @@ -14,7 +15,9 @@ export class BuildCache { static async get(key: string): Promise { return this.enqueue(async () => { try { - return await fs.readFile("build/cache.json", "utf-8").then(data => JSON.parse(data)[key] ?? null) + return await fs + .readFile(`${buildFolder}/cache.json`, "utf-8") + .then(data => JSON.parse(data)[key] ?? null) } catch (error) { return null } @@ -33,13 +36,13 @@ export class BuildCache { let data: Record = {} try { - data = JSON.parse(await fs.readFile("build/cache.json", "utf-8")) + data = JSON.parse(await fs.readFile(`${buildFolder}/cache.json`, "utf-8")) } catch {} if (key in data) return data[key] data[key] = outVal - await fs.writeFile("build/cache.json", JSON.stringify(data), "utf-8") + await fs.writeFile(`${buildFolder}/cache.json`, JSON.stringify(data), "utf-8") return outVal }) } @@ -48,15 +51,15 @@ export class BuildCache { return this.enqueue(async () => { try { return fs.writeFile( - "build/cache.json", + `${buildFolder}/cache.json`, JSON.stringify({ - ...(await fs.readFile("build/cache.json", "utf-8").then(data => JSON.parse(data))), + ...(await fs.readFile(`${buildFolder}/cache.json`, "utf-8").then(data => JSON.parse(data))), [key]: value, }), "utf-8", ) } catch (error) { - return fs.writeFile("build/cache.json", JSON.stringify({ [key]: value }), "utf-8") + return fs.writeFile(`${buildFolder}/cache.json`, JSON.stringify({ [key]: value }), "utf-8") } }) } diff --git a/src/compilers/ui/builder.ts b/src/compilers/ui/builder.ts index 65709dc..33fe14b 100644 --- a/src/compilers/ui/builder.ts +++ b/src/compilers/ui/builder.ts @@ -1,4 +1,4 @@ -import { config, isBuildMode, isLinkMode, isTestMode, unLinked } from "../Configuration.js" +import { buildFolder, config, isBuildMode, isLinkMode, isTestMode, unLinked } from "../Configuration.js" import { Memory } from "../Memory.js" import { createBuildFolder, gamePath, getBuildFolderName, linkToGame, unlink } from "./linker.js" import { genManifest, version } from "./manifest.js" @@ -9,6 +9,7 @@ import { BuildCache } from "./buildcache.js" import { disableRSP, enableRSP } from "./installer.js" import { Log } from "../PreCompile.js" import path from "path" +import { API_events } from "../../components/API.js" async function buildUI() { const build = Memory.build() @@ -20,8 +21,8 @@ async function buildUI() { if (config.global_variables) build.set("ui/_global_variables.json", config.global_variables) const out = await Promise.all( - build.entries().map(async ([file, value]) => { - const outFile = `build/${file}` + Array.from(build.entries()).map(async ([file, value]) => { + const outFile = `${buildFolder}/${file}` await fs .stat(outFile.split(/\\|\//g).slice(0, -1).join("/")) .catch(async () => await fs.mkdir(outFile.split(/\\|\//g).slice(0, -1).join("/"), { recursive: true })) @@ -47,23 +48,24 @@ async function buildUI() { await Promise.all([ fs - .writeFile("build/manifest.json", await genManifest(), "utf-8") - .then(() => Log("INFO", "build/manifest.json created!")), + .writeFile(`${buildFolder}/manifest.json`, await genManifest(), "utf-8") + .then(() => Log("INFO", `${buildFolder}/manifest.json created!`)), fs - .writeFile("build/.gitignore", [...out, "manifest.json"].join("\n"), "utf-8") - .then(() => Log("INFO", "build/.gitignore created!")), + .writeFile(`${buildFolder}/.gitignore`, [...out, "manifest.json"].join("\n"), "utf-8") + .then(() => Log("INFO", `${buildFolder}/.gitignore created!`)), BuildCache.set("build-files", [...out, "manifest.json"]).then(() => Log("INFO", "build-files set!")), BuildCache.set("version", version).then(() => Log("INFO", "version set!")), fs - .stat("build/pack_icon.png") + .stat(`${buildFolder}/pack_icon.png`) .catch(() => fs.copyFile( isTestMode ? "resources/pack_icon.png" : "node_modules/asajs/resources/pack_icon.png", - "build/pack_icon.png", + `${buildFolder}/pack_icon.png`, ), ) - .then(() => Log("INFO", "build/pack_icon.png copied!")), - ]) + .then(() => Log("INFO", `${buildFolder}/pack_icon.png copied!`)) + .catch(() => Log("WARN", `cannot copy ${buildFolder}/pack_icon.png!`)), + ]).catch(error => console.error(error)) return out.length } @@ -93,6 +95,9 @@ if (isBuildMode) { `\x1b[32m"${path.join(gamePath, "development_resource_packs", await getBuildFolderName())}"\x1b[0m`, ) console.log("=============================================================") + + // API events + API_events.onBuildFinish.forEach(v => v(config)) } }) } else if (isLinkMode) linkToGame() diff --git a/src/components/API.ts b/src/components/API.ts new file mode 100644 index 0000000..32b883a --- /dev/null +++ b/src/components/API.ts @@ -0,0 +1,11 @@ +import { Config } from "../../config.js" + +export const API_events: { onBuildFinish: ((config: Config) => void)[] } = { + onBuildFinish: [], +} + +export const API = { + onBuildFinish: function (callback: (config: Config) => void) { + API_events.onBuildFinish.push(callback) + }, +} diff --git a/src/index.ts b/src/index.ts index 5a642f4..239aff6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,3 +16,4 @@ export { ItemAuxID } from "./types/enums/Items.js" export { ArrayName, Operation } from "./types/properties/index.js" export * from "./compilers/bindings/Binary.js" +export { API } from "./components/API.js"