Skip to content

Commit

Permalink
feat: FileHandle.writeFile
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Apr 23, 2022
1 parent a8feb13 commit cc04d53
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/yarnpkg-fslib/sources/patchFs/FileHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ export class FileHandle<P extends Path> {
data: string | Uint8Array,
options?: (ObjectEncodingOptions & FlagAndOpenMode & Abortable) | BufferEncoding | null,
): Promise<void> {
throw new Error(`Method not implemented.`);
const encoding = (typeof options === `string` ? options : options?.encoding) ?? undefined;
return this._baseFs.writeFilePromise(this.fd, data, encoding);
}

async write(...args: WriteArgsString): Promise<{ bytesWritten: number, buffer: string }>
Expand Down
15 changes: 15 additions & 0 deletions packages/yarnpkg-fslib/tests/patchedFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ describe(`patchedFs`, () => {
});
});

it(`should support FileHandle.writeFile`, async () => {
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));

await xfs.mktempPromise(async dir => {
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);

const fd = await patchedFs.promises.open(filepath, `w`);
await fd.writeFile(`foo`);
await fd.writeFile(`bar`);
await fd.close();

await expect(patchedFs.promises.readFile(filepath, `utf8`)).resolves.toEqual(`foobar`);
});
});

it(`should support FileHandle.appendFile`, async () => {
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));

Expand Down

0 comments on commit cc04d53

Please sign in to comment.