refactor(runner): use global varibales
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
import path from 'path'
|
||||
import { appendSync, readSync } from './file.js'
|
||||
|
||||
export function appendReadme(config, operatorName, rootDir) {
|
||||
const operatorConfig = config.operators[operatorName]
|
||||
const projectJson = JSON.parse(readSync(path.join(rootDir, config.folder.operator, operatorName, 'project.json')))
|
||||
export function appendReadme() {
|
||||
const operatorConfig = __config.operators[__operator_name]
|
||||
const projectJson = JSON.parse(readSync(path.join(__dirname, __config.folder.operator, __operator_name, 'project.json')))
|
||||
appendSync(
|
||||
`\n| ${operatorConfig.title.split(' - ')[0].split('Arknights: ')[1]} | [Link](https://arknights.halyul.dev/${operatorConfig.link}/) | [Link](https://steamcommunity.com/sharedfiles/filedetails/?id=${projectJson.workshopid}) |`,
|
||||
path.join(rootDir, 'README.md')
|
||||
path.join(__dirname, 'README.md')
|
||||
)
|
||||
}
|
||||
|
||||
export function appendMainConfig(operatorName, rootDir) {
|
||||
export function appendMainConfig() {
|
||||
appendSync(
|
||||
`\n ${operatorName}: !include config/${operatorName}.yaml`,
|
||||
path.join(rootDir, 'config.yaml')
|
||||
`\n ${__operator_name}: !include config/${__operator_name}.yaml`,
|
||||
path.join(__dirname, 'config.yaml')
|
||||
)
|
||||
}
|
||||
@@ -3,25 +3,21 @@ import { copy, read, write } from './file.js'
|
||||
import AlphaComposite from './alpha_composite.js'
|
||||
|
||||
export default class AssetsProcessor {
|
||||
#config
|
||||
#operatorName
|
||||
#operatorSourceFolder
|
||||
#alphaCompositer
|
||||
|
||||
constructor(config, operatorName, rootDir) {
|
||||
this.#config = config
|
||||
this.#operatorName = operatorName
|
||||
this.#operatorSourceFolder = path.join(rootDir, this.#config.folder.operator)
|
||||
this.#alphaCompositer = new AlphaComposite(config, operatorName, rootDir)
|
||||
constructor() {
|
||||
this.#operatorSourceFolder = path.join(__dirname, __config.folder.operator)
|
||||
this.#alphaCompositer = new AlphaComposite()
|
||||
}
|
||||
|
||||
async process(publicAssetsDir, extractedDir) {
|
||||
async process(extractedDir) {
|
||||
const BASE64_BINARY_PREFIX = 'data:application/octet-stream;base64,'
|
||||
const BASE64_PNG_PREFIX = 'data:image/png;base64,'
|
||||
const assetsJson = {}
|
||||
const skelFilename = `${this.#config.operators[this.#operatorName].filename}.skel`
|
||||
const skelFilename = `${__config.operators[__operator_name].filename}.skel`
|
||||
const skel = await read(path.join(extractedDir, skelFilename), null)
|
||||
const atlasFilename = `${this.#config.operators[this.#operatorName].filename}.atlas`
|
||||
const atlasFilename = `${__config.operators[__operator_name].filename}.atlas`
|
||||
const atlas = await read(path.join(extractedDir, atlasFilename))
|
||||
const dimensions = atlas.match(new RegExp(/^size:(.*),(.*)/gm))[0].replace('size: ', '').split(',')
|
||||
const matches = atlas.match(new RegExp(/(.*).png/g))
|
||||
@@ -32,9 +28,9 @@ export default class AssetsProcessor {
|
||||
assetsJson[`./assets/${skelFilename.replace('#', '%23')}`] = BASE64_BINARY_PREFIX + skel.toString('base64')
|
||||
assetsJson[`./assets/${atlasFilename.replace('#', '%23')}`] = BASE64_BINARY_PREFIX + Buffer.from(atlas).toString('base64')
|
||||
|
||||
const fallbackFilename = `${this.#config.operators[this.#operatorName].fallback_name}.png`
|
||||
const fallbackFilename = `${__config.operators[__operator_name].fallback_name}.png`
|
||||
const fallbackBuffer = await this.#alphaCompositer.process(fallbackFilename, extractedDir)
|
||||
await write(fallbackBuffer, path.join(this.#operatorSourceFolder, this.#operatorName, fallbackFilename))
|
||||
await write(fallbackBuffer, path.join(this.#operatorSourceFolder, __operator_name, fallbackFilename))
|
||||
return {
|
||||
dimensions,
|
||||
assetsJson
|
||||
|
||||
@@ -3,16 +3,12 @@ import fs from 'fs';
|
||||
import sharp from "sharp";
|
||||
|
||||
export default class Background {
|
||||
#config
|
||||
#rootDir
|
||||
#backgroundFolder
|
||||
#extractFolder
|
||||
#files
|
||||
|
||||
constructor(config, rootDir) {
|
||||
this.#config = config;
|
||||
this.#rootDir = rootDir;
|
||||
this.#backgroundFolder = path.join(rootDir, config.folder.operator, '_share', config.folder.background);
|
||||
constructor() {
|
||||
this.#backgroundFolder = path.join(__dirname, __config.folder.operator, '_share', __config.folder.background);
|
||||
this.#extractFolder = path.join(this.#backgroundFolder, 'extracted');
|
||||
}
|
||||
|
||||
@@ -60,7 +56,7 @@ export default class Background {
|
||||
return {
|
||||
filename: f.replace('_left', ''),
|
||||
source: path.join(this.#backgroundFolder),
|
||||
target: path.join(publicAssetsDir, this.#config.folder.background)
|
||||
target: path.join(publicAssetsDir, __config.folder.background)
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import path from 'path'
|
||||
import { read } from './yaml.js'
|
||||
|
||||
export default function (dirname) {
|
||||
export default function () {
|
||||
return {
|
||||
basedir: dirname,
|
||||
...read(path.join(dirname, 'config.yaml'))
|
||||
basedir: __dirname,
|
||||
...read(path.join(__dirname, 'config.yaml'))
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
import path from 'path'
|
||||
import { writeSync, copy, rmdir } from './file.js'
|
||||
|
||||
export default function (config, rootDir) {
|
||||
const targetFolder = path.join(rootDir, config.folder.release, config.folder.directory);
|
||||
const sourceFolder = path.join(rootDir, config.folder.operator);
|
||||
export default function () {
|
||||
const targetFolder = path.join(__dirname, __config.folder.release, __config.folder.directory);
|
||||
const sourceFolder = path.join(__dirname, __config.folder.operator);
|
||||
rmdir(targetFolder);
|
||||
const filesToCopy = [];
|
||||
const directoryJson = []
|
||||
for (const [key, value] of Object.entries(config.operators)) {
|
||||
for (const [key, value] of Object.entries(__config.operators)) {
|
||||
filesToCopy.push(key);
|
||||
directoryJson.push(value);
|
||||
}
|
||||
writeSync(JSON.stringify(directoryJson, null), path.join(targetFolder, "directory.json"))
|
||||
filesToCopy.forEach((key) => {
|
||||
const filename = `${config.operators[key].filename}.json`;
|
||||
const filename = `${__config.operators[key].filename}.json`;
|
||||
copy(path.join(sourceFolder, key, filename), path.join(targetFolder, filename))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
export default class EnvGenerator {
|
||||
#config
|
||||
#assets
|
||||
#operatorConfig
|
||||
|
||||
constructor(config, operatorName, assets) {
|
||||
this.#config = config
|
||||
this.#operatorConfig = config.operators[operatorName]
|
||||
constructor(assets) {
|
||||
this.#operatorConfig = __config.operators[__operator_name]
|
||||
this.#assets = assets
|
||||
}
|
||||
|
||||
@@ -23,7 +21,7 @@ export default class EnvGenerator {
|
||||
`VITE_IMAGE_WIDTH=2048`,
|
||||
`VITE_IMAGE_HEIGHT=2048`,
|
||||
`VITE_BACKGROUND_FILES=${JSON.stringify(this.#assets.backgrounds)}`,
|
||||
`VITE_BACKGROUND_FOLDER=${this.#config.folder.background}`,
|
||||
`VITE_BACKGROUND_FOLDER=${__config.folder.background}`,
|
||||
].join('\n')
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@ import path from 'path'
|
||||
import { mkdir, copy } from './file.js'
|
||||
import { appendMainConfig } from './append.js'
|
||||
|
||||
export default function init(operatorName, __dirname, extractedDir) {
|
||||
export default function init(extractedDir) {
|
||||
mkdir(extractedDir)
|
||||
copy(path.join(__dirname, 'config', '_template.yaml'), path.join(__dirname, 'config', `${operatorName}.yaml`))
|
||||
appendMainConfig(operatorName, __dirname)
|
||||
copy(path.join(__dirname, 'config', '_template.yaml'), path.join(__dirname, 'config', `${__operator_name}.yaml`))
|
||||
appendMainConfig(__operator_name, __dirname)
|
||||
}
|
||||
@@ -13,14 +13,11 @@ export default class ProjectJson {
|
||||
#rootDir
|
||||
#template
|
||||
|
||||
constructor(config, operatorName, __dirname, operatorShareFolder, assets) {
|
||||
this.#config = config
|
||||
this.#operatorName = operatorName
|
||||
this.#operatorSourceFolder = path.join(__dirname, this.#config.folder.operator)
|
||||
constructor(operatorShareFolder, assets) {
|
||||
this.#operatorSourceFolder = path.join(__dirname, __config.folder.operator)
|
||||
this.#operatorShareFolder = operatorShareFolder
|
||||
this.#assets = assets
|
||||
this.#rootDir = __dirname
|
||||
this.#template = this.#processYAML(readYAML(path.join(this.#rootDir, 'config', '_project_json.yaml')))
|
||||
this.#template = this.#processYAML(readYAML(path.join(__dirname, 'config', '_project_json.yaml')))
|
||||
}
|
||||
|
||||
async load() {
|
||||
@@ -32,7 +29,7 @@ export default class ProjectJson {
|
||||
|
||||
#getPath() {
|
||||
// if exists, do not use the template
|
||||
const defaultPath = path.join(this.#operatorSourceFolder, this.#operatorName, 'project.json')
|
||||
const defaultPath = path.join(this.#operatorSourceFolder, __operator_name, 'project.json')
|
||||
if (exists(defaultPath)) {
|
||||
return defaultPath
|
||||
} else {
|
||||
@@ -44,7 +41,7 @@ export default class ProjectJson {
|
||||
this.#json = {
|
||||
...this.#json,
|
||||
description: this.#template.description,
|
||||
title: this.#config.operators[this.#operatorName].title,
|
||||
title: __config.operators[__operator_name].title,
|
||||
general: {
|
||||
...this.#json.general,
|
||||
localization: this.#template.localization,
|
||||
@@ -57,7 +54,7 @@ export default class ProjectJson {
|
||||
}
|
||||
|
||||
#processYAML(template) {
|
||||
const matcher = new Matcher(template.description, '${', '}', this.#config.operators[this.#operatorName])
|
||||
const matcher = new Matcher(template.description, '${', '}', __config.operators[__operator_name])
|
||||
if (matcher.match() !== null) {
|
||||
template.description = matcher.process()
|
||||
}
|
||||
@@ -77,25 +74,25 @@ export default class ProjectJson {
|
||||
{
|
||||
key: "paddingleft",
|
||||
value: {
|
||||
value: this.#config.operators[this.#operatorName].viewport_left
|
||||
value: __config.operators[__operator_name].viewport_left
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "paddingright",
|
||||
value: {
|
||||
value: this.#config.operators[this.#operatorName].viewport_right
|
||||
value: __config.operators[__operator_name].viewport_right
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "paddingtop",
|
||||
value: {
|
||||
value: this.#config.operators[this.#operatorName].viewport_top
|
||||
value: __config.operators[__operator_name].viewport_top
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "paddingbottom",
|
||||
value: {
|
||||
value: this.#config.operators[this.#operatorName].viewport_bottom
|
||||
value: __config.operators[__operator_name].viewport_bottom
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
21
libs/vite.js
21
libs/vite.js
@@ -2,15 +2,6 @@ import path from 'path'
|
||||
import { createServer, build } from 'vite'
|
||||
|
||||
export default class Vite {
|
||||
#config
|
||||
#rootDir
|
||||
#operatorName
|
||||
|
||||
constructor(config, operatorName, rootDir) {
|
||||
this.#config = config
|
||||
this.#operatorName = operatorName
|
||||
this.#rootDir = rootDir
|
||||
}
|
||||
|
||||
dev() {
|
||||
; (async () => {
|
||||
@@ -28,17 +19,17 @@ export default class Vite {
|
||||
get #viteConfig() {
|
||||
return {
|
||||
base: "",
|
||||
publicDir: path.resolve(this.#rootDir, this.#config.folder.release, this.#operatorName),
|
||||
root: path.resolve(this.#rootDir),
|
||||
publicDir: path.resolve(__dirname, __config.folder.release, __operator_name),
|
||||
root: path.resolve(__dirname),
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': path.resolve(this.#rootDir, './src'),
|
||||
'!': path.resolve(this.#rootDir, this.#config.folder.operator, this.#operatorName),
|
||||
'#': path.resolve(this.#config.basedir, this.#config.folder.operator, this.#operatorName, `${this.#config.operators[this.#operatorName].filename}.json`),
|
||||
'@': path.resolve(__dirname, './src'),
|
||||
'!': path.resolve(__dirname, __config.folder.operator, __operator_name),
|
||||
'#': path.resolve(__config.basedir, __config.folder.operator, __operator_name, `${__config.operators[__operator_name].filename}.json`),
|
||||
},
|
||||
},
|
||||
build: {
|
||||
outDir: path.resolve(this.#rootDir, this.#config.folder.release, this.#operatorName),
|
||||
outDir: path.resolve(__dirname, __config.folder.release, __operator_name),
|
||||
emptyOutDir: false,
|
||||
chunkSizeWarningLimit: 10000,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user