Skip to content

Commit

Permalink
fix(sasjs-add): fix tests and restructure code
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna-acondy committed Dec 8, 2020
1 parent 69b175a commit 890c922
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 252 deletions.
Expand Up @@ -3,13 +3,14 @@ import dotenv from 'dotenv'
import { Logger, LogLevel } from '@sasjs/utils/logger'
import { getString } from '@sasjs/utils/input'
import { SasAuthResponse, Target } from '@sasjs/utils/types'

import SASjs from '@sasjs/adapter/node'
import { getNewAccessToken } from '../../utils/auth-utils'
import {
findTargetInConfiguration,
saveToGlobalConfig
} from '../utils/config-utils'
import { getNewAccessToken } from '../utils/auth-utils'
import SASjs from '@sasjs/adapter/node'
import { createFile } from '../utils/file-utils'
} from '../../utils/config-utils'
import { createFile } from '../../utils/file-utils'

/**
* Creates a .env file for the specified target.
Expand Down
102 changes: 102 additions & 0 deletions src/commands/add/add-target.ts
@@ -0,0 +1,102 @@
import { Logger, LogLevel } from '@sasjs/utils/logger'
import { Target, ServerType } from '@sasjs/utils/types'

import path from 'path'

import {
findTargetInConfiguration,
saveToGlobalConfig
} from '../../utils/config-utils'
import { createFile } from '../../utils/file-utils'
import { TargetScope } from '../../types/TargetScope'
import {
getCommonFields,
getAndValidateSasViyaFields,
getAndValidateSas9Fields
} from './input'
import { getLocalConfig } from './config'

export async function addTarget(): Promise<boolean> {
const logLevel = (process.env.LOG_LEVEL || LogLevel.Error) as LogLevel
const logger = new Logger(logLevel)
const { scope, serverType, name, appLoc, serverUrl } = await getCommonFields()

let target: Partial<Target> | Target = {
name,
serverType: serverType,
serverUrl,
appLoc
}

let filePath = await saveConfig(scope, target as Target)
logger.info(`Target configuration has been saved to ${filePath}.`)

if (serverType === ServerType.Sas9) {
const sas9FieldValues = await getAndValidateSas9Fields()
target = {
...target,
tgtBuildVars: sas9FieldValues,
tgtDeployVars: sas9FieldValues
}
} else {
const { contextName } = await getAndValidateSasViyaFields(
name,
scope,
serverUrl,
logger
)

target = {
...target,
tgtBuildVars: { contextName },
tgtDeployVars: { contextName },
deployServicePack: true,
tgtDeployScripts: []
}

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

filePath = await saveConfig(scope, target as Target)
logger.info(`Target configuration has been saved to ${filePath}.`)
return true
}

async function saveConfig(scope: TargetScope, target: Target) {
let filePath = ''
if (scope === TargetScope.Local) {
filePath = await saveToLocalConfig(target as Target)
} else if (scope === TargetScope.Global) {
filePath = await saveToGlobalConfig(target as Target)
}

return filePath
}

async function saveToLocalConfig(buildTarget: Target) {
const buildSourceFolder = require('../../constants').get().buildSourceFolder
let config = await getLocalConfig()
if (config) {
if (config.targets && config.targets.length) {
const existingTargetIndex = config.targets.findIndex(
(t: Target) => t.name === buildTarget.name
)
if (existingTargetIndex > -1) {
config.targets[existingTargetIndex] = buildTarget
} else {
config.targets.push(buildTarget)
}
} else {
config.targets = [buildTarget]
}
} else {
config = { targets: [buildTarget] }
}

const configPath = path.join(buildSourceFolder, 'sasjsconfig.json')

await createFile(configPath, JSON.stringify(config, null, 2))

return configPath
}
12 changes: 12 additions & 0 deletions src/commands/add/config.ts
@@ -0,0 +1,12 @@
import { create } from '../create'
import path from 'path'
import { getConfiguration } from '../../utils/config-utils'

export async function getLocalConfig() {
const buildSourceFolder = require('../../constants').get().buildSourceFolder
const config = await getConfiguration(
path.join(buildSourceFolder, 'sasjsconfig.json')
)
if (!config) await create('.', 'sasonly')
return config
}
118 changes: 9 additions & 109 deletions src/commands/add-target.ts → src/commands/add/input.ts
@@ -1,98 +1,25 @@
import { create } from './create'
import { Logger, LogLevel } from '@sasjs/utils/logger'
import { Target, ServerType } from '@sasjs/utils/types'
import {
getNumber,
getString,
getConfirmation,
getChoice,
getUrl
} from '@sasjs/utils/input'
import { Target, ServerType } from '@sasjs/utils/types'
import { Logger, LogLevel } from '@sasjs/utils/logger'
import chalk from 'chalk'
import path from 'path'
import dotenv from 'dotenv'
import SASjs from '@sasjs/adapter/node'
import { TargetScope } from '../../types/TargetScope'
import {
getGlobalRcFile,
getConfiguration,
findTargetInConfiguration,
saveToGlobalConfig
} from '../utils/config-utils'
import { createFile } from '../utils/file-utils'
getGlobalRcFile
} from '../../utils/config-utils'
import { addCredential } from './add-credential'
import { getLocalConfig } from './config'

export enum TargetScope {
Global = 'Global',
Local = 'Local'
}

export async function addTarget() {
const logLevel = (process.env.LOG_LEVEL || LogLevel.Error) as LogLevel
const logger = new Logger(logLevel)
const { scope, serverType, name, appLoc, serverUrl } = await getCommonFields()

let target: Partial<Target> | Target = {
name,
serverType: serverType,
serverUrl,
appLoc
}

let filePath = await saveConfig(scope, target as Target)
logger.info(`Target configuration has been saved to ${filePath}.`)

if (serverType === ServerType.Sas9) {
const sas9FieldValues = await getAndValidateSas9Fields()
target = {
...target,
tgtBuildVars: sas9FieldValues,
tgtDeployVars: sas9FieldValues
}
} else {
const { contextName } = await getAndValidateSasViyaFields(
name,
scope,
serverUrl,
logger
)

target = {
...target,
tgtBuildVars: { contextName },
tgtDeployVars: { contextName },
deployServicePack: true,
tgtDeployScripts: []
}

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

filePath = await saveConfig(scope, target as Target)
logger.info(`Target configuration has been saved to ${filePath}.`)
}

async function saveConfig(scope: TargetScope, target: Target) {
let filePath = ''
if (scope === TargetScope.Local) {
filePath = await saveToLocalConfig(target as Target)
} else if (scope === TargetScope.Global) {
filePath = await saveToGlobalConfig(target as Target)
}

return filePath
}

async function getLocalConfig() {
const buildSourceFolder = require('../constants').get().buildSourceFolder
const config = await getConfiguration(
path.join(buildSourceFolder, 'sasjsconfig.json')
)
if (!config) await create('.', 'sasonly')
return config
}

async function getCommonFields() {
export async function getCommonFields() {
const scope = await getAndValidateScope()
const serverType = await getAndValidateServerType()
const name = await getAndValidateTargetName(scope, serverType)
Expand All @@ -104,33 +31,6 @@ async function getCommonFields() {
return { scope, serverType, name, appLoc, serverUrl }
}

async function saveToLocalConfig(buildTarget: Target) {
const buildSourceFolder = require('../constants').get().buildSourceFolder
let config = await getLocalConfig()
if (config) {
if (config.targets && config.targets.length) {
const existingTargetIndex = config.targets.findIndex(
(t: Target) => t.name === buildTarget.name
)
if (existingTargetIndex > -1) {
config.targets[existingTargetIndex] = buildTarget
} else {
config.targets.push(buildTarget)
}
} else {
config.targets = [buildTarget]
}
} else {
config = { targets: [buildTarget] }
}

const configPath = path.join(buildSourceFolder, 'sasjsconfig.json')

await createFile(configPath, JSON.stringify(config, null, 2))

return configPath
}

async function getAndValidateScope(): Promise<TargetScope> {
const { scope } = await getChoice(
'scope',
Expand Down Expand Up @@ -217,7 +117,7 @@ async function getAndValidateTargetName(
return targetName
}

async function getAndValidateSas9Fields() {
export async function getAndValidateSas9Fields() {
const { serverName } = await getString(
'serverName',
'Please enter a server name (default is SASApp): ',
Expand All @@ -234,7 +134,7 @@ async function getAndValidateSas9Fields() {
return { serverName, repositoryName }
}

async function getAndValidateSasViyaFields(
export async function getAndValidateSasViyaFields(
targetName: string,
scope: TargetScope,
serverUrl: string,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/index.js
@@ -1,5 +1,5 @@
export { addTarget } from './add-target'
export { addCredential } from './add-credential'
export { addTarget } from './add/add-target'
export { addCredential } from './add/add-credential'

export {
build,
Expand Down
4 changes: 4 additions & 0 deletions src/types/TargetScope.ts
@@ -0,0 +1,4 @@
export enum TargetScope {
Global = 'Global',
Local = 'Local'
}

0 comments on commit 890c922

Please sign in to comment.