var Plugin = require('broccoli-plugin');
var path = require('path');
var fs = require('fs');
var RSVP = require('rsvp');
var required = require('required');
var symlinkOrCopySync = require('symlink-or-copy').sync;

// Create a subclass NodeImporter derived from Plugin
NodeImporter.prototype = Object.create(Plugin.prototype);
NodeImporter.prototype.constructor = NodeImporter;

function NodeImporter(inputNodes, options) {
  options = options || {};
  Plugin.call(this, inputNodes, {
    annotation: options.annotation
  });
  this.options = options;
  this.options.ignore = this.options.ignore || [];
  this.options.mainFile = this.options.mainFile || 'index.js';
}

NodeImporter.prototype.build = function() {
	this.npmPaths = [];
	var indexPath = path.join(this.inputPaths[0], this.options.mainFile);

	var output = "";
	var nodeModulesOutPath = path.join(this.outputPath, 'node_modules');
	fs.mkdirSync(nodeModulesOutPath);

	var self = this;

	return new RSVP.Promise(function(resolve, reject) {
		required(indexPath, function(err, deps) {
			if (err) {
				reject(err);
			}
			else {
		    self.linkDeps(deps, nodeModulesOutPath);
		    resolve(null);
		  }
		});
  });
};

NodeImporter.prototype.linkDeps = function(deps, nodeModulesOutPath) {
	const moduleFolderRegex = /(.*\\node_modules\\)([^\\]*)\\.*/;
	if (deps) {
		var len = deps.length;
		for (var i = 0; i < len; i++) {
			var dependency = deps[i];
			if (!dependency.core && 
				  !this.options.ignore.contains(dependency.id))
			{
				var parts = moduleFolderRegex.exec(dependency.filename);
				var folder = parts[2];
		    if (this.npmPaths.indexOf(folder) === -1)
		    {
		    	// haven't seen this folder before
		    	this.npmPaths.push(folder);
		    	var modulePath = path.join(parts[1], folder);
		    	this.linkNpmModule(modulePath, nodeModulesOutPath);
		    }
	    	// recurse
				this.linkDeps(dependency.deps, nodeModulesOutPath);
			}
		}
	}
}

NodeImporter.prototype.linkNpmModule = function(folder, nodeModulesOutPath) {
	var module = path.basename(folder);
	moduleOutPath = path.join(nodeModulesOutPath, module);
	symlinkOrCopySync(folder, moduleOutPath);
}

module.exports = NodeImporter;
@@ -417,18 +417,17 @@
 ions) %7B%0A
-  
+%09
 options 
@@ -443,18 +443,17 @@
  %7C%7C %7B%7D;%0A
-  
+%09
 Plugin.c
@@ -476,20 +476,18 @@
 odes, %7B%0A
-    
+%09%09
 annotati
@@ -513,16 +513,14 @@
 ion%0A
-  
+%09
 %7D);%0A
-  
+%09
 this
@@ -539,18 +539,17 @@
 ptions;%0A
-  
+%09
 this.opt
@@ -589,18 +589,17 @@
  %7C%7C %5B%5D;%0A
-  
+%09
 this.opt
@@ -1079,20 +1079,18 @@
 lse %7B%0A%09%09
-    
+%09%09
 self.lin
@@ -1124,20 +1124,18 @@
 ath);%0A%09%09
-    
+%09%09
 resolve(
@@ -1147,18 +1147,17 @@
 ;%0A%09%09
-  
+%09
 %7D%0A%09%09%7D);%0A
   %7D)
@@ -1152,18 +1152,17 @@
 %7D%0A%09%09%7D);%0A
-  
+%09
 %7D);%0A%7D;%0A%0A
@@ -1428,18 +1428,17 @@
 && %0A%09%09%09%09
-  
+%09
 !this.op
@@ -1541,16 +1541,38 @@
 ename);%0A
+%09%09%09%09if (parts)%0A%09%09%09%09%7B%0A%09
 %09%09%09%09var 
@@ -1592,20 +1592,19 @@
 s%5B2%5D;%0A%09%09
-    
+%09%09%09
 if (this
@@ -1643,20 +1643,18 @@
 )%0A%09%09
-    %7B%0A%09%09    
+%09%09%09%7B%0A%09%09%09%09%09
 %09// 
@@ -1687,20 +1687,19 @@
 efore%0A%09%09
-    
+%09%09%09
 %09this.np
@@ -1721,20 +1721,19 @@
 der);%0A%09%09
-    
+%09%09%09
 %09var mod
@@ -1773,20 +1773,19 @@
 der);%0A%09%09
-    
+%09%09%09
 %09this.li
@@ -1835,19 +1835,17 @@
 ;%0A%09%09
-    %7D%0A%09    
+%09%09%09%7D%0A%09%09%09%09
 %09// 
@@ -1852,16 +1852,17 @@
 recurse%0A
+%09
 %09%09%09%09this
@@ -1909,16 +1909,22 @@
 tPath);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

fix bug when no dependencies