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 SfProject optional #638

Merged
merged 11 commits into from
Aug 15, 2024
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
externalProjectGitUrl: 'https://github.com/salesforcecli/plugin-packaging'
command: 'yarn test:nuts:package'
os: ${{ matrix.os }}
preSwapCommands: 'yarn upgrade @salesforce/core; npx yarn-deduplicate; yarn install'
preSwapCommands: 'yarn upgrade @salesforce/core; yarn upgrade @jsforce/jsforce-node@latest; npx yarn-deduplicate; yarn install'
preExternalBuildCommands: 'npm why @salesforce/core --json'
useCache: false
secrets: inherit
2,395 changes: 725 additions & 1,670 deletions CHANGELOG.md

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion messages/package_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ The %s %s is invalid, as a corresponding %s was not found

The provided alias or ID: [%s] could not be resolved to a valid package version ID (05i) or subscriber package version ID (04t).

# errorInvalidPackageVersionIdNoProject

The provided alias or ID: [%s] could not be resolved to a valid package version ID (05i) or subscriber package version ID (04t).

# errorInvalidPackageVersionIdNoProject.actions

If you are using a package alias, make sure you are inside your sfdx project and it's defined in the `packageDirectories` section in `sfdx-project.json`

# packageAliasNotFound

The provided package id: [%s] could not be resolved to an alias.
The provided package ID: [%s] could not be resolved to an alias.

# createResultIdCannotBeEmpty

Expand All @@ -21,3 +29,9 @@ Could not fetch the subscriber package version ID (04t).
# maxPackage2VersionRecords

The maximum result size (2000) was reached when querying the Package2Version SObject. This means there could be more records that were not returned by the query. If all records are required you may have to break the query into multiple requests filtered by date, then aggregate the results.

# errors.RequiresProject

This method expects an sfdx project to be available to write the new package version data in it.
Make sure to pass `options.project` when instantiating `PackageVersion`.
https://forcedotcom.github.io/packaging/classes/package_packageVersion.PackageVersion.html#constructor
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@salesforce/packaging",
"version": "4.1.13",
"version": "4.1.14-dev.2",
"description": "Packaging library for the Salesforce packaging platform",
"main": "lib/exported",
"types": "lib/exported.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/packagingInterfacesAndType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type IPackageVersion2GP = {

export type PackageOptions = {
connection: Connection;
project: SfProject;
project?: SfProject;
packageAliasOrId: string;
};

Expand Down Expand Up @@ -288,7 +288,7 @@ export type PackageVersionOptions = {
* 3. Alias for a 04t or 05i, defined in sfdx-project.json
*/
idOrAlias: string;
project: SfProject;
project?: SfProject;
};

export type SubscriberPackageVersionOptions = {
Expand Down Expand Up @@ -431,7 +431,7 @@ export type PackageAncestryNodeData = {

export type PackageAncestryOptions = {
packageId: string;
project: SfProject;
project?: SfProject;
connection: Connection;
};

Expand Down
11 changes: 6 additions & 5 deletions src/package/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export class Package {
public constructor(private options: PackageOptions) {
let packageId = this.options.packageAliasOrId;
if (!packageId.startsWith(packagePrefixes.PackageId)) {
packageId =
this.options.project.getPackageIdFromAlias(this.options.packageAliasOrId) ?? this.options.packageAliasOrId;
packageId = this.options.project
? this.options.project.getPackageIdFromAlias(this.options.packageAliasOrId) ?? this.options.packageAliasOrId
: this.options.packageAliasOrId;
if (packageId === this.options.packageAliasOrId) {
throw messages.createError('packageAliasNotFound', [this.options.packageAliasOrId]);
Copy link
Member Author

Choose a reason for hiding this comment

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

the constructor will fail if you pass an alias without project or an invalid alias with a project instance.

}
Expand Down Expand Up @@ -136,12 +137,12 @@ export class Package {
*/
public static async listVersions(
connection: Connection,
project: SfProject,
project?: SfProject,
options?: PackageVersionListOptions
): Promise<PackageVersionListResult[]> {
// resolve/verify packages
const packages = options?.packages?.map((pkg) => {
const id = project.getPackageIdFromAlias(pkg) ?? pkg;
const id = project ? project.getPackageIdFromAlias(pkg) ?? pkg : pkg;
Copy link
Member Author

Choose a reason for hiding this comment

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

these IDs are validated below


// validate ID
if (id.startsWith('0Ho')) {
Expand All @@ -166,7 +167,7 @@ export class Package {
*/
public static async getAncestry(
packageId: string,
project: SfProject,
project: SfProject | undefined,
connection: Connection
): Promise<PackageAncestry> {
return PackageAncestry.create({
Expand Down
4 changes: 3 additions & 1 deletion src/package/packageAncestry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ export class PackageAncestry extends AsyncCreatable<PackageAncestryOptions> {

private async getRootsFromRequestedId(): Promise<PackageAncestryNode[]> {
let roots: PackageAncestryNode[] = [];
this.packageId = this.options.project.getPackageIdFromAlias(this.options.packageId) ?? this.options.packageId;
this.packageId = this.options.project
? this.options.project.getPackageIdFromAlias(this.options.packageId) ?? this.options.packageId
: this.options.packageId;
switch (this.requestedPackageId?.slice(0, 3)) {
case '0Ho':
pkgUtils.validateId(pkgUtils.BY_LABEL.PACKAGE_ID, this.requestedPackageId);
Expand Down
4 changes: 2 additions & 2 deletions src/package/packageDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { PackageSaveResult } from '../interfaces';

export async function deletePackage(
idOrAlias: string,
project: SfProject,
project: SfProject | undefined,
connection: Connection,
undelete: boolean
): Promise<PackageSaveResult> {
const packageId = project.getPackageIdFromAlias(idOrAlias) ?? idOrAlias;
const packageId = project ? project.getPackageIdFromAlias(idOrAlias) ?? idOrAlias : idOrAlias;
validateId(BY_LABEL.PACKAGE_ID, packageId);

const request = { Id: packageId, IsDeprecated: !undelete };
Expand Down
27 changes: 25 additions & 2 deletions src/package/packageVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export type Package2VersionQueryOptions = {
* `new PackageVersion({connection, project, idOrAlias}).promote();`
*/
export class PackageVersion {
private readonly project: SfProject;
private readonly project?: SfProject;
private readonly connection: Connection;

private data?: Package2Version;
Expand Down Expand Up @@ -619,6 +619,9 @@ export class PackageVersion {
}

private async updateProjectWithPackageVersion(results: PackageVersionCreateRequestResult): Promise<void> {
if (!this.project) {
throw new SfError('errors.RequiresProject');
}
if (!env.getBoolean('SF_PROJECT_AUTOUPDATE_DISABLE_FOR_PACKAGE_VERSION_CREATE')) {
// get the newly created package version from the server
const versionResult = (
Expand Down Expand Up @@ -659,6 +662,26 @@ export class PackageVersion {
}

private resolveId(): string {
return this.project.getPackageIdFromAlias(this.options.idOrAlias) ?? this.options.idOrAlias;
let packageId = this.options.idOrAlias;

if (packageId.startsWith('04t') || packageId.startsWith('05i')) {
return packageId;
}

if (!this.options.project) {
throw messages.createError('errorInvalidPackageVersionIdNoProject', [this.options.idOrAlias]);
}

packageId = this.options.project.getPackageIdFromAlias(this.options.idOrAlias) ?? this.options.idOrAlias;

if (packageId === this.options.idOrAlias) {
throw messages.createError('packageAliasNotFound', [this.options.idOrAlias]);
}
// validate the resolved alias value from sfdx-project is a valid ID
if (packageId.startsWith('04t') || packageId.startsWith('05i')) {
return packageId;
} else {
throw messages.createError('errorInvalidPackageVersionId', [this.options.idOrAlias]);
}
}
}
2 changes: 1 addition & 1 deletion src/package/packageVersionReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function constructQuery(connectionVersion: number, verbose: boolean): string {
export async function getPackageVersionReport(options: {
packageVersionId: string;
connection: Connection;
project: SfProject;
project?: SfProject;
verbose: boolean;
}): Promise<PackageVersionReportResult[]> {
getLogger().debug(`entering getPackageVersionReport(${util.inspect(options, { depth: null })})`);
Expand Down
31 changes: 3 additions & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5048,16 +5048,7 @@ srcset@^5.0.0:
resolved "https://registry.yarnpkg.com/srcset/-/srcset-5.0.0.tgz#9df6c3961b5b44a02532ce6ae4544832609e2e3f"
integrity sha512-SqEZaAEhe0A6ETEa9O1IhSPC7MdvehZtCnTR0AftXk3QhY2UNgb+NApFOUPZILXk/YTDfFxMTNJOBpzrJsEdIA==

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -5116,14 +5107,7 @@ string_decoder@~1.1.1:
dependencies:
safe-buffer "~5.1.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@6.0.1, strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@6.0.1, strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -5589,7 +5573,7 @@ workerpool@^6.5.1:
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.5.1.tgz#060f73b39d0caf97c6db64da004cd01b4c099544"
integrity sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -5607,15 +5591,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down