Skip to content

Commit

Permalink
Show all action templates in categories (#305)
Browse files Browse the repository at this point in the history
* first commit demo ready

* move console api interraction to console generator

* add tests - ready for review

* bump generator-aio-console

* bump generator-aio-app

* dont repeat config.project

* rename long vars
  • Loading branch information
moritzraho authored Oct 13, 2020
1 parent a3e5f55 commit 3b74468
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 98 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"@adobe/aio-lib-ims": "^4.1.0",
"@adobe/aio-lib-runtime": "^1.0.0-0",
"@adobe/aio-lib-web": "^4.0.0",
"@adobe/generator-aio-app": "^1.7.0",
"@adobe/generator-aio-console": "^1.1.1",
"@adobe/generator-aio-app": "^1.9.0",
"@adobe/generator-aio-console": "^1.2.0",
"@oclif/command": "^1.5.11",
"@oclif/config": "^1.12.9",
"@oclif/plugin-help": "^2.2.3",
Expand Down
10 changes: 8 additions & 2 deletions src/commands/app/add/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const yeoman = require('yeoman-environment')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:add:action', { provider: 'debug' })
const { flags } = require('@oclif/command')

const { servicesToGeneratorInput } = require('../../../lib/app-helper')
const config = require('@adobe/aio-lib-core-config')

class AddActionCommand extends BaseCommand {
Expand All @@ -22,15 +23,20 @@ class AddActionCommand extends BaseCommand {

aioLogger.debug(`adding component ${args.component} to the project, using flags: ${flags}`)

const services = (config.get('services') || config.get('project.workspace.details.services') || []).map(s => s.code).join(',')
const workspaceServices =
config.get('services') || // legacy
config.get('project.workspace.details.services') ||
[]
const supportedOrgServices = config.get('project.org.details.services') || []

const generator = '@adobe/generator-aio-app/generators/add-action'
const env = yeoman.createEnv()
env.register(require.resolve(generator), 'gen')
const res = await env.run('gen', {
'skip-install': flags['skip-install'],
'skip-prompt': flags.yes,
'adobe-services': services
'adobe-services': servicesToGeneratorInput(workspaceServices),
'supported-adobe-services': servicesToGeneratorInput(supportedOrgServices)
})
return res
}
Expand Down
25 changes: 13 additions & 12 deletions src/commands/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const path = require('path')
const fs = require('fs-extra')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:init', { provider: 'debug' })
const { flags } = require('@oclif/command')
const { loadAndValidateConfigFile, importConfigJson, writeAio } = require('../../lib/import')
const { loadAndValidateConfigFile, importConfigJson } = require('../../lib/import')
const { getCliInfo } = require('../../lib/app-helper')
const chalk = require('chalk')
const { servicesToGeneratorInput } = require('../../lib/app-helper')

const SERVICE_API_KEY_ENV = 'SERVICE_API_KEY'

Expand All @@ -42,8 +43,10 @@ class InitCommand extends BaseCommand {

// default project name and services
let projectName = path.basename(process.cwd())
// list of supported service templates
let services = 'AdobeTargetSDK,AdobeAnalyticsSDK,CampaignSDK,McDataServicesSdk,AudienceManagerCustomerSDK,AssetComputeSDK'
// list of services added to the workspace
let workspaceServices = []
// list of services supported by the organization
let supportedServices = []

// client id of the console's workspace jwt credentials
let serviceClientId = ''
Expand Down Expand Up @@ -74,9 +77,11 @@ class InitCommand extends BaseCommand {
if (flags.import) {
const { values: config } = loadAndValidateConfigFile(flags.import)

projectName = config.project.name
services = config.project.workspace.details.services.map(s => s.code).join(',') || ''
const jwtConfig = config.project.workspace.details.credentials && config.project.workspace.details.credentials.find(c => c.jwt)
const project = config.project
projectName = project.name
workspaceServices = project.workspace.details.services
supportedServices = project.org.details.services
const jwtConfig = project.workspace.details.credentials && project.workspace.details.credentials.find(c => c.jwt)
serviceClientId = (jwtConfig && jwtConfig.jwt.client_id) || serviceClientId // defaults to ''
}

Expand All @@ -88,7 +93,8 @@ class InitCommand extends BaseCommand {
'skip-install': flags['skip-install'],
'skip-prompt': flags.yes,
'project-name': projectName,
'adobe-services': services
'adobe-services': servicesToGeneratorInput(workspaceServices),
'supported-adobe-services': servicesToGeneratorInput(supportedServices)
})

// config import
Expand All @@ -100,11 +106,6 @@ class InitCommand extends BaseCommand {
if (deleteConsoleCredentials) {
fs.unlinkSync(flags.import)
}
} else {
// write default services value to .aio
await writeAio({
services: services.split(',').map(code => ({ code }))
}, process.cwd(), { merge, interactive })
}

// finalize configuration data
Expand Down
3 changes: 2 additions & 1 deletion src/commands/app/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class Use extends BaseCommand {
}

const { values: config } = loadAndValidateConfigFile(filePath)
const jwtConfig = config.project.workspace.details.credentials && config.project.workspace.details.credentials.find(c => c.jwt)
const project = config.project
const jwtConfig = project.workspace.details.credentials && project.workspace.details.credentials.find(c => c.jwt)
const serviceClientId = (jwtConfig && jwtConfig.jwt.client_id) || ''
const extraEnvVars = { [SERVICE_API_KEY_ENV]: serviceClientId }

Expand Down
14 changes: 13 additions & 1 deletion src/lib/app-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ function saveAndReplaceDotEnvCredentials (dotenvFile, saveFile, apihost, namespa
fs.writeFileSync(dotenvFile, envContent)
}

/**
*
* Converts a service array to an input string that can be consumed by generator-aio-app
*
* @param {Array} services array of services [{ code: 'xxx', name: 'xxx' }, ...]
* @returns {string} 'code1,code2,code3'
*/
function servicesToGeneratorInput (services) {
return services.map(s => s.code).filter(s => s).join(',')
}

module.exports = {
isNpmInstalled,
isGitInstalled,
Expand All @@ -359,5 +370,6 @@ module.exports = {
writeConfig,
downloadOWJar,
runOpenWhiskJar,
saveAndReplaceDotEnvCredentials
saveAndReplaceDotEnvCredentials,
servicesToGeneratorInput
}
4 changes: 2 additions & 2 deletions src/lib/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ function mergeJson (oldData, newData) {
*/
function mergeData (oldData, newData, fileFormat) {
aioLogger.debug(`mergeData - oldData: ${oldData}`)
aioLogger.debug(`mergeData - newData:${newData}`)
aioLogger.debug(`mergeData - newData: ${newData}`)

if (fileFormat === FILE_FORMAT_ENV) {
return mergeEnv(oldData, newData)
Expand Down Expand Up @@ -329,7 +329,7 @@ async function writeFile (destination, data, flags = {}) {

if (interactive) {
answer = await checkFileConflict(destination)
aioLogger.debug(`writeEnv - answer (interactive): ${JSON.stringify(answer)}`)
aioLogger.debug(`writeFile - answer (interactive): ${JSON.stringify(answer)}`)
}

if (answer.abort) {
Expand Down
42 changes: 30 additions & 12 deletions test/commands/app/add/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': true,
'skip-install': false,
'adobe-services': ''
'adobe-services': '',
'supported-adobe-services': ''
})
})

Expand All @@ -81,7 +82,8 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': true,
'skip-install': true,
'adobe-services': ''
'adobe-services': '',
'supported-adobe-services': ''
})
})

Expand All @@ -94,7 +96,8 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false,
'skip-install': true,
'adobe-services': ''
'adobe-services': '',
'supported-adobe-services': ''
})
})
test('no flags', async () => {
Expand All @@ -106,12 +109,22 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false,
'skip-install': false,
'adobe-services': ''
'adobe-services': '',
'supported-adobe-services': ''
})
})

test('pass services config codes to generator-aio-app', async () => {
config.get.mockReturnValue([{ code: 'CampaignSDK' }, { code: 'AdobeAnalyticsSDK' }])
config.get.mockImplementation(c => {
if (c === 'project.org.details.services') {
// supported services
return [{ code: 'CampaignSDK' }, { code: 'AdobeAnalyticsSDK' }, { code: 'AnotherOneSDK' }]
} else if (c === 'project.workspace.details.services') {
// added to workspace
return [{ code: 'CampaignSDK' }, { code: 'AdobeAnalyticsSDK' }]
}
return undefined
})

await TheCommand.run([])

Expand All @@ -121,17 +134,21 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false,
'skip-install': false,
'adobe-services': 'CampaignSDK,AdobeAnalyticsSDK'
'adobe-services': 'CampaignSDK,AdobeAnalyticsSDK',
'supported-adobe-services': 'CampaignSDK,AdobeAnalyticsSDK,AnotherOneSDK'
})
})

test('pass services config codes from imported config to generator-aio-app', async () => {
config.get.mockImplementation((key) => {
if (key === 'services') {
return undefined
} else if (key === 'project.workspace.details.services') {
test('pass services config codes from legacy service config key to generator-aio-app', async () => {
config.get.mockImplementation(c => {
if (c === 'project.org.details.services') {
// supported services
return [{ code: 'CampaignSDK' }, { code: 'AdobeAnalyticsSDK' }, { code: 'AnotherOneSDK' }]
} else if (c === 'services') {
// added to workspace
return [{ code: 'CampaignSDK' }, { code: 'AdobeAnalyticsSDK' }]
}
return undefined
})

await TheCommand.run([])
Expand All @@ -142,7 +159,8 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false,
'skip-install': false,
'adobe-services': 'CampaignSDK,AdobeAnalyticsSDK'
'adobe-services': 'CampaignSDK,AdobeAnalyticsSDK',
'supported-adobe-services': 'CampaignSDK,AdobeAnalyticsSDK,AnotherOneSDK'
})
})
})
Loading

0 comments on commit 3b74468

Please sign in to comment.