Skip to content

Commit

Permalink
feat: most of sourceStatus logic, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Oct 19, 2021
1 parent b8877e1 commit f100cb8
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 66 deletions.
10 changes: 9 additions & 1 deletion src/index.ts
Expand Up @@ -7,4 +7,12 @@

export * from './sourceTracking';
export * from './compatibility';
export { RemoteSyncInput } from './shared/types';
export {
RemoteSyncInput,
ChangeOptionType,
ChangeOptions,
LocalUpdateOptions,
ChangeResult,
ConflictError,
} from './shared/types';
export { getKeyFromObject } from './shared/functions';
19 changes: 19 additions & 0 deletions src/shared/functions.ts
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2020, 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 { RemoteChangeElement, ChangeResult } from './types';

export const getMetadataKey = (metadataType: string, metadataName: string): string => {
return `${metadataType}__${metadataName}`;
};

export const getKeyFromObject = (element: RemoteChangeElement | ChangeResult): string => {
if (element.type && element.name) {
return getMetadataKey(element.type, element.name);
}
throw new Error(`unable to complete key from ${JSON.stringify(element)}`);
};
15 changes: 15 additions & 0 deletions src/shared/guards.ts
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2020, 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 { SourceComponent } from '@salesforce/source-deploy-retrieve';

export const stringGuard = (input: string | undefined): input is string => {
return typeof input === 'string';
};

export const sourceComponentGuard = (input: SourceComponent | undefined): input is SourceComponent => {
return input instanceof SourceComponent;
};
2 changes: 1 addition & 1 deletion src/shared/metadataKeys.ts
Expand Up @@ -6,7 +6,7 @@
*/
import * as path from 'path';
import { RemoteSyncInput } from './types';
import { getMetadataKey } from './remoteSourceTrackingService';
import { getMetadataKey } from './functions';

// LWC can have child folders (ex: dynamic templates like /templates/noDataIllustration.html
const pathAfterFullName = (fileResponse: RemoteSyncInput): string =>
Expand Down
14 changes: 2 additions & 12 deletions src/shared/remoteSourceTrackingService.ts
Expand Up @@ -13,8 +13,9 @@ import { ConfigFile, Logger, Org, SfdxError, Messages, fs } from '@salesforce/co
import { ComponentStatus } from '@salesforce/source-deploy-retrieve';
import { Dictionary, Optional } from '@salesforce/ts-types';
import { env, toNumber } from '@salesforce/kit';
import { RemoteSyncInput } from '../shared/types';
import { RemoteSyncInput, RemoteChangeElement } from '../shared/types';
import { getMetadataKeyFromFileResponse } from './metadataKeys';
import { getMetadataKey } from './functions';

export type MemberRevision = {
serverRevisionCounter: number;
Expand All @@ -30,13 +31,6 @@ export type SourceMember = {
RevisionCounter: number;
};

export type RemoteChangeElement = {
name: string;
type: string;
deleted?: boolean;
modified?: boolean;
};

// represents the contents of the config file stored in 'maxRevision.json'
interface Contents {
serverMaxRevisionCounter: number;
Expand All @@ -52,10 +46,6 @@ export namespace RemoteSourceTrackingService {
}
}

export const getMetadataKey = (metadataType: string, metadataName: string): string => {
return `${metadataType}__${metadataName}`;
};

/**
* This service handles source tracking of metadata between a local project and an org.
* Source tracking state is persisted to .sfdx/orgs/<orgId>/maxRevision.json.
Expand Down
42 changes: 41 additions & 1 deletion src/shared/types.ts
Expand Up @@ -5,6 +5,46 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { FileResponse } from '@salesforce/source-deploy-retrieve';
import { FileResponse, SourceComponent } from '@salesforce/source-deploy-retrieve';
import { getMetadataKey } from '../shared/functions';

export interface ChangeOptions {
origin: 'local' | 'remote';
state: 'add' | 'delete' | 'modify' | 'nondelete';
format: 'ChangeResult' | 'SourceComponent' | 'string' | 'ChangeResultWithPaths';
}

export type RemoteSyncInput = Pick<FileResponse, 'fullName' | 'filePath' | 'type' | 'state'>;

export type StatusOutputRow = Pick<FileResponse, 'fullName' | 'filePath' | 'type'> & {
conflict?: boolean;
ignored?: boolean;
} & Pick<ChangeOptions, 'origin' | 'state'>;

export interface LocalUpdateOptions {
files?: string[];
deletedFiles?: string[];
}

export type RemoteChangeElement = {
name: string;
type: string;
deleted?: boolean;
modified?: boolean;
};

/**
* Summary type that supports both local and remote change types
*/
export type ChangeResult = Partial<RemoteChangeElement> & {
origin: 'local' | 'remote';
filenames?: string[];
};

export interface ConflictError {
message: string;
name: 'conflict';
conflicts: ChangeResult[];
}

export type ChangeOptionType = ChangeResult | SourceComponent | string;

0 comments on commit f100cb8

Please sign in to comment.