mirror of
https://github.com/smogon/sprites.git
synced 2026-03-24 19:05:23 -05:00
86 lines
2.3 KiB
TypeScript
86 lines
2.3 KiB
TypeScript
|
|
import fs from 'fs';
|
|
import nodePath from 'path';
|
|
import vm from 'vm';
|
|
import * as pathlib from './path.js';
|
|
import * as _spritename from './spritename.js';
|
|
|
|
export class ActionQueue {
|
|
private queue: {src : pathlib.Path, dst : pathlib.Path}[];
|
|
|
|
constructor() {
|
|
this.queue = [];
|
|
}
|
|
|
|
copy(src : pathlib.Path, dst : pathlib.Path) {
|
|
// TODO: detect conflicts
|
|
this.queue.push({src, dst});
|
|
}
|
|
|
|
describe() : {src : pathlib.Path, dst : pathlib.Path}[] {
|
|
return this.queue;
|
|
}
|
|
|
|
run(mode : 'link' | 'copy') {
|
|
for (const {src, dst} of this.queue) {
|
|
fs.mkdirSync(dst.dir, {recursive: true});
|
|
if (mode === 'link') {
|
|
fs.linkSync(pathlib.format(src), pathlib.format(dst));
|
|
} else {
|
|
fs.copyFileSync(pathlib.format(src), pathlib.format(dst));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class Env {
|
|
constructor(private srcDir : string,
|
|
private dstDir : string,
|
|
private queue : ActionQueue) {}
|
|
|
|
list(dir : string) : pathlib.Path[] {
|
|
const result = [];
|
|
for (const filename of fs.readdirSync(nodePath.join(this.srcDir, dir))) {
|
|
result.push(pathlib.path(filename, {dir}));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
get spritename() {
|
|
return _spritename;
|
|
}
|
|
|
|
copy(src : pathlib.PathLike, dst : pathlib.PathLike) {
|
|
const srcp = pathlib.path(src);
|
|
const dstp = pathlib.path(dst);
|
|
this.queue.copy(pathlib.join(this.srcDir, srcp), pathlib.join(this.dstDir, dstp));
|
|
}
|
|
}
|
|
|
|
export class Script {
|
|
private script : vm.Script;
|
|
|
|
constructor(code : string) {
|
|
this.script = new vm.Script(code);
|
|
}
|
|
|
|
runOnFile(src : pathlib.Path) : pathlib.Path {
|
|
const input = pathlib.update(src, {dir: ""});
|
|
const result = this.script.runInNewContext({
|
|
spritename: _spritename,
|
|
path: input,
|
|
...input
|
|
});
|
|
if (result === undefined) {
|
|
throw new Error(`undefined output on ${pathlib.format(src)}`);
|
|
}
|
|
const output = pathlib.path(input, result);
|
|
return output;
|
|
}
|
|
|
|
run(srcDir : string, dstDir : string, queue : ActionQueue) {
|
|
this.script.runInNewContext(new Env(srcDir, dstDir, queue));
|
|
}
|
|
}
|
|
|