安装
npm install cp-file
yarn add cp-file
pnpm add cp-file
bun add cp-file
README

cp-file

Copy a file

Highlights

  • Fast by using streams in the async version and fs.copyFileSync() in the synchronous version.
  • Resilient by using graceful-fs.
  • User-friendly by creating non-existent destination directories for you.
  • Can be safe by turning off overwriting.
  • Preserves file mode, but not ownership.
  • User-friendly errors.

Install

npm install cp-file

Usage

import {copyFile} from 'cp-file';

await copyFile('source/unicorn.png', 'destination/unicorn.png');
console.log('File copied');

API

copyFile(source, destination, options?)

Returns a Promise that resolves when the file is copied.

copyFileSync(source, destination, options?)

source

Type: string

The file you want to copy.

destination

Type: string

Where you want the file copied.

options

Type: object

overwrite

Type: boolean
Default: true

Overwrite existing destination file.

cwd

Type: string
Default: process.cwd()

The working directory to find source files.

The source and destination path are relative to this.

directoryMode

Type: number
Default: 0o777

Permissions for created directories.

It has no effect on Windows.

onProgress

Type: (progress: ProgressData) => void

The given function is called whenever there is measurable progress.

Only available when using the async method.

ProgressData
{
	sourcePath: string,
	destinationPath: string,
	size: number,
	writtenBytes: number,
	percent: number
}
  • sourcePath and destinationPath are absolute paths.
  • size and writtenBytes are in bytes.
  • percent is a value between 0 and 1.
Notes
  • For empty files, the onProgress callback function is emitted only once.
import {copyFile} from 'cp-file';

await copyFile(source, destination, {
	onProgress: progress => {
		// …
	}
});
  • cpy - Copy files
  • cpy-cli - Copy files on the command-line
  • move-file - Move a file
  • make-dir - Make a directory and its parents if needed
版本列表
11.0.0 2023-11-05
10.0.0 2022-06-21
9.1.0 2021-06-13
9.0.0 2020-03-05
8.0.1 2020-02-24
8.0.0 2020-02-24
7.0.0 2019-04-19
6.2.0 2019-04-04
6.1.0 2019-03-04
6.0.0 2018-05-12
5.0.0 2017-09-22
4.2.0 2017-05-11
4.1.1 2016-11-25
4.1.0 2016-10-07
4.0.1 2016-10-03
4.0.0 2016-10-03
3.2.0 2016-07-22
3.1.0 2015-11-14
3.0.1 2015-10-26
3.0.0 2015-10-19
2.3.0 2015-10-15
2.2.1 2015-06-30
2.2.0 2015-04-24
2.1.0 2015-03-14
2.0.2 2015-01-21
2.0.1 2015-01-21
2.0.0 2015-01-18
1.0.0 2014-08-17
0.1.1 2014-06-28
0.1.0 2014-06-28