import dateformat from "dateformat"; import fs from "fs"; import os from "os"; import path from "path"; import template from "../common/template"; import { Sdk } from "../core/sdk"; import type { ConfigJson, ConfigSource, PackageJson, PresetJson } from "../core/type"; import { Haxe } from "../impl/haxe"; import { Project } from "../project"; export const DEFAULT_CONFIG_JSON: ConfigJson = { SdkDir: "$HOME/sdk", }; export const resolvePath = (path: string) => path.replace("$HOME", os.homedir()); export const applyConfig = (config: ConfigJson) => { if (config.UseRuffle) { Project.useRuffle(); } if (config.SdkDir) { Sdk.dir = resolvePath(config.SdkDir); } if (config.BuildDir) { Haxe.buildDir = resolvePath(config.BuildDir); } }; export default (basedir: string = "."): PresetJson => { const presetFile = path.join(basedir, "haxetool.json"); const configFile = path.join(basedir, "config.json"); const packageFile = path.join(basedir, "package.json"); const presetJson: PresetJson = JSON.parse(fs.readFileSync(presetFile, "utf8")); const configJson: ConfigJson = { ...DEFAULT_CONFIG_JSON, ...(fs.existsSync(configFile) ? JSON.parse(fs.readFileSync(configFile, "utf8")) : {}), }; applyConfig(configJson); const packageJson: PackageJson = fs.existsSync(packageFile) ? JSON.parse(fs.readFileSync(packageFile, "utf8")) : {}; for (const [k, v] of Object.entries(presetJson)) { presetJson[k] = JSON.parse( template.renderString(JSON.stringify(v), { config: configJson, package: packageJson, env: process.env, dateformat: dateformat, }), ); } const defaultConfig: ConfigSource = presetJson["default"] || {}; defaultConfig.meta = { ...{ title: packageJson.name, filename: packageJson.name, version: packageJson.version, pack: `com.${packageJson.name}`, author: packageJson.author as string, }, ...(defaultConfig.meta || {}), }; presetJson["default"] = defaultConfig; return presetJson; };