Skip to content

Commit

Permalink
feat: 🎸 add unlinkSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 4caf28b commit 4b3444d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
await fs.promises.writeFile('/truncated.txt', 'truncate here: abcdefghijklmnopqrstuvwxyz');
fs.truncateSync('/truncated.txt', 14);
strictEqual(fs.readFileSync('/truncated.txt', 'utf8'), 'truncate here:');

console.log('unlinkSync() - can delete a file');
await fs.promises.writeFile('/delete-me.txt', 'abc');
fs.unlinkSync('/delete-me.txt');
strictEqual(fs.existsSync('/delete-me.txt'), false);
};

const main = async () => {
Expand Down
6 changes: 5 additions & 1 deletion src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,11 +902,15 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
this.truncateSync(filename, len);
};

public readonly unlinkSync: FsSynchronousApi['unlinkSync'] = (path: misc.PathLike): void => {
const filename = pathToFilename(path);
this.getSyncAdapter().call('unlink', [filename]);
};

public readonly openSync: FsSynchronousApi['openSync'] = notSupported;
public readonly readdirSync: FsSynchronousApi['readdirSync'] = notSupported;
public readonly readSync: FsSynchronousApi['readSync'] = notSupported;
public readonly realpathSync: FsSynchronousApi['realpathSync'] = notSupported;
public readonly unlinkSync: FsSynchronousApi['unlinkSync'] = notSupported;
public readonly writeSync: FsSynchronousApi['writeSync'] = notSupported;

public readonly chmodSync: FsSynchronousApi['chmodSync'] = noop;
Expand Down
1 change: 1 addition & 0 deletions src/fsa-to-node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface FsaNodeSyncAdapterApi {
mkdir(req: [filename: string, opts?: misc.TMode | opts.IMkdirOptions]): string | undefined;
mkdtemp(req: [filename: string, opts?: misc.TMode | opts.IOptions]): string;
trunc(req: [filename: string, len: number]): void;
unlink(req: [filename: string]): void;
}

export interface FsaNodeSyncAdapter {
Expand Down
3 changes: 3 additions & 0 deletions src/fsa-to-node/worker/FsaNodeSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@ export class FsaNodeSyncWorker {
trunc: async ([filename, len]): Promise<void> => {
await this.fs.promises.truncate(filename, len);
},
unlink: async ([filename]): Promise<void> => {
await this.fs.promises.unlink(filename);
},
};
}

0 comments on commit 4b3444d

Please sign in to comment.