Skip to content

Commit

Permalink
fix: several fixes for pkg commands
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Aug 22, 2022
1 parent ea53915 commit bc1614d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
4 changes: 0 additions & 4 deletions messages/package_uninstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ The ID (starts with 04t) or alias of the package version to uninstall.

Include either a %s value or a %s value.

# invalidIdOrPackage

Invalid alias or ID: %s. Either your alias is invalid or undefined, or the ID provided is invalid (must start with 04t).

# InProgress

PackageUninstallRequest is currently InProgress.
Expand Down
1 change: 1 addition & 0 deletions src/commands/force/package/beta/installed/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class PackageInstalledListCommand extends SfdxCommand {
public static readonly longDescription = messages.getMessage('cliLongDescription');
public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static readonly requiresUsername = true;
public static readonly requiresProject = true;

public async run(): Promise<PackageInstalledListResult[]> {
const result = await packageInstalledList(this.org.getConnection());
Expand Down
36 changes: 30 additions & 6 deletions src/commands/force/package/beta/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

import * as os from 'os';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Lifecycle, Messages } from '@salesforce/core';
import { getPackageIdFromAlias, PackagingSObjects, uninstallPackage } from '@salesforce/packaging';
import { Lifecycle, Messages, SfProject } from '@salesforce/core';
import { Package, PackagingSObjects, uninstallPackage } from '@salesforce/packaging';
import { Duration } from '@salesforce/kit';

type UninstallResult = PackagingSObjects.SubscriberPackageVersionUninstallRequest;

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_uninstall');
const installMsgs = Messages.loadMessages('@salesforce/plugin-packaging', 'package_install');

export class PackageUninstallCommand extends SfdxCommand {
public static readonly description = messages.getMessage('cliDescription');
Expand Down Expand Up @@ -44,15 +45,38 @@ export class PackageUninstallCommand extends SfdxCommand {
this.ux.log(`Waiting for the package uninstall request to get processed. Status = ${data.Status}`);
});

const packageId = getPackageIdFromAlias(this.flags.package, this.project);
if (!packageId.startsWith('04t')) {
throw messages.createError('invalidIdOrPackage', [packageId]);
}
const packageId = this.resolveSubscriberPackageVersionKey(this.flags.package);

const result: UninstallResult = await uninstallPackage(packageId, this.org.getConnection(), this.flags.wait);
const arg = result.Status === 'Success' ? [result.SubscriberPackageVersionId] : [result.Id, this.org.getUsername()];
this.ux.log(messages.getMessage(result.Status, arg));

return result;
}

// Given a package version ID (04t) or an alias for the package, validate and
// return the package version ID (aka SubscriberPackageVersionKey).
private resolveSubscriberPackageVersionKey(idOrAlias: string): string {
let resolvedId: string;

if (idOrAlias.startsWith('04t')) {
Package.validateId(idOrAlias, 'SubscriberPackageVersionId');
resolvedId = idOrAlias;
} else {
let packageAliases: { [k: string]: string };
try {
const projectJson = SfProject.getInstance().getSfProjectJson();
packageAliases = projectJson.getContents().packageAliases ?? {};
} catch (e) {
throw installMsgs.createError('projectNotFound', [idOrAlias]);
}
resolvedId = packageAliases[idOrAlias];
if (!resolvedId) {
throw installMsgs.createError('packageAliasNotFound', [idOrAlias]);
}
Package.validateId(resolvedId, 'SubscriberPackageVersionId');
}

return resolvedId;
}
}

0 comments on commit bc1614d

Please sign in to comment.