Skip to content

Commit

Permalink
feat: determine if a org is a dev hub
Browse files Browse the repository at this point in the history
  • Loading branch information
amphro committed Jan 28, 2019
1 parent 28de75c commit 586d7ba
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 45 deletions.
76 changes: 34 additions & 42 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Remote",
"address": "localhost",
"port": 9229,
"localRoot": "${workspaceFolder}"
},
{
"type": "node",
"request": "launch",
"name": "Launch exports",
"preLaunchTask": "compile",
"program": "${workspaceRoot}/lib/exports.js",
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/out/**/*.js"
]
},
{
"type": "node",
"request": "launch",
"name": "Debug Unit Tests",
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
"preLaunchTask": "compile",
"args": [
"--timeout",
"999999",
"dist/test/unit/**/*.js"
],
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/dist/**/*.js"
]
}
]
}
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Remote",
"address": "localhost",
"port": 9229,
"skipFiles": ["<node_internals>/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "Launch exports",
"preLaunchTask": "compile",
"program": "${workspaceRoot}/lib/exports.js",
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "Debug Unit Tests",
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
"preLaunchTask": "compile",
"args": ["--timeout", "999999", "dist/test/unit/**/*.js"],
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/dist/**/*.js"]
}
]
}
47 changes: 44 additions & 3 deletions src/org.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getNumber,
getString,
isArray,
isBoolean,
isString,
JsonArray,
JsonMap,
Expand Down Expand Up @@ -143,7 +144,7 @@ export class Org extends AsyncCreatable<Org.Options> {
orgForUser = await Org.create({ connection });
}

const orgType = (await this.isDevHubOrg()) ? Config.DEFAULT_DEV_HUB_USERNAME : Config.DEFAULT_USERNAME;
const orgType = this.isDevHubOrg() ? Config.DEFAULT_DEV_HUB_USERNAME : Config.DEFAULT_USERNAME;

const configInfo: ConfigInfo = await orgForUser.configAggregator.getInfo(orgType);

Expand Down Expand Up @@ -208,7 +209,7 @@ export class Org extends AsyncCreatable<Org.Options> {
*/
public async getDevHubOrg(): Promise<Optional<Org>> {
if (this.isDevHubOrg()) {
return Promise.resolve(this);
return this;
} else if (this.getField(Org.Fields.DEV_HUB_USERNAME)) {
const devHubUsername = ensureString(this.getField(Org.Fields.DEV_HUB_USERNAME));
return Org.create({
Expand All @@ -221,9 +222,49 @@ export class Org extends AsyncCreatable<Org.Options> {

/**
* Returns `true` if the org is a Dev Hub.
*
* **Note** This relies on a cached value in the auth file. If that property
* is not cached, this method will **always return false even if the org is a
* dev hub**. If you need accuracy, use the {@link Org.determineIfDevhub} method.
*/
public isDevHubOrg(): boolean {
return this.getField(Org.Fields.IS_DEV_HUB) as boolean;
const isDevHub = this.getField(Org.Fields.IS_DEV_HUB);
if (isBoolean(isDevHub)) {
return isDevHub;
} else {
return false;
}
}

/**
* Returns `true` if the org is a Dev Hub.
*
* Use a cached value. If the cached value is not set, then check access to the
* ScratchOrgInfo object to determine if the org is a dev hub.
*/
public async determineIfDevHubOrg() {
if (this.isDevHubOrg()) {
return true;
}

const conn = this.getConnection();
let isDevHub = false;
try {
await conn.query('SELECT Id FROM ScratchOrgInfo');
isDevHub = true;
} catch (err) {
/* Not a dev hub */
}

const username = ensure(this.getUsername());
const auth = await AuthInfo.create({ username });
await auth.save({ isDevHub });
AuthInfo.clearCache(username);
// Reset the connection with the updated auth file
this.connection = await Connection.create({
authInfo: await AuthInfo.create({ username })
});
return isDevHub;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ const _testSetup = (sinon?: any) => {
}
return testContext.fakeConnectionRequest.call(this, request, options);
});

// Always start with the default and tests beforeEach or it methods can override it.
testContext.fakeConnectionRequest = defaultFakeConnectionRequest;
});

afterEach(() => {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/orgTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,25 @@ describe('Org Tests', () => {
});
});
});

describe('determineDevHub', () => {
it('should return true and cache if dev hub', async () => {
const org: Org = await Org.create({ aliasOrUsername: testData.username });
$$.fakeConnectionRequest = async (request: AnyJson, options?: AnyJson) => {
return { records: [] };
};
expect(org.isDevHubOrg()).to.be.false;
expect(await org.determineIfDevHubOrg()).to.be.true;
expect(org.isDevHubOrg()).to.be.true;
});
it('should return false and cache if dev hub', async () => {
const org: Org = await Org.create({ aliasOrUsername: testData.username });
$$.fakeConnectionRequest = async (request: AnyJson, options?: AnyJson) => {
throw new Error();
};
expect(org.isDevHubOrg()).to.be.false;
expect(await org.determineIfDevHubOrg()).to.be.false;
expect(org.isDevHubOrg()).to.be.false;
});
});
});

0 comments on commit 586d7ba

Please sign in to comment.