Skip to content

Commit

Permalink
feat(store-file-system): check if file exists before creating
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Oct 8, 2024
1 parent e394e4a commit 1aa437e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/store-file-system/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export default class FileSystemStore {
const absolutePath = this.#absolutePath(filePath);
const dirname = path.dirname(absolutePath);

if (existsSync(absolutePath)) {
return;
}

if (!existsSync(dirname)) {
await fs.mkdir(dirname, { recursive: true });
}
Expand Down
8 changes: 8 additions & 0 deletions packages/store-file-system/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ describe("store-file-system", () => {
assert.equal(await fs.readFile("directory/foo.txt", "utf8"), "foo");
});

it("Doesn’t create file if already exists", async () => {
mockFs({ "directory/bar.txt": "foo" });

const result = await fileSystem.createFile("bar.txt", "foo");

assert.equal(result, undefined);
});

it("Throws error creating file", async () => {
mockFs();

Expand Down

0 comments on commit 1aa437e

Please sign in to comment.