Skip to content

Commit 7ced03e

Browse files
committed
Different npm install result from npm 5.
1 parent fb869c0 commit 7ced03e

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed

lib/declarations.d.ts

+74-2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,37 @@ interface INpmPeerDependencyInfo {
149149
peerMissing: boolean;
150150
}
151151

152+
/**
153+
* Describes information about dependency update packages.
154+
*/
155+
interface INpm5DependencyInfo {
156+
/**
157+
* Npm action type.
158+
* @type {string}
159+
*/
160+
action: string;
161+
/**
162+
* Dependency name.
163+
* @type {string}
164+
*/
165+
name: string;
166+
/**
167+
* Dependency version.
168+
* @type {string}
169+
*/
170+
version: string;
171+
/**
172+
* Destination of the installation.
173+
* @type {string}
174+
*/
175+
path: string;
176+
/**
177+
* Dependency previous version.
178+
* @type {string}
179+
*/
180+
previousVersion: string;
181+
}
182+
152183
/**
153184
* Describes information returned by the npm CLI upon calling install with --json flag.
154185
*/
@@ -168,13 +199,54 @@ interface INpmInstallCLIResult {
168199
* Whenever installing npm prints the information by reversing the tree of operations and because the initial dependency was installed last it is listed first.
169200
* @type {INpmDependencyInfo | INpmPeerDependencyInfo}
170201
*/
171-
dependencies: INpmDependencyInfo | INpmPeerDependencyInfo;
202+
dependencies?: INpmDependencyInfo | INpmPeerDependencyInfo;
172203
/**
173204
* Describes problems that might have occurred during installation. For example missing peer dependencies.
174205
*/
175206
problems?: string[];
176207
}
177208

209+
/**
210+
* Describes information returned by the npm 5 CLI upon calling install with --json flag.
211+
*/
212+
interface INpm5InstallCliResult {
213+
/**
214+
* Added dependencies. Note that whenever add a particular dependency with npm 5 it is listed inside of array with key "Added".
215+
* @type {INpmDependencyUpdateInfo[]}
216+
*/
217+
added: INpm5DependencyInfo[];
218+
/**
219+
* Removed dependencies. Note that whenever remove a particular dependency with npm 5 it is listed inside of array with key "removed".
220+
* @type {INpmDependencyUpdateInfo[]}
221+
*/
222+
removed: INpm5DependencyInfo[];
223+
/**
224+
* Updated dependencies. Note that whenever update a particular dependency with npm 5 it is listed inside of array with key "updated".
225+
* @type {INpmDependencyUpdateInfo[]}
226+
*/
227+
updated: INpm5DependencyInfo[];
228+
/**
229+
* Moved dependencies. Note that whenever move a particular dependency with npm 5 it is listed inside of array with key "moved".
230+
* @type {INpmDependencyUpdateInfo[]}
231+
*/
232+
moved: INpm5DependencyInfo[];
233+
/**
234+
* Failed dependencies. Note that whenever use npm 5 and the operation over particular dependency fail it is listed inside of array with key "failed".
235+
* @type {INpmDependencyUpdateInfo[]}
236+
*/
237+
failed: INpm5DependencyInfo[];
238+
/**
239+
* Warnings. Note that whenever use npm 5 and the operation over particular dependency have warnings they are listed inside of array with key "warnings".
240+
* @type {INpmDependencyUpdateInfo[]}
241+
*/
242+
warnings: INpm5DependencyInfo[];
243+
/**
244+
*Time elapsed.
245+
* @type {Number}
246+
*/
247+
elapsed: Number
248+
}
249+
178250
/**
179251
* Describes information about installed package.
180252
*/
@@ -193,7 +265,7 @@ interface INpmInstallResultInfo {
193265
* The original output that npm CLI produced upon installation.
194266
* @type {INpmInstallCLIResult}
195267
*/
196-
originalOutput?: INpmInstallCLIResult;
268+
originalOutput?: INpmInstallCLIResult | INpm5InstallCliResult;
197269
}
198270

199271
interface INpmInstallOptions {

lib/node-package-manager.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,23 @@ export class NodePackageManager implements INodePackageManager {
143143
private parseNpmInstallResult(npmDryRunInstallOutput: string, npmInstallOutput: string, userSpecifiedPackageName: string): INpmInstallResultInfo {
144144
// TODO: Add tests for this functionality
145145
try {
146-
const originalOutput: INpmInstallCLIResult = JSON.parse(npmDryRunInstallOutput);
147-
const name = _.head(_.keys(originalOutput.dependencies));
148-
const dependency = _.pick<INpmDependencyInfo, INpmDependencyInfo | INpmPeerDependencyInfo>(originalOutput.dependencies, name);
146+
const originalOutput: INpmInstallCLIResult | INpm5InstallCliResult = JSON.parse(npmDryRunInstallOutput);
147+
const npm5Output = <INpm5InstallCliResult> originalOutput;
148+
const npmOutput = <INpmInstallCLIResult> originalOutput;
149+
const name = _.head(_.keys(npmOutput.dependencies));
150+
151+
// Npm 5 return different object after performing `npm install --dry-run`.
152+
// Considering that the dependency is already installed we should
153+
// find it in the `updated` key as a first element of the array.
154+
if (!name && npm5Output.updated) {
155+
const updatedDependency = npm5Output.updated[0];
156+
return {
157+
name: updatedDependency.name,
158+
originalOutput,
159+
version: updatedDependency.version
160+
};
161+
}
162+
const dependency = _.pick<INpmDependencyInfo, INpmDependencyInfo | INpmPeerDependencyInfo>(npmOutput.dependencies, name);
149163
return {
150164
name,
151165
originalOutput,

0 commit comments

Comments
 (0)