Skip to content
This repository was archived by the owner on Mar 26, 2018. It is now read-only.

options.copy work with tests #31

Closed
wants to merge 18 commits into from
Closed
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: 34 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function (grunt) {
withExpand: {
expand: true,
cwd: 'test/fixtures',
src: ['*'],
src: ['*.png'],
dest: 'test/tmp/expand'
},
withSummaryAttributeName: {
Expand All @@ -51,6 +51,39 @@ module.exports = function (grunt) {
},
src: ['test/fixtures/file.png', 'test/fixtures/another.png'],
dest: 'test/tmp'
},
withCopyTrue: {
options: {
algorithm: 'sha1',
length: 4,
copy: 1
},
src: ['test/tmp/another.png'],
dest: 'test/tmp'
},
withCopyFalse: {
options: {
algorithm: 'sha1',
length: 4,
copy: 0
},
src: ['test/tmp/movedfile.png'],
dest: 'test/tmp/copyfalse'
},
withCopyNullDest: {
options: {
algorithm: 'sha1',
length: 4
},
src: ['test/tmp/another.png'],
dest: 'test/tmp/nocopy'
},
withCopyNullNoDest: {
options: {
algorithm: 'sha1',
length: 4
},
src: ['test/tmp/movednocopyfile.png']
}
},
simplemocha: {
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# grunt-filerev [![Build Status](https://secure.travis-ci.org/yeoman/grunt-filerev.svg?branch=master)](http://travis-ci.org/yeoman/grunt-filerev)
# grunt-filerev [![Build Status](https://secure.travis-ci.org/yeoman/grunt-filerev.png?branch=master)](http://travis-ci.org/yeoman/grunt-filerev) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)

> Static asset revisioning through file content hash

Expand Down Expand Up @@ -61,6 +61,14 @@ Default: `8`

The number of characters of the file hash to prefix the file name with.

#### options.copy

Type: `Boolean`
Default: `null`

Flag controlling whether the original file is copied or moved. If not set,
other parameters, such as setting a [destination](#destination) control the behaviour.

### Destination

It will overwrite the `src` files if you don't specify a `dest`:
Expand Down
24 changes: 9 additions & 15 deletions tasks/filerev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ module.exports = function (grunt) {
var options = this.options({
encoding: 'utf8',
algorithm: 'md5',
length: 8
length: 8,
copy: null
});
var target = this.target;
var filerev = grunt.filerev || {summary: {}};

eachAsync(this.files, function (el, i, next) {
var move = true;

// If dest is furnished it should indicate a directory
if (el.dest) {
Expand All @@ -28,35 +28,29 @@ module.exports = function (grunt) {
try {
var stat = fs.lstatSync(el.dest);
if (stat && !stat.isDirectory()) {
grunt.fail.fatal('Destination ' + el.dest + ' for target ' + target + ' is not a directory');
grunt.fail.fatal('Destination for target %s is not a directory', target);
}
} catch (err) {
grunt.log.writeln('Destination dir ' + el.dest + ' does not exists for target ' + target + ': creating');
grunt.file.mkdir(el.dest);
}
// We need to copy file as we now have a dest different from the src
move = false;
}

el.src.forEach(function (file) {
if (grunt.file.isDir(file)) {
return;
}
var dirname;
var hash = crypto.createHash(options.algorithm).update(grunt.file.read(file), options.encoding).digest('hex');
var suffix = hash.slice(0, options.length);
var ext = path.extname(file);
var newName = [path.basename(file, ext), suffix, ext.slice(1)].join('.');
var resultPath;

if (move) {
dirname = path.dirname(file);
resultPath = path.resolve(dirname, newName);
fs.renameSync(file, resultPath);
} else {
dirname = el.dest;
resultPath = path.resolve(dirname, newName);
dirname = el.dest ? el.dest : path.dirname(file);
resultPath = path.resolve(dirname, newName);

if (options.copy || (options.copy === null && el.dest)) {
grunt.file.copy(file, resultPath);
} else {
fs.renameSync(file, resultPath);
}

filerev.summary[path.normalize(file)] = path.join(dirname, newName);
Expand Down
32 changes: 32 additions & 0 deletions test/filerev_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,36 @@ describe('filerev', function () {
var revisioned= fs.statSync('test/tmp/expand/file.a0539763.png').size;
assert(revisioned === original);
});

it('should copy the file when copy option is true', function () {
var original = fs.statSync('test/fixtures/another.png').size;
var revisioned= fs.statSync('test/tmp/another.37ba.png').size;
assert(revisioned === original);
var fileExists = fs.existsSync('test/tmp/another.png');
assert(fileExists === true);
});

it('should move the file when copy option is false', function () {
var original = fs.statSync('test/fixtures/movedfile.png').size;
var revisioned= fs.statSync('test/tmp/copyfalse/movedfile.37ba.png').size;
assert(revisioned === original);
var fileExists = fs.existsSync('test/tmp/movedfile.png');
assert(fileExists === false);
});

it('should copy the file without copy option when dest is specified', function () {
var original = fs.statSync('test/fixtures/another.png').size;
var revisioned= fs.statSync('test/tmp/nocopy/another.37ba.png').size;
assert(revisioned === original);
var fileExists = fs.existsSync('test/tmp/another.png');
assert(fileExists === true);
});

it('should move the file without copy option when dest is not specified', function () {
var original = fs.statSync('test/fixtures/movednocopyfile.png').size;
var revisioned= fs.statSync('test/tmp/movednocopyfile.37ba.png').size;
assert(revisioned === original);
var fileExists = fs.existsSync('test/tmp/movednocopyfile.png');
assert(fileExists === false);
});
});
1 change: 0 additions & 1 deletion test/fixtures/dir/.gitkeep

This file was deleted.

Binary file added test/fixtures/movedfile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/movednocopyfile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.