Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@11ty/recursive-copy

Package Overview
Dependencies
Maintainers
0
Versions
2
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

  • 3.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14K
decreased by-9.68%
Maintainers
0
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.

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('@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

FAQs

Package last updated on 06 Dec 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc