Skip to content
This repository was archived by the owner on May 29, 2020. It is now read-only.

Pass timestamp option for svgo(node engine) #358

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,41 @@ module.exports = function(grunt) {
types: 'woff'
}
},
timestamp_same_1: {
src: 'test/src/*.svg',
dest: 'test/tmp/timestamp_same',
options: {
fontFilename: 'same_1',
timestamp: 0,
engine: 'node'
}
},
timestamp_same_2: {
src: 'test/src/*.svg',
dest: 'test/tmp/timestamp_same',
options: {
fontFilename: 'same_2',
timestamp: 0,
engine: 'node'
}
},
timestamp_different_1: {
src: 'test/src/*.svg',
dest: 'test/tmp/timestamp_different',
options: {
fontFilename: 'different_1',
timestamp: 0,
engine: 'node'
}
},
timestamp_different_2: {
src: 'test/src/*.svg',
dest: 'test/tmp/timestamp_different',
options: {
fontFilename: 'different_2',
engine: 'node'
}
},
},
nodeunit: {
all: ['test/webfont_test.js']
Expand Down
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ At compile-time each template will have access to the same context as the compil
#### execMaxBuffer
If you get stderr maxBuffer exceeded warning message, engine probably logged a lot of warning messages. To see this warnings run grunt in verbose mode `grunt --verbose`. To go over this warning you can try to increase buffer size by this option. Default value is `1024 * 200`

#### timestamp

Type: `number` Default: `Date.now()`
Unix timestamp (in seconds) to override font creation time
NOTE: Work only with node engine

### Config Examples

#### Simple font generation
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"grunt-contrib-watch": "~0.6.1",
"grunt-jscs": "~1.0.0",
"load-grunt-tasks": "~3.4.0",
"promise": "^7.1.1",
"stylus": "~0.53.0",
"xml2js": "~0.4.16"
},
Expand Down
4 changes: 3 additions & 1 deletion tasks/engines/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ module.exports = function(o, allDone) {

ttf: function(done) {
getFont('svg', function(svgFont) {
var font = svg2ttf(svgFont, {});
var font = svg2ttf(svgFont, {
ts: o.timestamp
});
font = new Buffer(font.buffer);
autohintTtfFont(font, function(hintedFont) {
// ttfautohint is optional
Expand Down
3 changes: 2 additions & 1 deletion tasks/webfont.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ module.exports = function(grunt) {
cache: options.cache || path.join(__dirname, '..', '.cache'),
callback: options.callback,
customOutputs: options.customOutputs,
execMaxBuffer: options.execMaxBuffer || 1024 * 200
execMaxBuffer: options.execMaxBuffer || 1024 * 200,
timestamp: (options.timestamp === 0) ? 0 : options.timestamp || Math.floor(Date.now() / 1000)
};

o = _.extend(o, {
Expand Down
50 changes: 49 additions & 1 deletion test/webfont_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var path = require('path');
var grunt = require('grunt');
var parseXMLString = require('xml2js').parseString;
var wf = require('../tasks/util/util');
var crypto = require('crypto');
var Promise = require('promise');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promises are supported since Node 0.12, so you don’t need a polyfill.


function find(haystack, needle) {
return haystack.indexOf(needle) !== -1;
Expand Down Expand Up @@ -930,6 +932,52 @@ exports.webfont = {
test.ok(fs.existsSync('test/tmp/filename_length/icons.html'));

test.done();
}
},

// If font created multiple times with same value as timestamp option
// then md5 of created files must be the same too
timestamp_same: function (test) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t it be easier to compare file contents? ;-)

var promise_1 = new Promise(function(resolve, reject) {
fs.readFile('test/tmp/timestamp_same/same_1.ttf', function (err, data) {
resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex'));
});
});

var promise_2 = new Promise(function(resolve, reject) {
fs.readFile('test/tmp/timestamp_same/same_2.ttf', function (err, data) {
resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex'));
});
});

Promise.all([promise_1, promise_2])
.then(function(data) {
// md5 must be the same
test.ok(data[0] === data[1]);
test.done();
});
},

// If font created multiple times with different value as timestamp option
// then md5 of created files must be different
timestamp_different: function (test) {
var promise_1 = new Promise(function(resolve, reject) {
fs.readFile('test/tmp/timestamp_different/different_1.ttf', function (err, data) {
resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex'));
});
});

var promise_2 = new Promise(function(resolve, reject) {
fs.readFile('test/tmp/timestamp_different/different_2.ttf', function (err, data) {
resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex'));
});
});

Promise.all([promise_1, promise_2])
.then(function(data) {
// md5 must be the different
test.ok(data[0] !== data[1]);
test.done();
});
},

};