What is grunt-contrib-copy?
The grunt-contrib-copy package is a Grunt plugin that provides file copying capabilities. It allows you to copy files and directories from one location to another, which is useful for tasks such as preparing files for deployment, creating backups, or organizing project files.
What are grunt-contrib-copy's main functionalities?
Copying individual files
This feature allows you to copy a single file from a source location to a destination location. In this example, 'src/file1.txt' is copied to 'dest/file1.txt'.
{
"copy": {
"main": {
"src": "src/file1.txt",
"dest": "dest/file1.txt"
}
}
}
Copying multiple files
This feature allows you to copy multiple files by specifying an array of source and destination file paths. In this example, 'src/file1.txt' and 'src/file2.txt' are copied to 'dest/file1.txt' and 'dest/file2.txt' respectively.
{
"copy": {
"main": {
"files": [
{"src": "src/file1.txt", "dest": "dest/file1.txt"},
{"src": "src/file2.txt", "dest": "dest/file2.txt"}
]
}
}
}
Copying entire directories
This feature allows you to copy entire directories, including all subdirectories and files. The 'expand' option is set to true to enable dynamic building of the destination paths. The 'cwd' option specifies the current working directory, and 'src' specifies the pattern to match files. In this example, all files and directories under 'src/' are copied to 'dest/'.
{
"copy": {
"main": {
"expand": true,
"cwd": "src/",
"src": ["**"],
"dest": "dest/"
}
}
}
Flattening directory structure
This feature allows you to flatten the directory structure when copying files. The 'flatten' option is set to true, which means all files will be copied directly into the destination directory without preserving the directory structure. In this example, all files under 'src/' are copied directly into 'dest/'.
{
"copy": {
"main": {
"expand": true,
"cwd": "src/",
"src": ["**"],
"dest": "dest/",
"flatten": true
}
}
}
Other packages similar to grunt-contrib-copy
gulp-copy
gulp-copy is a Gulp plugin that provides similar file copying capabilities as grunt-contrib-copy. It allows you to copy files and directories using Gulp tasks. Compared to grunt-contrib-copy, gulp-copy is used within the Gulp task runner ecosystem, which may be preferred by developers who use Gulp for their build processes.
cpx
cpx is a command-line tool and library for copying files and directories. It supports advanced features such as watching for changes and copying only modified files. Compared to grunt-contrib-copy, cpx can be used both as a standalone CLI tool and as a library within Node.js scripts, providing more flexibility in different environments.
ncp
ncp (Node Copy) is a Node.js module for copying files and directories. It provides a simple API for recursive copying and supports various options for filtering and transforming files during the copy process. Compared to grunt-contrib-copy, ncp is a lower-level library that can be used directly in Node.js scripts without relying on a task runner like Grunt.
grunt-contrib-copy
Copy files and folders.
Getting Started
This plugin requires Grunt ~0.4.0
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-contrib-copy --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-contrib-copy');
This plugin was designed to work with Grunt 0.4.x. If you're still using grunt v0.3.x it's strongly recommended that you upgrade, but in case you can't please use v0.3.2.
Copy task
Run this task with the grunt copy
command.
Task targets, files and options may be specified according to the grunt Configuring tasks guide.
Options
processContent
Type: Function(content, srcpath)
This option is passed to grunt.file.copy
as an advanced way to control the file contents that are copied.
processContentExclude
Type: String
This option is passed to grunt.file.copy
as an advanced way to control which file contents are processed.
Usage Examples
copy: {
main: {
files: [
{src: ['path/*'], dest: 'dest/', filter: 'isFile'},
{src: ['path/**'], dest: 'dest/'},
{expand: true, cwd: 'path/', src: ['**'], dest: 'dest/'},
{expand: true, flatten: true, src: ['path/**'], dest: 'dest/', filter: 'isFile'}
]
}
}
Release History
- 2013-02-14 v0.4.0 First official release for Grunt 0.4.0.
- 2013-01-22 v0.4.0rc7 Updating grunt/gruntplugin dependencies to rc7. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions.
- 2013-01-13 v0.4.0rc5 Updating to work with grunt v0.4.0rc5. Conversion to grunt v0.4 conventions. Replace basePath with cwd. Empty directory support.
- 2012-10-17 v0.3.2 Pass copyOptions on single file copy.
- 2012-10-11 v0.3.1 Rename grunt-contrib-lib dep to grunt-lib-contrib.
- 2012-09-23 v0.3.0 General cleanup and consolidation. Global options depreciated.
- 2012-09-17 v0.2.4 No valid source check.
- 2012-09-16 v0.2.3 Path.sep fallback for node <= 0.7.9.
- 2012-09-16 v0.2.2 Single file copy support. Test refactoring.
- 2012-09-06 v0.2.0 Refactored from grunt-contrib into individual repo.
Task submitted by Chris Talkington
This file was generated on Fri Feb 22 2013 09:26:58.