Skip to content

Commit

Permalink
fix(add): code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
saadjutt01 committed Apr 21, 2021
1 parent 9490fa6 commit 60e1128
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 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.toJsonNoDefaults(), serverUrl })
target = new Target({ ...target.toJson(false), 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, true)
await saveToLocalConfig(target, false, false)
} else {
target = new Target({
...target.toJsonNoDefaults(),
...target.toJson(false),
authConfig: { client, secret, access_token, refresh_token }
})
await saveToGlobalConfig(target, false, true)
await saveToGlobalConfig(target, false, false)
}
}

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

const saveWithoutDefaultValues = !!existingTarget
const saveWithDefaultValues = !existingTarget

let targetJson: any = {
...existingTarget,
Expand All @@ -43,7 +43,7 @@ export async function addTarget(insecure: boolean = false): Promise<boolean> {
scope,
new Target(targetJson),
false,
saveWithoutDefaultValues
saveWithDefaultValues
)

process.logger?.info(`Target configuration has been saved to ${filePath} .`)
Expand Down Expand Up @@ -73,7 +73,7 @@ export async function addTarget(insecure: boolean = false): Promise<boolean> {
}

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

const isDefault = await getIsDefault()
Expand All @@ -82,7 +82,7 @@ export async function addTarget(insecure: boolean = false): Promise<boolean> {
scope,
new Target(targetJson),
isDefault,
saveWithoutDefaultValues
saveWithDefaultValues
)

process.logger?.info(`Target configuration has been saved to ${filePath}`)
Expand All @@ -94,21 +94,17 @@ async function saveConfig(
scope: TargetScope,
target: Target,
isDefault: boolean,
saveWithoutDefaultValues: boolean
saveWithDefaultValues: boolean
) {
let filePath = ''

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

Expand Down
12 changes: 4 additions & 8 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,10 @@ export async function saveGlobalRcFile(content: string) {
export async function saveToGlobalConfig(
target: Target,
isDefault: boolean = false,
saveWithoutDefaultValues: boolean = false
saveWithDefaultValues: boolean = false
) {
let globalConfig = await getGlobalRcFile()
const targetJson = saveWithoutDefaultValues
? target.toJsonNoDefaults()
: target.toJson()
const targetJson = target.toJson(saveWithDefaultValues)
if (globalConfig) {
if (globalConfig.targets && globalConfig.targets.length) {
const existingTargetIndex = globalConfig.targets.findIndex(
Expand Down Expand Up @@ -333,11 +331,9 @@ export async function saveLocalConfigFile(content: string) {
export async function saveToLocalConfig(
target: Target,
isDefault: boolean = false,
saveWithoutDefaultValues: boolean = false
saveWithDefaultValues: boolean = true
) {
const targetJson = saveWithoutDefaultValues
? target.toJsonNoDefaults()
: target.toJson()
const targetJson = target.toJson(saveWithDefaultValues)
let config = await getLocalConfig()
if (config) {
if (config.targets && config.targets.length) {
Expand Down

0 comments on commit 60e1128

Please sign in to comment.