User-friendly by accepting globs and creating non-existent destination directories.
User-friendly error messages.
Progress reporting.
Install
npm install cpy
Usage
import cpy from 'cpy';await cpy([ 'source/*.png', // Copy all .png files '!source/goat.png', // Ignore goat.png], 'destination');// Copy node_modules to destination/node_modulesawait cpy('node_modules', 'destination');// Copy node_modules content to destinationawait cpy('node_modules/**', 'destination');// Copy node_modules structure but skip all files except .json filesawait cpy('node_modules/**/*.json', 'destination');// Copy all png files into destination without keeping directory structureawait cpy('**/*.png', 'destination'
API
cpy(source, destination, options?)
Returns a Promise<string[]> with the destination file paths.
source
Type: string | string[]
Files to copy.
If any of the files do not exist, an error will be thrown (does not apply to globs).
When multiple source files resolve to the same destination path, for example with overlapping globs, flat, or rename, the destination is written once and the last source wins. The ignoreExisting option keeps the first source instead, and update keeps the newest unless overwrite is false, which disables update.
Note: Dotfiles are excluded by default. Set dot: true to include them.
In addition, you can specify the below options.
cwd
Type: string
Default: process.cwd()
Working directory to find source files.
overwrite
Type: boolean
Default: true
Overwrite existing files.
ignoreExisting
Type: boolean
Default: false
Skip files when the destination path already exists.
This option takes precedence over overwrite.
update
Type: boolean
Default: false
Only overwrite when the source is newer, or when sizes differ with the same modification time. Ignored when overwrite is false or ignoreExisting is true.
flat
Type: boolean
Default: false
Flatten directory structure. All copied files will be put in the same directory.
import cpy from 'cpy';await cpy('src/**/*.js', 'destination', { flat: true});
base
Type: 'cwd' | 'pattern'
Default: undefined
Choose how destination paths are calculated for patterns. By default, globs are resolved relative to their parent and explicit paths are resolved relative to cwd. Set to 'pattern' to make explicit paths behave like globs, or 'cwd' to make globs behave like explicit paths.
rename
Type: string | Function
Filename or function used to rename every file in source. Use a two-argument function to receive a frozen source file object and a mutable destination file object. The destination path must stay within the original destination directory. The legacy single-argument form is deprecated, emits a warning, and will be removed in the next major release.