2.0.14 • Published 2 years ago

recursive-copy v2.0.14

Weekly downloads
109,728
License
ISC
Repository
github
Last release
2 years ago

recursive-copy

npm version Stability Build Status Windows Build Status

Simple, flexible file copy utility

Features

  • Recursively copy whole directory hierarchies
  • Choose which files are copied by passing a filter function, regular expression or glob
  • Rename files dynamically, including changing the output path
  • Transform file contents using streams
  • Choose whether to overwrite existing files
  • Choose whether to copy system files
  • Filters out junk files by default
  • Uses graceful-fs and mkdirp to avoid filesystem errors
  • Emits start, finish and error events for each file that is processed
  • Optional promise-based interface

Examples

Node-style callback interface

var copy = require('recursive-copy');

copy('src', 'dest', function(error, results) {
	if (error) {
		console.error('Copy failed: ' + error);
	} else {
		console.info('Copied ' + results.length + ' files');
	}
});

Promise interface

var copy = require('recursive-copy');

copy('src', 'dest')
	.then(function(results) {
		console.info('Copied ' + results.length + ' files');
	})
	.catch(function(error) {
		console.error('Copy failed: ' + error);
	});

ES2015+ usage

import copy from 'recursive-copy';

try {
	const results = await copy('src', 'dest');
	console.info('Copied ' + results.length + ' files');
} catch (error) {
	console.error('Copy failed: ' + error);
}

Advanced options

var copy = require('recursive-copy');

var path = require('path');
var through = require('through2');

var options = {
	overwrite: true,
	expand: true,
	dot: true,
	junk: true,
	filter: [
		'**/*',
		'!.htpasswd'
	],
	rename: function(filePath) {
		return filePath + '.orig';
	},
	transform: function(src, dest, stats) {
		if (path.extname(src) !== '.txt') { return null; }
		return through(function(chunk, enc, done)  {
			var output = chunk.toString().toUpperCase();
			done(null, output);
		});
	}
};

copy('src', 'dest', options)
	.on(copy.events.COPY_FILE_START, function(copyOperation) {
		console.info('Copying file ' + copyOperation.src + '...');
	})
	.on(copy.events.COPY_FILE_COMPLETE, function(copyOperation) {
		console.info('Copied to ' + copyOperation.dest);
	})
	.on(copy.events.ERROR, function(error, copyOperation) {
		console.error('Unable to copy ' + copyOperation.dest);
	})
	.then(function(results) {
		console.info(results.length + ' file(s) copied');
	})
	.catch(function(error) {
		return console.error('Copy failed: ' + error);
	});

Usage

copy(src, dest, [options], [callback])

Recursively copy files and folders from src to dest

Arguments:

NameTypeRequiredDefaultDescription
srcstringYesN/ASource file/folder path
deststringYesN/ADestination file/folder path
options.overwritebooleanNofalseWhether to overwrite destination files
options.expandbooleanNofalseWhether to expand symbolic links
options.dotbooleanNofalseWhether to copy files beginning with a .
options.junkbooleanNofalseWhether to copy OS junk files (e.g. .DS_Store, Thumbs.db)
options.filterfunction, RegExp, string, arrayNonullFilter function / regular expression / glob that determines which files to copy (uses maximatch)
options.renamefunctionNonullFunction that maps source paths to destination paths
options.transformfunctionNonullFunction that returns a transform stream used to modify file contents
options.resultsbooleanNotrueWhether to return an array of copy results
options.concurrencynumberNo255Maximum number of simultaneous copy operations
options.debugbooleanNofalseWhether to log debug information
callbackfunctionNonullCallback, invoked on success/failure

Returns:

Promise<Array> Promise, fulfilled with array of copy results:

[
	{
		"src": "/path/to/src",
		"dest": "/path/to/dest",
		"stats": <Stats>
	},
	{
		"src": "/path/to/src/file.txt",
		"dest": "/path/to/dest/file.txt",
		"stats": <Stats>
	},
	{
		"src": "/path/to/src/subfolder",
		"dest": "/path/to/dest/subfolder",
		"stats": <Stats>
	},
	{
		"src": "/path/to/src/subfolder/nested.txt",
		"dest": "/path/to/dest/subfolder/nested.txt",
		"stats": <Stats>
	}
]

Events

The value returned by the copy function implements the EventEmitter interface, and emits the following events:

EventHandler signature
copy.events.ERRORfunction(error, ErrorInfo)
copy.events.COMPLETEfunction(Array<CopyOperation>)
copy.events.CREATE_DIRECTORY_STARTfunction(CopyOperation)
copy.events.CREATE_DIRECTORY_ERRORfunction(error, CopyOperation)
copy.events.CREATE_DIRECTORY_COMPLETEfunction(CopyOperation)
copy.events.CREATE_SYMLINK_STARTfunction(CopyOperation)
copy.events.CREATE_SYMLINK_ERRORfunction(error, CopyOperation)
copy.events.CREATE_SYMLINK_COMPLETEfunction(CopyOperation)
copy.events.COPY_FILE_STARTfunction(CopyOperation)
copy.events.COPY_FILE_ERRORfunction(error, CopyOperation)
copy.events.COPY_FILE_COMPLETEfunction(CopyOperation)

...where the types referred to in the handler signature are as follows:

ErrorInfo

PropertyTypeDescription
srcstringSource path of the file/folder/symlink that failed to copy
deststringDestination path of the file/folder/symlink that failed to copy

CopyOperation

PropertyTypeDescription
srcstringSource path of the relevant file/folder/symlink
deststringDestination path of the relevant file/folder/symlink
statsfs.StatsStats for the relevant file/folder/symlink
2.0.14

2 years ago

2.0.13

3 years ago

2.0.12

3 years ago

2.0.11

4 years ago

2.0.10

5 years ago

2.0.9

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

7 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

2.0.0-beta

8 years ago

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.7

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.0.1

9 years ago