read-components
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -0,1 +1,4 @@ | ||
# read-components 0.4.0 (7 July 2013) | ||
* Changed API. | ||
# read-components 0.3.0 (19 June 2013) | ||
@@ -2,0 +5,0 @@ * Added new API syntax. |
122
index.js
@@ -5,2 +5,13 @@ var sysPath = require('path'); | ||
var jsonPaths = { | ||
bower: 'bower.json', | ||
component: 'component.json' | ||
}; | ||
var dirs = { | ||
bower: 'bower_components', | ||
component: 'components' | ||
}; | ||
var jsonProps = ['main', 'scripts', 'styles']; | ||
// Return unique list items. | ||
@@ -14,6 +25,10 @@ var unique = function(list) { | ||
var resolveComponent = function(name) { | ||
return name.replace('/', '-'); | ||
}; | ||
var readJson = function(file, callback) { | ||
fs.exists(file, function(exists) { | ||
if (!exists) { | ||
return callback(new Error('Component must have "' + bowerJson + '"')); | ||
return callback(new Error('Component must have "' + file + '"')); | ||
}; | ||
@@ -33,6 +48,2 @@ | ||
if (!json.main && !json.scripts && !json.styles) { | ||
return callback(new Error('Component JSON file must have `main` property. See git.io/brunch-bower. Contact component author of "' + file + '"')); | ||
} | ||
callback(null, json); | ||
@@ -43,9 +54,2 @@ }); | ||
// Read bower.json. | ||
// Returns String. | ||
var jsonPaths = { | ||
bower: 'bower.json', | ||
component: 'component.json' | ||
}; | ||
var getJsonPath = function(path, type) { | ||
@@ -58,3 +62,3 @@ return sysPath.resolve(sysPath.join(path, jsonPaths[type])); | ||
if (data.main && !Array.isArray(data.main)) data.main = [data.main]; | ||
['main', 'scripts', 'styles'].forEach(function(_) { | ||
jsonProps.forEach(function(_) { | ||
if (!data[_]) data[_] = []; | ||
@@ -67,3 +71,3 @@ }); | ||
var list = []; | ||
['main', 'scripts', 'styles'].forEach(function(property) { | ||
jsonProps.forEach(function(property) { | ||
pkg[property].forEach(function(item) { | ||
@@ -76,5 +80,18 @@ list.push(item); | ||
var processPackage = function(path, callback) { | ||
readJson(getJsonPath(path, 'bower'), function(error, json) { | ||
var processPackage = function(type, pkg, callback) { | ||
var path = pkg.path; | ||
var overrides = pkg.overrides; | ||
readJson(getJsonPath(path, type), function(error, json) { | ||
if (error) return callback(error); | ||
if (overrides) { | ||
Object.keys(overrides).forEach(function(key) { | ||
json[key] = overrides[key]; | ||
}); | ||
} | ||
if (!json.main && !json.scripts && !json.styles) { | ||
return callback(new Error('Component JSON file "' + path + '" must have `main` property. See git.io/brunch-bower')); | ||
} | ||
var pkg = standardizePackage(json); | ||
@@ -91,9 +108,38 @@ var files = getPackageFiles(pkg).map(function(relativePath) { | ||
var readBowerPackages = function(list, parent, callback) { | ||
if (parent == null) parent = sysPath.join('.', 'components'); | ||
var gatherDeps = function(packages) { | ||
return Object.keys(packages.reduce(function(obj, item) { | ||
if (!obj[item.name]) obj[item.name] = true; | ||
Object.keys(item.dependencies).forEach(function(dep) { | ||
if (!obj[dep]) obj[dep] = true; | ||
}); | ||
return obj; | ||
}, {})); | ||
}; | ||
var readPackages = function(root, type, allProcessed, list, overrides, callback) { | ||
var parent = sysPath.join(root, dirs[type]); | ||
var paths = list.map(function(item) { | ||
return sysPath.join(parent, item); | ||
return {path: sysPath.join(parent, item), override: overrides[item]}; | ||
}); | ||
each(paths, processPackage, callback); | ||
each(paths, processPackage.bind(null, type), function(error, newProcessed) { | ||
if (error) return callback(error); | ||
var processed = allProcessed.concat(newProcessed); | ||
var processedNames = {}; | ||
processed.forEach(function(_) { | ||
processedNames[_.name] = true; | ||
}); | ||
var newDeps = gatherDeps(newProcessed).filter(function(item) { | ||
return !processedNames[item]; | ||
}); | ||
if (newDeps.length === 0) { | ||
callback(error, processed); | ||
} else { | ||
readPackages(root, type, processed, newDeps, overrides, callback); | ||
} | ||
}); | ||
}; | ||
@@ -116,3 +162,3 @@ | ||
var deps = Object.keys(pkg.dependencies); | ||
// console.debug('setLevel', pkg.name, level); | ||
// console.log('setLevel', pkg.name, level); | ||
pkg.sortingLevel = level; | ||
@@ -137,12 +183,24 @@ deps.forEach(function(dep) { | ||
var readBowerComponents = exports.readBowerComponents = function(directory, callback) { | ||
var readAllPackages = function(list, callback) { | ||
each(list, function() {}) | ||
}; | ||
var init = function(directory, type, callback) { | ||
readJson(sysPath.join(directory, jsonPaths[type]), callback); | ||
}; | ||
var readBowerComponents = function(directory, callback) { | ||
if (typeof directory === 'function') { | ||
callback = directory; | ||
directory = null; | ||
callback = directory; | ||
} | ||
if (directory == null) directory = '.'; | ||
var parent = sysPath.join(directory, 'components'); | ||
fs.readdir(parent, function(error, packages) { | ||
if (error) return callback(new Error(error)); | ||
readBowerPackages(packages, parent, function(error, data) { | ||
init(directory, 'bower', function(error, json) { | ||
if (error) return callback(error); | ||
var deps = Object.keys(json.dependencies || {}); | ||
var overrides = json.override || {}; | ||
readPackages(directory, 'bower', [], deps, overrides, function(error, data) { | ||
if (error) return callback(error); | ||
@@ -156,2 +214,12 @@ var sorted = sortPackages(data); | ||
var read = function(root, type, callback) { | ||
if (type === 'bower') { | ||
readBowerComponents(root, callback); | ||
} else { | ||
throw new Error('read-components: unknown type'); | ||
} | ||
}; | ||
module.exports = read; | ||
// getBowerPaths('.', console.log.bind(console)); |
{ | ||
"name": "read-components", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Read bower and component(1) components", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "./node_modules/.bin/mocha" | ||
}, | ||
@@ -16,5 +16,5 @@ "repository": "git://github.com/paulmillr/read-components.git", | ||
"devDependencies": { | ||
"mocha": "~1.10.0", | ||
"chai": "~1.6.0" | ||
"mocha": "~1.12.0", | ||
"chai": "~1.7.0" | ||
} | ||
} |
@@ -10,5 +10,7 @@ # read-components | ||
```javascript | ||
var reader = require('read-components'); | ||
var read = require('read-components'); | ||
reader.readBowerComponents('your-project-dir-with-bower.json', function(error, components) { | ||
// Second argument is type, | ||
// in future it will support component(1). | ||
read('your-project-dir-with-bower.json', 'bower', function(error, components) { | ||
console.log('All components:', components); | ||
@@ -15,0 +17,0 @@ }); |
@@ -5,5 +5,4 @@ { | ||
"dependencies": { | ||
"backbone": "~1.0.0", | ||
"bootstrap": "~2.3.2" | ||
"e": "~1.0.0" | ||
} | ||
} | ||
} |
require('chai').should(); | ||
var items = require('../'); | ||
var read = require('../'); | ||
var getAttr = function(name) { | ||
return function(item) { | ||
return item[name]; | ||
}; | ||
}; | ||
describe('Main', function() { | ||
describe('getPackageFiles', function() { | ||
it('should extract all package files', function() { | ||
var result = items.getPackageFiles({ | ||
main: ['a.js', 'b.js'], | ||
scripts: ['a.js', 'b.js', 'c.js'], | ||
styles: ['a.css'] | ||
it('should extract all package files', function(done) { | ||
read(__dirname, 'bower', function(error, value) { | ||
value.map(getAttr('name')).should.eql(['a', 'b', 'c', 'd', 'e']); | ||
done(); | ||
}); | ||
result.should.eql(['a.js', 'b.js', 'c.js', 'a.css']); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
8890
234
2
42