Skip to content

Commit 96ab06c

Browse files
authored
implement rollback of package.json contents of project if platform/plugin installation fails (#2531)
throw error when npm-installation-manager install fails
1 parent 34f5efd commit 96ab06c

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

lib/node-package-manager.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export class NodePackageManager implements INodePackageManager {
1717
config["ignore-scripts"] = true;
1818
}
1919

20-
let jsonContentBefore = this.$fs.readJson(path.join(pathToSave, "package.json"));
20+
let packageJsonPath = path.join(pathToSave, "package.json");
21+
let jsonContentBefore = this.$fs.readJson(packageJsonPath);
2122
let dependenciesBefore = _.keys(jsonContentBefore.dependencies).concat(_.keys(jsonContentBefore.devDependencies));
2223

2324
let flags = this.getFlagsString(config, true);
@@ -47,6 +48,8 @@ export class NodePackageManager implements INodePackageManager {
4748
this.$logger.warn(err.message);
4849
} else {
4950
// All other errors should be handled by the caller code.
51+
// Revert package.json contents to preserve valid state
52+
this.$fs.writeJson(packageJsonPath, jsonContentBefore);
5053
throw err;
5154
}
5255
}

lib/npm-installation-manager.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ import * as semver from "semver";
33
import * as constants from "./constants";
44

55
export class NpmInstallationManager implements INpmInstallationManager {
6-
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
7-
86
constructor(private $npm: INodePackageManager,
97
private $childProcess: IChildProcess,
108
private $logger: ILogger,
11-
private $errors: IErrors,
129
private $options: IOptions,
1310
private $fs: IFileSystem,
1411
private $staticConfig: IStaticConfig) {
@@ -35,7 +32,6 @@ export class NpmInstallationManager implements INpmInstallationManager {
3532
}
3633

3734
public async install(packageName: string, projectDir: string, opts?: INpmInstallOptions): Promise<any> {
38-
3935
try {
4036
let packageToInstall = this.$options.frameworkPath || packageName;
4137
let pathToSave = projectDir;
@@ -45,7 +41,8 @@ export class NpmInstallationManager implements INpmInstallationManager {
4541
return await this.installCore(packageToInstall, pathToSave, version, dependencyType);
4642
} catch (error) {
4743
this.$logger.debug(error);
48-
this.$errors.fail("%s. Error: %s", NpmInstallationManager.NPM_LOAD_FAILED, error);
44+
45+
throw new Error(error);
4946
}
5047
}
5148

lib/services/platform-service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ export class PlatformService implements IPlatformService {
9191
}
9292

9393
let spinner = new clui.Spinner("Installing " + packageToInstall);
94+
let projectDir = this.$projectData.projectDir;
9495
try {
9596
spinner.start();
96-
let downloadedPackagePath = await this.$npmInstallationManager.install(packageToInstall, this.$projectData.projectDir, npmOptions);
97+
let downloadedPackagePath = await this.$npmInstallationManager.install(packageToInstall, projectDir, npmOptions);
9798
let frameworkDir = path.join(downloadedPackagePath, constants.PROJECT_FRAMEWORK_FOLDER_NAME);
9899
frameworkDir = path.resolve(frameworkDir);
99100

0 commit comments

Comments
 (0)