feat(aklive2d): use cf pages to store assets (upload) [1/2]
This commit is contained in:
@@ -2,29 +2,29 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import sharp from "sharp";
|
||||
import { mkdir } from './file.js'
|
||||
|
||||
export default class Background {
|
||||
#backgroundFolder
|
||||
#backgroundOutputFolder
|
||||
#extractFolder
|
||||
#files
|
||||
|
||||
constructor() {
|
||||
this.#backgroundFolder = path.join(__projectRoot, __config.folder.operator, __config.folder.share, __config.folder.background);
|
||||
constructor(shareDir, targetDir) {
|
||||
this.#backgroundFolder = path.join(shareDir, __config.folder.background);
|
||||
this.#backgroundOutputFolder = path.join(targetDir, `_${__config.folder.background}`);
|
||||
this.#extractFolder = path.join(this.#backgroundFolder, 'extracted');
|
||||
mkdir(this.#backgroundOutputFolder);
|
||||
}
|
||||
|
||||
async process() {
|
||||
this.#files = fs.readdirSync(this.#extractFolder).filter((f) => {
|
||||
return f.endsWith('.png') && f.includes('_left');
|
||||
})
|
||||
if (this.#files.length !== fs.readdirSync(this.#backgroundFolder).length - 1) {
|
||||
await Promise.all(this.#files.map(async (f) => {
|
||||
const filenamePrefix = path.parse(f).name.replace('_left', '');
|
||||
await this.#composite(filenamePrefix, '.png');
|
||||
}))
|
||||
} else {
|
||||
console.log('Background images already exist, skip generation.')
|
||||
}
|
||||
await Promise.all(this.#files.map(async (f) => {
|
||||
const filenamePrefix = path.parse(f).name.replace('_left', '');
|
||||
await this.#composite(filenamePrefix, '.png');
|
||||
}))
|
||||
}
|
||||
|
||||
async #composite(filenamePrefix, fileExt) {
|
||||
@@ -45,7 +45,7 @@ export default class Background {
|
||||
left: metadata.width,
|
||||
},
|
||||
])
|
||||
.toFile(path.join(this.#backgroundFolder, `${filenamePrefix}${fileExt}`));
|
||||
.toFile(path.join(this.#backgroundOutputFolder, `${filenamePrefix}${fileExt}`));
|
||||
}
|
||||
|
||||
get files() {
|
||||
@@ -56,7 +56,7 @@ export default class Background {
|
||||
return this.#files.map((f) => {
|
||||
return {
|
||||
filename: f.replace('_left', ''),
|
||||
source: path.join(this.#backgroundFolder),
|
||||
source: path.join(this.#backgroundOutputFolder),
|
||||
target: path.join(publicAssetsDir, __config.folder.background)
|
||||
};
|
||||
})
|
||||
|
||||
66
libs/cf_pages.js
Normal file
66
libs/cf_pages.js
Normal file
@@ -0,0 +1,66 @@
|
||||
/* eslint-disable no-undef */
|
||||
import path from 'path';
|
||||
import { spawnSync } from 'child_process';
|
||||
import { readdirSync, fileTypeSync, writeSync } from './file.js';
|
||||
|
||||
export default class CFPages {
|
||||
#uploadPath = path.join(__projectRoot, __config.folder.operator_data);
|
||||
#gitignorePath = path.join(__projectRoot, '.gitignore');
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
upload() {
|
||||
const tree = this.#generateDirTree(this.#uploadPath);
|
||||
writeSync(JSON.stringify(tree, null), path.join(this.#uploadPath, 'index.json'));
|
||||
const wrangler = spawnSync('pnpm', ['wrangler', 'pages', 'deploy', this.#uploadPath]);
|
||||
|
||||
console.log('error', wrangler.error);
|
||||
console.log('stdout ', wrangler.stdout.toString());
|
||||
console.log('stderr ', wrangler.stderr.toString());
|
||||
}
|
||||
|
||||
download() {
|
||||
|
||||
}
|
||||
|
||||
#generateDirTree(dir) {
|
||||
const files = readdirSync(dir);
|
||||
let tree = {
|
||||
name: path.basename(dir),
|
||||
type: 'dir',
|
||||
children: []
|
||||
};
|
||||
for (const file of files) {
|
||||
if (this.#isToIgnore(file)) {
|
||||
continue;
|
||||
}
|
||||
const filePath = path.join(dir, file);
|
||||
const dirType = fileTypeSync(filePath);
|
||||
if (dirType === 'dir') {
|
||||
tree.children.push(this.#generateDirTree(filePath))
|
||||
} else {
|
||||
tree.children.push({
|
||||
name: file,
|
||||
type: 'file'
|
||||
});
|
||||
}
|
||||
}
|
||||
if (tree.children.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// TODO
|
||||
#isToIgnore(file) {
|
||||
switch (file) {
|
||||
case '.DS_Store':
|
||||
case 'index.json':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ export function getOperatorId(operatorConfig) {
|
||||
|
||||
export default class CharwordTable {
|
||||
#operatorIDs = Object.values(__config.operators).map(operator => { return getOperatorId(operator) })
|
||||
#charwordTablePath = path.join(__projectRoot, __config.folder.operator, __config.folder.share)
|
||||
#charwordTablePath = path.join(__projectRoot, __config.folder.auto_update_data)
|
||||
#charwordTableFile = path.join(this.#charwordTablePath, 'charword_table.json')
|
||||
#charwordTable = JSON.parse(readSync(this.#charwordTableFile)) || {
|
||||
config: {
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
/* eslint-disable no-undef */
|
||||
import path from 'path'
|
||||
import { writeSync, copy, readSync as readFile } from './file.js'
|
||||
import { read } from './yaml.js';
|
||||
import AssetsProcessor from './assets_processor.js'
|
||||
import EnvGenerator from './env_generator.js'
|
||||
|
||||
export default function ({ backgrounds, musicMapping }) {
|
||||
const extractedFolder = path.join(__projectRoot, __config.folder.operator, '_directory')
|
||||
export default function (dataDir, { backgrounds, musicMapping }) {
|
||||
const extractedFolder = path.join(dataDir, '_directory')
|
||||
const targetFolder = path.join(__projectRoot, __config.folder.release, __config.folder.directory);
|
||||
const directoryAssetFolder = path.join(__projectRoot, 'directory', 'src');
|
||||
const sourceFolder = path.join(__projectRoot, __config.folder.operator);
|
||||
@@ -24,7 +23,7 @@ export default function ({ backgrounds, musicMapping }) {
|
||||
|
||||
cur.workshopId = null
|
||||
try {
|
||||
cur.workshopId = JSON.parse(readFile(path.join(__projectRoot, __config.folder.operator, cur.link, 'project.json'))).workshopid
|
||||
cur.workshopId = JSON.parse(readFile(path.join(dataDir, cur.link, 'project.json'))).workshopid
|
||||
} catch (e) {
|
||||
console.log(`No workshop id for ${cur.link}!`)
|
||||
}
|
||||
|
||||
@@ -73,3 +73,11 @@ export function readdirSync(dir) {
|
||||
}
|
||||
return fs.readdirSync(dir)
|
||||
}
|
||||
|
||||
export function fileTypeSync(dir) {
|
||||
if (!exists(dir)) {
|
||||
console.warn(`Source ${dir} does not exist.`)
|
||||
return null
|
||||
}
|
||||
return fs.statSync(dir).isDirectory() ? 'dir' : 'file'
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import Downloader from "./downloader.js"
|
||||
|
||||
export default class Music {
|
||||
#downloader = new Downloader()
|
||||
#sharedPath = path.join(__projectRoot, __config.folder.operator, __config.folder.share)
|
||||
#sharedPath = path.join(__projectRoot, __config.folder.auto_update_data)
|
||||
|
||||
async process() {
|
||||
const { metaTable, audioDataTable } = await this.#download()
|
||||
@@ -49,8 +49,8 @@ export default class Music {
|
||||
}
|
||||
}
|
||||
|
||||
copy() {
|
||||
const musicFolder = path.join(__projectRoot, __config.folder.operator, __config.folder.share, __config.folder.music);
|
||||
copy(shareDir) {
|
||||
const musicFolder = path.join(shareDir, __config.folder.music);
|
||||
const musicTable = JSON.parse(readSync(path.join(this.#sharedPath, `music_table.json`)))
|
||||
const musicMapping = {}
|
||||
const musicToCopy = []
|
||||
|
||||
Reference in New Issue
Block a user