2.0.14 • Published 3 years ago

recursive-copy v2.0.14

Weekly downloads
109,728
License
ISC
Repository
github
Last release
3 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
flowrty-cli@asep.setiawan/react-kit@animalus/gulp-dev-toolsydd-buildcaven-react-cli@kidsagree/ui@mdhnpm/aws-lambda-template-generatorpackage-assets@diy-ui/diy-uitemp-eleventy-tempcopy-detection@peerio/desktop-release-buildermethod-clientesp3d-mocking-serverns-devtoolscreate-opinionated-react-app@narwhalosaurus/uigps-cli@gloxy/demo-cli@yuetlooo/uiares-nexttestba1ltestbalyml-public-ui@cloudkite/next@kaviar/terminal-bundlegswlcumulocity-adkeasydevopsgatsby-source-modular-graphql@nathangasc/fwc@infinitebrahmanuniverse/nolb-recuindexeddb-to-json@everything-registry/sub-chunk-2633@explodingcamera/next@vcee/sgenaem-component-scaffoldingsymphony-joywoozweexpackwitch-doctorweexplusweextoolweextoolsweld-nextwx2wx2anywx2swanwx2swan-prowx_miniprogram_traversewx-to-ttsafe-obfsstarwish-clitest-test-43h4h29423test-stream-nextswan2wxmpthor-spa-oldti-lambda-clivemsy-clivertestvoyance-blocksviur-ignite-icons@docsoc/mailmerge-cli@dogedollar/ui@croakarr/create-croakarr-plugin@croakerr/create-croakerr-plugin@cross2d/next@cspotcode/opendocumenter@commonsswarm/ui@concepto/eb@concepto/interface@concepto/nuxt@concepto/react@concepto/vue@compgen/core@coserv/cli@coraltravelcenter/create-b2c-landing@devbaze/hcon@devdavi/sumo@dragonsahead/dragonsahead-ui@dollarcode/dollar-generators@blinkmobile/server-cli@blinkmobile/forms-template-helperjeduxjudpackjet-nextjam-pack-nodeguikloilb-watchmanlandr@gloxy/studiostyleguide-starter-kitss-sass-bundlerspk-clistaticiastrapi-plugin-toolkitspace-kittyugeez-cliucd-theme-tasksui-1hive
2.0.14

3 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

5 years ago

2.0.10

6 years ago

2.0.9

7 years ago

2.0.8

8 years ago

2.0.7

8 years ago

2.0.6

8 years ago

2.0.5

9 years ago

2.0.4

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

2.0.0-beta

9 years ago

1.0.10

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.1

10 years ago