Skip to content

Commit

Permalink
feat: 🎸 add copyFileSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 57192fe commit 7e0137c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions demo/fsa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
console.log('appendFileSync() - can append to an existing file');
fs.appendFileSync('/cool.txt', '!');
strictEqual(fs.readFileSync('/cool.txt', 'utf8'), 'worlds!');

console.log('copyFileSync() - can copy a file');
fs.copyFileSync('/cool.txt', '/cool (Copy).txt');
strictEqual(fs.readFileSync('/cool (Copy).txt', 'utf8'), 'worlds!');
};

const main = async () => {
Expand Down
9 changes: 7 additions & 2 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,13 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
}
};

public readonly copyFileSync: FsSynchronousApi['copyFileSync'] = notSupported;
public readonly copyFileSync: FsSynchronousApi['copyFileSync'] = (src: misc.PathLike, dest: misc.PathLike, flags?: misc.TFlagsCopy): void => {
const srcFilename = pathToFilename(src);
const destFilename = pathToFilename(dest);
const adapter = this.getSyncAdapter();
adapter.call('copy', {src: srcFilename, dst: destFilename, flags});
};

public readonly ftruncateSync: FsSynchronousApi['ftruncateSync'] = notSupported;
public readonly linkSync: FsSynchronousApi['linkSync'] = notSupported;
public readonly mkdirSync: FsSynchronousApi['mkdirSync'] = notSupported;
Expand All @@ -861,7 +867,6 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
public readonly truncateSync: FsSynchronousApi['truncateSync'] = notSupported;
public readonly unlinkSync: FsSynchronousApi['unlinkSync'] = notSupported;
public readonly writeSync: FsSynchronousApi['writeSync'] = notSupported;

public readonly chmodSync: FsSynchronousApi['chmodSync'] = noop;
public readonly chownSync: FsSynchronousApi['chownSync'] = noop;
public readonly fchmodSync: FsSynchronousApi['fchmodSync'] = 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 @@ -11,6 +11,7 @@ export interface FsaNodeSyncAdapterApi {
readFile(req: { filename: string; opts?: opts.IReadFileOptions }): Uint8Array;
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;
}

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 @@ -135,5 +135,8 @@ export class FsaNodeSyncWorker {
appendFile: async ({ filename, data, opts }): Promise<void> => {
await this.fs.promises.appendFile(filename, data, { ...opts, encoding: 'buffer' });
},
copy: async ({ src, dst, flags }): Promise<void> => {
await this.fs.promises.copyFile(src, dst, flags);
},
};
}

0 comments on commit 7e0137c

Please sign in to comment.