cp-file
Copy a file
Last updated 4 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install cp-file 
SYNC missed versions from official npm registry.

cp-file Build Status Coverage Status

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

const cpFile = require('cp-file');

(async () => {
	await cpFile('source/unicorn.png', 'destination/unicorn.png');
	console.log('File copied');
})();

API

cpFile(source, destination, options?)

Returns a Promise that resolves when the file is copied.

cpFile.sync(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.

cpFile.on('progress', handler)

Progress reporting. Only available when using the async method.

handler(data)

Type: Function

data
{
	sourcePath: string,
	destinationPath: string,
	size: number,
	writtenBytes: number,
	percent: number
}
  • source and destination are absolute paths.
  • size and writtenBytes are in bytes.
  • percent is a value between 0 and 1.
Notes
  • For empty files, the progress event is emitted only once.
  • The .on() method is available only right after the initial cpFile() call. So make sure you add a handler before .then():
const cpFile = require('cp-file');

(async () => {
	await cpFile(source, destination).on('progress', data => {
		// …
	});
})();

Related

  • 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

Current Tags

  • 9.0.0                                ...           latest (4 months ago)

27 Versions

  • 9.0.0                                ...           4 months ago
  • 8.0.1                                ...           5 months ago
  • 8.0.0                                ...           5 months ago
  • 7.0.0                                ...           a year ago
  • 6.2.0                                ...           a year ago
  • 6.1.0                                ...           a year ago
  • 6.0.0                                ...           2 years ago
  • 5.0.0                                ...           3 years ago
  • 4.2.0                                ...           3 years ago
  • 4.1.1                                ...           4 years ago
  • 4.1.0                                ...           4 years ago
  • 4.0.1                                ...           4 years ago
  • 4.0.0                                ...           4 years ago
  • 3.2.0                                ...           4 years ago
  • 3.1.0                                ...           5 years ago
  • 3.0.1                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.3.0                                ...           5 years ago
  • 2.2.1                                ...           5 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.0                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 13
This Week 13
This Month 5,319
Last Day 140
Last Week 3,248
Last Month 13,986
Dependencies (4)
Dev Dependencies (10)
Dependents (203)

Copyright 2014 - 2016 © taobao.org |