-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a3a9fb2
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
._DSStore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
rnpm - Recursive NPM | ||
==================== | ||
|
||
`rnpm` is a wrapper aroung `npm` to allow it to recursively read dependencies from multiple `package.json` files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env node | ||
|
||
var app = require('commander') | ||
, fs = require('fs') | ||
, rnpm = require('../lib'); | ||
|
||
app | ||
.version(JSON.parse(fs.readFileSync('package.json', 'utf-8')).version); | ||
|
||
app | ||
.command('install') | ||
.description('Recursively install dependencies') | ||
.action(function() { | ||
rnpm.install() | ||
}); | ||
|
||
app | ||
.parse(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.install = require('./install') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
var dive = require('dive') | ||
, fs = require('fs') | ||
, path = require('path') | ||
, exec = require('child_process').exec | ||
, npm = require('npm') | ||
|
||
module.exports = function install() { | ||
|
||
// Initialize NPM as a lib | ||
npm.load({}, function() { | ||
|
||
var cwd = process.cwd() | ||
, depsPerDir = { | ||
'.': {} | ||
} | ||
, done = false | ||
, pending = 0 | ||
|
||
// Iterate through all directories | ||
eachDir(null, cwd) | ||
dive(cwd, { directories: true, files: false }, eachDir, whenDone) | ||
|
||
function eachDir(err, dir) { | ||
if (err) throw err; | ||
if (dir.match(/node_modules/)) { | ||
return | ||
} | ||
|
||
// Check for existance of package.json and read it | ||
This comment has been minimized.
Sorry, something went wrong. |
||
var pkgjson = path.join(dir, 'package.json') | ||
pending++; | ||
fs.exists(pkgjson, function(exists) { | ||
if (exists) { | ||
fs.readFile(pkgjson, 'utf-8', function(err, json) { | ||
if (err) throw err; | ||
var data = JSON.parse(json); | ||
|
||
// Extract all dependencies | ||
addDependencies(data.dependencies, path.relative(cwd, dir)) | ||
--pending | ||
checkReady(); | ||
}) | ||
} else { | ||
--pending | ||
checkReady(); | ||
} | ||
}) | ||
} | ||
|
||
function addDependencies(deps, dir) { | ||
for (dep in deps) { | ||
|
||
// Try to add dependencies to root directory | ||
if (!depsPerDir['.'][dep]) { | ||
depsPerDir['.'][dep] = deps[dep] | ||
} else { | ||
// check for inconsistent dependency versions. | ||
if (depsPerDir['.'][dep] != deps[dep]) { | ||
console.warn('(WARNING) ' + dir + ': inconsistent dependency version ' + dep + '@' + deps[dep]) | ||
|
||
// create dir entry if necessary | ||
if (!depsPerDir[dir]) { | ||
depsPerDir[dir] = {} | ||
} | ||
|
||
// Add dependency to nested directory | ||
depsPerDir[dir][dep] = deps[dep] | ||
} | ||
} | ||
} | ||
} | ||
|
||
// called when all directories are visited | ||
function whenDone() { | ||
done = true | ||
checkReady(); | ||
} | ||
|
||
// Check if all package.json files were read | ||
function checkReady() { | ||
if (done && !pending) { | ||
install() | ||
} | ||
} | ||
|
||
// install everything | ||
function install() { | ||
var dirs = Object.keys(depsPerDir) | ||
|
||
// install each directory with dependencies | ||
function installDir(i) { | ||
if (i >= dirs.length) { | ||
return completed() | ||
} | ||
|
||
var dir = dirs[i] | ||
|
||
console.log('Installing deps for directory ' + dir) | ||
|
||
var deps = depsPerDir[dir]; | ||
var depStrings = []; | ||
|
||
for (var dep in deps) { | ||
depStrings.push(dep + '@' + deps[dep]) | ||
} | ||
|
||
npm.prefix = path.join(cwd, dir); | ||
|
||
installDep(0) | ||
|
||
// install dependencies (by groups of ~20) | ||
function installDep(j) { | ||
if (j >= depStrings.length) { | ||
installDir(i+1) | ||
return; | ||
} | ||
|
||
npm.commands.install(depStrings.slice(j, j+20), function(err) { | ||
if (err) throw err; | ||
installDep(j+20) | ||
}) | ||
} | ||
|
||
} | ||
installDir(0) | ||
} | ||
|
||
function completed() { | ||
|
||
} | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "rnpm", | ||
"description": "Recursive NPM", | ||
"tags": [ | ||
"npm", | ||
"package manager" | ||
], | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"commander": "~1.0.5", | ||
"dive": "~0.2.0", | ||
"npm": "~1.1.65" | ||
}, | ||
"bin": { | ||
"rnpm": "./bin/rnpm" | ||
} | ||
} |
existence