Skip to content

Commit

Permalink
Merge pull request #325 from sasjs/select-target
Browse files Browse the repository at this point in the history
fix: select target logic fixed
  • Loading branch information
allanbowe authored Apr 26, 2022
2 parents d82809f + f96321f commit 85e9fe2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/commands/execute-code/ExecuteCodeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { getEditorContent } from '../../utils/editor'
import { createFile } from '../../utils/file'
import { getAuthConfig, getAuthConfigSas9 } from './internal/configuration'
import { selectTarget } from '../../utils/selectTarget'
import { selectTarget } from '../../utils/target'
import { getTimestamp } from './internal/utils'

export class ExecuteCodeCommand {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/select-target/selectTargetCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { window, ExtensionContext, commands, OutputChannel } from 'vscode'
import { selectTarget } from '../../utils/selectTarget'
import { configureTarget } from '../../utils/target'

export class SelectTargetCommand {
private outputChannel: OutputChannel
Expand All @@ -17,7 +17,7 @@ export class SelectTargetCommand {
}

private selectTarget = async () => {
selectTarget(this.outputChannel).then(() => {
configureTarget(this.outputChannel).then(() => {
window.showInformationMessage('Target Selected!')
})
}
Expand Down
42 changes: 42 additions & 0 deletions src/utils/selectTarget.ts → src/utils/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { getChoiceInput, getIsDefault } from './input'
import { getGlobalConfiguration } from './config'
import { createTarget } from './createTarget'

/**
* This function will be called from execute command for selecting target before execution
*/
export const selectTarget = async (outputChannel: OutputChannel) => {
const config = (await getGlobalConfiguration(outputChannel)) as Configuration

Expand Down Expand Up @@ -52,3 +55,42 @@ export const selectTarget = async (outputChannel: OutputChannel) => {
return await createTarget(outputChannel)
}
}

/**
* This function will be called from select target command for configuration of setting
*/
export const configureTarget = async (outputChannel: OutputChannel) => {
const config = (await getGlobalConfiguration(outputChannel)) as Configuration

if (config?.targets?.length) {
const extConfig = workspace.getConfiguration('sasjs-for-vscode')
const targetNames = (config?.targets || []).map((t: any) => t.name)
const targetName = await getChoiceInput(
[...targetNames, 'add and select new target'],
'Please select a target'
)
if (targetName === 'add and select new target') {
const target = await createTarget(outputChannel)
const isDefault = await getIsDefault()
if (isDefault) {
await extConfig.update('target', target.name, true)
}
return target
} else if (!!targetName) {
const isDefault = await getIsDefault()
if (isDefault) {
await extConfig.update('target', targetName, true)
}
const selectedTarget = config.targets.find(
(t: any) => t.name === targetName
)
return new Target(selectedTarget)
}
} else {
const response = await getChoiceInput(
['Yes', 'No'],
'No targets are found. Would you like to create a new target?'
)
if (response === 'Yes') return await createTarget(outputChannel)
}
}

0 comments on commit 85e9fe2

Please sign in to comment.