Skip to content

Commit

Permalink
feat: support mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 22, 2023
1 parent 489e421 commit 5b46f39
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ import { copy } from 'rcpy'; // "copy" is an alias of "rcpy"
- `option`: `RcpyOption` optional.
- `dereference`: `boolean` optional. whether to dereference symbolic links, default to `false`.
- `force`: `boolean` optional. whether to overwrite existing file/directory, default to `true`. Note that the copy operation will silently fail if you set this to false and the destination exists. Use the `errorOnExist` option to change this behavior.
- `overwrite`: `boolean` optional. The alias of `force`, serves as a compatibility option for `fs-extra`.
- `overwrite`: `boolean` optional. Deprecated, now is the alias of `force`. Serves as a compatibility option for `fs-extra`.
- `errorOnExist`: `boolean` optional. whether to throw an error if `dest` already exists, default to `false`.
- `filter`: `(src: string, dest: string) => boolean | Promise<boolean>` optional. filter copied files/directories, return `true` to copy, `false` to skip.
- `mode`: `number` optional. modifiers for copy operation, default to `0`. See `mode` flag of [`fs.copyFile()`](https://nodejs.org/api/fs.html#fscopyfilesrc-dest-mode-callback)
- `preserveTimestamps`: `boolean` optional. whether to preserve file timestamps, default to `false`, where the behavior is OS-dependent.
- `concurrency`: `number` optional. the number of concurrent copy operations, default to `32`.

Expand All @@ -53,6 +54,15 @@ import { copy } from 'rcpy'; // "copy" is an alias of "rcpy"
- Asynchronous and Promise-based API only. No synchronous API, no Node.js callback style API.
- Use `require('util').callbackify` to convert `rcpy` to Node.js callback style API.P

## Differences between `rcpy` and Node.js `fs.cp()`

- Doesn't support `recursive` option.
- `rcpy` will always copy directories' content recursively.
- Doesn't support `verbatimSymlinks` option.
- `rcpy` will always perform path resolution for symlinks if `dereference` option is enabled.
- Extra `concurrency` option.
- `rcpy` will use this option to limit the number of concurrent copy operations to prevent `EMFILE` error.

## License

[MIT](./LICENSE)
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface RcpyOption {
* @default true
*/
overwrite?: boolean,
/** @default 0 */
mode?: number,
/** @default false */
preserveTimestamps?: boolean,
/** @default false */
Expand All @@ -35,6 +37,7 @@ const rcpy = async (src: string, dest: string, opt: RcpyOption = {}): Promise<vo
force: opt.overwrite ?? true,
overwrite: true,
errorOnExist: false,
mode: 0,
preserveTimestamps: false,
concurrency: 32
}, opt);
Expand Down Expand Up @@ -92,7 +95,7 @@ const rcpy = async (src: string, dest: string, opt: RcpyOption = {}): Promise<vo
}

async function copyFile(srcStat: fs.Stats, src: string, dest: string) {
await fsp.copyFile(src, dest);
await fsp.copyFile(src, dest, _opt.mode);

if (_opt.preserveTimestamps) {
// Make sure the file is writable before setting the timestamp
Expand Down

0 comments on commit 5b46f39

Please sign in to comment.