Skip to content

Commit

Permalink
fix: sfProject types matches schemas repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jun 7, 2024
1 parent a0dce8a commit 82fee15
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 55 deletions.
3 changes: 2 additions & 1 deletion src/org/scratchOrgInfoGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { promises as fs } from 'node:fs';
import { parseJson } from '@salesforce/kit';
import { ensureString } from '@salesforce/ts-types';
import { SfProjectJson } from '../sfProject';
import { SfProjectJson, isPackagingDirectory } from '../sfProject';
import { WebOAuthServer } from '../webOAuthServer';
import { Messages } from '../messages';
import { SfError } from '../sfError';
Expand Down Expand Up @@ -90,6 +90,7 @@ export const getAncestorIds = async (
throw new SfError(messages.getMessage('Package2AncestorsIdsKeyNotSupportedError'), 'DeprecationError');
}
const packagesWithAncestors = (await projectJson.getPackageDirectories())
.filter(isPackagingDirectory)
// check that the package has any ancestor types (id or version)
.filter((packageDir) => packageDir.ancestorId ?? packageDir.ancestorVersion);
if (packagesWithAncestors.length === 0) {
Expand Down
91 changes: 37 additions & 54 deletions src/sfProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { basename, dirname, isAbsolute, normalize, resolve, sep } from 'node:pat
import * as fs from 'node:fs';
import { defaults, env } from '@salesforce/kit';
import { Dictionary, ensure, JsonMap, Nullable, Optional } from '@salesforce/ts-types';
import {
PackageDir as PackageDirSchema,
PackageDirDependency as PackageDirDependencySchema,
ProjectJson as ProjectJsonSchema,
PackagePackageDir,
} from '@salesforce/schemas';
import { SfdcUrl } from './util/sfdcUrl';
import { ConfigAggregator } from './config/configAggregator';
import { ConfigFile } from './config/configFile';
Expand All @@ -25,38 +31,9 @@ const messages = Messages.loadMessages('@salesforce/core', 'config');

const coreMessages = Messages.loadMessages('@salesforce/core', 'core');

export type PackageDirDependency = {
[k: string]: unknown;
package: string;
versionNumber?: string;
};

export type PackageDir = {
ancestorId?: string;
ancestorVersion?: string;
default?: boolean;
definitionFile?: string;
dependencies?: PackageDirDependency[];
includeProfileUserLicenses?: boolean;
package?: string;
packageMetadataAccess?: {
permissionSets: string | string[];
permissionSetLicenses: string | string[];
};
path: string;
postInstallScript?: string;
postInstallUrl?: string;
releaseNotesUrl?: string;
scopeProfiles?: boolean;
uninstallScript?: string;
versionDescription?: string;
versionName?: string;
versionNumber?: string;
unpackagedMetadata?: { path: string };
seedMetadata?: { path: string };
};
export type PackageDirDependency = PackageDirDependencySchema;

export type NamedPackageDir = PackageDir & {
type NamedDirAdditions = {
/**
* The [normalized](https://nodejs.org/api/path.html#path_path_normalize_path) path used as the package name.
*/
Expand All @@ -67,16 +44,11 @@ export type NamedPackageDir = PackageDir & {
fullPath: string;
};

export type ProjectJson = ConfigContents & {
packageDirectories: PackageDir[];
namespace?: string;
sourceApiVersion?: string;
sfdcLoginUrl?: string;
signupTargetLoginUrl?: string;
oauthLocalPort?: number;
plugins?: { [k: string]: unknown };
packageAliases?: { [k: string]: string };
};
export type PackageDir = PackageDirSchema;
export type NamedPackagingDir = PackagePackageDir & NamedDirAdditions;
export type NamedPackageDir = PackageDir & NamedDirAdditions;

export type ProjectJson = ConfigContents & ProjectJsonSchema;

/**
* The sfdx-project.json config object. This file determines if a folder is a valid sfdx project.
Expand Down Expand Up @@ -338,18 +310,8 @@ export class SfProjectJson extends ConfigFile<ConfigFile.Options, ProjectJson> {
* @param packageDir
*/
public addPackageDirectory(packageDir: NamedPackageDir): void {
// there is no notion of uniqueness in package directory entries
// so an attempt of matching an existing entry is a bit convoluted
// an entry w/o a package or id is considered a directory entry for which a package has yet to be created
// so first attempt is to find a matching dir entry that where path is the same and id and package are not present
// if that fails, then find a matching dir entry package is present and is same as the new entry
const dirIndex = this.getContents().packageDirectories.findIndex((pd) => {
const withId = pd as NamedPackageDir & { id: string };
return (
(withId.path === packageDir.path && !withId.id && !withId.package) ||
(!!packageDir.package && packageDir.package === withId.package)
);
});
const dirIndex = this.getContents().packageDirectories.findIndex(findPackageDir(packageDir));

// merge new package dir with existing entry, if present
const packageDirEntry: PackageDir = Object.assign(
{},
Expand All @@ -367,6 +329,7 @@ export class SfProjectJson extends ConfigFile<ConfigFile.Options, ProjectJson> {
this.set('packageDirectories', modifiedPackagesDirs);
}

// keep it because testSetup stubs it!
// eslint-disable-next-line class-methods-use-this
private doesPackageExist(packagePath: string): boolean {
return fs.existsSync(packagePath);
Expand Down Expand Up @@ -600,7 +563,8 @@ export class SfProject {
*/
public getPackageNameFromPath(path: string): Optional<string> {
const packageDir = this.getPackageFromPath(path);
return packageDir ? packageDir.package ?? packageDir.path : undefined;
if (!packageDir) return undefined;
return isNamedPackagingDirectory(packageDir) ? packageDir.package : packageDir.path;
}

/**
Expand Down Expand Up @@ -760,3 +724,22 @@ export class SfProject {
.map(([key]) => key);
}
}

/** differentiate between the Base PackageDir (path, maybe default) and the Packaging version (path) by whether is has the `package` property */
export const isPackagingDirectory = (packageDir: PackageDir): packageDir is PackagePackageDir =>
'package' in packageDir && typeof packageDir.package === 'string';
export const isNamedPackagingDirectory = (packageDir: NamedPackageDir): packageDir is NamedPackagingDir =>
'package' in packageDir && typeof packageDir.package === 'string';

/**
* there is no notion of uniqueness in package directory entries
* so an attempt of matching an existing entry is a bit convoluted
*/
const findPackageDir =
(target: NamedPackageDir) =>
(potentialMatch: PackageDir): boolean =>
// an entry w/o a package or id is considered a directory entry for which a package has yet to be created
// find a matching dir entry that where path is the same and id and package are not present
(potentialMatch.path === target.path && !('id' in potentialMatch) && !isPackagingDirectory(potentialMatch)) ||
// if that fails, then find a matching dir entry package is present and is same as the new entry
(isPackagingDirectory(target) && isPackagingDirectory(potentialMatch) && target.package === potentialMatch.package);

3 comments on commit 82fee15

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 82fee15 Previous: 6572e57 Ratio
Child logger creation 479686 ops/sec (±1.90%) 477661 ops/sec (±1.96%) 1.00
Logging a string on root logger 833111 ops/sec (±10.23%) 766683 ops/sec (±8.25%) 0.92
Logging an object on root logger 629697 ops/sec (±6.43%) 625753 ops/sec (±6.14%) 0.99
Logging an object with a message on root logger 1938 ops/sec (±255.72%) 5388 ops/sec (±213.19%) 2.78
Logging an object with a redacted prop on root logger 446902 ops/sec (±10.27%) 403423 ops/sec (±12.18%) 0.90
Logging a nested 3-level object on root logger 384443 ops/sec (±8.89%) 386832 ops/sec (±5.30%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Logger Benchmarks - ubuntu-latest'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 82fee15 Previous: 6572e57 Ratio
Logging an object with a message on root logger 1938 ops/sec (±255.72%) 5388 ops/sec (±213.19%) 2.78

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Logger Benchmarks - windows-latest

Benchmark suite Current: 82fee15 Previous: 6572e57 Ratio
Child logger creation 323617 ops/sec (±1.03%) 324970 ops/sec (±1.75%) 1.00
Logging a string on root logger 828942 ops/sec (±6.68%) 746579 ops/sec (±5.89%) 0.90
Logging an object on root logger 626486 ops/sec (±5.04%) 584705 ops/sec (±6.29%) 0.93
Logging an object with a message on root logger 19580 ops/sec (±185.13%) 7177 ops/sec (±201.24%) 0.37
Logging an object with a redacted prop on root logger 479325 ops/sec (±7.16%) 425337 ops/sec (±13.95%) 0.89
Logging a nested 3-level object on root logger 325845 ops/sec (±4.93%) 307176 ops/sec (±8.78%) 0.94

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.