Skip to content

Commit

Permalink
fix(add): shouldn't add unnecessary attributes if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
saadjutt01 committed Apr 21, 2021
1 parent e231424 commit 9490fa6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/commands/add/addCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const addCredential = async (

if (!target.serverUrl) {
const serverUrl = await getAndValidateServerUrl(target)
target = new Target({ ...target.toJson(), serverUrl })
target = new Target({ ...target.toJsonNoDefaults(), serverUrl })
}

const { client, secret } = await getCredentialsInput(target.name)
Expand All @@ -58,13 +58,13 @@ export const addCredential = async (
access_token,
refresh_token
)
await saveToLocalConfig(target, false)
await saveToLocalConfig(target, false, true)
} else {
target = new Target({
...target.toJson(),
...target.toJsonNoDefaults(),
authConfig: { client, secret, access_token, refresh_token }
})
await saveToGlobalConfig(target, false)
await saveToGlobalConfig(target, false, true)
}
}

Expand Down
33 changes: 27 additions & 6 deletions src/commands/add/addTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export async function addTarget(insecure: boolean = false): Promise<boolean> {
existingTarget
} = await getCommonFields()

const saveWithoutDefaultValues = !!existingTarget

let targetJson: any = {
...existingTarget,
name,
Expand All @@ -37,7 +39,12 @@ export async function addTarget(insecure: boolean = false): Promise<boolean> {
appLoc
}

let filePath = await saveConfig(scope, new Target(targetJson), false)
let filePath = await saveConfig(
scope,
new Target(targetJson),
false,
saveWithoutDefaultValues
)

process.logger?.info(`Target configuration has been saved to ${filePath} .`)

Expand Down Expand Up @@ -66,12 +73,17 @@ export async function addTarget(insecure: boolean = false): Promise<boolean> {
}

const { target: currentTarget } = await findTargetInConfiguration(name)
targetJson = { ...currentTarget.toJson(), ...targetJson }
targetJson = { ...currentTarget.toJsonNoDefaults(), ...targetJson }
}

const isDefault = await getIsDefault()

filePath = await saveConfig(scope, new Target(targetJson), isDefault)
filePath = await saveConfig(
scope,
new Target(targetJson),
isDefault,
saveWithoutDefaultValues
)

process.logger?.info(`Target configuration has been saved to ${filePath}`)

Expand All @@ -81,14 +93,23 @@ export async function addTarget(insecure: boolean = false): Promise<boolean> {
async function saveConfig(
scope: TargetScope,
target: Target,
isDefault: boolean
isDefault: boolean,
saveWithoutDefaultValues: boolean
) {
let filePath = ''

if (scope === TargetScope.Local) {
filePath = await saveToLocalConfig(target, isDefault)
filePath = await saveToLocalConfig(
target,
isDefault,
saveWithoutDefaultValues
)
} else if (scope === TargetScope.Global) {
filePath = await saveToGlobalConfig(target, isDefault)
filePath = await saveToGlobalConfig(
target,
isDefault,
saveWithoutDefaultValues
)
}

return filePath
Expand Down
5 changes: 4 additions & 1 deletion src/commands/add/internal/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ export async function getAndValidateSasViyaFields(
process.env.ACCESS_TOKEN as string
)
} else {
const { target } = await findTargetInConfiguration(targetName)
const { target } = await findTargetInConfiguration(
targetName,
TargetScope.Global
)
if (!target.authConfig || !target.authConfig.access_token) {
throw new Error(
`No access token available for target ${target.name}. Please run \`sasjs add cred -t ${targetName}\` to authenticate.`
Expand Down
14 changes: 10 additions & 4 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,13 @@ export async function saveGlobalRcFile(content: string) {

export async function saveToGlobalConfig(
target: Target,
isDefault: boolean = false
isDefault: boolean = false,
saveWithoutDefaultValues: boolean = false
) {
let globalConfig = await getGlobalRcFile()
const targetJson = target.toJson()
const targetJson = saveWithoutDefaultValues
? target.toJsonNoDefaults()
: target.toJson()
if (globalConfig) {
if (globalConfig.targets && globalConfig.targets.length) {
const existingTargetIndex = globalConfig.targets.findIndex(
Expand Down Expand Up @@ -329,9 +332,12 @@ export async function saveLocalConfigFile(content: string) {

export async function saveToLocalConfig(
target: Target,
isDefault: boolean = false
isDefault: boolean = false,
saveWithoutDefaultValues: boolean = false
) {
const targetJson = target.toJson()
const targetJson = saveWithoutDefaultValues
? target.toJsonNoDefaults()
: target.toJson()
let config = await getLocalConfig()
if (config) {
if (config.targets && config.targets.length) {
Expand Down

0 comments on commit 9490fa6

Please sign in to comment.