From 24edce9b94686bae48056d72f8bc02bedaa65b1a Mon Sep 17 00:00:00 2001 From: Christopher Monsanto Date: Sat, 8 Aug 2020 04:41:13 -0400 Subject: [PATCH] deploy/script: factor out more cli logic into runOnFile --- tools/deploy/index.ts | 7 +------ tools/deploy/script.test.ts | 15 ++++----------- tools/deploy/script.ts | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/tools/deploy/index.ts b/tools/deploy/index.ts index 4c6a2812..6c866ff8 100644 --- a/tools/deploy/index.ts +++ b/tools/deploy/index.ts @@ -36,12 +36,7 @@ program for (const file of files) { const src = pathlib.path(file); - const input = pathlib.update(src, {dir: ""}); - const result = scr.runOnFile(input); - if (result === undefined) { - throw new Error(`undefined output on ${file}`); - } - const output = pathlib.path(input, result); + const output = scr.runOnFile(src); const dst = pathlib.join(outputDir, output); aq.copy(src, dst); } diff --git a/tools/deploy/script.test.ts b/tools/deploy/script.test.ts index 5d5dd7bc..ff4b60ad 100644 --- a/tools/deploy/script.test.ts +++ b/tools/deploy/script.test.ts @@ -2,17 +2,10 @@ import * as pathlib from './path.js'; import * as script from './script.js'; -test('aq', () => { - const aq = new script.ActionQueue(); - const src = pathlib.path('/foo/bar/pikachu.png'); - const input = pathlib.update(src, {dir: ""}); +test('correct output', () => { const scr = new script.Script('({name: "25"})'); - const result = scr.runOnFile(input); - const output = pathlib.path(input, result); - const dst = output; - aq.copy(src, dst); - - expect(aq.describe()).toEqual([{src: pathlib.path('/foo/bar/pikachu.png'), - dst: pathlib.path('25.png')}]); + const src = pathlib.path('/foo/bar/pikachu.png'); + const dst = scr.runOnFile(src); + expect(dst).toEqual(pathlib.path('25.png')); }); diff --git a/tools/deploy/script.ts b/tools/deploy/script.ts index fae5c1fc..686b9472 100644 --- a/tools/deploy/script.ts +++ b/tools/deploy/script.ts @@ -64,8 +64,18 @@ export class Script { this.script = new vm.Script(code); } - runOnFile(p : pathlib.Path) : any { - return this.script.runInNewContext({spritename: _spritename, path: p, ...p}); + 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) {