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

feat: make project optional #771

Merged
merged 13 commits into from
Aug 15, 2024
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@oclif/core": "^4",
"@salesforce/core": "^8.0.1",
"@salesforce/kit": "^3.1.6",
"@salesforce/packaging": "^4.1.0",
"@salesforce/packaging": "^4.1.14-dev.1",
"@salesforce/sf-plugins-core": "^11.1.2",
"chalk": "^5.3.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/commands/package/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
import { Messages } from '@salesforce/core/messages';
import { Package, PackageSaveResult } from '@salesforce/packaging';
import { requiredHubFlag } from '../../utils/hubFlag.js';
import { maybeGetProject } from '../../utils/getProject.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_delete');
Expand All @@ -19,7 +20,6 @@ export class PackageDeleteCommand extends SfCommand<PackageSaveResult> {
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:delete'];
public static readonly requiresProject = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

also would have expected this to modify sfdx-project after deleting the pkg

public static readonly flags = {
loglevel,
'target-dev-hub': requiredHubFlag,
Expand Down Expand Up @@ -52,7 +52,7 @@ export class PackageDeleteCommand extends SfCommand<PackageSaveResult> {

const pkg = new Package({
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
project: this.project!,
project: await maybeGetProject(),
packageAliasOrId: flags.package,
});
const result = flags.undelete ? await pkg.undelete() : await pkg.delete();
Expand Down
5 changes: 3 additions & 2 deletions src/commands/package/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
import { Messages } from '@salesforce/core/messages';
import { Package, PackageSaveResult } from '@salesforce/packaging';
import { requiredHubFlag } from '../../utils/hubFlag.js';
import { maybeGetProject } from '../../utils/getProject.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_update');
Expand All @@ -20,7 +21,6 @@ export class PackageUpdateCommand extends SfCommand<PackageSaveResult> {
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:update'];
public static readonly requiresProject = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

same (I'd expect it to update the packageDirectories description/etc with my changes)

Choose a reason for hiding this comment

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

@mshanemc - I tested something similar with package version update.

I updated the --version-name and --version-description. Both were updated in the DevHub, but neither impacted sfdx-project.json

public static readonly flags = {
loglevel,
'target-dev-hub': requiredHubFlag,
Expand Down Expand Up @@ -54,10 +54,11 @@ export class PackageUpdateCommand extends SfCommand<PackageSaveResult> {

public async run(): Promise<PackageSaveResult> {
const { flags } = await this.parse(PackageUpdateCommand);

const pkg = new Package({
packageAliasOrId: flags.package,
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
project: this.project!,
project: await maybeGetProject(),
});

const result = await pkg.update({
Expand Down
4 changes: 2 additions & 2 deletions src/commands/package/version/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
import { Messages } from '@salesforce/core/messages';
import { PackageSaveResult, PackageVersion } from '@salesforce/packaging';
import { requiredHubFlag } from '../../../utils/hubFlag.js';
import { maybeGetProject } from '../../../utils/getProject.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_delete');
Expand All @@ -19,7 +20,6 @@ export class PackageVersionDeleteCommand extends SfCommand<PackageSaveResult> {
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:version:delete'];
public static readonly requiresProject = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm...[unrelated to this PR] I would have expected the delete command to remove the packageVersion from my project.

I guess it never has (and this was that whole "delete doesn't really delete, just deprecates" so maybe that's correct?)

Copy link
Contributor

Choose a reason for hiding this comment

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

so maybe that should happen if there's a project, but it's not required?

public static readonly flags = {
loglevel,
'target-dev-hub': requiredHubFlag,
Expand All @@ -45,7 +45,7 @@ export class PackageVersionDeleteCommand extends SfCommand<PackageSaveResult> {
const { flags } = await this.parse(PackageVersionDeleteCommand);
const packageVersion = new PackageVersion({
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
project: this.project!,
project: await maybeGetProject(),
idOrAlias: flags.package,
});
await this.confirmDelete(flags['no-prompt'], flags.undelete);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/package/version/displayancestry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
import { Messages } from '@salesforce/core/messages';
import { Package, PackageAncestryNodeData } from '@salesforce/packaging';
import { requiredHubFlag } from '../../../utils/hubFlag.js';
import { maybeGetProject } from '../../../utils/getProject.js';

// Import i18n messages
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
Expand All @@ -21,7 +22,6 @@ export class PackageVersionDisplayAncestryCommand extends SfCommand<DisplayAnces
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:version:displayancestry'];
public static readonly requiresProject = true;

public static readonly flags = {
loglevel,
Expand All @@ -48,7 +48,7 @@ export class PackageVersionDisplayAncestryCommand extends SfCommand<DisplayAnces
const { flags } = await this.parse(PackageVersionDisplayAncestryCommand);
const packageAncestry = await Package.getAncestry(
flags.package,
this.project!,
await maybeGetProject(),
flags['target-dev-hub'].getConnection(flags['api-version'])
);
const jsonProducer = packageAncestry.getJsonProducer();
Expand Down
7 changes: 4 additions & 3 deletions src/commands/package/version/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand, Ux } from '@salesforce/sf-plugins-core';
import { Messages, SfProject } from '@salesforce/core';
import { Messages } from '@salesforce/core';
import {
getContainerOptions,
getPackageVersionStrings,
Expand All @@ -15,6 +15,7 @@ import {
PackageVersionListResult,
} from '@salesforce/packaging';
import { requiredHubFlag } from '../../../utils/hubFlag.js';
import { maybeGetProject } from '../../../utils/getProject.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_list');
Expand Down Expand Up @@ -105,7 +106,7 @@ export class PackageVersionListCommand extends SfCommand<PackageVersionListComma
public async run(): Promise<PackageVersionListCommandResult> {
const { flags } = await this.parse(PackageVersionListCommand);
const connection = flags['target-dev-hub'].getConnection(flags['api-version']);
const project = SfProject.getInstance();
Copy link
Member Author

Choose a reason for hiding this comment

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

this command didn't have requiresProject = true but this line was enforcing it anyway.

const project = await maybeGetProject();

const records = await Package.listVersions(connection, project, {
createdLastDays: flags['created-last-days'],
Expand Down Expand Up @@ -135,7 +136,7 @@ export class PackageVersionListCommand extends SfCommand<PackageVersionListComma

records.forEach((record) => {
const ids = [record.Id, record.SubscriberPackageVersionId];
const aliases = ids.map((id) => project.getAliasesFromPackageId(id)).flat();
const aliases = ids.map((id) => (project ? project.getAliasesFromPackageId(id) : id)).flat();
cristiand391 marked this conversation as resolved.
Show resolved Hide resolved
const AliasStr = aliases.length > 0 ? aliases.join() : '';

// set Ancestor display values
Expand Down
4 changes: 2 additions & 2 deletions src/commands/package/version/promote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
import { Messages, SfError } from '@salesforce/core';
import { PackageSaveResult, PackageVersion } from '@salesforce/packaging';
import { requiredHubFlag } from '../../../utils/hubFlag.js';
import { maybeGetProject } from '../../../utils/getProject.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_promote');
Expand All @@ -19,7 +20,6 @@ export class PackageVersionPromoteCommand extends SfCommand<PackageSaveResult> {
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:version:promote'];
public static readonly examples = messages.getMessages('examples');
public static readonly requiresProject = true;
public static readonly flags = {
loglevel,
'target-dev-hub': requiredHubFlag,
Expand All @@ -41,7 +41,7 @@ export class PackageVersionPromoteCommand extends SfCommand<PackageSaveResult> {
const { flags } = await this.parse(PackageVersionPromoteCommand);
const packageVersion = new PackageVersion({
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
project: this.project!,
project: await maybeGetProject(),
idOrAlias: flags.package,
});
const packageVersionData = await packageVersion.getData();
Expand Down
4 changes: 2 additions & 2 deletions src/commands/package/version/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '@salesforce/packaging';
import chalk from 'chalk';
import { requiredHubFlag } from '../../../utils/hubFlag.js';
import { maybeGetProject } from '../../../utils/getProject.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_report');
Expand All @@ -39,7 +40,6 @@ export class PackageVersionReportCommand extends SfCommand<PackageVersionReportR
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:package:version:report'];
public static readonly requiresProject = true;
public static readonly flags = {
loglevel,
'target-dev-hub': requiredHubFlag,
Expand All @@ -59,7 +59,7 @@ export class PackageVersionReportCommand extends SfCommand<PackageVersionReportR
const { flags } = await this.parse(PackageVersionReportCommand);
const packageVersion = new PackageVersion({
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
project: this.project!,
project: await maybeGetProject(),
idOrAlias: flags.package,
});
const results = await packageVersion.report(flags.verbose);
Expand Down
22 changes: 22 additions & 0 deletions src/utils/getProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { SfProject } from '@salesforce/core';

/*
* Get the sfdx project from the current dir.
* It will return `undefined` if there's no project.
* */
export async function maybeGetProject(): Promise<SfProject | undefined> {
try {
return await SfProject.resolve();
} catch (err) {
if (err instanceof Error && err.name === 'InvalidProjectWorkspaceError') {
return undefined;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

any swallow all other errors?
What's the logic of checking the error name then?

Copy link
Member Author

Choose a reason for hiding this comment

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

What's the logic of checking the error name then?

none, I think I was just following the error msg from SfProject.resolve's jsdoc.
Changed to make it swallow all errors that could come from trying to resolve a project.

}
}
46 changes: 33 additions & 13 deletions test/commands/package/packageVersionCreate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,46 @@ import { env } from '@salesforce/kit';
import { PackageVersionCreateCommand } from '../../../src/commands/package/version/create.js';
import Package2VersionStatus = PackagingSObjects.Package2VersionStatus;

const pkgVersionCreateErrorResult: Partial<PackageVersionCreateRequestResult> = {
const pkgVersionCreateErrorResult: PackageVersionCreateRequestResult = {
Copy link
Member Author

Choose a reason for hiding this comment

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

this type changed here:
forcedotcom/packaging#627

Id: '08c3i000000fylXXXX',
Status: Package2VersionStatus.error,
Package2Id: '0Ho3i000000TNHXXXX',
Package2VersionId: undefined,
SubscriberPackageVersionId: undefined,
Tag: undefined,
Branch: undefined,
Package2VersionId: '',
SubscriberPackageVersionId: '',
Tag: '',
Branch: '',
Error: [
'PropertyController: Invalid type: Schema.Property__c',
'SampleDataController: Invalid type: Schema.Property__c',
'SampleDataController: Invalid type: Schema.Broker__c',
],
CreatedDate: '2022-11-03 09:21',
HasMetadataRemoved: undefined,
HasMetadataRemoved: null,
CreatedBy: '0053i000001ZIyXXXX',
Package2Name: null,
HasPassedCodeCoverageCheck: null,
CodeCoverage: null,
VersionNumber: null,
ConvertedFromVersionId: null,
};

const pkgVersionCreateSuccessResult: Partial<PackageVersionCreateRequestResult> = {
const pkgVersionCreateSuccessResult: PackageVersionCreateRequestResult = {
Id: '08c3i000000fylgAAA',
Status: Package2VersionStatus.success,
Package2Id: '0Ho3i000000TNHYCA4',
Package2VersionId: '05i3i000000fxw1AAA',
SubscriberPackageVersionId: '04t3i000002eya2AAA',
Tag: undefined,
Branch: undefined,
Tag: '',
Branch: '',
Error: [],
CreatedDate: '2022-11-03 09:46',
HasMetadataRemoved: false,
CreatedBy: '0053i000001ZIyGAAW',
Package2Name: null,
HasPassedCodeCoverageCheck: null,
CodeCoverage: null,
VersionNumber: null,
ConvertedFromVersionId: null,
};

describe('package:version:create - tests', () => {
Expand Down Expand Up @@ -84,7 +94,7 @@ describe('package:version:create - tests', () => {
const res = await cmd.run();
expect(envSpy.calledOnce).to.equal(true);
expect(res).to.deep.equal({
Branch: undefined,
Branch: '',
CreatedBy: '0053i000001ZIyGAAW',
CreatedDate: '2022-11-03 09:46',
Error: [],
Expand All @@ -94,7 +104,12 @@ describe('package:version:create - tests', () => {
Package2VersionId: '05i3i000000fxw1AAA',
Status: 'Success',
SubscriberPackageVersionId: '04t3i000002eya2AAA',
Tag: undefined,
Tag: '',
Package2Name: null,
HasPassedCodeCoverageCheck: null,
CodeCoverage: null,
VersionNumber: null,
ConvertedFromVersionId: null,
});
expect(logStub.callCount).to.equal(1);
expect(logStub.args[0]).to.deep.equal([
Expand All @@ -115,7 +130,7 @@ describe('package:version:create - tests', () => {
const res = await cmd.run();
expect(envSpy.calledOnce).to.equal(true);
expect(res).to.deep.equal({
Branch: undefined,
Branch: '',
CreatedBy: '0053i000001ZIyGAAW',
CreatedDate: '2022-11-03 09:46',
Error: [],
Expand All @@ -125,7 +140,12 @@ describe('package:version:create - tests', () => {
Package2VersionId: '05i3i000000fxw1AAA',
Status: 'Success',
SubscriberPackageVersionId: '04t3i000002eya2AAA',
Tag: undefined,
Tag: '',
Package2Name: null,
HasPassedCodeCoverageCheck: null,
CodeCoverage: null,
VersionNumber: null,
ConvertedFromVersionId: null,
});
expect(logStub.callCount).to.equal(1);
expect(logStub.args[0]).to.deep.equal([
Expand Down
Loading
Loading