Skip to content

Commit

Permalink
fix add action command, fix auto merge in init (#137)
Browse files Browse the repository at this point in the history
* fixes done, need tests

* add tests
  • Loading branch information
moritzraho authored and purplecabbage committed Jan 16, 2020
1 parent 1b8ef64 commit 3d7b203
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/commands/app/add/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ const yeoman = require('yeoman-environment')
const debug = require('debug')('aio-cli-plugin-app:init')
const { flags } = require('@oclif/command')

const config = require('@adobe/aio-lib-core-config')

class AddActionCommand extends BaseCommand {
async run () {
const { args, flags } = this.parse(AddActionCommand)

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

const services = (config.get('services') || []).map(s => s.code).join(',')

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': 'target,analytics,campaign-standard' // todo update with real sdk codes from console later
'adobe-services': services
})
return res
}
Expand Down
14 changes: 10 additions & 4 deletions src/commands/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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 { importConfigJson, loadConfigFile } = require('../../lib/import')
const { importConfigJson, loadConfigFile, writeAio } = require('../../lib/import')

class InitCommand extends BaseCommand {
async run () {
Expand Down Expand Up @@ -52,11 +52,17 @@ class InitCommand extends BaseCommand {
})

// config import
// todo do also when fetching from console
// always auto merge
const interactive = false
const merge = true
if (flags.import) {
const interactive = !flags.yes
const merge = true
return importConfigJson(flags.import, process.cwd(), { interactive, merge })
} else {
// write default services value to .aio
// todo use real imported values from console
writeAio({
services: services.split(',').map(code => ({ code }))
}, process.cwd(), { merge, interactive })
}

// finalize configuration data
Expand Down
27 changes: 23 additions & 4 deletions test/commands/app/add/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const fs = require('fs-extra')
const TheCommand = require('../../../../src/commands/app/add/action')
const BaseCommand = require('../../../../src/BaseCommand')

const config = require('@adobe/aio-lib-core-config')
jest.mock('@adobe/aio-lib-core-config')

jest.mock('fs-extra')

jest.mock('yeoman-environment')
Expand All @@ -31,6 +34,7 @@ beforeEach(() => {
mockRun.mockReset()
yeoman.createEnv.mockClear()
fs.ensureDirSync.mockClear()
config.get.mockReset()
})

describe('Command Prototype', () => {
Expand Down Expand Up @@ -64,7 +68,7 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': true,
'skip-install': false,
'adobe-services': 'target,analytics,campaign-standard'
'adobe-services': ''
})
})

Expand All @@ -77,7 +81,7 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': true,
'skip-install': true,
'adobe-services': 'target,analytics,campaign-standard'
'adobe-services': ''
})
})

Expand All @@ -90,7 +94,7 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false,
'skip-install': true,
'adobe-services': 'target,analytics,campaign-standard'
'adobe-services': ''
})
})
test('no flags', async () => {
Expand All @@ -102,7 +106,22 @@ describe('good flags', () => {
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false,
'skip-install': false,
'adobe-services': 'target,analytics,campaign-standard'
'adobe-services': ''
})
})

test('pass services config codes to generator-aio-app', async () => {
config.get.mockReturnValue([{ code: 'CampaignSDK' }, { code: 'AdobeAnalyticsSDK' }])

await TheCommand.run([])

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(1)
const genName = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false,
'skip-install': false,
'adobe-services': 'CampaignSDK,AdobeAnalyticsSDK'
})
})
})
28 changes: 26 additions & 2 deletions test/commands/app/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ beforeEach(() => {
mockRun.mockReset()
yeoman.createEnv.mockClear()
fs.ensureDirSync.mockClear()
importLib.importConfigJson.mockReset()
importLib.writeAio.mockReset()
})

describe('Command Prototype', () => {
Expand Down Expand Up @@ -191,6 +193,28 @@ describe('run', () => {
})
})

test('no imports should write aio config', async () => {
await TheCommand.run([])

expect(fs.ensureDirSync).not.toHaveBeenCalled()
expect(spyChdir).not.toHaveBeenCalled()

expect(yeoman.createEnv).toHaveBeenCalled()
expect(mockRegister).toHaveBeenCalledTimes(1)
const genName = mockRegister.mock.calls[0][1]
expect(mockRun).toHaveBeenCalledWith(genName, {
'skip-prompt': false,
'skip-install': false,
'project-name': 'yolo',
'adobe-services': 'AdobeTargetSDK,AdobeAnalyticsSDK,CampaignSDK'
})
expect(importLib.writeAio).toHaveBeenCalledWith(
{ services: [{ code: 'AdobeTargetSDK' }, { code: 'AdobeAnalyticsSDK' }, { code: 'CampaignSDK' }] },
process.cwd(),
{ interactive: false, merge: true }
)
})

test('no-path --import file={name: yolo, services:AdobeTargetSDK,CampaignSDK}', async () => {
// mock config file
importLib.loadConfigFile.mockReturnValueOnce({
Expand All @@ -215,7 +239,7 @@ describe('run', () => {
'adobe-services': 'AdobeTargetSDK,CampaignSDK'
})

expect(importLib.importConfigJson).toHaveBeenCalledWith('config.json', process.cwd(), { interactive: true, merge: true })
expect(importLib.importConfigJson).toHaveBeenCalledWith('config.json', process.cwd(), { interactive: false, merge: true })
})

test('no-path --yes --import file={name: yolo, services:AdobeTargetSDK,CampaignSDK}', async () => {
Expand Down Expand Up @@ -268,6 +292,6 @@ describe('run', () => {
'adobe-services': ''
})

expect(importLib.importConfigJson).toHaveBeenCalledWith('config.json', process.cwd(), { interactive: true, merge: true })
expect(importLib.importConfigJson).toHaveBeenCalledWith('config.json', process.cwd(), { interactive: false, merge: true })
})
})

0 comments on commit 3d7b203

Please sign in to comment.