Skip to content

Commit

Permalink
feat: 🎸 add renameSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 18, 2023
1 parent 5fc1bac commit 5b1cd63
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
console.log('copyFileSync() - can copy a file');
fs.copyFileSync('/cool.txt', '/cool (Copy).txt');
strictEqual(fs.readFileSync('/cool (Copy).txt', 'utf8'), 'worlds!');

console.log('renameSync() - can move a file');
fs.renameSync('/cool (Copy).txt', '/dir/very-cool.txt');
strictEqual(fs.readFileSync('/dir/very-cool.txt', 'utf8'), 'worlds!');
};

const main = async () => {
Expand Down
8 changes: 7 additions & 1 deletion src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,13 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
adapter.call('copy', {src: srcFilename, dst: destFilename, flags});
};

public readonly renameSync: FsSynchronousApi['renameSync'] = (oldPath: misc.PathLike, newPath: misc.PathLike): void => {
const srcFilename = pathToFilename(oldPath);
const destFilename = pathToFilename(newPath);
const adapter = this.getSyncAdapter();
adapter.call('move', {src: srcFilename, dst: destFilename});
};

public readonly ftruncateSync: FsSynchronousApi['ftruncateSync'] = notSupported;
public readonly linkSync: FsSynchronousApi['linkSync'] = notSupported;
public readonly mkdirSync: FsSynchronousApi['mkdirSync'] = notSupported;
Expand All @@ -860,7 +867,6 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
public readonly readlinkSync: FsSynchronousApi['readlinkSync'] = notSupported;
public readonly readSync: FsSynchronousApi['readSync'] = notSupported;
public readonly realpathSync: FsSynchronousApi['realpathSync'] = notSupported;
public readonly renameSync: FsSynchronousApi['renameSync'] = notSupported;
public readonly rmdirSync: FsSynchronousApi['rmdirSync'] = notSupported;
public readonly rmSync: FsSynchronousApi['rmSync'] = notSupported;
public readonly symlinkSync: FsSynchronousApi['symlinkSync'] = notSupported;
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 @@ -12,6 +12,7 @@ export interface FsaNodeSyncAdapterApi {
writeFile(req: { filename: string; data: Uint8Array; opts?: opts.IWriteFileOptions }): void;
appendFile(req: { filename: string; data: Uint8Array; opts?: opts.IAppendFileOptions }): void;
copy(req: { src: string; dst: string, flags?: number }): void;
move(req: { src: string; dst: 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 @@ -138,5 +138,8 @@ export class FsaNodeSyncWorker {
copy: async ({ src, dst, flags }): Promise<void> => {
await this.fs.promises.copyFile(src, dst, flags);
},
move: async ({ src, dst }): Promise<void> => {
await this.fs.promises.rename(src, dst);
},
};
}

0 comments on commit 5b1cd63

Please sign in to comment.