require("shelljs/global");

var path = require("path");
var fs   = require("fs");

// uninstall global babel install
try {
  exec("npm uninstall -g babel");
} catch (err) {}

// get packages
var packages = [];
ls("packages/*").forEach(function (loc) {
  var name = path.basename(loc);
  if (name[0] === ".") return;

  var pkgLoc = __dirname + "/../packages/" + name + "/package.json";
  if (!fs.existsSync(pkgLoc)) return;

  var pkg = require(pkgLoc);
  packages.push({
    folder: name,
    pkg: pkg,
    name: pkg.name
  });
});

// create links
packages.forEach(function (root) {
  console.log(root.name);

  var nodeModulesLoc = "packages/" + root.folder + "/node_modules";
  mkdir("-p", nodeModulesLoc);

  packages.forEach(function (sub) {
    if (!root.pkg.dependencies || !root.pkg.dependencies[sub.name]) return;

    if (!fs.existsSync(nodeModulesLoc + "/" + sub.name)) {
      console.log("Linking", "packages/" + sub.folder, "to", nodeModulesLoc + "/" + sub.name);
      ln("-s", "packages/" + sub.folder, nodeModulesLoc + "/" + sub.name);
    }
  });

  cd("packages/" + root.folder);
  exec("npm install");
  exec("npm link");
  cd("../..");
});

exec("make build");

@@ -741,24 +741,47 @@
 (sub) %7B%0A
+    var valid = false;%0A
     if (
 !root.pk
@@ -772,17 +772,16 @@
     if (
-!
 root.pkg
@@ -798,12 +798,11 @@
 ies 
-%7C%7C !
+&& 
 root
@@ -834,104 +834,150 @@
 e%5D) 
-return;%0A%0A    if (!fs.existsSync(nodeModulesLoc + %22/%22 + sub.name)) %7B%0A      console.log(%22Linking%22,
+valid = true;%0A    if (root.pkg.devDependencies && root.pkg.devDependencies%5Bsub.name%5D) valid = true;%0A    if (!valid) return;%0A%0A    var linkSrc =
  %22pa
@@ -1001,15 +1001,28 @@
 lder
-, %22to%22,
+;%0A    var linkDest =
  nod
@@ -1053,91 +1053,154 @@
 name
-)
 ;
+%0A
 %0A    
-  ln(%22-s%22, %22packages/%22 + sub.folder, nodeModulesLoc + %22/%22 + sub.name);%0A    %7D
+console.log(%22Linking%22, linkSrc, %22to%22, linkDest);%0A    if (fs.existsSync(linkDest)) fs.unlinkSync(linkDest);%0A    ln(%22-s%22, linkSrc, linkDest);
 %0A  %7D

allow local packages to be specified as devDependencies in bootstrap script