Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow some cmds to run outside a project #733

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions src/commands/package/installed/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import {
requiredOrgFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
import { SubscriberPackageVersion } from '@salesforce/packaging';
import { InstalledPackages, SubscriberPackageVersion } from '@salesforce/packaging';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_installed_list');

export type PackageInstalledListResult = {
Id: string;
SubscriberPackageId: string | undefined;
SubscriberPackageName: string | undefined;
SubscriberPackageNamespace: string | undefined;
SubscriberPackageVersionId: string | undefined;
SubscriberPackageVersionName: string | undefined;
SubscriberPackageVersionNumber: string | undefined;
SubscriberPackageId?: string;
SubscriberPackageName?: string;
SubscriberPackageNamespace?: string;
SubscriberPackageVersionId?: string;
SubscriberPackageVersionName?: string;
SubscriberPackageVersionNumber?: string;
};

export type PackageInstalledCommandResult = PackageInstalledListResult[];
Expand All @@ -34,7 +34,6 @@ export class PackageInstalledListCommand extends SfCommand<PackageInstalledComma
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:installed:list'];
public static readonly requiresProject = true;

public static readonly flags = {
loglevel,
Expand All @@ -44,34 +43,37 @@ export class PackageInstalledListCommand extends SfCommand<PackageInstalledComma

public async run(): Promise<PackageInstalledCommandResult> {
const { flags } = await this.parse(PackageInstalledListCommand);
const result = await SubscriberPackageVersion.installedList(
flags['target-org'].getConnection(flags['api-version'])
);

const records = result.map((record) => ({
Id: record.Id,
SubscriberPackageId: record.SubscriberPackageId,
SubscriberPackageName: record.SubscriberPackage?.Name,
SubscriberPackageNamespace: record.SubscriberPackage?.NamespacePrefix,
SubscriberPackageVersionId: record.SubscriberPackageVersion?.Id,
SubscriberPackageVersionName: record.SubscriberPackageVersion?.Name,
SubscriberPackageVersionNumber: `${record.SubscriberPackageVersion?.MajorVersion}.${record.SubscriberPackageVersion?.MinorVersion}.${record.SubscriberPackageVersion?.PatchVersion}.${record.SubscriberPackageVersion?.BuildNumber}`,
}));
const records = (
await SubscriberPackageVersion.installedList(flags['target-org'].getConnection(flags['api-version']))
).map(transformRow);

this.table(
records,
{
Id: { header: 'ID' },
SubscriberPackageId: { header: 'Package ID' },
SubscriberPackageName: { header: 'Package Name' },
SubscriberPackageNamespace: { header: 'Namespace' },
SubscriberPackageVersionId: { header: 'Package Version ID' },
SubscriberPackageVersionName: { header: 'Version Name' },
SubscriberPackageVersionNumber: { header: 'Version' },
},
columns,

{ 'no-truncate': true }
);

return records;
}
}

const transformRow = (r: InstalledPackages): PackageInstalledListResult => ({
Id: r.Id,
SubscriberPackageId: r.SubscriberPackageId,
SubscriberPackageName: r.SubscriberPackage?.Name,
SubscriberPackageNamespace: r.SubscriberPackage?.NamespacePrefix,
SubscriberPackageVersionId: r.SubscriberPackageVersion?.Id,
SubscriberPackageVersionName: r.SubscriberPackageVersion?.Name,
SubscriberPackageVersionNumber: `${r.SubscriberPackageVersion?.MajorVersion}.${r.SubscriberPackageVersion?.MinorVersion}.${r.SubscriberPackageVersion?.PatchVersion}.${r.SubscriberPackageVersion?.BuildNumber}`,
});

const columns = {
Id: { header: 'ID' },
SubscriberPackageId: { header: 'Package ID' },
SubscriberPackageName: { header: 'Package Name' },
SubscriberPackageNamespace: { header: 'Namespace' },
SubscriberPackageVersionId: { header: 'Package Version ID' },
SubscriberPackageVersionName: { header: 'Version Name' },
SubscriberPackageVersionNumber: { header: 'Version' },
};
149 changes: 68 additions & 81 deletions src/commands/package/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,36 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
import { Messages } from '@salesforce/core/messages';
import { Package, PackagingSObjects } from '@salesforce/packaging';
import chalk from 'chalk';
import { SfProject } from '@salesforce/core';
import { requiredHubFlag } from '../../utils/hubFlag.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_list');

export type Package2Result = Partial<
Pick<
PackagingSObjects.Package2,
| 'Id'
| 'SubscriberPackageId'
| 'Name'
| 'Description'
| 'NamespacePrefix'
| 'ContainerOptions'
| 'ConvertedFromPackageId'
| 'PackageErrorUsername'
| 'AppAnalyticsEnabled'
> & {
Alias: string;
CreatedBy: string;
IsOrgDependent: string;
}
>;
export type Package2Result = Required<Pick<PackagingSObjects.Package2, 'Id' | 'Name'>> &
Partial<
Pick<
PackagingSObjects.Package2,
| 'SubscriberPackageId'
| 'Description'
| 'NamespacePrefix'
| 'ContainerOptions'
| 'ConvertedFromPackageId'
| 'PackageErrorUsername'
| 'AppAnalyticsEnabled'
> & {
Alias: string;
CreatedBy: string;
IsOrgDependent: string;
}
>;

export type PackageListCommandResult = Package2Result[];

export class PackageListCommand extends SfCommand<PackageListCommandResult> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly requiresProject = true;
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:list'];
public static readonly flags = {
Expand All @@ -51,55 +50,17 @@ export class PackageListCommand extends SfCommand<PackageListCommandResult> {
}),
};

private results: Package2Result[] = [];

public async run(): Promise<PackageListCommandResult> {
const { flags } = await this.parse(PackageListCommand);
const connection = flags['target-dev-hub'].getConnection(flags['api-version']);
const queryResult = await Package.list(connection);
this.mapRecordsToResults(queryResult);
this.displayResults(flags.verbose, connection.getApiVersion());
return this.results;
}

private mapRecordsToResults(records: PackagingSObjects.Package2[]): void {
if (records && records.length > 0) {
this.results = records
.filter((record) => record.IsDeprecated === false)
.map(
({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
IsOrgDependent,
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedById,
}) =>
({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
Alias: this.project!.getAliasesFromPackageId(Id).join(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this alias info could be different depending on wether this runs inside an sfdx project or not.

IsOrgDependent: ContainerOptions === 'Managed' ? 'N/A' : IsOrgDependent ? 'Yes' : 'No',
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedBy: CreatedById,
} as Package2Result)
);
}
const results = mapRecordsToResults(queryResult);
this.displayResults(results, flags.verbose, connection.getApiVersion());
return results;
}

private displayResults(verbose = false, apiVersion: string): void {
this.styledHeader(chalk.blue(`Packages [${this.results.length}]`));
private displayResults(results: Package2Result[], verbose = false, apiVersion: string): void {
this.styledHeader(chalk.blue(`Packages [${results.length}]`));
const columns = {
NamespacePrefix: { header: messages.getMessage('namespace') },
Name: { header: messages.getMessage('name') },
Expand All @@ -109,26 +70,52 @@ export class PackageListCommand extends SfCommand<PackageListCommandResult> {
ContainerOptions: {
header: messages.getMessage('package-type'),
},
...(verbose
? {
SubscriberPackageId: { header: messages.getMessage('package-id') },
ConvertedFromPackageId: { header: messages.getMessage('convertedFromPackageId') },
IsOrgDependent: { header: messages.getMessage('isOrgDependent') },
PackageErrorUsername: { header: messages.getMessage('error-notification-username') },
CreatedBy: { header: messages.getMessage('createdBy') },
}
: {}),
...(verbose && parseInt(apiVersion, 10) >= 59
? { AppAnalyticsEnabled: { header: messages.getMessage('app-analytics-enabled') } }
: {}),
};

if (verbose) {
Object.assign(columns, {
SubscriberPackageId: { header: messages.getMessage('package-id') },
ConvertedFromPackageId: { header: messages.getMessage('convertedFromPackageId') },
IsOrgDependent: { header: messages.getMessage('isOrgDependent') },
PackageErrorUsername: { header: messages.getMessage('error-notification-username') },
});
if (apiVersion >= '59.0') {
Object.assign(columns, {
AppAnalyticsEnabled: { header: messages.getMessage('app-analytics-enabled') },
});
}
Object.assign(columns, {
CreatedBy: {
header: messages.getMessage('createdBy'),
},
});
}
this.table(this.results, columns);
this.table(results, columns);
}
}

const mapRecordsToResults = (records: PackagingSObjects.Package2[], project?: SfProject): Package2Result[] =>
records
.filter((record) => record.IsDeprecated === false)
.map(
({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
IsOrgDependent,
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedById,
}) => ({
Id,
SubscriberPackageId,
Name,
Description,
NamespacePrefix,
ContainerOptions,
ConvertedFromPackageId,
Alias: project ? project.getAliasesFromPackageId(Id).join() : '',
IsOrgDependent: ContainerOptions === 'Managed' ? 'N/A' : IsOrgDependent ? 'Yes' : 'No',
PackageErrorUsername,
AppAnalyticsEnabled,
CreatedBy: CreatedById,
})
);
1 change: 0 additions & 1 deletion src/commands/package1/version/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export class Package1VersionCreateCommand extends SfCommand<PackageUploadRequest
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly requiresProject = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't see any usage of this.project in the command, probably a leftover from toolbelt migration

public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package1:version:create'];

Expand Down
1 change: 0 additions & 1 deletion test/commands/package/packageInstalledList.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('package:installed:list', () => {
before(async () => {
session = await TestSession.create({
devhubAuthStrategy: 'AUTO',
project: { name: 'packageInstalledList' },
});
});

Expand Down
15 changes: 7 additions & 8 deletions test/commands/package/packageList.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
*/

import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import chai from 'chai';
import { expect, config } from 'chai';
import { Org } from '@salesforce/core';
import { Package } from '@salesforce/packaging';

const { expect } = chai;
config.truncateThreshold = 50_000;

describe('package list', () => {
let session: TestSession;
let hubOrg: Org;
let apiVersion: string;
let apiVersion: number;
before(async () => {
session = await TestSession.create({
devhubAuthStrategy: 'AUTO',
project: { name: 'packageList' },
});
hubOrg = await Org.create({ aliasOrUsername: session.hubOrg.username });
apiVersion = hubOrg.getConnection().getApiVersion();
apiVersion = parseInt(hubOrg.getConnection().getApiVersion(), 10);
});

after(async () => {
Expand All @@ -46,7 +45,7 @@ describe('package list', () => {
'CreatedBy',
'IsOrgDependent',
];
if (apiVersion < '59.0') {
if (apiVersion < 59) {
keys.splice(keys.indexOf('AppAnalyticsEnabled'), 1);
}
const deprecatedPackages = packages.filter((pkg) => pkg.IsDeprecated);
Expand All @@ -67,8 +66,8 @@ describe('package list', () => {
const output = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout;
expect(output).to.contain('=== Packages');
let headerExpression =
'Namespace Prefix\\s+?Name\\s+?Id\\s+?Alias\\s+?Description\\s+?Type\\s+?Subscriber Package Id\\s+?Converted From Package Id\\s+?Org-Dependent Unlocked Package\\s+?Error Notification Username\\s+?App Analytics Enabled\\s+?Created By';
if (apiVersion < '59.0') {
'Namespace Prefix\\s+?Name\\s+?Id\\s+?Alias\\s+?Description\\s+?Type\\s+?Subscriber Package Id\\s+?Converted From Package Id\\s+?Org-Dependent Unlocked Package\\s+?Error Notification Username\\s+?Created By\\s+?App Analytics Enabled';
if (apiVersion < 59.0) {
headerExpression = headerExpression.replace('App Analytics Enabled\\s+?', '');
}
expect(output).to.match(new RegExp(headerExpression));
Expand Down
1 change: 0 additions & 1 deletion test/commands/package1/versionCreate.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('package1:version:create', () => {
throw new Error('"ONEGP_TESTKIT_AUTH_URL" env var required for 1gp NUTs');
}
session = await TestSession.create({
project: { name: 'package1VersionCreate' },
devhubAuthStrategy: 'AUTO',
});

Expand Down
Loading