You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2019/01/23 04:45:05 UTC
[cordova-fetch] branch master updated: Properly detect npm install
package name (#60)
This is an automated email from the ASF dual-hosted git repository.
erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-fetch.git
The following commit(s) were added to refs/heads/master by this push:
new dfe158e Properly detect npm install package name (#60)
dfe158e is described below
commit dfe158e555ecddd6e47dac0c93d2643129890bd4
Author: Gedas Gardauskas <ge...@gmail.com>
AuthorDate: Wed Jan 23 13:44:58 2019 +0900
Properly detect npm install package name (#60)
---
index.js | 17 +++++++++--------
spec/fetch-unit.spec.js | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 47 insertions(+), 8 deletions(-)
diff --git a/index.js b/index.js
index 4853e2a..af47280 100644
--- a/index.js
+++ b/index.js
@@ -92,15 +92,16 @@ function npmArgs (target, userOptions) {
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('npmArgs', function () {
});
});
+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;
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org