Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@11ty/recursive-copy

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@11ty/recursive-copy

A fork of `recursive-copy`: Simple, flexible file copy utility

Source
npmnpm
Version
4.0.1
Version published
Weekly downloads
92K
-26.99%
Maintainers
2
Weekly downloads
 
Created
Source

@11ty/recursive-copy

A temporary fork of timkendrick/recursive-copy to satisfy https://github.com/11ty/eleventy/issues/3299 as Eleventy slowly moves to use Node native API fs.cp.

  • v4.x requires Node 18+
  • v3.x requires Node 0.10+
  • v2.x requires Node 0.10+

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
  • Emits start, finish and error events for each file that is processed
  • Optional promise-based interface

Examples

Node-style callback interface

var copy = require('@11ty/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('@11ty/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 '@11ty/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('@11ty/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
stats fs.StatsStats for the relevant file/folder/symlink

Keywords

copy

FAQs

Package last updated on 23 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts