read-components
Advanced tools
Comparing version 0.5.4 to 0.6.0
113
index.js
var sysPath = require('path'); | ||
var fs = require('fs'); | ||
var each = require('async-each'); | ||
var events = require('events'); | ||
var emitter = new events.EventEmitter(); | ||
var path = require('path'); | ||
@@ -15,2 +18,10 @@ var jsonPaths = { | ||
}; | ||
var dependencyLocator = { | ||
bower: 'name', | ||
component: 'repo' | ||
}; | ||
var Builder = require('component-builder'); | ||
var jsonProps = ['main', 'scripts', 'styles']; | ||
@@ -26,11 +37,20 @@ | ||
var resolveComponent = function(name) { | ||
var sanitizeRepo = function(repo) { | ||
if (repo.indexOf('/') !== repo.lastIndexOf('/')) { | ||
var res = repo.split('/'); | ||
return res[res.length-1] + '=' + res[res.length]; | ||
} | ||
return repo.replace('/', '-'); | ||
} | ||
/*var resolveComponent = function(name) { | ||
return name.replace('/', '-'); | ||
}; | ||
};*/ | ||
var readJson = function(file, callback) { | ||
var readJson = function(file, type, callback) { | ||
fs.exists(file, function(exists) { | ||
if (!exists) { | ||
var err = new Error('Component must have "' + file + '"'); | ||
err.code = 'NO_BOWER_JSON'; | ||
err.code = 'NO_'+ type.toUpperCase() +'_JSON'; | ||
return callback(err); | ||
@@ -86,3 +106,3 @@ }; | ||
var _read = function(actualPath) { | ||
readJson(actualPath, function(error, json) { | ||
readJson(actualPath, type, function(error, json) { | ||
if (error) return callback(error); | ||
@@ -95,3 +115,3 @@ if (overrides) { | ||
if (!json.main) { | ||
if (type === 'bower' && !json.main) { | ||
return callback(new Error('Component JSON file "' + actualPath + '" must have `main` property. See https://github.com/paulmillr/read-components#README')); | ||
@@ -105,9 +125,9 @@ } | ||
}); | ||
callback(null, { | ||
name: pkg.name, version: pkg.version, files: files, | ||
dependencies: pkg.dependencies || {} | ||
name: pkg.name, version: pkg.version, repo: pkg.repo, | ||
files: files, dependencies: pkg.dependencies || {} | ||
}); | ||
}); | ||
}; | ||
fs.exists(dotpath, function(isStableBower) { | ||
@@ -118,5 +138,5 @@ _read(isStableBower ? dotpath : fullPath) | ||
var gatherDeps = function(packages) { | ||
var gatherDeps = function(packages, type) { | ||
return Object.keys(packages.reduce(function(obj, item) { | ||
if (!obj[item.name]) obj[item.name] = true; | ||
if (!obj[item[dependencyLocator[type]]]) obj[item[dependencyLocator[type]]] = true; | ||
Object.keys(item.dependencies).forEach(function(dep) { | ||
@@ -132,2 +152,3 @@ if (!obj[dep]) obj[dep] = true; | ||
var paths = list.map(function(item) { | ||
if (type === 'component') item = sanitizeRepo(item); | ||
return {path: sysPath.join(parent, item), overrides: overrides[item]}; | ||
@@ -138,3 +159,2 @@ }); | ||
if (error) return callback(error); | ||
var processed = allProcessed.concat(newProcessed); | ||
@@ -144,6 +164,6 @@ | ||
processed.forEach(function(_) { | ||
processedNames[_.name] = true; | ||
processedNames[_[dependencyLocator[type]]] = true; | ||
}); | ||
var newDeps = gatherDeps(newProcessed).filter(function(item) { | ||
var newDeps = gatherDeps(newProcessed, type).filter(function(item) { | ||
return !processedNames[item]; | ||
@@ -169,5 +189,6 @@ }); | ||
// Iterate recursively over each dependency and increase level | ||
// on each iteration. | ||
var setSortingLevels = function(packages) { | ||
var setSortingLevels = function(packages, type) { | ||
var setLevel = function(initial, pkg) { | ||
@@ -180,4 +201,16 @@ var level = Math.max(pkg.sortingLevel || 0, initial); | ||
var dep = find(packages, function(_) { | ||
return _.name === depName; | ||
if (type === 'component') { | ||
var repo = _[dependencyLocator[type]]; | ||
if (repo === depName) | ||
return true; | ||
// nasty hack to ensure component repo ends with the specified repo | ||
// e.g. "repo": "https://raw.github.com/component/typeof" | ||
var suffix = '/' + depName; | ||
return repo.indexOf(suffix, repo.length - suffix.length) !== -1; | ||
} else { | ||
return _[dependencyLocator[type]] === depName; | ||
} | ||
}); | ||
if (!dep) { | ||
@@ -192,3 +225,2 @@ var names = Object.keys(packages).map(function(_) { | ||
}; | ||
packages.forEach(setLevel.bind(null, 1)); | ||
@@ -198,5 +230,5 @@ return packages; | ||
// Sort packages automatically, based on their dependencies. | ||
var sortPackages = function(packages) { | ||
return setSortingLevels(packages).sort(function(a, b) { | ||
// Sort packages automatically, bas'component'ed on their dependencies. | ||
var sortPackages = function(packages, type) { | ||
return setSortingLevels(packages, type).sort(function(a, b) { | ||
return b.sortingLevel - a.sortingLevel; | ||
@@ -207,6 +239,7 @@ }); | ||
var init = function(directory, type, callback) { | ||
readJson(sysPath.join(directory, jsonPaths[type]), callback); | ||
readJson(sysPath.join(directory, jsonPaths[type]), type, callback); | ||
}; | ||
var readBowerComponents = function(directory, callback) { | ||
var readComponents = function(directory, callback, type) { | ||
if (typeof directory === 'function') { | ||
@@ -218,5 +251,5 @@ callback = directory; | ||
init(directory, 'bower', function(error, json) { | ||
init(directory, type, function(error, json) { | ||
if (error) { | ||
if (error.code === 'NO_BOWER_JSON') { | ||
if (error.code === 'NO_' + type.toUpperCase() + '_JSON') { | ||
return callback(null, []); | ||
@@ -231,7 +264,17 @@ } else { | ||
readPackages(directory, 'bower', [], deps, overrides, function(error, data) { | ||
readPackages(directory, type, [], deps, overrides, function(error, data) { | ||
if (error) return callback(error); | ||
var sorted = sortPackages(data); | ||
// console.debug('getPaths', sorted.map(function(_) {return _.name;})); | ||
callback(null, sorted); | ||
var sorted = sortPackages(data, type); | ||
aliases = []; | ||
if (type === 'component') | ||
{ | ||
builder = new Builder(directory); | ||
var aliases = []; | ||
emitter.on('addAlias', function(alias) {aliases.push(alias);}) | ||
builder.buildAliases(function(err, res) { | ||
callback(null, sorted, aliases); | ||
}); | ||
} | ||
else | ||
callback(null, sorted); | ||
}); | ||
@@ -241,5 +284,17 @@ }); | ||
Builder.prototype.alias = function(a,b) { | ||
var name = this.root | ||
? this.config.name | ||
: this.basename; | ||
var res = {}; | ||
res[name + '/' + b] = a; | ||
emitter.emit('addAlias', res) | ||
return 'require.alias("' + name + '/' + b + '", "' + a + '");'; | ||
}; | ||
var read = function(root, type, callback) { | ||
if (type === 'bower') { | ||
readBowerComponents(root, callback); | ||
readComponents(root, callback, type); | ||
} else if (type === 'component') { | ||
readComponents(root, callback, type); | ||
} else { | ||
@@ -246,0 +301,0 @@ throw new Error('read-components: unknown type'); |
{ | ||
"name": "read-components", | ||
"version": "0.5.4", | ||
"version": "0.6.0", | ||
"description": "Read bower and component(1) components", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha" | ||
"test": "cd test && ../node_modules/.bin/mocha main-test.js" | ||
}, | ||
@@ -13,3 +13,4 @@ "repository": "git://github.com/paulmillr/read-components.git", | ||
"dependencies": { | ||
"async-each": "~0.1.3" | ||
"async-each": "~0.1.3", | ||
"component-builder": "~0.10.0" | ||
}, | ||
@@ -16,0 +17,0 @@ "devDependencies": { |
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
12218
244
2
+ Addedcomponent-builder@~0.10.0
+ Addedbatch@0.2.1(transitive)
+ Addedcomponent-builder@0.10.1(transitive)
+ Addedcomponent-require@0.3.1(transitive)
+ Addedcp@0.1.1(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addedmkdirp@0.3.4(transitive)
+ Addedms@2.1.3(transitive)
+ Addedstring-to-js@0.0.1(transitive)