deploy: check that ActionQueue dst isnt absolute & no duplicates

This commit is contained in:
Christopher Monsanto 2020-08-10 05:37:12 -04:00
parent 1d9d10cf63
commit 30ffa1f2d0
2 changed files with 33 additions and 10 deletions

View File

@ -2,6 +2,15 @@
import * as pathlib from './path.js';
import * as script from './script.js';
test('aq', () => {
const aq = new script.ActionQueue();
expect(() => {
aq.copy("foo", "bar");
aq.copy("baz", "./bar");
}).toThrow();
expect(() => aq.copy("foo", "/bar")).toThrow();
});
test('runOnFile', () => {
const scr = new script.Script('({name: "25"})', 'expr');
const src = pathlib.path('/foo/bar/pikachu.png');
@ -14,8 +23,8 @@ test('run identity', () => {
const scr = new script.Script(` list(".").forEach(p => copy(p, p))`, 'expr');
script.run(scr, "testsrc", aq);
expect(aq.describe()).toEqual(expect.arrayContaining([
{src: 'testsrc/32.png', dst: "./32.png"},
{src: 'testsrc/192-g-vsmogon.png', dst: "./192-g-vsmogon.png"},
{src: 'testsrc/32.png', dst: "32.png"},
{src: 'testsrc/192-g-vsmogon.png', dst: "192-g-vsmogon.png"},
]));
});

View File

@ -6,35 +6,49 @@ import * as pathlib from './path.js';
import * as spritename from './spritename.js';
export class ActionQueue {
private queue: {src : string, dst : string}[];
private map: Map<string, string>;
constructor() {
this.queue = [];
this.map = new Map;
}
copy(src : string, dst : string) {
// TODO: detect conflicts
this.queue.push({src, dst});
dst = nodePath.normalize(dst);
if (nodePath.isAbsolute(dst)) {
throw new Error(`destination ${dst} is absolute`);
}
const result = this.map.get(dst);
if (result !== undefined) {
throw new Error(`already copying ${result} to ${dst}`);
}
this.map.set(dst, src);
}
describe() : {src : string, dst : string}[] {
return this.queue;
const result = [];
for (const [dst, src] of this.map) {
result.push({src,dst});
}
return result;
}
join(dir : string) {
for (const pair of this.queue) {
pair.dst = nodePath.join(dir, pair.dst);
const newMap = new Map;
for (const [dst, src] of this.map) {
newMap.set(nodePath.join(dir, dst), src);
}
this.map = newMap;
}
print() {
for (const {src, dst} of this.queue) {
for (const {src, dst} of this.describe()) {
console.log(`${src} ==> ${dst}`);
}
}
run(mode : 'link' | 'copy') {
for (const {src, dst} of this.queue) {
for (const {src, dst} of this.describe()) {
fs.mkdirSync(nodePath.dirname(dst), {recursive: true});
if (mode === 'link') {
fs.linkSync(src, dst);