pax_global_header00006660000000000000000000000064123570470750014524gustar00rootroot0000000000000052 comment=986f28334fa5c893d9f3af2eb53034a8a1a1aaf4 ncp-0.6.0/000077500000000000000000000000001235704707500123075ustar00rootroot00000000000000ncp-0.6.0/.gitignore000066400000000000000000000000641235704707500142770ustar00rootroot00000000000000node_modules .*.sw[op] .DS_Store test/*fixtures/out ncp-0.6.0/.travis.yml000066400000000000000000000000721235704707500144170ustar00rootroot00000000000000language: node_js node_js: - 0.8 - "0.10" - "0.11" ncp-0.6.0/LICENSE.md000066400000000000000000000020721235704707500137140ustar00rootroot00000000000000# MIT License ###Copyright (C) 2011 by Charlie McConnell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ncp-0.6.0/README.md000066400000000000000000000052771235704707500136010ustar00rootroot00000000000000# ncp - Asynchronous recursive file & directory copying [![Build Status](https://secure.travis-ci.org/AvianFlu/ncp.png)](http://travis-ci.org/AvianFlu/ncp) Think `cp -r`, but pure node, and asynchronous. `ncp` can be used both as a CLI tool and programmatically. ## Command Line usage Usage is simple: `ncp [source] [dest] [--limit=concurrency limit] [--filter=filter] --stopOnErr` The 'filter' is a Regular Expression - matched files will be copied. The 'concurrency limit' is an integer that represents how many pending file system requests `ncp` has at a time. 'stoponerr' is a boolean flag that will tell `ncp` to stop immediately if any errors arise, rather than attempting to continue while logging errors. The default behavior is to complete as many copies as possible, logging errors along the way. If there are no errors, `ncp` will output `done.` when complete. If there are errors, the error messages will be logged to `stdout` and to `./ncp-debug.log`, and the copy operation will attempt to continue. ## Programmatic usage Programmatic usage of `ncp` is just as simple. The only argument to the completion callback is a possible error. ```javascript var ncp = require('ncp').ncp; ncp.limit = 16; ncp(source, destination, function (err) { if (err) { return console.error(err); } console.log('done!'); }); ``` You can also call ncp like `ncp(source, destination, options, callback)`. `options` should be a dictionary. Currently, such options are available: * `options.filter` - a `RegExp` instance, against which each file name is tested to determine whether to copy it or not, or a function taking single parameter: copied file name, returning `true` or `false`, determining whether to copy file or not. * `options.transform` - a function: `function (read, write) { read.pipe(write) }` used to apply streaming transforms while copying. * `options.clobber` - boolean=true. if set to false, `ncp` will not overwrite destination files that already exist. * `options.dereference` - boolean=false. If set to true, `ncp` will follow symbolic links. For example, a symlink in the source tree pointing to a regular file will become a regular file in the destination tree. Broken symlinks will result in errors. * `options.stopOnErr` - boolean=false. If set to true, `ncp` will behave like `cp -r`, and stop on the first error it encounters. By default, `ncp` continues copying, logging all errors and returning an array. * `options.errs` - stream. If `options.stopOnErr` is `false`, a stream can be provided, and errors will be written to this stream. Please open an issue if any bugs arise. As always, I accept (working) pull requests, and refunds are available at `/dev/null`. ncp-0.6.0/bin/000077500000000000000000000000001235704707500130575ustar00rootroot00000000000000ncp-0.6.0/bin/ncp000077500000000000000000000020741235704707500135700ustar00rootroot00000000000000#!/usr/bin/env node var ncp = require('../lib/ncp'), args = process.argv.slice(2), source, dest; if (args.length < 2) { console.error('Usage: ncp [source] [destination] [--filter=filter] [--limit=concurrency limit]'); process.exit(1); } // parse arguments the hard way function startsWith(str, prefix) { return str.substr(0, prefix.length) == prefix; } var options = {}; args.forEach(function (arg) { if (startsWith(arg, "--limit=")) { options.limit = parseInt(arg.split('=', 2)[1], 10); } if (startsWith(arg, "--filter=")) { options.filter = new RegExp(arg.split('=', 2)[1]); } if (startsWith(arg, "--stoponerr")) { options.stopOnErr = true; } }); ncp.ncp(args[0], args[1], options, function (err) { if (Array.isArray(err)) { console.error('There were errors during the copy.'); err.forEach(function (err) { console.error(err.stack || err.message); }); process.exit(1); } else if (err) { console.error('An error has occurred.'); console.error(err.stack || err.message); process.exit(1); } }); ncp-0.6.0/lib/000077500000000000000000000000001235704707500130555ustar00rootroot00000000000000ncp-0.6.0/lib/ncp.js000066400000000000000000000124321235704707500141750ustar00rootroot00000000000000var fs = require('fs'), path = require('path'); const modern = /^v0\.1\d\.\d+$/.test(process.version); module.exports = ncp; ncp.ncp = ncp; function ncp (source, dest, options, callback) { var cback = callback; if (!callback) { cback = options; options = {}; } var basePath = process.cwd(), currentPath = path.resolve(basePath, source), targetPath = path.resolve(basePath, dest), filter = options.filter, rename = options.rename, transform = options.transform, clobber = options.clobber !== false, dereference = options.dereference, errs = null, eventName = modern ? 'finish' : 'close', defer = modern ? setImmediate : process.nextTick, started = 0, finished = 0, running = 0, limit = options.limit || ncp.limit || 16; limit = (limit < 1) ? 1 : (limit > 512) ? 512 : limit; startCopy(currentPath); function startCopy(source) { started++; if (filter) { if (filter instanceof RegExp) { if (!filter.test(source)) { return cb(true); } } else if (typeof filter === 'function') { if (!filter(source)) { return cb(true); } } } return getStats(source); } function getStats(source) { var stat = dereference ? fs.stat : fs.lstat; if (running >= limit) { return defer(function () { getStats(source); }); } running++; stat(source, function (err, stats) { var item = {}; if (err) { return onError(err); } // We need to get the mode from the stats object and preserve it. item.name = source; item.mode = stats.mode; if (stats.isDirectory()) { return onDir(item); } else if (stats.isFile()) { return onFile(item); } else if (stats.isSymbolicLink()) { // Symlinks don't really need to know about the mode. return onLink(source); } }); } function onFile(file) { var target = file.name.replace(currentPath, targetPath); if(rename) { target = rename(target); } isWritable(target, function (writable) { if (writable) { return copyFile(file, target); } if(clobber) { rmFile(target, function () { copyFile(file, target); }); } else { return cb(); } }); } function copyFile(file, target) { var readStream = fs.createReadStream(file.name), writeStream = fs.createWriteStream(target, { mode: file.mode }); readStream.on('error', onError); writeStream.on('error', onError); if(transform) { transform(readStream, writeStream, file); } else { writeStream.on('open', function() { readStream.pipe(writeStream); }); } writeStream.once(eventName, cb); } function rmFile(file, done) { fs.unlink(file, function (err) { if (err) { return onError(err); } return done(); }); } function onDir(dir) { var target = dir.name.replace(currentPath, targetPath); isWritable(target, function (writable) { if (writable) { return mkDir(dir, target); } copyDir(dir.name); }); } function mkDir(dir, target) { fs.mkdir(target, dir.mode, function (err) { if (err) { return onError(err); } copyDir(dir.name); }); } function copyDir(dir) { fs.readdir(dir, function (err, items) { if (err) { return onError(err); } items.forEach(function (item) { startCopy(dir + '/' + item); }); return cb(); }); } function onLink(link) { var target = link.replace(currentPath, targetPath); fs.readlink(link, function (err, resolvedPath) { if (err) { return onError(err); } checkLink(resolvedPath, target); }); } function checkLink(resolvedPath, target) { isWritable(target, function (writable) { if (writable) { return makeLink(resolvedPath, target); } fs.readlink(target, function (err, targetDest) { if (err) { return onError(err); } if (targetDest === resolvedPath) { return cb(); } return rmFile(target, function () { makeLink(resolvedPath, target); }); }); }); } function makeLink(linkPath, target) { fs.symlink(linkPath, target, function (err) { if (err) { return onError(err); } return cb(); }); } function isWritable(path, done) { fs.lstat(path, function (err) { if (err) { if (err.code === 'ENOENT') return done(true); return done(false); } return done(false); }); } function onError(err) { if (options.stopOnError) { return cback(err); } else if (!errs && options.errs) { errs = fs.createWriteStream(options.errs); } else if (!errs) { errs = []; } if (typeof errs.write === 'undefined') { errs.push(err); } else { errs.write(err.stack + '\n\n'); } return cb(); } function cb(skipped) { if (!skipped) running--; finished++; if ((started === finished) && (running === 0)) { if (cback !== undefined ) { return errs ? cback(errs) : cback(null); } } } } ncp-0.6.0/package.json000066400000000000000000000012161235704707500145750ustar00rootroot00000000000000{ "name" : "ncp", "version" : "0.6.0", "author": "AvianFlu ", "description" : "Asynchronous recursive file copy utility.", "bin": { "ncp": "./bin/ncp" }, "devDependencies" : { "mocha": "1.15.x", "rimraf" : "1.0.x", "read-dir-files" : "0.0.x" }, "main": "./lib/ncp.js", "repository" : { "type" : "git", "url" : "https://github.com/AvianFlu/ncp.git" }, "keywords" : [ "cli", "copy" ], "license" : "MIT", "engine" : { "node" : ">=0.6" }, "scripts" : { "test": "mocha -R spec" } } ncp-0.6.0/test/000077500000000000000000000000001235704707500132665ustar00rootroot00000000000000ncp-0.6.0/test/broken-symlink-fixtures/000077500000000000000000000000001235704707500201015ustar00rootroot00000000000000ncp-0.6.0/test/broken-symlink-fixtures/src/000077500000000000000000000000001235704707500206705ustar00rootroot00000000000000ncp-0.6.0/test/broken-symlink-fixtures/src/broken-symlink000077700000000000000000000000001235704707500263572does-not-existustar00rootroot00000000000000ncp-0.6.0/test/ncp.js000066400000000000000000000121031235704707500144010ustar00rootroot00000000000000 var assert = require('assert'), fs = require('fs'), path = require('path'), rimraf = require('rimraf'), readDirFiles = require('read-dir-files'), ncp = require('../').ncp; describe('ncp', function () { describe('regular files and directories', function () { var fixtures = path.join(__dirname, 'regular-fixtures'), src = path.join(fixtures, 'src'), out = path.join(fixtures, 'out'); before(function (cb) { rimraf(out, function() { ncp(src, out, cb); }); }); describe('when copying a directory of files', function () { it('files are copied correctly', function (cb) { readDirFiles(src, 'utf8', function (srcErr, srcFiles) { readDirFiles(out, 'utf8', function (outErr, outFiles) { assert.ifError(srcErr); assert.deepEqual(srcFiles, outFiles); cb(); }); }); }); }); describe('when copying files using filter', function () { before(function (cb) { var filter = function(name) { return name.substr(name.length - 1) != 'a'; }; rimraf(out, function () { ncp(src, out, {filter: filter}, cb); }); }); it('files are copied correctly', function (cb) { readDirFiles(src, 'utf8', function (srcErr, srcFiles) { function filter(files) { for (var fileName in files) { var curFile = files[fileName]; if (curFile instanceof Object) return filter(curFile); if (fileName.substr(fileName.length - 1) == 'a') delete files[fileName]; } } filter(srcFiles); readDirFiles(out, 'utf8', function (outErr, outFiles) { assert.ifError(outErr); assert.deepEqual(srcFiles, outFiles); cb(); }); }); }); }); describe('when using clobber=false', function () { it('the copy is completed successfully', function (cb) { ncp(src, out, function() { ncp(src, out, {clobber: false}, function(err) { assert.ifError(err); cb(); }); }); }); }); describe('when using transform', function () { it('file descriptors are passed correctly', function (cb) { ncp(src, out, { transform: function(read,write,file) { assert.notEqual(file.name, undefined); assert.strictEqual(typeof file.mode,'number'); read.pipe(write); } }, cb); }); }); describe('when using rename', function() { it('output files are correctly redirected', function(cb) { ncp(src, out, { rename: function(target) { if(path.basename(target) == 'a') return path.resolve(path.dirname(target), 'z'); return target; } }, function(err) { if(err) return cb(err); readDirFiles(src, 'utf8', function (srcErr, srcFiles) { readDirFiles(out, 'utf8', function (outErr, outFiles) { assert.ifError(srcErr); assert.deepEqual(srcFiles.a, outFiles.z); cb(); }); }); }); }); }); }); describe('symlink handling', function () { var fixtures = path.join(__dirname, 'symlink-fixtures'), src = path.join(fixtures, 'src'), out = path.join(fixtures, 'out'); beforeEach(function (cb) { rimraf(out, cb); }); it('copies symlinks by default', function (cb) { ncp(src, out, function (err) { if (err) return cb(err); assert.equal(fs.readlinkSync(path.join(out, 'file-symlink')), 'foo'); assert.equal(fs.readlinkSync(path.join(out, 'dir-symlink')), 'dir'); cb(); }) }); it('copies file contents when dereference=true', function (cb) { ncp(src, out, { dereference: true }, function (err) { var fileSymlinkPath = path.join(out, 'file-symlink'); assert.ok(fs.lstatSync(fileSymlinkPath).isFile()); assert.equal(fs.readFileSync(fileSymlinkPath), 'foo contents'); var dirSymlinkPath = path.join(out, 'dir-symlink'); assert.ok(fs.lstatSync(dirSymlinkPath).isDirectory()); assert.deepEqual(fs.readdirSync(dirSymlinkPath), ['bar']); cb(); }); }); }); describe('broken symlink handling', function () { var fixtures = path.join(__dirname, 'broken-symlink-fixtures'), src = path.join(fixtures, 'src'), out = path.join(fixtures, 'out'); beforeEach(function (cb) { rimraf(out, cb); }); it('copies broken symlinks by default', function (cb) { ncp(src, out, function (err) { if (err) return cb(err); assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), 'does-not-exist'); cb(); }) }); it('returns an error when dereference=true', function (cb) { ncp(src, out, {dereference: true}, function (err) { assert.equal(err.length, 1); assert.equal(err[0].code, 'ENOENT'); cb(); }); }); }); }); ncp-0.6.0/test/regular-fixtures/000077500000000000000000000000001235704707500165765ustar00rootroot00000000000000ncp-0.6.0/test/regular-fixtures/src/000077500000000000000000000000001235704707500173655ustar00rootroot00000000000000ncp-0.6.0/test/regular-fixtures/src/a000066400000000000000000000000141235704707500175230ustar00rootroot00000000000000Hello world ncp-0.6.0/test/regular-fixtures/src/b000066400000000000000000000000121235704707500175220ustar00rootroot00000000000000Hello ncp ncp-0.6.0/test/regular-fixtures/src/c000066400000000000000000000000001235704707500175200ustar00rootroot00000000000000ncp-0.6.0/test/regular-fixtures/src/d000066400000000000000000000000001235704707500175210ustar00rootroot00000000000000ncp-0.6.0/test/regular-fixtures/src/e000066400000000000000000000000001235704707500175220ustar00rootroot00000000000000ncp-0.6.0/test/regular-fixtures/src/f000066400000000000000000000000001235704707500175230ustar00rootroot00000000000000ncp-0.6.0/test/regular-fixtures/src/sub/000077500000000000000000000000001235704707500201565ustar00rootroot00000000000000ncp-0.6.0/test/regular-fixtures/src/sub/a000066400000000000000000000000201235704707500203110ustar00rootroot00000000000000Hello nodejitsu ncp-0.6.0/test/regular-fixtures/src/sub/b000066400000000000000000000000001235704707500203100ustar00rootroot00000000000000ncp-0.6.0/test/symlink-fixtures/000077500000000000000000000000001235704707500166235ustar00rootroot00000000000000ncp-0.6.0/test/symlink-fixtures/src/000077500000000000000000000000001235704707500174125ustar00rootroot00000000000000ncp-0.6.0/test/symlink-fixtures/src/dir-symlink000077700000000000000000000000001235704707500222732dirustar00rootroot00000000000000ncp-0.6.0/test/symlink-fixtures/src/dir/000077500000000000000000000000001235704707500201705ustar00rootroot00000000000000ncp-0.6.0/test/symlink-fixtures/src/dir/bar000066400000000000000000000000141235704707500206520ustar00rootroot00000000000000bar contentsncp-0.6.0/test/symlink-fixtures/src/file-symlink000077700000000000000000000000001235704707500224412fooustar00rootroot00000000000000ncp-0.6.0/test/symlink-fixtures/src/foo000066400000000000000000000000141235704707500201130ustar00rootroot00000000000000foo contents