Skip to content

Commit

Permalink
options.modified added.
Browse files Browse the repository at this point in the history
options.modified value is true, the target file exists, the file's modification date is compared. the target is smaller than the modified date of the file from the source file's modification date, the copy operation is performed. target file's modification date is set.
  • Loading branch information
KiPSOFT authored and AvianFlu committed Sep 24, 2014
1 parent d2427ec commit 85cd50a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
27 changes: 24 additions & 3 deletions lib/ncp.js
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
});
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions test/modified-files/out/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2
1 change: 1 addition & 0 deletions test/modified-files/src/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test3
30 changes: 29 additions & 1 deletion test/ncp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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();
});
});
});

});
});

0 comments on commit 85cd50a

Please sign in to comment.