Skip to content

Commit

Permalink
parameterize extensions (#476)
Browse files Browse the repository at this point in the history
* parameterize extensions

* add test for poorly formed extension ids
  • Loading branch information
purplecabbage authored Sep 16, 2021
1 parent d15b5a9 commit 0c90263
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 25 deletions.
61 changes: 36 additions & 25 deletions src/commands/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,32 +135,37 @@ class InitCommand extends BaseCommand {
async selectExtensionPoints (flags, orgSupportedServices = null) {
if (!flags.extensions) {
return [implPromptChoices.find(i => i.value.name === 'application').value]
} else if (flags.extension) {
const extList = implPromptChoices.filter(i => flags.extension.indexOf(i.value.name) > -1)
.map(i => i.value)
if (extList.length < 1) {
throw new Error(`--extension=${flags.extension} not found.`)
}
return extList
} else {
const choices = cloneDeep(implPromptChoices).filter(i => i.value.name !== 'application')

// disable extensions that lack required services
if (orgSupportedServices) {
const supportedServiceCodes = new Set(orgSupportedServices.map(s => s.code))
// filter choices
choices.forEach(c => {
const missingServices = c.value.requiredServices.filter(s => !supportedServiceCodes.has(s))
if (missingServices.length > 0) {
c.disabled = true
c.name = `${c.name}: missing service(s) in Org: '${missingServices}'`
}
})
}
const answers = await this.prompt([{
type: 'checkbox',
name: 'res',
message: 'Which extension point(s) do you wish to implement ?',
choices,
validate: atLeastOne
}])
return answers.res
}

const choices = cloneDeep(implPromptChoices).filter(i => i.value.name !== 'application')

// disable extensions that lack required services
if (orgSupportedServices) {
const supportedServiceCodes = new Set(orgSupportedServices.map(s => s.code))
// filter choices
choices.forEach(c => {
const missingServices = c.value.requiredServices.filter(s => !supportedServiceCodes.has(s))
if (missingServices.length > 0) {
c.disabled = true
c.name = `${c.name}: missing service(s) in Org: '${missingServices}'`
}
})
}

const answers = await this.prompt([{
type: 'checkbox',
name: 'res',
message: 'Which extension point(s) do you wish to implement ?',
choices,
validate: atLeastOne
}])

return answers.res
}

async selectConsoleOrg (consoleCLI) {
Expand Down Expand Up @@ -315,6 +320,12 @@ InitCommand.flags = {
default: true,
allowNo: true
}),
extension: flags.string({
description: 'Extension point(s) to implement',
char: 'e',
multiple: true,
exclusive: ['extensions']
}),
workspace: flags.string({
description: 'Specify the Adobe Developer Console Workspace to init from, defaults to Stage',
default: DEFAULT_WORKSPACE,
Expand Down
57 changes: 57 additions & 0 deletions test/commands/app/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ describe('bad args/flags', () => {
test('--no-login and --workspace', async () => {
await expect(TheCommand.run(['--no-login', '--workspace', 'dev'])).rejects.toThrow('--no-login and --workspace flags cannot be used together.')
})
test('--no-login and --extension does not exist', async () => {
await expect(TheCommand.run(['--no-login', '--extension', 'dev'])).rejects.toThrow('--extension=dev not found.')
})
})

describe('run', () => {
Expand Down Expand Up @@ -277,6 +280,24 @@ describe('run', () => {
expect(mockImport.importConfigJson).not.toHaveBeenCalled()
})

test('--no-login --yes --skip-install, --extension dx/asset-compute/worker/1', async () => {
mockExtensionPrompt.mockReturnValue({ res: excshellSelection })
await TheCommand.run(['--no-login', '--yes', '--skip-install', '--extension', 'dx/asset-compute/worker/1'])
expect(mockGenInstantiate).toHaveBeenCalledTimes(2)
expect(mockGenInstantiate).toHaveBeenCalledWith(
'fake-gen-base-app',
{ options: { 'skip-prompt': true, 'project-name': 'cwd' } }
)
expect(mockGenInstantiate).toHaveBeenCalledWith(
'fake-gen-nui',
{ options: { 'skip-prompt': true, force: true } }
)
expect(mockInstallPackages).not.toHaveBeenCalled()
expect(LibConsoleCLI.init).not.toHaveBeenCalled()
expect(mockExtensionPrompt).not.toHaveBeenCalled()
expect(mockImport.importConfigJson).not.toHaveBeenCalled()
})

// fake imported config
const fakeConfig = {
project: {
Expand Down Expand Up @@ -518,6 +539,42 @@ describe('run', () => {
expect(mockConsoleCLIInstance.createProject).toHaveBeenCalledWith(fakeOrg.id, 'fakedetails')
})

test('with login, --extension excshell, create new project', async () => {
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(null) // null = user selects to create a project
mockConsoleCLIInstance.createProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
mockConsoleCLIInstance.getServicePropertiesFromWorkspace.mockResolvedValue(fakeServicePropertiesNoAssetCompute)
mockConsoleCLIInstance.getEnabledServicesForOrg.mockResolvedValue(fakeSupportedOrgServices)
mockConsoleCLIInstance.getWorkspaceConfig.mockResolvedValue(fakeConfig)
mockConsoleCLIInstance.promptForCreateProjectDetails.mockResolvedValue('fakedetails')
mockExtensionPrompt.mockReturnValue({})

await TheCommand.run(['--extension', 'dx/excshell/1'])
expect(mockGenInstantiate).toHaveBeenCalledTimes(2)
expect(mockGenInstantiate).toHaveBeenCalledWith(
'fake-gen-base-app',
{ options: { 'skip-prompt': false, 'project-name': 'hola' } }
)
expect(mockGenInstantiate).toHaveBeenCalledWith(
'fake-gen-excshell',
{ options: { 'skip-prompt': false, force: true } }
)
expect(mockInstallPackages).toHaveBeenCalled()
expect(LibConsoleCLI.init).toHaveBeenCalled()
expect(mockExtensionPrompt).not.toHaveBeenCalled()
expect(mockImport.importConfigJson).toHaveBeenCalledWith(
Buffer.from(JSON.stringify(fakeConfig)),
'cwd',
{ interactive: false, merge: true },
{ SERVICE_API_KEY: 'fakeclientid' }
)
expect(mockConsoleCLIInstance.getWorkspaceConfig).toHaveBeenCalledWith(fakeOrg.id, fakeProject.id, fakeWorkspaces[0].id, fakeSupportedOrgServices)
// exchshell has no required service to be added
expect(mockConsoleCLIInstance.subscribeToServices).not.toHaveBeenCalled()
expect(mockConsoleCLIInstance.createProject).toHaveBeenCalledWith(fakeOrg.id, 'fakedetails')
})

test('with login, select excshell, -w dev', async () => {
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
Expand Down

0 comments on commit 0c90263

Please sign in to comment.