mirror of
https://github.com/smogon/sprites.git
synced 2026-04-20 00:37:33 -05:00
deploy: add vm.Script wrapper, remove fs dependency in index.ts
This commit is contained in:
parent
8fb0f51b8f
commit
830c0fadfc
|
|
@ -3,7 +3,6 @@ import program from 'commander';
|
|||
import * as script from './script.js';
|
||||
import * as pathlib from './path.js';
|
||||
import nodePath from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
function collect(value : string, previous : string[]) {
|
||||
return previous.concat([value]);
|
||||
|
|
@ -20,21 +19,20 @@ program
|
|||
// from rename(1)
|
||||
.option('-n, --no-act', 'No act')
|
||||
.action(async (files : string[], {eval: expr, module: mod, output: outputDir, /*tag: tags,*/ act}) => {
|
||||
let code : string;
|
||||
let scr;
|
||||
if (expr !== undefined) {
|
||||
code = `(${expr})`;
|
||||
scr = new script.Script(expr, 'expr');
|
||||
} else if (mod !== undefined) {
|
||||
code = fs.readFileSync(mod, 'utf8');
|
||||
scr = new script.Script(mod, 'file');
|
||||
} else {
|
||||
throw new Error(`one of -e or -m must be provided`);
|
||||
}
|
||||
|
||||
const aq = new script.ActionQueue;
|
||||
const scr = new script.Script(code);
|
||||
|
||||
|
||||
for (const file of files) {
|
||||
const src = pathlib.path(file);
|
||||
const output = scr.runOnFile(src);
|
||||
const output = script.runOnFile(scr, src);
|
||||
const dst = pathlib.join(outputDir, output);
|
||||
aq.copy(src, dst);
|
||||
}
|
||||
|
|
@ -54,9 +52,8 @@ program
|
|||
const aq = new script.ActionQueue;
|
||||
|
||||
for (const file of scripts) {
|
||||
const code = fs.readFileSync(file, 'utf8');
|
||||
const scr = new script.Script(code);
|
||||
scr.run(nodePath.dirname(file), aq);
|
||||
const scr = new script.Script(file, 'file');
|
||||
script.run(scr, nodePath.dirname(file), aq);
|
||||
}
|
||||
|
||||
aq.join(outputDir);
|
||||
|
|
|
|||
|
|
@ -3,20 +3,16 @@ import * as pathlib from './path.js';
|
|||
import * as script from './script.js';
|
||||
|
||||
test('runOnFile', () => {
|
||||
const scr = new script.Script('({name: "25"})');
|
||||
const scr = new script.Script('({name: "25"})', 'expr');
|
||||
const src = pathlib.path('/foo/bar/pikachu.png');
|
||||
const dst = scr.runOnFile(src);
|
||||
const dst = script.runOnFile(scr, src);
|
||||
expect(dst).toEqual(pathlib.path('25.png'));
|
||||
});
|
||||
|
||||
test('run identity', () => {
|
||||
const aq = new script.ActionQueue();
|
||||
const scr = new script.Script(`
|
||||
for (const p of list(".")) {
|
||||
copy(p, p);
|
||||
}
|
||||
`);
|
||||
scr.run("testsrc", aq);
|
||||
const scr = new script.Script(` list(".").forEach(p => copy(p, p))`, 'expr');
|
||||
script.run(scr, "testsrc", aq);
|
||||
expect(aq.describe()).toEqual(expect.arrayContaining([
|
||||
{src: pathlib.path('testsrc/32.png'), dst: pathlib.path("./32.png")},
|
||||
{src: pathlib.path('testsrc/192-g-vsmogon.png'), dst: pathlib.path("./192-g-vsmogon.png")},
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ export class ActionQueue {
|
|||
print() {
|
||||
for (const {src, dst} of this.queue) {
|
||||
console.log(`${pathlib.format(src)} ==> ${pathlib.format(dst)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
run(mode : 'link' | 'copy') {
|
||||
|
|
@ -45,6 +45,24 @@ export class ActionQueue {
|
|||
}
|
||||
}
|
||||
|
||||
export class Script extends vm.Script {
|
||||
public readonly filename : string | null;
|
||||
|
||||
constructor(x : string, type : 'file' | 'expr') {
|
||||
let code : string;
|
||||
let filename : string | null = null;
|
||||
if (type === 'expr') {
|
||||
// Force expression parsing
|
||||
code = `(${x})`;
|
||||
} else {
|
||||
code = fs.readFileSync(x, 'utf8');
|
||||
filename = x;
|
||||
}
|
||||
super(code, filename !== null ? {filename} : undefined);
|
||||
this.filename = filename;
|
||||
}
|
||||
}
|
||||
|
||||
const ENV_PROTO = {
|
||||
spritename
|
||||
};
|
||||
|
|
@ -69,29 +87,20 @@ function makeEnv(srcDir : string, queue: ActionQueue) {
|
|||
}
|
||||
}
|
||||
|
||||
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({
|
||||
__proto__: ENV_PROTO,
|
||||
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, queue : ActionQueue) {
|
||||
this.script.runInNewContext(makeEnv(srcDir, queue));
|
||||
export function runOnFile(scr : Script, src : pathlib.Path) : pathlib.Path {
|
||||
const input = pathlib.update(src, {dir: ""});
|
||||
const result = scr.runInNewContext({
|
||||
__proto__: ENV_PROTO,
|
||||
path: input,
|
||||
...input
|
||||
});
|
||||
if (result === undefined) {
|
||||
throw new Error(`undefined output on ${pathlib.format(src)}`);
|
||||
}
|
||||
const output = pathlib.path(input, result);
|
||||
return output;
|
||||
}
|
||||
|
||||
export function run(scr : Script, srcDir : string, queue : ActionQueue) {
|
||||
scr.runInNewContext(makeEnv(srcDir, queue));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user