From 80d9a901fc2545fda9c0b509ea9d49500a9ebe3a Mon Sep 17 00:00:00 2001 From: ido Date: Tue, 11 Nov 2014 14:38:58 +0200 Subject: [PATCH 1/2] check for stale files --- src/reactTemplates.js | 7 +++++++ src/util.js | 21 +++++++++++++++++++++ test/src/test.js | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/util.js diff --git a/src/reactTemplates.js b/src/reactTemplates.js index f0ec735..6006dca 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -263,6 +263,13 @@ function convertFile(source, target) { // console.log('invalid file, only handle html files'); // return;// only handle html files // } + var util = require('./util'); + + if (!util.isStale(source, target)) { + console.log('target file ' + target + ' is up to date, skipping'); +// return; + } + var html = fs.readFileSync(source).toString(); if (!html.match(/\<\!doctype jsx/i)) { throw new Error('invalid file, missing header'); diff --git a/src/util.js b/src/util.js new file mode 100644 index 0000000..ef05289 --- /dev/null +++ b/src/util.js @@ -0,0 +1,21 @@ +'use strict'; +var fs = require('fs'); +var path = require('path'); + +/** + * @param {string} source + * @param {string} target + * @return {boolean} + */ +function isStale(source, target) { + if (!fs.existsSync(target)) { + return true; + } + var sourceTime = fs.statSync(source).mtime; + var targetTime = fs.statSync(target).mtime; + return sourceTime.getTime() > targetTime.getTime(); +} + +module.exports = { + isStale: isStale +}; \ No newline at end of file diff --git a/test/src/test.js b/test/src/test.js index 09ead69..de9c9e3 100644 --- a/test/src/test.js +++ b/test/src/test.js @@ -56,3 +56,21 @@ test('html tests', function (t) { }); +test('util.isStale', function (t) { + t.plan(2); + var a = path.join(dataPath, 'a.tmp'); + var b = path.join(dataPath, 'b.tmp'); + + var mtime1 = new Date(1995, 11, 17, 3, 24, 0); + fs.utimesSync(a, mtime1, mtime1); + + var mtime2 = new Date(1995, 11, 17, 3, 24, 1); + fs.utimesSync(b, mtime2, mtime2); + + var util = require('../../src/util'); + var actual = util.isStale(a, b); + t.equal(actual, false); + actual = util.isStale(b, a); + t.equal(actual, true); +}); + From 6a89082074c9de8d770290499eddcc49c3c938ee Mon Sep 17 00:00:00 2001 From: ido Date: Tue, 11 Nov 2014 14:43:16 +0200 Subject: [PATCH 2/2] remove temp files --- test/src/test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/src/test.js b/test/src/test.js index de9c9e3..b65095d 100644 --- a/test/src/test.js +++ b/test/src/test.js @@ -61,6 +61,9 @@ test('util.isStale', function (t) { var a = path.join(dataPath, 'a.tmp'); var b = path.join(dataPath, 'b.tmp'); + fs.writeFileSync(a, 'actual'); + fs.writeFileSync(b, 'actual'); + var mtime1 = new Date(1995, 11, 17, 3, 24, 0); fs.utimesSync(a, mtime1, mtime1); @@ -72,5 +75,8 @@ test('util.isStale', function (t) { t.equal(actual, false); actual = util.isStale(b, a); t.equal(actual, true); + + fs.unlinkSync(a); + fs.unlinkSync(b); });