Skip to content

Commit

Permalink
Fixed links to project in pacakge.json.
Browse files Browse the repository at this point in the history
Refactored set min into it's own function.
Added a check for if the referencing file is already using the min version
  • Loading branch information
hypexr committed Apr 11, 2014
1 parent 7aefb53 commit 86bd878
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "grunt-version-copy-bower-components",
"description": "Version and stage Bower components for release.",
"version": "0.1.7",
"homepage": "http://app.multisight.com",
"version": "0.1.8",
"homepage": "https://github.com/hypexr/grunt-version-copy-bower-components",
"author": {
"name": "hypexr",
"name": "Scott Rippee",
"email": "scott@hypexr.org"
},
"repository": {
"type": "git",
"url": "https://github.com/MultiSight/webclient.git"
"url": "https://github.com/hypexr/grunt-version-copy-bower-components.git"
},
"bugs": {
"url": "https://github.com/MultiSight/webclient/issues"
"url": "https://github.com/hypexr/grunt-version-copy-bower-components/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/MultiSight/webclient/blob/master/LICENSE-MIT"
"url": "https://github.com/hypexr/grunt-version-copy-bower-components/blob/master/LICENSE-MIT"
}
],
"engines": {
Expand Down
72 changes: 39 additions & 33 deletions tasks/version_copy_bower_components.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ module.exports = function(grunt) {
// Iterate over each component and fix references to bower components
Object.keys(components).forEach(function(componentName) {
var file = grunt.file.read(fileName);
var newFile = '';

// Find replace file with the new path including the version number
var baseDirParts = components[componentName].directory.split('/');
Expand All @@ -66,43 +65,50 @@ module.exports = function(grunt) {
file = file.replace(originalPathRegex, newPath + '/');
grunt.log.ok(componentName + ' modified to reference version ' + components[componentName].version + ' in ' + fileName);

if(useComponentMin) {
if(useComponentMin && ! /\.min\./.test(fileName)) {
// Replace component file name with the minified version
var escapedNewPath = newPath.replace(/\//g, '\\/');
var componentReferenceRegex = new RegExp(escapedNewPath + '(.+)[\'"]');

// Check each line for component
file.split('\n').forEach(function(line) {
var matches = line.match(componentReferenceRegex);
var match = false;
if(matches != null) {
// verify that match 1 exists
if(matches[1] != null) {
var componentFn = matches[1];
var componentFnParts = componentFn.split('.');
var componentFnMin = componentFn.substr(0, componentFn.lastIndexOf(".")) + ".min" + componentFn.substr(componentFn.lastIndexOf("."), componentFn.length);
var newComponentPath = matches[0].replace(matches[1], componentFnMin).replace(/["']/, '');

var componentFilePath = path.resolve(optionsDest, path.join('../', newComponentPath));

grunt.log.debug("Checking if minified file exists " + componentFilePath);
// If the minified file exists use it
if(fs.existsSync(componentFilePath)) {
newFile += line.replace(componentFn, componentFnMin) + '\n';
grunt.log.ok(componentName + ' set to use minified library: ' + newComponentPath);
match = true;
}
}
}
if(! match) {
newFile += line + '\n';
}
});
grunt.file.write(fileName, setMin(newPath, componentName, file, optionsDest));
} else {
grunt.file.write(fileName, file);
}
grunt.file.write(fileName, newFile);
});
}

function setMin(newPath, componentName, file, optionsDest) {
var newFile = '';
var escapedNewPath = newPath.replace(/\//g, '\\/');
var componentReferenceRegex = new RegExp(escapedNewPath + '(.+)[\'"]');

// Check each line for component
file.split('\n').forEach(function(line) {
var matches = line.match(componentReferenceRegex);
var match = false;
if(matches != null) {
// verify that match 1 exists
if(matches[1] != null) {
var componentFn = matches[1];
var componentFnParts = componentFn.split('.');
var componentFnMin = componentFn.substr(0, componentFn.lastIndexOf(".")) + ".min" + componentFn.substr(componentFn.lastIndexOf("."), componentFn.length);
var newComponentPath = matches[0].replace(matches[1], componentFnMin).replace(/["']/, '');

var componentFilePath = path.resolve(optionsDest, path.join('../', newComponentPath));

grunt.log.debug("Checking if minified file exists " + componentFilePath);
// If the minified file exists use it
if(fs.existsSync(componentFilePath)) {
newFile += line.replace(componentFn, componentFnMin) + '\n';
grunt.log.ok(componentName + ' set to use minified library: ' + newComponentPath);
match = true;
}
}
}
if(! match) {
newFile += line + '\n';
}
});
return newFile;
}

grunt.registerTask('versionCopyBowerComponents', 'Version and stage Bower components for release.', function() {
// Set default options
var options = this.options({
Expand Down

0 comments on commit 86bd878

Please sign in to comment.