From 4ad9e832bdeda189cd5e0aac77a70c42a85b7af0 Mon Sep 17 00:00:00 2001 From: Asaki Yuki Date: Fri, 30 Jan 2026 03:33:27 +0700 Subject: [PATCH] asajs configuration file --- config.d.ts | 14 ++++++++++++++ resources/asajs.config.cjs | 9 +++++++++ src/compilers/Configuration.ts | 21 ++++++++++++++++++++- src/compilers/ui/builder.ts | 21 ++++++++++++--------- src/compilers/ui/linker.ts | 13 +++++++++++-- src/compilers/ui/manifest.ts | 7 ++++--- src/compilers/ui/prevdata.ts | 2 +- src/components/AnimationKeyframe.ts | 2 +- src/index.ts | 3 +++ tsconfig.json | 2 +- 10 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 config.d.ts create mode 100644 resources/asajs.config.cjs diff --git a/config.d.ts b/config.d.ts new file mode 100644 index 0000000..8960a80 --- /dev/null +++ b/config.d.ts @@ -0,0 +1,14 @@ +import { Variable } from "./src/types/properties/value.ts" + +export interface Config { + compiler?: { + enabled?: boolean + linked?: boolean + } + packinfo?: { + name?: string + description?: string + version?: [number, number, number] + } + global_variables?: Record +} diff --git a/resources/asajs.config.cjs b/resources/asajs.config.cjs new file mode 100644 index 0000000..7e4026f --- /dev/null +++ b/resources/asajs.config.cjs @@ -0,0 +1,9 @@ +/** + * Configuration object for the AsaJS build process. + * @type {import('asajs/config.d.ts').Config} + */ +export const config = { + compiler: { + enabled: true, + }, +} diff --git a/src/compilers/Configuration.ts b/src/compilers/Configuration.ts index 32b62ef..f34c6ca 100644 --- a/src/compilers/Configuration.ts +++ b/src/compilers/Configuration.ts @@ -1 +1,20 @@ -export const isBuildMode = process.argv.includes("--build") +import fs from "fs" +import path from "path" +// @ts-ignore +import { Config } from "../../config.js" + +if (!fs.existsSync("asajs.config.cjs")) { + fs.copyFileSync("node_modules/asajs/resources/asajs.config.cjs", "asajs.config.cjs") +} + +export const config: Config = require(path.resolve(process.cwd(), "asajs.config.cjs")).config + +export let isBuildMode = config.compiler?.enabled ?? false +export let isLinkMode = config.compiler?.linked ?? false +export let unLinked = !(config.compiler?.linked ?? true) + +for (const arg of process.argv) { + if (arg === "--build") isBuildMode = true + if (arg === "--link") isLinkMode = true + else if (arg === "--unlink") unLinked = true +} diff --git a/src/compilers/ui/builder.ts b/src/compilers/ui/builder.ts index 8ad69de..1343fa2 100644 --- a/src/compilers/ui/builder.ts +++ b/src/compilers/ui/builder.ts @@ -1,6 +1,6 @@ -import { isBuildMode } from "../Configuration.js" +import { config, isBuildMode, isLinkMode, unLinked } from "../Configuration.js" import { Memory } from "../Memory.js" -import { createBuildFolder, linkToGame } from "./linker.js" +import { createBuildFolder, linkToGame, unlink } from "./linker.js" import { genManifest } from "./manifest.js" import { UI } from "../../components/UI.js" import { Type } from "../../types/enums/Type.js" @@ -13,13 +13,15 @@ async function buildUI() { ui_defs: Array.from(build.keys()), }) + if (config.global_variables) build.set("ui/_global_variables.json", config.global_variables) + build.set("build.json", { files: Array.from(build.keys()), }) const out = await Promise.all( build.entries().map(async ([file, value]) => { - const outFile = `build/build/${file}` + const outFile = `build/${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 })) @@ -42,11 +44,11 @@ async function buildUI() { ) await Promise.all([ - fs.writeFile("build/build/manifest.json", await genManifest(), "utf-8"), - fs.writeFile("build/build/.gitignore", [...out, "manifest.json"].join("\n"), "utf-8"), + fs.writeFile("build/manifest.json", await genManifest(), "utf-8"), + fs.writeFile("build/.gitignore", [...out, "manifest.json"].join("\n"), "utf-8"), fs - .stat("build/build/pack_icon.png") - .catch(() => fs.copyFile("node_modules/asajs/resources/pack_icon.png", "build/build/pack_icon.png")), + .stat("build/pack_icon.png") + .catch(() => fs.copyFile("node_modules/asajs/resources/pack_icon.png", "build/pack_icon.png")), ]) return out.length @@ -58,8 +60,9 @@ if (isBuildMode) { if (first) { await createBuildFolder() await buildUI() - await linkToGame() + if (isLinkMode) await linkToGame() } first = false }) -} +} else if (isLinkMode) linkToGame() +else if (unLinked) unlink() diff --git a/src/compilers/ui/linker.ts b/src/compilers/ui/linker.ts index 9a6ae15..ca33802 100644 --- a/src/compilers/ui/linker.ts +++ b/src/compilers/ui/linker.ts @@ -25,7 +25,7 @@ function genUUID(): string { } export async function clearBuild() { - await Promise.all(prevData.files.map(file => fs.rm(`build/build/${file}`).catch(() => null))) + await Promise.all(prevData.files.map(file => fs.rm(`build/${file}`).catch(() => null))) } export async function createBuildFolder() { @@ -40,13 +40,22 @@ export async function getBuildFolderName() { } export async function linkToGame() { - const sourcePath = path.resolve("build/build") + const sourcePath = path.resolve("build") const targetPath = path.resolve(getGamedataPath(), "development_resource_packs", await getBuildFolderName()) await fs.stat(targetPath).catch(async () => { await fs.symlink(sourcePath, targetPath, "junction") }) } +export async function unlink() { + const targetPath = path.resolve(getGamedataPath(), "development_resource_packs", await getBuildFolderName()) + try { + await fs.unlink(targetPath) + } catch (error) { + console.error(error) + } +} + export async function getUUID(): Promise<[string, string]> { return await BuildCache.getWithSetDefault("uuid", () => { return [genUUID(), genUUID()] diff --git a/src/compilers/ui/manifest.ts b/src/compilers/ui/manifest.ts index 67442f5..7b22595 100644 --- a/src/compilers/ui/manifest.ts +++ b/src/compilers/ui/manifest.ts @@ -1,3 +1,4 @@ +import { config } from "../Configuration.js" import { getUUID } from "./linker.js" export async function genManifest() { @@ -5,10 +6,10 @@ export async function genManifest() { return JSON.stringify({ format_version: 2, header: { - name: "AsaJS UI", - description: "A framework for creating UIs for AsaJS.", + name: config.packinfo?.name || "AsaJS", + description: config.packinfo?.description || "A framework for creating UIs for AsaJS.", uuid: uuid1, - version: [4, 0, 0], + version: config.packinfo?.version || [4, 0, 0], min_engine_version: [1, 21, 132], }, modules: [ diff --git a/src/compilers/ui/prevdata.ts b/src/compilers/ui/prevdata.ts index 868db96..1d34dfe 100644 --- a/src/compilers/ui/prevdata.ts +++ b/src/compilers/ui/prevdata.ts @@ -5,7 +5,7 @@ export let prevData: { } try { - prevData = JSON.parse(fs.readFileSync("build/build/build.json", "utf-8")) + prevData = JSON.parse(fs.readFileSync("build/build.json", "utf-8")) } catch (error) { prevData = { files: [] } } diff --git a/src/components/AnimationKeyframe.ts b/src/components/AnimationKeyframe.ts index c33ee0e..18df5c4 100644 --- a/src/components/AnimationKeyframe.ts +++ b/src/components/AnimationKeyframe.ts @@ -37,7 +37,7 @@ export class AnimationKeyframe extends Class { this.name = name || RandomString(16) this.namespace = namespace || RandomNamespace() - this.path = path || `@/${this.namespace}.json` + this.path = path || `asajs/${this.namespace}.json` Memory.add(this) } diff --git a/src/index.ts b/src/index.ts index e337632..cbd3433 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import "./compilers/Configuration.js" import "./compilers/PreCompile.js" import "./compilers/ui/builder.js" import "./compilers/ui/installer.js" @@ -11,3 +12,5 @@ export * from "./types/enums/index.js" export * as Properties from "./types/properties/index.js" export { ItemAuxID } from "./types/enums/Items.js" + +export { ArrayName, Operation } from "./types/properties/index.js" diff --git a/tsconfig.json b/tsconfig.json index 86ebf2d..faa887d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,6 @@ "incremental": true, "tsBuildInfoFile": ".tsbuildinfo" }, - "include": ["src/**/*"], + "include": ["src/**/*", "config.d.ts"], "exclude": ["node_modules"] }