feat(yaml): use !match tag to process content
This commit is contained in:
@@ -1,24 +1,20 @@
|
||||
export default class Matcher {
|
||||
#start
|
||||
#end
|
||||
#content
|
||||
#reExp
|
||||
#config
|
||||
#assets
|
||||
|
||||
constructor(content, start, end, config) {
|
||||
constructor(start, end, config, assets) {
|
||||
this.#start = start
|
||||
this.#end = end
|
||||
this.#content = content
|
||||
this.#reExp = new RegExp(`\\${start}.+?${end}`, 'g')
|
||||
this.#config = config
|
||||
this.#assets = assets
|
||||
}
|
||||
|
||||
match() {
|
||||
return this.#content.match(this.#reExp)
|
||||
}
|
||||
|
||||
process() {
|
||||
const matches = this.match()
|
||||
get result() {
|
||||
const matches = this.content.match(this.#reExp)
|
||||
if (matches !== null) {
|
||||
matches.forEach((match) => {
|
||||
const matchTypeName = match.replace(this.#start, '').replace(this.#end, '')
|
||||
@@ -33,40 +29,55 @@ export default class Matcher {
|
||||
} catch (e) {
|
||||
throw new Error(`Cannot find variable ${name}.`)
|
||||
}
|
||||
this.#content = this.#content.replace(match, replaceValue)
|
||||
this.content = this.content.replace(match, replaceValue)
|
||||
})
|
||||
break
|
||||
case 'func':
|
||||
case 'replaceFunc':
|
||||
try {
|
||||
this.#content = this.#content.replace(match, (new Function('Evalable', 'config', `return new Evalable(config).${name}`))(Evalable, this.#config))
|
||||
this.content = this.content.replace(match, (new Function('Evalable', 'config', 'assets', `return new Evalable(config, assets).${name}`))(Evalable, this.#config, this.#assets))
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
}
|
||||
break
|
||||
case 'directFunc':
|
||||
this.content = (new Function('Evalable', 'config', 'assets', `return new Evalable(config, assets).${name}`))(Evalable, this.#config, this.#assets)
|
||||
break
|
||||
default:
|
||||
throw new Error(`Cannot find type ${type}.`)
|
||||
}
|
||||
})
|
||||
}
|
||||
return this.#content
|
||||
return this.content
|
||||
}
|
||||
}
|
||||
|
||||
class Evalable {
|
||||
#config
|
||||
#assets
|
||||
|
||||
constructor(config) {
|
||||
constructor(config, assets) {
|
||||
this.#config = config
|
||||
this.#assets = assets
|
||||
}
|
||||
|
||||
split(varName, separator) {
|
||||
split(location, varName, separator) {
|
||||
return this.#step(location, varName).split(separator)
|
||||
}
|
||||
|
||||
getVar(location, varName) {
|
||||
return this.#step(location, varName)
|
||||
}
|
||||
|
||||
#step(location, varName) {
|
||||
let content = this.#config
|
||||
varName.split("->").forEach((item) => {
|
||||
try {
|
||||
this.#config = this.#config[item]
|
||||
if (location === 'assets') content = this.#assets
|
||||
content = content[item]
|
||||
} catch (e) {
|
||||
throw new Error(`Cannot split ${varName} with separator ${separator}.`)
|
||||
throw new Error(`Cannot step ${varName}.`)
|
||||
}
|
||||
})
|
||||
return this.#config.split(separator)
|
||||
return content
|
||||
}
|
||||
}
|
||||
@@ -16,12 +16,29 @@ export default class ProjectJson {
|
||||
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()))
|
||||
const matcher = new Matcher('${', '}', __config.operators[this.#operatorName], {
|
||||
...this.#assets,
|
||||
backgroundOptions: this.#assets.backgrounds.map((b) => {
|
||||
return {
|
||||
"label": b,
|
||||
"value": b
|
||||
}
|
||||
})
|
||||
})
|
||||
const match = {
|
||||
identify: value => value.startsWith('!match'),
|
||||
tag: '!match',
|
||||
resolve(str) {
|
||||
matcher.content = str
|
||||
return matcher.result
|
||||
}
|
||||
}
|
||||
this.#template = readYAML(path.join(__dirname, 'config', '_project_json.yaml'), [match])
|
||||
this.#process()
|
||||
return this.#json
|
||||
}
|
||||
@@ -52,59 +69,6 @@ export default class ProjectJson {
|
||||
}
|
||||
}
|
||||
|
||||
#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 = {}
|
||||
|
||||
@@ -2,7 +2,7 @@ import path from 'path'
|
||||
import { parse } from 'yaml'
|
||||
import fs from 'fs'
|
||||
|
||||
export function read(file_dir) {
|
||||
export function read(file_dir, customTags = []) {
|
||||
const include = {
|
||||
identify: value => value.startsWith('!include'),
|
||||
tag: '!include',
|
||||
@@ -14,6 +14,6 @@ export function read(file_dir) {
|
||||
}
|
||||
const file = fs.readFileSync(file_dir, 'utf8')
|
||||
return parse(file, {
|
||||
customTags: [include],
|
||||
customTags: [include, ...customTags],
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user