Skip to content

Commit

Permalink
Different npm install result from npm 5.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomaNikolov committed Jun 27, 2017
1 parent fb869c0 commit 7ced03e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
76 changes: 74 additions & 2 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,37 @@ interface INpmPeerDependencyInfo {
peerMissing: boolean;
}

/**
* Describes information about dependency update packages.
*/
interface INpm5DependencyInfo {
/**
* Npm action type.
* @type {string}
*/
action: string;
/**
* Dependency name.
* @type {string}
*/
name: string;
/**
* Dependency version.
* @type {string}
*/
version: string;
/**
* Destination of the installation.
* @type {string}
*/
path: string;
/**
* Dependency previous version.
* @type {string}
*/
previousVersion: string;
}

/**
* Describes information returned by the npm CLI upon calling install with --json flag.
*/
Expand All @@ -168,13 +199,54 @@ interface INpmInstallCLIResult {
* Whenever installing npm prints the information by reversing the tree of operations and because the initial dependency was installed last it is listed first.
* @type {INpmDependencyInfo | INpmPeerDependencyInfo}
*/
dependencies: INpmDependencyInfo | INpmPeerDependencyInfo;
dependencies?: INpmDependencyInfo | INpmPeerDependencyInfo;
/**
* Describes problems that might have occurred during installation. For example missing peer dependencies.
*/
problems?: string[];
}

/**
* Describes information returned by the npm 5 CLI upon calling install with --json flag.
*/
interface INpm5InstallCliResult {
/**
* Added dependencies. Note that whenever add a particular dependency with npm 5 it is listed inside of array with key "Added".
* @type {INpmDependencyUpdateInfo[]}
*/
added: INpm5DependencyInfo[];
/**
* Removed dependencies. Note that whenever remove a particular dependency with npm 5 it is listed inside of array with key "removed".
* @type {INpmDependencyUpdateInfo[]}
*/
removed: INpm5DependencyInfo[];
/**
* Updated dependencies. Note that whenever update a particular dependency with npm 5 it is listed inside of array with key "updated".
* @type {INpmDependencyUpdateInfo[]}
*/
updated: INpm5DependencyInfo[];
/**
* Moved dependencies. Note that whenever move a particular dependency with npm 5 it is listed inside of array with key "moved".
* @type {INpmDependencyUpdateInfo[]}
*/
moved: INpm5DependencyInfo[];
/**
* Failed dependencies. Note that whenever use npm 5 and the operation over particular dependency fail it is listed inside of array with key "failed".
* @type {INpmDependencyUpdateInfo[]}
*/
failed: INpm5DependencyInfo[];
/**
* Warnings. Note that whenever use npm 5 and the operation over particular dependency have warnings they are listed inside of array with key "warnings".
* @type {INpmDependencyUpdateInfo[]}
*/
warnings: INpm5DependencyInfo[];
/**
*Time elapsed.
* @type {Number}
*/
elapsed: Number
}

/**
* Describes information about installed package.
*/
Expand All @@ -193,7 +265,7 @@ interface INpmInstallResultInfo {
* The original output that npm CLI produced upon installation.
* @type {INpmInstallCLIResult}
*/
originalOutput?: INpmInstallCLIResult;
originalOutput?: INpmInstallCLIResult | INpm5InstallCliResult;
}

interface INpmInstallOptions {
Expand Down
20 changes: 17 additions & 3 deletions lib/node-package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,23 @@ export class NodePackageManager implements INodePackageManager {
private parseNpmInstallResult(npmDryRunInstallOutput: string, npmInstallOutput: string, userSpecifiedPackageName: string): INpmInstallResultInfo {
// TODO: Add tests for this functionality
try {
const originalOutput: INpmInstallCLIResult = JSON.parse(npmDryRunInstallOutput);
const name = _.head(_.keys(originalOutput.dependencies));
const dependency = _.pick<INpmDependencyInfo, INpmDependencyInfo | INpmPeerDependencyInfo>(originalOutput.dependencies, name);
const originalOutput: INpmInstallCLIResult | INpm5InstallCliResult = JSON.parse(npmDryRunInstallOutput);
const npm5Output = <INpm5InstallCliResult> originalOutput;
const npmOutput = <INpmInstallCLIResult> originalOutput;
const name = _.head(_.keys(npmOutput.dependencies));

// Npm 5 return different object after performing `npm install --dry-run`.
// Considering that the dependency is already installed we should
// find it in the `updated` key as a first element of the array.
if (!name && npm5Output.updated) {
const updatedDependency = npm5Output.updated[0];
return {
name: updatedDependency.name,
originalOutput,
version: updatedDependency.version
};
}
const dependency = _.pick<INpmDependencyInfo, INpmDependencyInfo | INpmPeerDependencyInfo>(npmOutput.dependencies, name);
return {
name,
originalOutput,
Expand Down

0 comments on commit 7ced03e

Please sign in to comment.