diff --git a/lib/ncp.js b/lib/ncp.js index 9b8ccf7..bc25a05 100644 --- a/lib/ncp.js +++ b/lib/ncp.js @@ -1,5 +1,6 @@ var fs = require('fs'), - path = require('path'); + path = require('path'), + util = require('util'); const modern = /^v0\.1\d\.\d+/.test(process.version); @@ -21,6 +22,7 @@ function ncp (source, dest, options, callback) { rename = options.rename, transform = options.transform, clobber = options.clobber !== false, + modified = options.modified, dereference = options.dereference, errs = null, eventName = modern ? 'finish' : 'close', @@ -68,6 +70,8 @@ function ncp (source, dest, options, callback) { // We need to get the mode from the stats object and preserve it. item.name = source; item.mode = stats.mode; + item.mtime = stats.mtime; //modified time + item.atime = stats.atime; //access time if (stats.isDirectory()) { return onDir(item); @@ -95,7 +99,17 @@ function ncp (source, dest, options, callback) { rmFile(target, function () { copyFile(file, target); }); - } else { + } + if (modified) { + var stat = dereference ? fs.stat : fs.lstat; + stat(target, function(err, stats) { + //if souce modified time greater to target modified time copy file + if (file.mtime.getTime()>stats.mtime.getTime()) + copyFile(file, target); + else return cb(); + }); + } + else { return cb(); } }); @@ -115,7 +129,14 @@ function ncp (source, dest, options, callback) { readStream.pipe(writeStream); }); } - writeStream.once(eventName, cb); + writeStream.once(eventName, function() { + if (modified) { + //target file modified date sync. + fs.utimesSync(target, file.atime, file.mtime); + cb(); + } + else cb(); + }); } function rmFile(file, done) { diff --git a/test/modified-files/out/a b/test/modified-files/out/a new file mode 100644 index 0000000..d606037 --- /dev/null +++ b/test/modified-files/out/a @@ -0,0 +1 @@ +test2 \ No newline at end of file diff --git a/test/modified-files/src/a b/test/modified-files/src/a new file mode 100644 index 0000000..29f446a --- /dev/null +++ b/test/modified-files/src/a @@ -0,0 +1 @@ +test3 \ No newline at end of file diff --git a/test/ncp.js b/test/ncp.js index 3ddc310..bc6df22 100644 --- a/test/ncp.js +++ b/test/ncp.js @@ -5,9 +5,11 @@ var assert = require('assert'), path = require('path'), rimraf = require('rimraf'), readDirFiles = require('read-dir-files'), + util = require('util'), ncp = require('../').ncp; + describe('ncp', function () { describe('regular files and directories', function () { var fixtures = path.join(__dirname, 'regular-fixtures'), @@ -166,4 +168,30 @@ describe('ncp', function () { }); }); }); -}); + + describe('modified files copies', function () { + var fixtures = path.join(__dirname, 'modified-files'), + src = path.join(fixtures, 'src'), + out = path.join(fixtures, 'out'); + + it('if file not exists copy file to target', function(cb) { + rimraf(out, function() { + ncp(src, out, {modified: true, clobber: false}, function (err) { + assert.equal(fs.existsSync(out), true); + cb(); + }); + }); + }); + + it('change source file mtime and copy', function(cb) { + fs.utimesSync(src+"/a", new Date().getTime()/1000, new Date('2015-01-01 00:00:00').getTime()/1000); + ncp(src, out, {modified: true, clobber: false}, function (err) { + fs.stat(out+"/a", function(err, stats) { + assert.equal(stats.mtime.getTime(), new Date('2015-01-01 00:00:00').getTime()); + cb(); + }); + }); + }); + + }); +}); \ No newline at end of file