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

dropCwd option, fixes #15 #44

Open
wants to merge 4 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
12 changes: 12 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ module.exports = function (grunt) {
src: ['*'],
dest: 'test/tmp/expand'
},
withDropCwd: {
options: {
dropCwd: true
},
expand: true,
cwd: 'test/fixtures',
src: ['file.png'],
dest: 'test/tmp/withDropCwd'
},
withSummaryAttributeName: {
options: {
summary: 'foo'
Expand Down Expand Up @@ -89,5 +98,8 @@ module.exports = function (grunt) {
var src = path.normalize('test/fixtures/file.png');
var expected = path.normalize('test/tmp/file.26365248.png');
assert.equal(grunt.filerev.summary[src], expected);
src = path.normalize('file.png');
expected = path.normalize('tmp/withDropCwd/file.26365248.png');
assert.equal(grunt.filerev.summary[src], expected);
});
};
24 changes: 21 additions & 3 deletions tasks/filerev.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module.exports = function (grunt) {
grunt.registerMultiTask('filerev', 'File revisioning based on content hashing', function () {
var options = this.options({
algorithm: 'md5',
length: 8
length: 8,
dropCwd: false
});
var target = this.target;
var filerev = grunt.filerev || {summary: {}};
var cwd = this.data.cwd || '';

eachAsync(this.files, function (el, i, next) {
var move = true;
Expand Down Expand Up @@ -74,10 +76,10 @@ module.exports = function (grunt) {
}
}

filerev.summary[path.normalize(file)] = path.join(dirname, newName);
filerev.summary[dropCwd(path.normalize(file))] = dropCwd(path.join(dirname, newName));
grunt.verbose.writeln(chalk.green('✔ ') + file + chalk.gray(' changed to ') + newName);
if (sourceMap) {
filerev.summary[path.normalize(file + '.map')] = path.join(dirname, newName + '.map');
filerev.summary[dropCwd(path.normalize(file + '.map'))] = dropCwd(path.join(dirname, newName + '.map'));
grunt.verbose.writeln(chalk.green('✔ ') + file + '.map' + chalk.gray(' changed to ') + newName + '.map');
}

Expand All @@ -90,6 +92,22 @@ module.exports = function (grunt) {
next();
}, this.async());

// Drop cwd from path if relative option is set to true
function dropCwd (file) {
if (options.dropCwd === true && cwd) {
var filePath = file.split(path.sep),
cwdPath = path.normalize(cwd).split(path.sep),
pos = 0;
for (var i = 0; i < cwdPath.length; i++) {
if (filePath[i] === cwdPath[i]) {
pos = i + 1;
}
}
file = filePath.slice(pos).join(path.sep);
}
return file;
}

grunt.filerev = filerev;
});
};