Skip to content

Commit

Permalink
refactor: cloneJson => structuredClone (#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc authored Jan 2, 2024
1 parent 75ea043 commit 0726b49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
7 changes: 2 additions & 5 deletions src/formatters/source/deployAsyncResultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@

import { EOL } from 'node:os';



import { Messages } from '@salesforce/core';
import { cloneJson } from '@salesforce/kit';
import { AsyncResult } from '@salesforce/source-deploy-retrieve';
import { Ux } from '@salesforce/sf-plugins-core';
import { ResultFormatter, ResultFormatterOptions } from '../resultFormatter.js';

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

export interface DeployCommandAsyncResult extends DeployAsyncStatus {
Expand Down Expand Up @@ -54,7 +51,7 @@ export class DeployAsyncResultFormatter extends ResultFormatter {
status: 'Queued',
timedOut: true,
} as DeployCommandAsyncResult;
const resultClone = cloneJson(asyncResult);
const resultClone = structuredClone(asyncResult);
asyncResult.outboundFiles = [];
asyncResult.deploys = [resultClone];

Expand Down
36 changes: 25 additions & 11 deletions test/commands/source/deployResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
RequestStatus,
RunTestResult,
} from '@salesforce/source-deploy-retrieve';
import { cloneJson, ensureArray } from '@salesforce/kit';
import { ensureArray } from '@salesforce/kit';

const baseDeployResponse = {
checkOnly: false,
Expand Down Expand Up @@ -83,8 +83,8 @@ export const getDeployResponse = (
type: DeployResponseType,
overrides?: Partial<MetadataApiDeployStatus>
): MetadataApiDeployStatus => {
// stringify --> parse to get a clone that doesn't affect the base deploy response
const response = JSON.parse(JSON.stringify({ ...baseDeployResponse, ...overrides })) as MetadataApiDeployStatus;
// get a clone that doesn't affect the base deploy response
const response = structuredClone({ ...baseDeployResponse, ...overrides }) as MetadataApiDeployStatus;

if (type === 'inProgress') {
response.status = RequestStatus.InProgress;
Expand All @@ -102,9 +102,11 @@ export const getDeployResponse = (
if (type === 'failed') {
response.status = RequestStatus.Failed;
response.success = false;
response.details.componentSuccesses = cloneJson(baseDeployResponse.details.componentSuccesses[0]) as DeployMessage;
response.details.componentSuccesses = structuredClone(
baseDeployResponse.details.componentSuccesses[0]
) as DeployMessage;
response.details.componentFailures = {
...(cloneJson(baseDeployResponse.details.componentSuccesses[1]) as DeployMessage),
...(structuredClone(baseDeployResponse.details.componentSuccesses[1]) as DeployMessage),
success: false,
problemType: 'Error',
problem: 'This component has some problems',
Expand All @@ -117,8 +119,12 @@ export const getDeployResponse = (
if (type === 'failedTest') {
response.status = RequestStatus.Failed;
response.success = false;
response.details.componentFailures = cloneJson(baseDeployResponse.details.componentSuccesses[1]) as DeployMessage;
response.details.componentSuccesses = cloneJson(baseDeployResponse.details.componentSuccesses[0]) as DeployMessage;
response.details.componentFailures = structuredClone(
baseDeployResponse.details.componentSuccesses[1]
) as DeployMessage;
response.details.componentSuccesses = structuredClone(
baseDeployResponse.details.componentSuccesses[0]
) as DeployMessage;
response.details.componentFailures.success = 'false';
delete response.details.componentFailures.id;
response.details.componentFailures.problemType = 'Error';
Expand Down Expand Up @@ -160,8 +166,12 @@ export const getDeployResponse = (
if (type === 'passedTest') {
response.status = RequestStatus.Failed;
response.success = false;
response.details.componentFailures = cloneJson(baseDeployResponse.details.componentSuccesses[1]) as DeployMessage;
response.details.componentSuccesses = cloneJson(baseDeployResponse.details.componentSuccesses[0]) as DeployMessage;
response.details.componentFailures = structuredClone(
baseDeployResponse.details.componentSuccesses[1]
) as DeployMessage;
response.details.componentSuccesses = structuredClone(
baseDeployResponse.details.componentSuccesses[0]
) as DeployMessage;
response.details.componentFailures.success = 'false';
delete response.details.componentFailures.id;
response.details.componentFailures.problemType = 'Error';
Expand Down Expand Up @@ -198,8 +208,12 @@ export const getDeployResponse = (
if (type === 'passedAndFailedTest') {
response.status = RequestStatus.Failed;
response.success = false;
response.details.componentFailures = cloneJson(baseDeployResponse.details.componentSuccesses[1]) as DeployMessage;
response.details.componentSuccesses = cloneJson(baseDeployResponse.details.componentSuccesses[0]) as DeployMessage;
response.details.componentFailures = structuredClone(
baseDeployResponse.details.componentSuccesses[1]
) as DeployMessage;
response.details.componentSuccesses = structuredClone(
baseDeployResponse.details.componentSuccesses[0]
) as DeployMessage;
response.details.componentFailures.success = 'false';
delete response.details.componentFailures.id;
response.details.componentFailures.problemType = 'Error';
Expand Down
12 changes: 6 additions & 6 deletions test/formatters/retrieveResultFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { join, relative } from 'node:path';
import sinon from 'sinon';
import { expect } from 'chai';
import { FileResponse } from '@salesforce/source-deploy-retrieve';
import { cloneJson, ensureArray } from '@salesforce/kit';
import { ensureArray } from '@salesforce/kit';
import { stubInterface } from '@salesforce/ts-sinon';
import { Ux } from '@salesforce/sf-plugins-core';
import { TestContext } from '@salesforce/core/lib/testSetup.js';
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('RetrieveResultFormatter', () => {
inboundFiles: retrieveResultSuccess.getFileResponses(),
packages: [],
warnings: [],
response: cloneJson(retrieveResultSuccess.response),
response: structuredClone(retrieveResultSuccess.response),
};
const formatter = new RetrieveResultFormatter(ux, {}, retrieveResultSuccess);
expect(formatter.getJson()).to.deep.equal(expectedSuccessResults);
Expand All @@ -73,7 +73,7 @@ describe('RetrieveResultFormatter', () => {
inboundFiles: retrieveResultSuccess.getFileResponses(),
packages: [],
warnings: [],
response: cloneJson(retrieveResultFailure.response),
response: structuredClone(retrieveResultFailure.response),
};
const formatter = new RetrieveResultFormatter(ux, {}, retrieveResultFailure);
expect(formatter.getJson()).to.deep.equal(expectedFailureResults);
Expand All @@ -84,7 +84,7 @@ describe('RetrieveResultFormatter', () => {
inboundFiles: retrieveResultSuccess.getFileResponses(),
packages: [],
warnings: [],
response: cloneJson(retrieveResultInProgress.response),
response: structuredClone(retrieveResultInProgress.response),
};
const formatter = new RetrieveResultFormatter(ux, {}, retrieveResultInProgress);
expect(formatter.getJson()).to.deep.equal(expectedInProgressResults);
Expand All @@ -96,7 +96,7 @@ describe('RetrieveResultFormatter', () => {
inboundFiles: retrieveResultSuccess.getFileResponses(),
packages: [testPkg],
warnings: [],
response: cloneJson(retrieveResultSuccess.response),
response: structuredClone(retrieveResultSuccess.response),
};
const formatter = new RetrieveResultFormatter(ux, { packages: [testPkg] }, retrieveResultSuccess);
expect(formatter.getJson()).to.deep.equal(expectedSuccessResults);
Expand All @@ -109,7 +109,7 @@ describe('RetrieveResultFormatter', () => {
inboundFiles: retrieveResultWarnings.getFileResponses(),
packages: [],
warnings,
response: cloneJson(retrieveResultWarnings.response),
response: structuredClone(retrieveResultWarnings.response),
};
const formatter = new RetrieveResultFormatter(ux, {}, retrieveResultWarnings);
expect(formatter.getJson()).to.deep.equal(expectedSuccessResults);
Expand Down

0 comments on commit 0726b49

Please sign in to comment.