deploy: add vm.Script wrapper, remove fs dependency in index.ts

This commit is contained in:
Christopher Monsanto
2020-08-10 03:46:21 -04:00
parent 8fb0f51b8f
commit 830c0fadfc
3 changed files with 44 additions and 42 deletions

View File

@@ -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));
}