Skip to content

Commit

Permalink
Merge pull request #33 from avdv/keep-mtime
Browse files Browse the repository at this point in the history
Keep modification time the same as the original file
  • Loading branch information
lukemelia authored Nov 24, 2020
2 parents 84a3787 + bfba0bf commit cef0f64
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
language: node_js
node_js:
- "6"
- "8"

sudo: false

Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ module.exports = {
reject(err);
});
out.on('finish', function(){
resolve();
// set mtime of gz file to mtime of original
fs.stat(fullPath, function(err, stats) {
if (err) resolve(); // ignore errors
else {
fs.utimes(outFilePath, Date.now(), stats.mtime, function () {
resolve();
});
}
});
});
}).then(function(){
if(!keep) {
Expand Down
17 changes: 16 additions & 1 deletion tests/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe('gzip plugin', function() {
if (!fs.existsSync(context.distDir)) { fs.mkdirSync(context.distDir); }
if (!fs.existsSync(path.join(context.distDir, 'assets'))) { fs.mkdirSync(path.join(context.distDir, 'assets')); }
fs.writeFileSync(path.join(context.distDir, context.distFiles[0]), 'alert("Hello foo world!");', 'utf8');
fs.utimesSync(path.join(context.distDir, context.distFiles[0]), Date.now(), new Date("2020-01-01T00:01:02Z"));
fs.writeFileSync(path.join(context.distDir, context.distFiles[1]), 'alert("Hello bar world!");', 'utf8');
fs.writeFileSync(path.join(context.distDir, context.distFiles[2]), 'alert("Hello ignore world!");', 'utf8');
plugin.beforeHook(context);
Expand All @@ -151,7 +152,7 @@ describe('gzip plugin', function() {
return rimraf(context.distDir);
});

it('gzips the matching files which are not ignored', function() {
it('gzips the matching files which are not ignored', function(done) {
assert.isFulfilled(plugin.willUpload(context))
.then(function(result) {
assert.deepEqual(result, { gzippedFiles: ['assets/foo.js'] });
Expand Down Expand Up @@ -186,6 +187,20 @@ describe('gzip plugin', function() {
});
});

it('has the same timestamp as the original', function(done) {
assert.isFulfilled(plugin.willUpload(context))
.then(function(result) {
var mtime_gz = fs.statSync(path.join(context.distDir, result.gzippedFiles[0])).mtime.valueOf();
var mtime_orig = fs.statSync(path.join(context.distDir, context.distFiles[0])).mtime.valueOf();

assert.strictEqual(mtime_gz, mtime_orig);

done();
}).catch(function(reason){
done(reason);
});
});

it('does not use the same object for gzippedFiles and distFiles', function(done) {
assert.isFulfilled(plugin.willUpload(context))
.then(function(result) {
Expand Down

0 comments on commit cef0f64

Please sign in to comment.