Skip to content

Commit

Permalink
Exclude package directories without package.jsons rather than prompti…
Browse files Browse the repository at this point in the history
…ng to delete them, this aligns with the behaviour of package managers. (#281)
  • Loading branch information
emmatown committed Apr 29, 2020
1 parent e63d6fa commit 8e5c29d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-queens-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preconstruct/cli": patch
---

Exclude package directories without package.jsons rather than prompting to delete them, this aligns with the behaviour of package managers.
33 changes: 7 additions & 26 deletions packages/cli/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,46 +94,27 @@ export class Project extends Item {
expandDirectories: false
});

let dirsWithoutPkgJson: string[] = [];
let lastErr;
let packages: Package[] = [];

let packages = await Promise.all(
await Promise.all(
filenames.map(async x => {
try {
return await Package.create(x, this);
packages.push(await Package.create(x, this));
} catch (err) {
if (
err.code === "ENOENT" &&
err.path === nodePath.join(x, "package.json")
) {
lastErr = err;
dirsWithoutPkgJson.push(x);
return (undefined as any) as Package;
return;
}
throw err;
}
})
);
if (dirsWithoutPkgJson.length) {
error(
"there are some package directories that do not have package.jsons\nthis is often caused by switching branches.\n\n" +
dirsWithoutPkgJson.join("\n") +
"\n"
);
if (
!(await promptConfirm(
"would you like preconstruct to delete these directories automatically?"
))
) {
throw lastErr;
}
await Promise.all(dirsWithoutPkgJson.map(dir => fs.remove(dir)));
return this._packages();
}

const errored = (await allSettled(
packages.map(pkg => validateIncludedFiles(pkg))
)).find(result => result.status === "rejected");
const errored = (
await allSettled(packages.map(pkg => validateIncludedFiles(pkg)))
).find(result => result.status === "rejected");

if (errored) {
// TS can't refine type based on .find predicate
Expand Down

0 comments on commit 8e5c29d

Please sign in to comment.