1. cp-file
Copy a file
cp-file
Package: cp-file
Created by: sindresorhus
Last modified: Sun, 05 Nov 2023 07:49:58 GMT
Version: 11.0.0
License: MIT
Downloads: 10,125,011
Repository: https://github.com/sindresorhus/cp-file

Install

npm install cp-file
yarn add cp-file

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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code