Skip to content

Commit

Permalink
Initial version.
Browse files Browse the repository at this point in the history
  • Loading branch information
coreh committed Nov 8, 2012
0 parents commit a3a9fb2
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
._DSStore
4 changes: 4 additions & 0 deletions Readme.md
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.
18 changes: 18 additions & 0 deletions bin/rnpm
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);
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.install = require('./install')
133 changes: 133 additions & 0 deletions lib/install.js
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.

Copy link
@TooTallNate

TooTallNate Nov 8, 2012

Contributor

existence

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() {

}
})
}

17 changes: 17 additions & 0 deletions package.json
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"
}
}

0 comments on commit a3a9fb2

Please sign in to comment.