Skip to content

Commit

Permalink
fix: iterate over all file name holding objects
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed May 21, 2019
1 parent 44a0b7c commit ff63664
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
32 changes: 19 additions & 13 deletions src/cleanup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ async function rm(file) {
}
}

async function iterate(o,cb)
{
switch(typeof o) {
case 'string':
return cb(o);
}

if(Array.isArray(o)) {
for(const x of o) {
await iterate(x,cb);
}
}
for(const k in o) {
await iterate(o[k],cb);
}
}

export async function cleanup(context, stagingDir) {
for (const name of await globby(["**/package.json"], {
cwd: stagingDir
Expand All @@ -27,19 +44,8 @@ export async function cleanup(context, stagingDir) {
await Promise.all(
["unpkg", "jspm", "shim", "browser", "testling", "source"].map(
async key => {
if (pkg[key] !== undefined) {
if (typeof pkg[key] !== "string") {
const o = pkg[key];
for (const k of o) {
await rm(join(stagingDir, o[k]));
}
delete pkg[key];
} else {
const file = join(stagingDir, pkg[key]);
delete pkg[key];
return rm(file);
}
}
await iterate(pkg[key], async o => { rm(join(stagingDir, o)) });
delete pkg[key];
}
)
);
Expand Down
9 changes: 8 additions & 1 deletion tests/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,12 @@
"units": {
"${name}": "systemd/${name}*"
}
}
},
"browser": {
"a": "src/a",
"b": "src/b"
},
"testling": [
"c"
]
}

0 comments on commit ff63664

Please sign in to comment.