Skip to content

Commit 5f674ee

Browse files
feat: make project optional (#771)
* feat: no project required * test: fix stubs * fix: manually resolve project * fix: package version list too * chore: pr review * ci: disable package1 nuts * fix: empty alias when if no project * test: update for error change from lib --------- Co-authored-by: mshanemc <shane.mclaughlin@salesforce.com>
1 parent ec7758b commit 5f674ee

File tree

13 files changed

+446
-403
lines changed

13 files changed

+446
-403
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
os: [ubuntu-latest, windows-latest]
2727
command:
2828
- 'yarn test:nuts:package'
29-
- 'yarn test:nuts:package1'
29+
# - 'yarn test:nuts:package1' disable because of the long-standing ORA bug
3030
fail-fast: false
3131
with:
3232
os: ${{ matrix.os }}

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
"bugs": "https://github.com/forcedotcom/cli/issues",
77
"dependencies": {
88
"@oclif/core": "^4",
9-
"@salesforce/core": "^8.0.1",
9+
"@salesforce/core": "^8.4.0",
1010
"@salesforce/kit": "^3.1.6",
11-
"@salesforce/packaging": "^4.1.0",
12-
"@salesforce/sf-plugins-core": "^11.1.2",
11+
"@salesforce/packaging": "^4.2.0",
12+
"@salesforce/sf-plugins-core": "^11.3.2",
1313
"chalk": "^5.3.0"
1414
},
1515
"devDependencies": {
1616
"@oclif/plugin-command-snapshot": "^5.2.3",
17-
"@salesforce/cli-plugins-testkit": "^5.3.15",
18-
"@salesforce/dev-scripts": "^10.2.7",
17+
"@salesforce/cli-plugins-testkit": "^5.3.25",
18+
"@salesforce/dev-scripts": "^10.2.9",
1919
"@salesforce/plugin-command-reference": "^3.1.5",
20-
"eslint-plugin-sf-plugin": "^1.18.8",
20+
"eslint-plugin-sf-plugin": "^1.20.4",
2121
"oclif": "^4.10.11",
2222
"ts-node": "^10.9.2",
2323
"typescript": "^5.5.4"

src/commands/package/delete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
99
import { Messages } from '@salesforce/core/messages';
1010
import { Package, PackageSaveResult } from '@salesforce/packaging';
1111
import { requiredHubFlag } from '../../utils/hubFlag.js';
12+
import { maybeGetProject } from '../../utils/getProject.js';
1213

1314
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1415
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_delete');
@@ -19,7 +20,6 @@ export class PackageDeleteCommand extends SfCommand<PackageSaveResult> {
1920
public static readonly examples = messages.getMessages('examples');
2021
public static readonly deprecateAliases = true;
2122
public static readonly aliases = ['force:package:delete'];
22-
public static readonly requiresProject = true;
2323
public static readonly flags = {
2424
loglevel,
2525
'target-dev-hub': requiredHubFlag,
@@ -52,7 +52,7 @@ export class PackageDeleteCommand extends SfCommand<PackageSaveResult> {
5252

5353
const pkg = new Package({
5454
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
55-
project: this.project!,
55+
project: await maybeGetProject(),
5656
packageAliasOrId: flags.package,
5757
});
5858
const result = flags.undelete ? await pkg.undelete() : await pkg.delete();

src/commands/package/update.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
99
import { Messages } from '@salesforce/core/messages';
1010
import { Package, PackageSaveResult } from '@salesforce/packaging';
1111
import { requiredHubFlag } from '../../utils/hubFlag.js';
12+
import { maybeGetProject } from '../../utils/getProject.js';
1213

1314
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1415
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_update');
@@ -20,7 +21,6 @@ export class PackageUpdateCommand extends SfCommand<PackageSaveResult> {
2021
public static readonly examples = messages.getMessages('examples');
2122
public static readonly deprecateAliases = true;
2223
public static readonly aliases = ['force:package:update'];
23-
public static readonly requiresProject = true;
2424
public static readonly flags = {
2525
loglevel,
2626
'target-dev-hub': requiredHubFlag,
@@ -54,10 +54,11 @@ export class PackageUpdateCommand extends SfCommand<PackageSaveResult> {
5454

5555
public async run(): Promise<PackageSaveResult> {
5656
const { flags } = await this.parse(PackageUpdateCommand);
57+
5758
const pkg = new Package({
5859
packageAliasOrId: flags.package,
5960
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
60-
project: this.project!,
61+
project: await maybeGetProject(),
6162
});
6263

6364
const result = await pkg.update({

src/commands/package/version/delete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
99
import { Messages } from '@salesforce/core/messages';
1010
import { PackageSaveResult, PackageVersion } from '@salesforce/packaging';
1111
import { requiredHubFlag } from '../../../utils/hubFlag.js';
12+
import { maybeGetProject } from '../../../utils/getProject.js';
1213

1314
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1415
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_delete');
@@ -19,7 +20,6 @@ export class PackageVersionDeleteCommand extends SfCommand<PackageSaveResult> {
1920
public static readonly examples = messages.getMessages('examples');
2021
public static readonly deprecateAliases = true;
2122
public static readonly aliases = ['force:package:version:delete'];
22-
public static readonly requiresProject = true;
2323
public static readonly flags = {
2424
loglevel,
2525
'target-dev-hub': requiredHubFlag,
@@ -45,7 +45,7 @@ export class PackageVersionDeleteCommand extends SfCommand<PackageSaveResult> {
4545
const { flags } = await this.parse(PackageVersionDeleteCommand);
4646
const packageVersion = new PackageVersion({
4747
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
48-
project: this.project!,
48+
project: await maybeGetProject(),
4949
idOrAlias: flags.package,
5050
});
5151
await this.confirmDelete(flags['no-prompt'], flags.undelete);

src/commands/package/version/displayancestry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
99
import { Messages } from '@salesforce/core/messages';
1010
import { Package, PackageAncestryNodeData } from '@salesforce/packaging';
1111
import { requiredHubFlag } from '../../../utils/hubFlag.js';
12+
import { maybeGetProject } from '../../../utils/getProject.js';
1213

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

2626
public static readonly flags = {
2727
loglevel,
@@ -48,7 +48,7 @@ export class PackageVersionDisplayAncestryCommand extends SfCommand<DisplayAnces
4848
const { flags } = await this.parse(PackageVersionDisplayAncestryCommand);
4949
const packageAncestry = await Package.getAncestry(
5050
flags.package,
51-
this.project!,
51+
await maybeGetProject(),
5252
flags['target-dev-hub'].getConnection(flags['api-version'])
5353
);
5454
const jsonProducer = packageAncestry.getJsonProducer();

src/commands/package/version/list.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand, Ux } from '@salesforce/sf-plugins-core';
9-
import { Messages, SfProject } from '@salesforce/core';
9+
import { Messages } from '@salesforce/core';
1010
import {
1111
getContainerOptions,
1212
getPackageVersionStrings,
@@ -15,6 +15,7 @@ import {
1515
PackageVersionListResult,
1616
} from '@salesforce/packaging';
1717
import { requiredHubFlag } from '../../../utils/hubFlag.js';
18+
import { maybeGetProject } from '../../../utils/getProject.js';
1819

1920
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
2021
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_list');
@@ -105,7 +106,7 @@ export class PackageVersionListCommand extends SfCommand<PackageVersionListComma
105106
public async run(): Promise<PackageVersionListCommandResult> {
106107
const { flags } = await this.parse(PackageVersionListCommand);
107108
const connection = flags['target-dev-hub'].getConnection(flags['api-version']);
108-
const project = SfProject.getInstance();
109+
const project = await maybeGetProject();
109110

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

136137
records.forEach((record) => {
137138
const ids = [record.Id, record.SubscriberPackageVersionId];
138-
const aliases = ids.map((id) => project.getAliasesFromPackageId(id)).flat();
139-
const AliasStr = aliases.length > 0 ? aliases.join() : '';
139+
const aliases = ids.map((id) => (project ? project.getAliasesFromPackageId(id) : id)).flat();
140+
const AliasStr = project ? (aliases.length > 0 ? aliases.join() : '') : '';
140141

141142
// set Ancestor display values
142143
let ancestorVersion: string | undefined;

src/commands/package/version/promote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand } from '@
99
import { Messages, SfError } from '@salesforce/core';
1010
import { PackageSaveResult, PackageVersion } from '@salesforce/packaging';
1111
import { requiredHubFlag } from '../../../utils/hubFlag.js';
12+
import { maybeGetProject } from '../../../utils/getProject.js';
1213

1314
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1415
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_promote');
@@ -19,7 +20,6 @@ export class PackageVersionPromoteCommand extends SfCommand<PackageSaveResult> {
1920
public static readonly deprecateAliases = true;
2021
public static readonly aliases = ['force:package:version:promote'];
2122
public static readonly examples = messages.getMessages('examples');
22-
public static readonly requiresProject = true;
2323
public static readonly flags = {
2424
loglevel,
2525
'target-dev-hub': requiredHubFlag,
@@ -41,7 +41,7 @@ export class PackageVersionPromoteCommand extends SfCommand<PackageSaveResult> {
4141
const { flags } = await this.parse(PackageVersionPromoteCommand);
4242
const packageVersion = new PackageVersion({
4343
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
44-
project: this.project!,
44+
project: await maybeGetProject(),
4545
idOrAlias: flags.package,
4646
});
4747
const packageVersionData = await packageVersion.getData();

src/commands/package/version/report.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from '@salesforce/packaging';
1717
import chalk from 'chalk';
1818
import { requiredHubFlag } from '../../../utils/hubFlag.js';
19+
import { maybeGetProject } from '../../../utils/getProject.js';
1920

2021
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
2122
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package_version_report');
@@ -39,7 +40,6 @@ export class PackageVersionReportCommand extends SfCommand<PackageVersionReportR
3940
public static readonly examples = messages.getMessages('examples');
4041
public static readonly deprecateAliases = true;
4142
public static readonly aliases = ['force:package:version:report'];
42-
public static readonly requiresProject = true;
4343
public static readonly flags = {
4444
loglevel,
4545
'target-dev-hub': requiredHubFlag,
@@ -59,7 +59,7 @@ export class PackageVersionReportCommand extends SfCommand<PackageVersionReportR
5959
const { flags } = await this.parse(PackageVersionReportCommand);
6060
const packageVersion = new PackageVersion({
6161
connection: flags['target-dev-hub'].getConnection(flags['api-version']),
62-
project: this.project!,
62+
project: await maybeGetProject(),
6363
idOrAlias: flags.package,
6464
});
6565
const results = await packageVersion.report(flags.verbose);

src/utils/getProject.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2022, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
import { SfProject } from '@salesforce/core';
9+
10+
/*
11+
* Get the sfdx project from the current dir.
12+
* It will return `undefined` if there's no project.
13+
* */
14+
export async function maybeGetProject(): Promise<SfProject | undefined> {
15+
try {
16+
return await SfProject.resolve();
17+
} catch {
18+
return undefined;
19+
}
20+
}

0 commit comments

Comments
 (0)