Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

By type keep structure #152

Closed
wants to merge 3 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
25 changes: 25 additions & 0 deletions specs/layouts_manager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ describe('Layout Manager', function() {
expect(byComponentLayout).to.exist;
expect(byComponentLayout('sass', 'bourbone')).to.equal(path.normalize('bourbone/sass'));
});

it('should support "byTypeKeepStructure" layout', function() {
var byTypeKeepStructure = layoutsManager.getLayout('byTypeKeepStructure');

expect(byTypeKeepStructure).to.exist;
expect(byTypeKeepStructure('styles', 'foundation', 'scss/foundation/components')).to.equal(path.normalize('styles/foundation/scss/foundation/components'));
});

it('should support "byComponentKeepStructure" layout', function() {
var byComponentKeepStructure = layoutsManager.getLayout('byComponentKeepStructure');

expect(byComponentKeepStructure).to.exist;
expect(byComponentKeepStructure('styles', 'foundation', 'scss/foundation/components')).to.equal(path.normalize('foundation/styles/scss/foundation/components'));
});
});

it('should support custom layouts as functions', function() {
Expand All @@ -31,4 +45,15 @@ describe('Layout Manager', function() {
expect(customLayout).to.be.a('function');
expect(customLayout('img', 'logo.png')).to.equal('imglogo.png');
});

it('should support keeping structure in custom layouts as functions', function() {
var customLayout = layoutsManager.getLayout(function(type, pkg, sourceDir) {
return path.join(type, pkg, sourceDir);
});

expect(customLayout).to.be.a('function');
expect(customLayout('styles', 'bootstrap', 'less')).to.equal('styles/bootstrap/less');
});


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding all these unit tests! This is something I'd love to see more of in this project so that we can know when a feature is broken. Would you be interested in adding tests to some of the other functions as well?

});
2 changes: 1 addition & 1 deletion tasks/bower_task.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = function (grunt) {
function copy(options, callback) {
var bowerAssets = new BowerAssets(bower, options.cwd);
bowerAssets.on("end", function (assets) {
var copier = new AssetCopier(assets, options, function (source, destination, isFile) {
var copier = new AssetCopier(assets, options, bower.config.directory, function (source, destination, isFile) {
log("grunt-bower " + "copying ".cyan + ((isFile ? "" : " dir ") + source + " -> " + destination).grey);
});

Expand Down
7 changes: 5 additions & 2 deletions tasks/lib/asset_copier.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ var path = require('path');
var grunt = require('grunt');
var fs = require('fs-extra');

var Copier = function(assets, options, report) {
var Copier = function(assets, options, componentsDir, report) {
this.assets = assets;
this.options = options;
this.componentsDir = componentsDir;
this.report = report;
};

Expand Down Expand Up @@ -39,7 +40,9 @@ Copier.prototype.copyAssets = function(type, assets) {
var destination;

var isFile = fs.statSync(source).isFile();
var destinationDir = path.join(self.options.targetDir, self.options.layout(type, pkg, source));
var sourceDir = path.relative(path.join(self.componentsDir, pkg), path.dirname(source));
var destinationDir = path.join(self.options.targetDir, self.options.layout(type, pkg, sourceDir));

grunt.file.mkdir(destinationDir);
if (isFile) {
destination = path.join(destinationDir, path.basename(source));
Expand Down
12 changes: 10 additions & 2 deletions tasks/lib/layouts_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ var _ = require('lodash');
var path = require('path');

var handleUntyped = function(layout) {
return function(type, pkg) {
return function(type, pkg, sourceDir) {
if (type === '__untyped__') {
return pkg;
}
return layout(type, pkg);
return layout(type, pkg, sourceDir);
};
};

Expand All @@ -17,6 +17,14 @@ var defaultLayouts = {

byComponent: handleUntyped(function(type, pkg) {
return path.join(pkg, type);
}),

byTypeKeepStructure: handleUntyped(function(type, pkg, sourceDir) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with this syntax byTypeKeepStructure: handleUntyped(function. Can you explain it please?

return path.join(type, pkg, sourceDir);
}),

byComponentKeepStructure: handleUntyped(function(type, pkg, sourceDir) {
return path.join(pkg, type, sourceDir);
})
};

Expand Down