Last active
October 26, 2015 10:54
-
-
Save afknapping/7421fb8096c7c0c229de to your computer and use it in GitHub Desktop.
appcache versioning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// appcacheTimestampComment.js | |
// script for appcache versioning | |
// appcache needs an update to the manifest file to allow the browser to check for updated content. | |
// see [Application Cache is a Douchebag](http://alistapart.com/article/application-cache-is-a-douchebag) | |
// This script adds a build timestamp to a file that looks like this | |
// | |
// # built: Mon Oct 26 2015 10:42:24 GMT+0100 (CET) | |
// If the last of the file already starts with '# built', it replaces that line with a new timestamp | |
// overview: | |
// - read the manifest file | |
// - check buffer for existing timestamp comment, delete if necessary | |
// - write back file, add new timestamp comment | |
var fs = require('fs'); | |
// define path and file to our manifest | |
var manifestFile = './test.txt'; | |
// first of all, we read the content of the file in question | |
fs.readFile(manifestFile,'utf8', function read(err, data) { | |
if (err) { | |
throw err; | |
} | |
// we want to work with lines – for example count them and find the last line of the file. | |
// for that we need to convert the returned data into an array so we can count the lines. we're splitting the string at newline characters (\n). | |
var data_array = data.split('\n'); | |
// we find the last line of the array by subtracting 1 from the array length (because arrays are indexed from 0): | |
// data_array[data_array.length - 1] | |
// if the last line starts with "# built", there already is a timestamp comment. this needs to be deleted, because we add a new and updated timestamp later | |
if ( (data_array[data_array.length - 1]).substring(0, 7) === '# built' ) { | |
console.log('\nthe last line of ' + manifestFile + ' seems to be a comment with the build timestamp, will update that...\n'); | |
delete data_array[data_array.length - 1]; | |
} else { | |
// if the last line does NOT match the built stamp pattern, leave original content as it is | |
// still, we want to tell the user what we found out and tell them that their file will be updated | |
console.log('\nthe last line of ' + manifestFile + ' does not seem to be a comment with a build timestamp, will add that...\n'); | |
} | |
// now we need to write back the updated file contents. | |
// we first write the file as empty... | |
fs.writeFile(manifestFile, '', 'utf8', function (err) { | |
if (err) throw err; | |
}); | |
// ...and then append each line from the array plus a line break (\n) | |
data_array.forEach(function (line) { | |
fs.appendFile(manifestFile, line.toString() + "\n"); | |
}); | |
// finally, append the timestamp to the file | |
fs.appendFile(manifestFile, '# built: ' + Date()); | |
}); | |
// sources: | |
// [Application Cache is a Douchebag · An A List Apart Article](http://alistapart.com/article/application-cache-is-a-douchebag) | |
// [File System Node.js v4.2.1 Manual & Documentation](https://nodejs.org/api/fs.html) | |
// [javascript - Find String in a Txt File, Delete Entire Line - Stack Overflow](http://stackoverflow.com/questions/22858206/find-string-in-a-txt-file-delete-entire-line/22858388#22858388) | |
// [javascript - Node.js read and write file lines - Stack Overflow](http://stackoverflow.com/questions/11986350/node-js-read-and-write-file-lines) | |
// [String.prototype.substring() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) | |
// [Comparison operators - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment