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

options.copy work with tests, this time as single commit #38

Open
wants to merge 5 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
33 changes: 33 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ module.exports = function (grunt) {
cwd: 'test/fixtures',
src: ['*.js', '*.css'],
dest: 'test/tmp/withSourceMaps'
},
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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.

#### options.process(basename, name, extension)

Type: `function`
Expand Down
25 changes: 11 additions & 14 deletions tasks/filerev.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ module.exports = function (grunt) {
var filerev = grunt.filerev || {summary: {}};
var options = this.options({
algorithm: 'md5',
length: 8
length: 8,
copy: null
});

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

// If dest is furnished it should indicate a directory
if (el.dest) {
Expand All @@ -35,8 +35,6 @@ module.exports = function (grunt) {
grunt.verbose.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) {
Expand All @@ -62,14 +60,13 @@ module.exports = function (grunt) {

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

// Source maps
Expand All @@ -80,10 +77,10 @@ module.exports = function (grunt) {
var resultPathMap = resultPath + '.map';

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

// rewrite the sourceMappingURL in files
Expand Down
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.
32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,35 @@ it('should allow a filename processing function', function () {
var revisioned = fs.statSync(hashes[file]).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.6d5a.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.6d5a.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.6d5a.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.6d5a.png').size;
assert(revisioned === original);
var fileExists = fs.existsSync('test/tmp/movednocopyfile.png');
assert(fileExists === false);
});