{"library":"ncp","title":"ncp - Asynchronous Recursive Copy","description":"ncp is an asynchronous recursive file and directory copying utility for Node.js, designed to mimic the `cp -r` command but with a pure JavaScript, non-blocking implementation. The current stable version, 2.0.0, as indicated in the prompt, has not seen active development for many years (last commit 6 years ago on GitHub), suggesting it is an abandoned project with no current release cadence. Its key differentiators include a programmatic API that allows for fine-grained control over the copying process, such as setting a global concurrency limit, filtering files via regular expressions or custom functions, applying streaming transformations during the copy, and flexible error handling (either stopping on the first error or continuing while logging all errors). It offers an entirely asynchronous approach to file system operations, which was a significant advantage in early Node.js environments for non-blocking I/O compared to synchronous `cp -r` alternatives.","language":"javascript","status":"abandoned","last_verified":"Sun Apr 19","install":{"commands":["npm install ncp"],"cli":null},"imports":["const { ncp } = require('ncp');","const { ncp } = require('ncp');\nncp.limit = 16;","const { ncp } = require('ncp');\nncp(source, destination, { filter: /\\.js$/, stopOnErr: true }, callback);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const path = require('path');\nconst fs = require('fs');\nconst { ncp } = require('ncp'); // Correct import\nconst { Transform } = require('stream');\n\n// Define source and destination paths\nconst sourceDir = path.join(__dirname, 'temp_source');\nconst destDir = path.join(__dirname, 'temp_destination');\n\n// Ensure source directory exists and has some content\nfs.mkdirSync(sourceDir, { recursive: true });\nfs.writeFileSync(path.join(sourceDir, 'file1.txt'), 'Hello from file 1!');\nfs.writeFileSync(path.join(sourceDir, 'file2.js'), 'console.log(\"file 2\");');\nfs.mkdirSync(path.join(sourceDir, 'subdir'), { recursive: true });\nfs.writeFileSync(path.join(sourceDir, 'subdir', 'subfile.txt'), 'Hello from subfile!');\n\nconsole.log(`Attempting to copy from ${sourceDir} to ${destDir}`);\n\n// Set a global concurrency limit (optional)\nncp.limit = 4; \n\n// Perform the copy operation with options\nncp(sourceDir, destDir, {\n  filter: function(src) {\n    // Only copy files that end with .txt or are directories\n    return /\\.txt$/.test(src) || fs.statSync(src).isDirectory();\n  },\n  stopOnErr: true, // Stop on the first error encountered\n  clobber: true, // Overwrite existing files\n  transform: function (read, write) {\n    // Example transform: convert content to uppercase\n    const transformStream = new Transform({\n      transform(chunk, encoding, callback) {\n        this.push(chunk.toString().toUpperCase());\n        callback();\n      }\n    });\n    read.pipe(transformStream).pipe(write);\n  }\n}, function (err) {\n  if (err) {\n    console.error('Copy failed:', err);\n    // Clean up temporary directories on error\n    fs.rmSync(sourceDir, { recursive: true, force: true });\n    fs.rmSync(destDir, { recursive: true, force: true });\n    process.exit(1);\n  }\n  console.log('Copy complete!');\n  // Verify copied files (optional)\n  if (fs.existsSync(path.join(destDir, 'file1.txt'))) {\n    console.log('file1.txt copied successfully (and should be uppercase):');\n    console.log(fs.readFileSync(path.join(destDir, 'file1.txt'), 'utf8'));\n  }\n  if (!fs.existsSync(path.join(destDir, 'file2.js'))) {\n    console.log('file2.js was filtered out as expected.');\n  }\n  if (fs.existsSync(path.join(destDir, 'subdir', 'subfile.txt'))) {\n    console.log('subfile.txt copied successfully (and should be uppercase):');\n    console.log(fs.readFileSync(path.join(destDir, 'subdir', 'subfile.txt'), 'utf8'));\n  }\n\n  // Clean up temporary directories\n  fs.rmSync(sourceDir, { recursive: true, force: true });\n  fs.rmSync(destDir, { recursive: true, force: true });\n  console.log('Temporary directories cleaned up.');\n});","lang":"javascript","description":"This quickstart demonstrates how to use `ncp` programmatically to recursively copy a directory. It showcases setting a global concurrency limit, applying a file filter (only copying `.txt` files and directories), using a transform stream to modify file content (uppercase), and configuring `ncp` to stop immediately on the first error encountered.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}