### Build Script Integration Example - Multiple Copy Operations Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt An example of integrating copyfiles into a build workflow, demonstrating sequential execution of multiple copy operations with error handling. ```javascript const copyfiles = require('copyfiles'); function copyAssets(callback) { copyfiles( ['src/assets/**/*', 'dist/assets'], { up: 2, all: true }, callback ); } function copyConfig(callback) { copyfiles( ['config/*.json', 'dist'], { up: 1, soft: true }, callback ); } function copyHtml(callback) { copyfiles( ['src/**/*.html', 'dist'], { up: 1, exclude: ['**/templates/**'] }, callback ); } // Sequential execution copyAssets(function(err) { if (err) { console.error('Failed to copy assets:', err); process.exit(1); } copyConfig(function(err) { if (err) { console.error('Failed to copy config:', err); process.exit(1); } copyHtml(function(err) { if (err) { console.error('Failed to copy HTML:', err); process.exit(1); } console.log('Build complete!'); }); }); }); ``` -------------------------------- ### Install copyfiles Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Install the package globally via npm. ```bash npm install copyfiles -g ``` -------------------------------- ### Tilde Home Directory Support - Copy from Home Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt Demonstrates copying files from the user's home directory using tilde expansion. This example copies PDF files from Documents to a backup folder. ```javascript const copyfiles = require('copyfiles'); // Copy from home directory copyfiles(['~/Documents/*.pdf', '~/backup'], function(err) { if (err) { console.error('Error:', err); return; } console.log('Backed up PDFs from Documents'); }); ``` -------------------------------- ### Programmatic API - Deep Path Stripping (Integer) Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt The `up` option accepts a positive integer to strip multiple directory levels from source paths. This example strips 2 levels. ```javascript const copyfiles = require('copyfiles'); // Strip 2 directory levels // input/other/file.txt -> output/file.txt copyfiles(['input/**/*.txt', 'output'], 2, function(err) { if (err) { console.error('Error:', err); return; } console.log('Deep strip complete'); }); ``` -------------------------------- ### Programmatic API - Deep Path Stripping (Object) Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt Using the `up` option within a configuration object to strip multiple directory levels from source paths. This example strips 3 levels. ```javascript const copyfiles = require('copyfiles'); // Using config object copyfiles( ['src/components/buttons/**/*.css', 'dist'], { up: 3 }, // strips src/components/buttons function(err) { if (err) { console.error('Error:', err); return; } // src/components/buttons/primary.css -> dist/primary.css console.log('Done'); } ); ``` -------------------------------- ### View CLI Usage Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Display the help menu and available options for the copyfiles command. ```bash Usage: copyfiles [options] inFile [more files ...] outDirectory Options: -u, --up slice a path off the bottom of the paths [number] -a, --all include files & directories begining with a dot (.) [boolean] -f, --flat flatten the output [boolean] -e, --exclude pattern or glob to exclude (may be passed multiple times) -E, --error throw error if nothing is copied [boolean] -V, --verbose print more information to console [boolean] -s, --soft do not overwrite destination files if they exist [boolean] -F, --follow follow symbolink links [boolean] -v, --version Show version number [boolean] -h, --help Show help [boolean] ``` -------------------------------- ### Tilde Home Directory Support - With Options Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt Shows tilde expansion for home directory paths when using options like `up`. Note the comment about Windows path issues. ```javascript const copyfiles = require('copyfiles'); // Note: On Windows, use -u or -f option to avoid path issues with tilde copyfiles(['~/projects/*.js', '~/archive'], { up: 1 }, function(err) { if (err) { console.error('Error:', err); return; } console.log('Archive complete'); }); ``` -------------------------------- ### Programmatic API - Following Symbolic Links Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt Enable the `follow` option to traverse symbolic links and copy the target files. This results in duplicated content from both the original and linked paths. ```javascript const copyfiles = require('copyfiles'); // Directory structure: // input/origin/inner/a.txt // input/dest -> input/origin (symlink) copyfiles( ['input/**/*.txt', 'output'], { up: 1, follow: true }, function(err) { if (err) { console.error('Error:', err); return; } // Results in: // output/origin/inner/a.txt // output/dest/inner/a.txt (from following symlink) console.log('Symlinks followed successfully'); } ); ``` -------------------------------- ### Basic File Copying Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Copy files and directories to a destination folder. ```bash copyfiles foo foobar foo/bar/*.js out ``` ```bash copyfiles something/*.js out ``` -------------------------------- ### Configure copyfiles Programmatically Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt Pass a configuration object to the programmatic API to enable advanced features like exclusions, verbose logging, and symlink following. ```javascript const copyfiles = require('copyfiles'); // Full configuration example copyfiles( ['src/**/*.js', 'src/**/*.json', 'dist'], { up: 1, // Strip 1 directory level all: true, // Include dotfiles exclude: ['**/*.test.js', '**/*.spec.js'], // Exclude test files soft: true, // Don't overwrite existing verbose: true, // Log operations error: true, // Error if nothing copied follow: true // Follow symlinks }, function(err) { if (err) { console.error('Copy operation failed:', err.message); process.exit(1); } console.log('All files copied successfully'); } ); // Exclude multiple patterns copyfiles( ['input/**/*.txt', 'output'], { exclude: ['**/*.js.txt', '**/*.ps.txt', '**/temp/**'] }, function(err) { if (err) { console.error('Error:', err); return; } console.log('Filtered copy complete'); } ); ``` -------------------------------- ### Execute copyfiles via CLI Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt Use the command line interface to copy files with various options like path stripping, flattening, and exclusion patterns. ```bash # Install globally npm install copyfiles -g # Basic usage: copy all .txt files to output directory copyfiles "src/**/*.txt" dist # Copy with path stripping - remove 1 directory level from paths copyfiles -u 1 "src/**/*.js" dist # Flatten all files into a single output directory copyfiles -f "src/**/*.css" "lib/**/*.css" dist # Include dotfiles (files starting with .) copyfiles -a "config/*" dist # Exclude specific patterns copyfiles -e "**/*.test.js" -e "**/*.spec.js" "src/**/*.js" dist # Soft copy - don't overwrite existing files copyfiles -s "templates/*" output # Follow symbolic links copyfiles -F "linked/**/*" output # Verbose mode for debugging copyfiles -V "src/**/*" dist # Throw error if nothing is copied copyfiles -E "src/**/*.xyz" dist # Windows: globs must be double-quoted copyfiles "src\\**\\*.js" dist ``` -------------------------------- ### Execute copyup via CLI Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt The copyup command defaults the --up option to 1, stripping the first directory level from source paths. ```bash # These two commands are equivalent: copyup "src/**/*.js" dist copyfiles -u 1 "src/**/*.js" dist # Copy from nested structure, stripping 'src' from output paths # Input: src/components/Button.js # Output: dist/components/Button.js (not dist/src/components/Button.js) copyup "src/**/*.js" dist ``` -------------------------------- ### Use copyfiles Programmatic API Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt The programmatic API accepts an array of paths and an optional configuration object or callback. ```javascript const copyfiles = require('copyfiles'); // Basic copy - last element is destination copyfiles(['src/*.txt', 'lib/*.txt', 'dist'], function(err) { if (err) { console.error('Copy failed:', err); process.exit(1); } console.log('Files copied successfully'); }); // With up option as number (strips 1 directory level) copyfiles(['input/*.txt', 'output'], 1, function(err) { if (err) { console.error('Error:', err); return; } // input/file.txt -> output/file.txt (not output/input/file.txt) console.log('Done'); }); // Flatten all files (up = true) copyfiles(['src/**/*.js', 'dist'], true, function(err) { if (err) { console.error('Error:', err); return; } // All .js files copied directly to dist/, preserving only filename console.log('Flattened copy complete'); }); ``` -------------------------------- ### Combining Commands Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Chain copyfiles with other shell commands. ```bash copyfiles some.json "./some_folder/*.json" ./dist/ && echo 'JSON files copied.' ``` -------------------------------- ### Flattening Output Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Use the -f option to flatten the directory structure into the destination folder. ```bash copyfiles -f ./foo/*.txt ./foo/bar/*.txt out ``` -------------------------------- ### Path Slicing with --up Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Use the --up option to remove directory levels from the source path when copying. ```bash copyfiles -u 1 something/*.js out ``` -------------------------------- ### Handling Globstars Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Quote globstars if the terminal does not support them natively. ```bash copyfiles -f ./foo/**/*.txt out ``` ```bash copyfiles -f "./foo/**/*.txt" out ``` -------------------------------- ### Programmatic API Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Use the copyfiles module within a Node.js script. ```js var copyfiles = require('copyfiles'); copyfiles([paths], opt, callback); ``` -------------------------------- ### Programmatic API - Soft Copy Mode Source: https://context7.com/calvinmetcalf/copyfiles/llms.txt Use soft copy mode to avoid overwriting existing files in the destination. This is useful for incremental builds or preserving manually edited files. ```javascript const copyfiles = require('copyfiles'); const fs = require('fs'); // Create existing file in output fs.mkdirSync('output/input', { recursive: true }); fs.writeFileSync('output/input/config.txt', 'custom content'); // Soft copy won't overwrite config.txt copyfiles( ['input/**/*.txt', 'output'], { soft: true }, function(err) { if (err) { console.error('Error:', err); return; } // output/input/config.txt still contains 'custom content' // New files are copied normally console.log('Soft copy complete'); } ); ``` -------------------------------- ### Excluding Files Source: https://github.com/calvinmetcalf/copyfiles/blob/master/readme.md Use the -e option to exclude specific patterns from the copy operation. ```bash copyfiles -e "**/*.test.js" -f ./foo/**/*.js out ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.