Skip to content

Commit

Permalink
check for functions on org before creating compute env (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
cafreeman authored May 13, 2021
1 parent 7028e32 commit 04e4c5e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/commands/env/create/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export default class EnvCreateCompute extends Command {
const org = await this.fetchOrg(flags['connected-org'])
const orgId = org.getOrgId()

if (!await this.isFunctionsEnabled(org)) {
this.error(
`The org you are attempting to create a compute environment for does not have the ${herokuColor.green('Functions')} feature enabled.\n` +
'\n' +
'Before you can create a compute environment, please:\n' +
'1. Enable Functions in your DevHub org\n' +
`2. Add ${herokuColor.green('Functions')} to the "features" list in your scratch org definition JSON file, e.g. "features": ["Functions"]`,
)
}

cli.action.start(`Creating compute environment for org ID ${orgId}`)

const project = await this.fetchSfdxProject()
Expand Down
4 changes: 2 additions & 2 deletions src/commands/generate/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ export default class GenerateFunction extends Command {
this.log('')
this.log(
'Before creating Scratch Orgs for development, please ensure that:\n' +
'1. Functions is enabled in your DevHub org\n' +
`2. ${herokuColor.green('"Functions"')} is added to your scratch org definition file ${herokuColor.green('"features"')} list`,
'1. Enable Functions in your DevHub org\n' +
`2. Add ${herokuColor.green('Functions')} to the "features" list in your scratch org definition JSON file, e.g. "features": ["Functions"]`,
)
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ export default abstract class Command extends Base {
return appName
}

protected async isFunctionsEnabled(org: Org) {
const conn = org.getConnection()

// This is a roundabout away of checking if a given org has Functions enabled. If they do NOT have functions enabled,
// then querying for FunctionReferences will throw an error which complains about not having access to the
// FunctionReference object for the given org.
try {
await conn.metadata.list({type: 'FunctionReference'})
return true
} catch (error) {
if (error.name.includes('INVALID_TYPE') || error.message.includes('Cannot use: FunctionReference in this organization')) {
return false
}

// If we get here, something very unexpected has happened so just bail
throw error
}
}

private fetchConfirmationValue(name: string, confirm?: string | string[]): string | undefined {
// If multiple confirm values have been specified, we iterate over each one until finding something that could match
// If there isn't a match, we'll simply return undefined
Expand Down
27 changes: 27 additions & 0 deletions test/commands/env/create/compute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const APP_MOCK = {

const USERNAME = '[email protected]'

const CONN_MOCK = {
metadata: {
list: sinon.stub(),
},
}

const ORG_MOCK = {
id: '1',
getOrgId() {
Expand All @@ -17,6 +23,9 @@ const ORG_MOCK = {
getUsername() {
return USERNAME
},
getConnection() {
return CONN_MOCK
},
}

const PROJECT_CONFIG_MOCK = {
Expand Down Expand Up @@ -145,4 +154,22 @@ describe('sf env create compute', () => {
expect(ctx.stderr).to.contain('error!')
expect(ctx.stdout).to.contain(`Compute Environment ID: ${APP_MOCK.name}`)
})

test
.stdout()
.stderr()
.do(() => {
orgStub = sandbox.stub(Org, 'create' as any).returns(ORG_MOCK)
sandbox.stub(SfdxProject, 'resolve' as any).returns(PROJECT_MOCK)
CONN_MOCK.metadata.list = sinon.stub().throws('INVALID_TYPE')
})
.finally(() => {
sandbox.restore()
sinon.restore()
})
.command(['env:create:compute'])
.catch(error => {
expect(error.message).to.contain('The org you are attempting to create a compute environment for does not have the Functions feature enabled.')
})
.it('displays an informative error message if the org doesn\'t have functions enabled')
})

0 comments on commit 04e4c5e

Please sign in to comment.