Skip to content

Commit

Permalink
Open command (#177)
Browse files Browse the repository at this point in the history
* Add open command to reference project in console ui
* added tests
* allow optional?.chaining
* linting - 100%
  • Loading branch information
purplecabbage authored Dec 15, 2022
1 parent 71ef88b commit 06cd602
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": ["@adobe/eslint-config-aio-lib-config"],
"parserOptions": {
"ecmaVersion": 2020
},
"settings": {
"jsdoc": {
"ignorePrivate": true
Expand Down
23 changes: 12 additions & 11 deletions src/commands/console/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,24 @@ class ConsoleCommand extends Command {
* @returns {*} config data
*/
getConfig (key) {
return config.get(`${CONFIG_KEYS.CONSOLE}.${key}`)
if (key) {
return config.get(`${CONFIG_KEYS.CONSOLE}.${key}`)
} else {
return config.get(CONFIG_KEYS.CONSOLE)
}
}

/**
* Clear console config
*
* @param {string} key key to clear
*/
clearConfigKey (key) {
config.delete(`${CONFIG_KEYS.CONSOLE}.${key}`)
}

/**
* Clear console config
* @param {string} key key to store value
*/
clearConfig () {
config.delete(CONFIG_KEYS.CONSOLE)
clearConfig (key) {
if (key) {
config.delete(`${CONFIG_KEYS.CONSOLE}.${key}`)
} else {
config.delete(CONFIG_KEYS.CONSOLE)
}
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/commands/console/open.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-console:open', { provider: 'debug' })
const { getCliEnv } = require('@adobe/aio-lib-env')
const { CliUx } = require('@oclif/core')
const { OPEN_URLS } = require('../../config')

const ConsoleCommand = require('./index')
class OpenCommand extends ConsoleCommand {
async run () {
// TODO: we could support --orgId and --projectId flags to open the console for a specific org/project
// currently we open the console for the local config org/project
// const { flags } = await this.parse(OpenCommand)

aioLogger.debug('Inquiring currently selected Org, Project and Workspace')
const cliEnv = getCliEnv()
const config = this.getConfig()
let url = OPEN_URLS[cliEnv]
if (config?.project?.id && config?.project?.org_id) {
url += `/${config.project.org_id}/${config.project.id}/`
if (config.workspace && config.workspace.id) {
url += `workspaces/${config.workspace.id}/details`
} else {
url += 'overview'
}
}
aioLogger.debug(`opening url ${url}`)
CliUx.ux.open(url)
}
}

OpenCommand.description = 'Open the developer console for the selected Organization, Project and Workspace'

OpenCommand.flags = {
...ConsoleCommand.flags
}

OpenCommand.aliases = [
'open'
]

module.exports = OpenCommand
4 changes: 2 additions & 2 deletions src/commands/console/org/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class SelectCommand extends ConsoleCommand {
aioConsoleLogger.debug('Setting console Org in config')

this.setConfig(CONFIG_KEYS.ORG, org)
this.clearConfigKey(CONFIG_KEYS.PROJECT)
this.clearConfigKey(CONFIG_KEYS.WORKSPACE)
this.clearConfig(CONFIG_KEYS.PROJECT)
this.clearConfig(CONFIG_KEYS.WORKSPACE)

this.log(`Org selected ${org.name}`)

Expand Down
2 changes: 1 addition & 1 deletion src/commands/console/project/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SelectCommand extends ConsoleCommand {
const project = await this.selectProjectInteractive(orgId, args.projectIdOrName)

this.setConfig(CONFIG_KEYS.PROJECT, project)
this.clearConfigKey(CONFIG_KEYS.WORKSPACE)
this.clearConfig(CONFIG_KEYS.WORKSPACE)

this.log(`Project selected ${project.name}`)

Expand Down
8 changes: 7 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ const CONFIG_KEYS = {
WORKSPACE: 'workspace'
}

const OPEN_URLS = {
prod: 'https://developer.adobe.com/console/projects',
stage: 'https://developer-stage.adobe.com/console/projects'
}

module.exports = {
ORG_TYPE_ENTERPRISE,
CONFIG_KEYS,
API_KEYS
API_KEYS,
OPEN_URLS
}
6 changes: 6 additions & 0 deletions test/commands/console/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ describe('ConsoleCommand', () => {
expect(config.get).toBeCalledWith(`${CONFIG_KEYS.CONSOLE}.test`)
})

test('getConfig - nokey', () => {
const command = new ConsoleCommand([])
command.getConfig()
expect(config.get).toBeCalledWith(`${CONFIG_KEYS.CONSOLE}`)
})

test('clearConfig', () => {
const command = new ConsoleCommand([])
command.clearConfig()
Expand Down
110 changes: 110 additions & 0 deletions test/commands/console/open.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const TestCommand = require('../../../src/commands/console/open')
const config = require('@adobe/aio-lib-core-config')
const { STAGE_ENV } = require('@adobe/aio-lib-env')

jest.mock('@oclif/core', () => {
return {
...jest.requireActual('@oclif/core'),
CliUx: {
ux: {
open: jest.fn()
}
}
}
})
const { Command, CliUx: { ux: cli } } = require('@oclif/core')

let command
let ORIGINAL_AIO_CLI_ENV
beforeAll(() => {
ORIGINAL_AIO_CLI_ENV = process.env.AIO_CLI_ENV
})
beforeEach(() => {
config.get.mockReset()
command = new TestCommand([])
})
afterEach(() => {
process.env.AIO_CLI_ENV = ORIGINAL_AIO_CLI_ENV
})

test('exports', async () => {
expect(typeof TestCommand).toEqual('function')
expect(TestCommand.prototype instanceof Command).toBeTruthy()
})

test('description', async () => {
expect(TestCommand.description).toBeDefined()
})

test('aliases', async () => {
expect(TestCommand.aliases).toBeDefined()
expect(TestCommand.aliases).toBeInstanceOf(Array)
expect(TestCommand.aliases.length).toBeGreaterThan(0)
})

test('flags', async () => {
expect(TestCommand.flags.help.type).toBe('boolean')
})

describe('console:open', () => {
test('exists', async () => {
expect(command.run).toBeInstanceOf(Function)
})

test('should open a browser', async () => {
await expect(command.run()).resolves.not.toThrowError()
expect(cli.open).toHaveBeenCalledWith('https://developer.adobe.com/console/projects')
})

test('should open a browser (stage_env)', async () => {
process.env.AIO_CLI_ENV = STAGE_ENV
await expect(command.run()).resolves.not.toThrowError()
expect(cli.open).toHaveBeenCalledWith('https://developer-stage.adobe.com/console/projects')
})

test('should open a browser with default view if no project/workspace selected', async () => {
config.get.mockReturnValue(null)
await expect(command.run()).resolves.not.toThrowError()
expect(cli.open).toHaveBeenLastCalledWith('https://developer.adobe.com/console/projects')
})

test('should open a browser with project overview', async () => {
config.get.mockReturnValue({
project: {
name: 'ghActionDeploy',
id: '4566206088344853970',
title: 'Deploy with Github actions',
description: 'see title ...',
org_id: '53444'
}
})
await expect(command.run()).resolves.not.toThrowError()
expect(cli.open).toHaveBeenLastCalledWith('https://developer.adobe.com/console/projects/53444/4566206088344853970/overview')
})

test('should open a browser with project workspace', async () => {
config.get.mockReturnValue({
project: {
name: 'ghActionDeploy',
id: '4566206088344853970',
title: 'Deploy with Github actions',
description: 'see title ...',
org_id: '53444'
},
workspace: { id: '4566206088344859372', name: 'Stage' }
})
await expect(command.run()).resolves.not.toThrowError()
expect(cli.open).toHaveBeenLastCalledWith('https://developer.adobe.com/console/projects/53444/4566206088344853970/workspaces/4566206088344859372/details')
})
})

0 comments on commit 06cd602

Please sign in to comment.