{"size": 1023, "ext": "js", "max_stars_repo_path": "packages/amplify-registry-sdk/src/installers/actions/amplify-cli-plugin/index.js", "max_stars_repo_name": "sdaclin/amplify-tooling", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/amplify-registry-sdk/src/installers/actions/amplify-cli-plugin/index.js", "max_issues_repo_name": "sdaclin/amplify-tooling", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/amplify-registry-sdk/src/installers/actions/amplify-cli-plugin/index.js", "max_forks_repo_name": "sdaclin/amplify-tooling", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 40.92, "max_line_length": 90, "alphanum_fraction": 0.7321603128}
import { addPackageToConfig, npmInstall, removePackageFromConfig } from '../../common';
import { loadConfig } from '@axway/amplify-cli-utils';
import { removeSync } from 'fs-extra';

export async function post({ pkgInfo, location }) {
	const cfg = loadConfig();
	const previousActivePackage = cfg.get(`extensions.${pkgInfo.name}`, undefined);
	try {
		await npmInstall({ directory: location });
		await addPackageToConfig(pkgInfo.name, location);
	} catch (err) {
		// If we error during the above steps:
		// 	- If the location to set is the currently active version remove it
		//	- Else if there was a version set, make sure it is set as the active
		// Then rethrow the error to allow the command implementation to handle it appropriately
		if (previousActivePackage === location) {
			await removePackageFromConfig(pkgInfo.name);
		} else if (previousActivePackage && previousActivePackage !== location) {
			await addPackageToConfig(pkgInfo.name, previousActivePackage);
		}
		removeSync(location);
		throw err;
	}
}
