1
0
mirror of https://github.com/bobwen-dev/react-templates synced 2025-04-12 00:56:39 +02:00

check for stale files

This commit is contained in:
ido 2014-11-11 14:38:58 +02:00
parent 1ed5eb873e
commit 80d9a901fc
3 changed files with 46 additions and 0 deletions

View File

@ -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');

21
src/util.js Normal file
View File

@ -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
};

View File

@ -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);
});