Skip to content

Commit

Permalink
feat: add config file for sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
khogan18 committed Jun 27, 2019
1 parent 96ee134 commit db2026e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/config/sandboxOrgConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { ConfigFile } from './configFile';

/**
* A config file that stores usernames for an org.
*/
export class SandboxOrgConfig extends ConfigFile<SandboxOrgConfig.Options> {
/**
* Gets the config options for a given org ID.
* @param orgId The orgId. Generally this org would have multiple users configured.
*/
public static getOptions(orgId: string): SandboxOrgConfig.Options {
return {
isGlobal: true,
isState: true,
filename: `${orgId}.sandbox.json`,
orgId
};
}

/**
* Constructor.
* **Do not directly construct instances of this class -- use {@link SandboxConfig.create} instead.**
* @param options The options for the class instance
* @ignore
*/
public constructor(options: SandboxOrgConfig.Options) {
super(options);
}
}

export namespace SandboxOrgConfig {
/**
* The config file options.
*/
export interface Options extends ConfigFile.Options {
/**
* The org id associated with this sandbox.
*/
orgId: string;
}

export enum Fields {
/**
* The username of the user who created the sandbox.
*/
PROD_ORG_USERNAME = 'prodOrgUsername'
}
}
38 changes: 38 additions & 0 deletions src/org.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Config } from './config/config';
import { ConfigAggregator, ConfigInfo } from './config/configAggregator';
import { ConfigContents } from './config/configStore';
import { OrgUsersConfig } from './config/orgUsersConfig';
import { SandboxOrgConfig } from './config/sandboxOrgConfig';
import { Connection } from './connection';
import { Global } from './global';
import { Logger } from './logger';
Expand Down Expand Up @@ -162,6 +163,14 @@ export class Org extends AsyncCreatable<Org.Options> {
}

await aliases.write();

// Delete the sandbox org config file if it exists
const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
await this.manageDelete(
async () => await sandboxOrgConfig.unlink(),
sandboxOrgConfig.getPath(),
throwWhenRemoveFails
);
}

/**
Expand Down Expand Up @@ -397,6 +406,28 @@ export class Org extends AsyncCreatable<Org.Options> {
return this;
}

/**
* Sets the key/value pair in the sandbox config for this org. For convenience `this` object is returned.
*
*
* @param {key} The key for this value
* @param {value} The value to save
*/
public async setSandboxOrgConfigField(field: SandboxOrgConfig.Fields, value: string): Promise<Org> {
const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
sandboxOrgConfig.set(field, value);
await sandboxOrgConfig.write();
return this;
}

/**
* Returns an org config field. Returns undefined if the field is not set or invalid.
*/
public async getSandboxOrgConfigField(field: SandboxOrgConfig.Fields) {
const sandboxOrgConfig = await this.retrieveSandboxOrgConfig();
return sandboxOrgConfig.get(field);
}

/**
* Retrieves the highest api version that is supported by the target server instance. If the apiVersion configured for
* Sfdx is greater than the one returned in this call an api version mismatch occurs. In the case of the CLI that
Expand Down Expand Up @@ -489,6 +520,13 @@ export class Org extends AsyncCreatable<Org.Options> {
throw new SfdxError('Not Supported');
}

/**
* @ignore
*/
private async retrieveSandboxOrgConfig(): Promise<SandboxOrgConfig> {
return await SandboxOrgConfig.create(SandboxOrgConfig.getOptions(this.getOrgId()));
}

private manageDelete(cb: AnyFunction<Promise<void>>, dirPath: string, throwWhenRemoveFails: boolean): Promise<void> {
return cb().catch(e => {
if (throwWhenRemoveFails) {
Expand Down
17 changes: 17 additions & 0 deletions test/unit/orgTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Config } from '../../src/config/config';
import { ConfigAggregator } from '../../src/config/configAggregator';
import { ConfigFile } from '../../src/config/configFile';
import { OrgUsersConfig } from '../../src/config/orgUsersConfig';
import { SandboxOrgConfig } from '../../src/config/sandboxOrgConfig';
import { Connection } from '../../src/connection';
import { Global } from '../../src/global';
import { Org } from '../../src/org';
Expand Down Expand Up @@ -233,12 +234,17 @@ describe('Org Tests', () => {
stubMethod($$.SANDBOX, fs, 'remove').callsFake(() => {
return Promise.resolve({});
});
await org.setSandboxOrgConfigField(SandboxOrgConfig.Fields.PROD_ORG_USERNAME, 'foo@sandbox.com');

await org.remove();

expect(deletedPaths).includes(
pathJoin(await $$.globalPathRetriever($$.id), Global.STATE_FOLDER, `${testData.orgId}.json`)
);

expect(deletedPaths).includes(
pathJoin(await $$.globalPathRetriever($$.id), Global.STATE_FOLDER, `${testData.orgId}.sandbox.json`)
);
});

it('should not fail when no scratch org has been written', async () => {
Expand Down Expand Up @@ -670,4 +676,15 @@ describe('Org Tests', () => {
expect(org.isDevHubOrg()).to.be.true;
});
});

describe('sandbox org config', () => {
it('set field', async () => {
const org: Org = await Org.create({ aliasOrUsername: testData.username });
expect(await org.getSandboxOrgConfigField(SandboxOrgConfig.Fields.PROD_ORG_USERNAME)).to.be.undefined;

await org.setSandboxOrgConfigField(SandboxOrgConfig.Fields.PROD_ORG_USERNAME, 'user@sandbox.org');

expect(await org.getSandboxOrgConfigField(SandboxOrgConfig.Fields.PROD_ORG_USERNAME)).to.eq('user@sandbox.org');
});
});
});

0 comments on commit db2026e

Please sign in to comment.