Properly detect npm install package name (#60)

diff --git a/index.js b/index.js
index 4853e2a..af47280 100644
--- a/index.js
+++ b/index.js
@@ -92,15 +92,16 @@
 
 function getTargetPackageSpecFromNpmInstallOutput (npmInstallOutput) {
     const lines = npmInstallOutput.split('\n');
-    if (lines[0].startsWith('+ ')) {
-        // npm >= 5
-        return lines[0].slice(2);
-    } else if (lines[1].startsWith('└─') || lines[1].startsWith('`-')) {
-        // 3 <= npm <= 4
-        return lines[1].slice(4).split(' ')[0];
-    } else {
-        throw new CordovaError('Could not determine package name from output:\n' + npmInstallOutput);
+    for (let i = 0; i < lines.length; i++) {
+        if (lines[i].startsWith('+ ')) {
+            // npm >= 5
+            return lines[i].slice(2);
+        } else if (lines[i].startsWith('└─') || lines[i].startsWith('`-')) {
+            // 3 <= npm <= 4
+            return lines[i].slice(4).split(' ')[0];
+        }
     }
+    throw new CordovaError('Could not determine package name from output:\n' + npmInstallOutput);
 }
 
 // Resolves to installation path of package defined by spec if the right version
diff --git a/spec/fetch-unit.spec.js b/spec/fetch-unit.spec.js
index e4c612f..514126e 100644
--- a/spec/fetch-unit.spec.js
+++ b/spec/fetch-unit.spec.js
@@ -78,6 +78,44 @@
     });
 });
 
+describe('getTargetPackageSpecFromNpmInstallOutput', () => {
+    const fetch = rewire('..');
+    const getTargetPackageSpecFromNpmInstallOutput = fetch.__get__('getTargetPackageSpecFromNpmInstallOutput');
+    const outputSampleNpm5 = '+ cordova-electron@1.0.0-dev';
+    const outputSampleNpm3 = 'helloworld@1.0.0 /cordova-project\n' +
+                             '└─┬ cordova-electron@1.0.0-dev  (git://github.com/apache/cordova-electron.git)';
+    const outputSampleNpm5WithPostinstall = '> electron@3.1.1 postinstall /cordova-project/node_modules/electron\n' +
+                                            '> node install.js\n\n' +
+                                            '+ cordova-electron@1.0.0-dev\n';
+    const outputSampleNpm3WithPostinstall = '> electron@3.1.1 postinstall /cordova-project/node_modules/electron\n' +
+                                            '> node install.js\n' +
+                                            'helloworld@1.0.0 /cordova-project\n' +
+                                            '└─┬ cordova-electron@1.0.0-dev  (git://github.com/apache/cordova-electron.git)';
+    const wrongOutputSample = 'Wrong output';
+
+    it('should parse the package name using  npm >= 5', () => {
+        expect(getTargetPackageSpecFromNpmInstallOutput(outputSampleNpm5)).toEqual('cordova-electron@1.0.0-dev');
+    });
+
+    it('should parse the package name using npm 3 <= npm <= 4', () => {
+        expect(getTargetPackageSpecFromNpmInstallOutput(outputSampleNpm3)).toEqual('cordova-electron@1.0.0-dev');
+    });
+
+    it('should parse the package name using npm >= 5 with postinstall ', () => {
+        expect(getTargetPackageSpecFromNpmInstallOutput(outputSampleNpm5WithPostinstall)).toEqual('cordova-electron@1.0.0-dev');
+    });
+
+    it('should parse the package name using npm 3 <= npm <= 4 with postinstall', () => {
+        expect(getTargetPackageSpecFromNpmInstallOutput(outputSampleNpm3WithPostinstall)).toEqual('cordova-electron@1.0.0-dev');
+    });
+
+    it('should gracefully handle if could not determine the package name from output', () => {
+        expect(() => {
+            getTargetPackageSpecFromNpmInstallOutput(wrongOutputSample);
+        }).toThrow(new Error('Could not determine package name from output:\n' + wrongOutputSample));
+    });
+});
+
 describe('resolvePathToPackage', () => {
     let tmpDir, resolvePathToPackage, expectedPath, NODE_PATH;