Files
aklive2d/libs/project_json.js
2023-01-20 17:21:08 -05:00

120 lines
3.1 KiB
JavaScript

import path from 'path'
import Matcher from './content_processor.js'
import { read as readFile, exists } from './file.js'
import { read as readYAML } from './yaml.js'
export default class ProjectJson {
#json
#operatorName
#operatorSourceFolder
#operatorShareFolder
#assets
#template
constructor(operatorName, operatorShareFolder, assets) {
this.#operatorName = operatorName
this.#operatorSourceFolder = path.join(__dirname, __config.folder.operator)
this.#operatorShareFolder = operatorShareFolder
this.#assets = assets
this.#template = this.#processYAML(readYAML(path.join(__dirname, 'config', '_project_json.yaml')))
}
async load() {
// load json from file
this.#json = JSON.parse(await readFile(this.#getPath()))
this.#process()
return this.#json
}
#getPath() {
// if exists, do not use the template
const defaultPath = path.join(this.#operatorSourceFolder, this.#operatorName, 'project.json')
if (exists(defaultPath)) {
return defaultPath
} else {
return path.join(this.#operatorShareFolder, 'project.json')
}
}
#process() {
this.#json = {
...this.#json,
description: this.#template.description,
title: __config.operators[this.#operatorName].title,
general: {
...this.#json.general,
localization: this.#template.localization,
properties: {
...this.#json.general.properties,
...this.#properties
}
},
}
}
#processYAML(template) {
const matcher = new Matcher(template.description, '${', '}', __config.operators[this.#operatorName])
if (matcher.match() !== null) {
template.description = matcher.process()
}
const replacePropertyPairs = [
{
key: "defaultbackground",
value: {
value: this.#assets.backgrounds[0],
options: this.#assets.backgrounds.map((b) => {
return {
"label": b,
"value": b
}
})
}
},
{
key: "paddingleft",
value: {
value: __config.operators[this.#operatorName].viewport_left
},
},
{
key: "paddingright",
value: {
value: __config.operators[this.#operatorName].viewport_right
},
},
{
key: "paddingtop",
value: {
value: __config.operators[this.#operatorName].viewport_top
},
},
{
key: "paddingbottom",
value: {
value: __config.operators[this.#operatorName].viewport_bottom
},
},
]
replacePropertyPairs.forEach((pair) => {
const property = template.properties.find((p) => p.key === pair.key)
property.value = {
...property.value,
...pair.value
}
})
return template
}
get #properties() {
const properties = this.#template.properties
const output = {}
for (let i = 0; i < properties.length; i++) {
output[properties[i].key] = {
index: i,
order: 100 + i,
...properties[i].value
}
}
return output
}
}