-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 223cce8
Showing
12 changed files
with
420 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
npm-debug.log | ||
tmp |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"curly": true, | ||
"eqeqeq": true, | ||
"immed": true, | ||
"latedef": true, | ||
"newcap": true, | ||
"noarg": true, | ||
"sub": true, | ||
"undef": true, | ||
"boss": true, | ||
"eqnull": true, | ||
"node": true | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* grunt-static-timestamp | ||
* https://github.com/pvieira/grunt-static-timestamp | ||
* | ||
* Copyright (c) 2016 Paulo Vieira | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
module.exports = function(grunt) { | ||
|
||
// Project configuration. | ||
grunt.initConfig({ | ||
jshint: { | ||
all: [ | ||
'Gruntfile.js', | ||
'tasks/*.js', | ||
'<%= nodeunit.tests %>' | ||
], | ||
options: { | ||
jshintrc: '.jshintrc' | ||
} | ||
}, | ||
|
||
// Before generating any new files, remove any previously-created files. | ||
clean: { | ||
tests: ['tmp'] | ||
}, | ||
|
||
// Configuration to be run (and then tested). | ||
static_timestamp: { | ||
default_options: { | ||
options: { | ||
}, | ||
files: { | ||
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123'] | ||
} | ||
}, | ||
custom_options: { | ||
options: { | ||
separator: ': ', | ||
punctuation: ' !!!' | ||
}, | ||
files: { | ||
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123'] | ||
} | ||
} | ||
}, | ||
|
||
// Unit tests. | ||
nodeunit: { | ||
tests: ['test/*_test.js'] | ||
} | ||
|
||
}); | ||
|
||
// Actually load this plugin's task(s). | ||
grunt.loadTasks('tasks'); | ||
|
||
// These plugins provide necessary tasks. | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
grunt.loadNpmTasks('grunt-contrib-clean'); | ||
grunt.loadNpmTasks('grunt-contrib-nodeunit'); | ||
|
||
// Whenever the "test" task is run, first clean the "tmp" dir, then run this | ||
// plugin's task(s), then test the result. | ||
grunt.registerTask('test', ['clean', 'static_timestamp', 'nodeunit']); | ||
|
||
// By default, lint and run all tests. | ||
grunt.registerTask('default', ['jshint', 'test']); | ||
|
||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2016 Paulo Vieira | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# grunt-static-timestamp | ||
|
||
> Add a timestamp to the static files, but only if files have actually changed | ||
## Getting Started | ||
This plugin requires Grunt `~0.4.5` | ||
|
||
If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: | ||
|
||
```shell | ||
npm install grunt-static-timestamp --save-dev | ||
``` | ||
|
||
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: | ||
|
||
```js | ||
grunt.loadNpmTasks('grunt-static-timestamp'); | ||
``` | ||
|
||
## The "static_timestamp" task | ||
|
||
### Overview | ||
In your project's Gruntfile, add a section named `static_timestamp` to the data object passed into `grunt.initConfig()`. | ||
|
||
```js | ||
grunt.initConfig({ | ||
static_timestamp: { | ||
options: { | ||
// Task-specific options go here. | ||
}, | ||
your_target: { | ||
// Target-specific file lists and/or options go here. | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
### Options | ||
|
||
#### options.separator | ||
Type: `String` | ||
Default value: `', '` | ||
|
||
A string value that is used to do something with whatever. | ||
|
||
#### options.punctuation | ||
Type: `String` | ||
Default value: `'.'` | ||
|
||
A string value that is used to do something else with whatever else. | ||
|
||
### Usage Examples | ||
|
||
#### Default Options | ||
In this example, the default options are used to do something with whatever. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result would be `Testing, 1 2 3.` | ||
|
||
```js | ||
grunt.initConfig({ | ||
static_timestamp: { | ||
options: {}, | ||
files: { | ||
'dest/default_options': ['src/testing', 'src/123'], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
#### Custom Options | ||
In this example, custom options are used to do something else with whatever else. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result in this case would be `Testing: 1 2 3 !!!` | ||
|
||
```js | ||
grunt.initConfig({ | ||
static_timestamp: { | ||
options: { | ||
separator: ': ', | ||
punctuation: ' !!!', | ||
}, | ||
files: { | ||
'dest/default_options': ['src/testing', 'src/123'], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "grunt-static-timestamp", | ||
"description": "Add a timestamp to the static files, but only if files have actually changed", | ||
"version": "0.1.0", | ||
"homepage": "https://github.com/pvieira/grunt-static-timestamp", | ||
"author": { | ||
"name": "Paulo Vieira", | ||
"email": "[email protected]" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/pvieira/grunt-static-timestamp.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/pvieira/grunt-static-timestamp/issues" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/pvieira/grunt-static-timestamp/blob/master/LICENSE-MIT" | ||
} | ||
], | ||
"engines": { | ||
"node": "0.10.40" | ||
}, | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"devDependencies": { | ||
"grunt-contrib-jshint": "^0.9.2", | ||
"grunt-contrib-clean": "^0.5.0", | ||
"grunt-contrib-nodeunit": "^0.3.3", | ||
"grunt": "~0.4.5" | ||
}, | ||
"keywords": [ | ||
"gruntplugin" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* grunt-static-timestamp | ||
* https://github.com/pvieira/grunt-static-timestamp | ||
* | ||
* Copyright (c) 2016 Paulo Vieira | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
var Path = require("path"); | ||
var Crypto = require('crypto'); | ||
|
||
'use strict'; | ||
|
||
module.exports = function(grunt) { | ||
|
||
// Please see the Grunt documentation for more information regarding task | ||
// creation: http://gruntjs.com/creating-tasks | ||
|
||
grunt.registerMultiTask('static-timestamp', 'Add a timestamp to the static files, but only if files have actually changed', function() { | ||
|
||
//console.log("this.files: ", JSON.stringify(this.files, null, 4)); | ||
|
||
// Merge task-specific and/or target-specific options with these defaults. | ||
var options = this.options({}); | ||
|
||
var updated = []; | ||
this.files.forEach(function(file) { | ||
|
||
var tempHashes = {}, | ||
publicHashes = {}; | ||
|
||
if (!grunt.file.exists(file.dest)) { | ||
grunt.fail.fatal("Destination does not exist: " + file.dest); | ||
} | ||
|
||
if (!grunt.file.isDir(file.dest)) { | ||
grunt.fail.fatal("Destination must be a directory: " + file.dest); | ||
} | ||
|
||
// compute md5 hash for the src files | ||
file.src.forEach(function(path) { | ||
|
||
if (!grunt.file.exists(path)) { | ||
grunt.log.warn('Source file "' + path + '" not found.'); | ||
return; | ||
} | ||
|
||
computeHash(path, tempHashes); | ||
}); | ||
|
||
// compute md5 files for files in the dest dir | ||
var publicFiles = grunt.file.expand({ filter: "isFile" }, Path.join(file.dest, "*")); | ||
|
||
publicFiles.forEach(function(path) { | ||
computeHash(path, publicHashes); | ||
}); | ||
|
||
var pairs = []; | ||
|
||
// find the corresponding pairs between temp files and public files; this is | ||
// done by simply comparing the file names (the timestamp is removed from the | ||
// files in the public files); it might happen that for a given temp file there | ||
// is no corresponding build file yet; | ||
for (var tempFile in tempHashes) { | ||
|
||
var pair = {}; | ||
pair[tempFile] = tempHashes[tempFile]; | ||
|
||
var tempName = Path.parse(tempFile).name; | ||
|
||
for (var publicFile in publicHashes) { | ||
|
||
var publicName = Path.parse(publicFile).name.split(".").slice(1).join("."); | ||
if (publicName === tempName) { | ||
pair[publicFile] = publicHashes[publicFile]; | ||
} | ||
} | ||
|
||
pairs.push(pair); | ||
} | ||
|
||
//console.log("pairs:\n", pairs); | ||
|
||
// verify if the md5 of the pairs match; if not, replace the public file with the corresponding temp file | ||
pairs.forEach(function(pair) { | ||
|
||
var paths = Object.keys(pair); | ||
|
||
if (paths.length === 1 || | ||
paths.length === 2 && pair[paths[0]] !== pair[paths[1]]) { | ||
|
||
var now = grunt.template.today('yymmdd-HHMMss'); | ||
|
||
// paths[0] is always the temp file | ||
var newBuildFile = Path.join(file.dest, now + "." + Path.parse(paths[0]).name + Path.parse(paths[0]).ext); | ||
|
||
grunt.file.copy(paths[0], newBuildFile); | ||
updated.push(newBuildFile) | ||
|
||
if (paths.length === 2) { | ||
grunt.file.delete(paths[1], { force: true }); | ||
} | ||
} | ||
|
||
}); | ||
|
||
}); | ||
|
||
if (updated.length === 0) { | ||
grunt.log.writeln("no changes detected (everything up to date)"); | ||
} else { | ||
grunt.log.writeln("updated timestamps:"); | ||
grunt.log.writeln(grunt.log.wordlist(updated, { separator: "\n" })); | ||
} | ||
|
||
|
||
function computeHash(path, obj) { | ||
|
||
// encoding is null, so returns a Buffer instead of a string | ||
var source = grunt.file.read(path, { | ||
encoding: null | ||
}); | ||
|
||
var hash = Crypto | ||
.createHash("md5") | ||
.update(source) | ||
.digest("hex"); | ||
|
||
obj[path] = hash; | ||
} | ||
|
||
}); | ||
|
||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Testing: 1 2 3 !!! |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Testing, 1 2 3. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 2 3 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Testing |
Oops, something went wrong.