zenstack
zenstack copied to clipboard
Zenstack deleted all my code projects
Description
I ran this
npx zenstack generate --schema schema/main.zmodel --output ..
It deleted everything in the directory above the current directory.
Expected behavior I didn't expect it delete all my files.
It should check the output directory does not include the whole project before deleting it.
Could do a check similar to what Prisma does: https://github.com/prisma/prisma/blob/main/packages/client/src/generation/generateClient.ts#L772
Prisma passes the generated name to ensure it is the generated package.
If this route sounds good I could create a pull request.
packages\sdk\src\utils.ts
export function ensureEmptyDir(dir: string) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
return;
}
const stats = fs.statSync(dir);
if (stats.isDirectory()) {
// *** If directory has files and we find the generated package ***
// *** Then it is safe to delete recursively ***
if (fs.readdirSync(dir).length !== 0 && require(`${dir}/package.json`).name?.startsWith(".zenstack")) {
fs.rmSync(dir, { recursive: true });
fs.mkdirSync(dir, { recursive: true });
} else {
throw new Error(`Path "${dir}" already exists and contains files that were not generated by zenstack`);
}
} else {
throw new Error(`Path "${dir}" already exists and is not a directory`);
}
}