Git Product home page Git Product logo

cp-file's Introduction

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

cp-file's People

Contributors

sindresorhus avatar schnittstabil avatar coreyfarrell avatar bendingbender avatar kevva avatar yurysolovyov avatar boneskull avatar feross avatar jamiebuilds avatar floatdrop avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.