Skip to content

Commit

Permalink
shortcut for dest supported
Browse files Browse the repository at this point in the history
  • Loading branch information
yanni4night committed Nov 26, 2014
1 parent 1cb36e7 commit c537aa8
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 10 deletions.
13 changes: 11 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ module.exports = function(grunt) {
ignore: 'dist/js/bootstrap.css'
},
dest: 'test/dest'
},
filetypes_dest: {
options: {
cssDest: 'test/dest/css',
jsDest: 'test/dest/js',
fontDest: 'test/dest/font'
},
dest: 'test/dest'
}
},

Expand All @@ -93,7 +101,8 @@ module.exports = function(grunt) {
default: ['test/default_test.js'],
custom_component_dest: ['test/custom_component_dest_test.js'],
function_dest: ['test/function_dest_test.js'],
ignore: ['test/ignore_test.js']
ignore: ['test/ignore_test.js'],
filetypes_dest: ['test/filetypes_dest_test.js']
},
coveralls: {
all: {
Expand All @@ -109,7 +118,7 @@ module.exports = function(grunt) {

// 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', 'copy_bower:default', 'nodeunit:default', 'clean', 'copy_bower:custom_component_dest', 'nodeunit:custom_component_dest', 'clean', 'copy_bower:function_dest', 'nodeunit:function_dest', 'clean', 'copy_bower:ignore', 'nodeunit:ignore']);
grunt.registerTask('test', ['clean', 'copy_bower:default', 'nodeunit:default', 'clean', 'copy_bower:custom_component_dest', 'nodeunit:custom_component_dest', 'clean', 'copy_bower:function_dest', 'nodeunit:function_dest', 'clean', 'copy_bower:ignore', 'nodeunit:ignore', 'clean', 'copy_bower:filetypes_dest', 'nodeunit:filetypes_dest']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
Expand Down
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ grunt.initConfig({
your_target: {
// Target-specific file lists and/or options go here.
},
},
}
});
```

Expand Down Expand Up @@ -73,15 +73,15 @@ grunt.initConfig({
dest: 'test/dest/plugin/'
},
'underscore': {
dest: 'test/dest/_.js'
dest: 'test/dest/us.js'
}
}
},
dest: 'test/dest/'
},
}
});
```
All the files defined in bower will be copied into `test/dest` keeping the origin file name except _underscore.js_ to _\_.js_,_text.js_ to _plugin/text.js_.Note that no main file defined will lead to an error,so the _main_ defined for _requirejs-text_ is highly required.
All the files defined in bower will be copied into `test/dest` keeping the origin file name except _underscore.js_ to _us.js_,_text.js_ to _plugin/text.js_.Note that no main file defined will lead to an error,so the _main_ defined for _requirejs-text_ is highly required.

You can also define _dest_ as a function:

Expand All @@ -108,11 +108,23 @@ grunt.initConfig({
return 'test/dest/bin';
}
}
},
}
});
```
But it's restricted that _dest_ in tasks does this,not in _shim_.

There are some shortcuts for _dest_,e.g,

```js
grunt.initConfig({
copy_bower: {
dest: 'test/dest',
cssDest: 'test/dest/css',
jsDest: 'test/dest/js'
}
});
```

_ignore_ could be pattens in **Array**/**Function**/**RegExp**/**String**,and nest of arrays is allowed,e.g,

```js
Expand All @@ -131,11 +143,13 @@ grunt.initConfig({
ignore: 'dist/js/bootstrap.css'
},
dest: 'test/dest/'
},
}
});
```
_ignore_ in _options_ could work too.Note that the patterns only match the main file in bower,not the real file name or path.



## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).

Expand Down
48 changes: 46 additions & 2 deletions tasks/copy_bower.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,47 @@ module.exports = function(grunt) {
return false;
};

var fileTypeDetector = {
_ext: function(ext, filename) {
if (Array.isArray(ext)) {
ext = '(' + ext.join('|') + ')';
}
return new RegExp('\\.' + ext + '$', 'i').test(filename);
},
detected: function(filename) {
var types = Object.keys(this).filter(function(func) {
return /^is\w+$/.test(func) && isFunction(this[func]);
}, this);

for (var i = 0, len = types.length; i < len; ++i) {
if (this[types[i]].call(this, filename)) {
return types[i].slice(2).toLowerCase();
}
}
},
isCss: function(filename) {
return this._ext.bind(this, 'css').apply(this, arguments);
},
isLess: function(filename) {
return this._ext.bind(this, 'less').apply(this, arguments);
},
isSass: function(filename) {
return this._ext.bind(this, ['scss', 'sass']).apply(this, arguments);
},
isCoffee: function(filename) {
return this._ext.bind(this, 'coffee').apply(this, arguments);
},
isJs: function(filename) {
return this._ext.bind(this, 'js').apply(this, arguments);
},
isImage: function(filename) {
return this._ext.bind(this, ['jpg', 'jpeg', 'ico', 'bmp', 'png', 'gif', 'webp']).apply(this, arguments);
},
isFont: function(filename) {
return this._ext.bind(this, ['eot', 'svg', 'ttf', 'woff']).apply(this, arguments);
}
};

//dest could be a function
if (!isFunction(uniformDest) && !likeDirectory(uniformDest)) {
grunt.fail.warn('"dest" should be a directory path');
Expand Down Expand Up @@ -158,7 +199,8 @@ module.exports = function(grunt) {

depsCollection.forEach(function(dep) {
if (dep.main) {
var dst, src = path.join(dep.dir, dep.main);
var dst, src = path.join(dep.dir, dep.main),
fileType;

//Lookup shim
if (uniformShim[dep.name] && uniformShim[dep.name].dest) {
Expand All @@ -171,6 +213,8 @@ module.exports = function(grunt) {
dst = uniformShim[dep.name].dest;
}

} else if ((fileType = fileTypeDetector.detected(src)) && likeDirectory(options[fileType + 'Dest'])) {
dst = path.join(options[fileType + 'Dest'], path.basename(dep.main));
} else {
dst = path.join(isFunction(uniformDest) ? uniformDest.call(self, dep.main) : uniformDest, path.basename(dep.main));
}
Expand All @@ -183,7 +227,7 @@ module.exports = function(grunt) {

//Invoke bower to list all the components
bower.commands.list(null, {
offline: true//We do not check new version(s) due to speed
offline: true //We do not check new version(s) due to speed
}).on('end', function(installed) {
var depsCollection = [];
pushDependency(installed, depsCollection);
Expand Down
44 changes: 44 additions & 0 deletions test/filetypes_dest_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

var grunt = require('grunt');

/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/
var fs = require('fs');
var path = require('path');

exports.copy_bower = {
setUp: function(done) {
done();
},
test: function(test) {
var css = ['css/bootstrap.css'];
var js = ['js/bootstrap.js', 'js/angular.js', 'js/backbone.js'];
var font = ['font/glyphicons-halflings-regular.eot', 'font/glyphicons-halflings-regular.svg', 'font/glyphicons-halflings-regular.ttf', 'font/glyphicons-halflings-regular.woff'];
var files = [].concat(css).concat(js).concat(font);
test.expect(files.length);

files.forEach(function(file) {
test.ok(fs.existsSync(path.join(__dirname, 'dest', file)), file + ' should exist');
});

test.done();
}
};

0 comments on commit c537aa8

Please sign in to comment.