Skip to content

Commit

Permalink
refactor(sasjs-adapter): used getSASjs utility to get instance of ada…
Browse files Browse the repository at this point in the history
…pter
  • Loading branch information
YuryShkoda committed Sep 15, 2023
1 parent 56d9126 commit 65fd688
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 156 deletions.
32 changes: 15 additions & 17 deletions src/commands/add/addCredential.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'path'
import { LogLevel } from '@sasjs/utils/logger'
import { ServerType, Target, HttpsAgentOptions } from '@sasjs/utils/types'

import SASjs from '@sasjs/adapter/node'
import { getNewAccessToken } from '../../utils/auth'
import { isSasJsServerInServerMode } from '../../utils'
import {
getNewAccessToken,
isSasJsServerInServerMode,
getSASjs
} from '../../utils'
import { createFile } from '@sasjs/utils'
import {
getAndValidateServerUrl,
Expand Down Expand Up @@ -54,8 +54,7 @@ export const addCredential = async (
const { access_token, refresh_token } = await getTokens(
target,
client,
secret,
httpsAgentOptions
secret
).catch((err) => {
throw err
})
Expand Down Expand Up @@ -108,8 +107,7 @@ export const addCredential = async (
const { access_token, refresh_token } = await getTokens(
target,
client,
'',
httpsAgentOptions
''
)

if (targetScope === TargetScope.Local) {
Expand Down Expand Up @@ -159,15 +157,9 @@ export const validateTargetName = (targetName: string): string => {
export const getTokens = async (
target: Target,
client: string,
secret: string,
httpsAgentOptions?: HttpsAgentOptions
secret: string
) => {
const adapter = new SASjs({
serverUrl: target.serverUrl,
serverType: target.serverType,
httpsAgentOptions,
debug: process.logger?.logLevel === LogLevel.Debug
})
const adapter = getSASjs(target)
const { access_token, refresh_token } = await getNewAccessToken(
adapter,
client,
Expand All @@ -189,7 +181,9 @@ export const createEnvFileForViya = async (
): Promise<void> => {
const envFileContent = `CLIENT=${client}\nSECRET=${secret}\nACCESS_TOKEN=${accessToken}\nREFRESH_TOKEN=${refreshToken}\n`
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)

await createFile(envFilePath, envFileContent)

process.logger?.success(`Environment file saved at ${envFilePath}`)
}

Expand All @@ -200,7 +194,9 @@ export const createEnvFileForSas9 = async (
): Promise<void> => {
const envFileContent = `SAS_USERNAME=${userName}\nSAS_PASSWORD=${password}\n`
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)

await createFile(envFilePath, envFileContent)

process.logger?.success(`Environment file saved at ${envFilePath}`)
}

Expand All @@ -212,6 +208,8 @@ export const createEnvFileForSasjs = async (
): Promise<void> => {
const envFileContent = `CLIENT=${client}\nACCESS_TOKEN=${accessToken}\nREFRESH_TOKEN=${refreshToken}\n`
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)

await createFile(envFilePath, envFileContent)

process.logger?.success(`Environment file saved at ${envFilePath}`)
}
11 changes: 2 additions & 9 deletions src/commands/add/internal/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import {
getUrl
} from '@sasjs/utils/input'
import { Target, TargetJson, ServerType } from '@sasjs/utils/types'
import { LogLevel } from '@sasjs/utils/logger'
import { encodeToBase64 } from '@sasjs/utils'
import path from 'path'
import dotenv from 'dotenv'
import SASjs from '@sasjs/adapter/node'
import { TargetScope } from '../../../types/targetScope'
import { CommonFields } from '../../../types/commonFields'
import { findTargetInConfiguration } from '../../../utils/config'
import { findTargetInConfiguration, getSASjs } from '../../../utils'

export async function getCommonFields(): Promise<CommonFields> {
const scope = await getAndValidateScope()
Expand Down Expand Up @@ -207,12 +205,7 @@ export async function getAndValidateSasViyaFields(
}
: target.httpsAgentOptions

const sasjs = new SASjs({
serverUrl,
serverType: ServerType.SasViya,
httpsAgentOptions,
debug: process.logger?.logLevel === LogLevel.Debug
})
const sasjs = getSASjs(target)
let contexts: any[] = []
if (scope === TargetScope.Local) {
dotenv.config({
Expand Down
89 changes: 27 additions & 62 deletions src/commands/context/contextCommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import SASjs from '@sasjs/adapter/node'
import { readFile, ServerType } from '@sasjs/utils'
import { CommandExample, ReturnCode } from '../../types/command'
import { TargetCommand } from '../../types/command/targetCommand'
import { getAuthConfig } from '../../utils'
import { getAuthConfig, getSASjs } from '../../utils'
import { create } from './create'
import { deleteContext } from './delete'
import { edit } from './edit'
Expand Down Expand Up @@ -145,12 +144,7 @@ export class ContextCommand extends TargetCommand {

async executeCreateContext() {
const { target } = await this.getTargetInfo()
const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)
const config = await this.getConfig().catch((err) => {
process.logger?.error(
`Unable to create context. Error fetching context configuration from ${this.parsed.source}: `,
Expand Down Expand Up @@ -186,12 +180,7 @@ export class ContextCommand extends TargetCommand {

async executeDeleteContext() {
const { target } = await this.getTargetInfo()
const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
Expand Down Expand Up @@ -221,119 +210,95 @@ export class ContextCommand extends TargetCommand {

async executeEditContext() {
const { target } = await this.getTargetInfo()
const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})

const sasjs = getSASjs(target)
const config = await this.getConfig().catch((err) => {
process.logger?.error(
`Unable to edit context. Error fetching context configuration from ${this.parsed.source}: `,
err
)

return null
})
if (!config) {
return ReturnCode.InternalError
}

if (!config) return ReturnCode.InternalError

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
'Unable to edit context. Error fetching auth config: ',
err
)

return null
})
if (!authConfig) {
return ReturnCode.InternalError
}

if (!authConfig) return ReturnCode.InternalError

const returnCode = await edit(
this.parsed.contextName as string,
config,
sasjs,
authConfig.access_token
)
.then(() => {
return ReturnCode.Success
})
.catch(() => {
return ReturnCode.InternalError
})
.then(() => ReturnCode.Success)
.catch(() => ReturnCode.InternalError)

return returnCode
}

async executeExportContext() {
const { target } = await this.getTargetInfo()
const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
'Unable to create context. Error fetching auth config: ',
err
)

return null
})
if (!authConfig) {
return ReturnCode.InternalError
}

if (!authConfig) return ReturnCode.InternalError

const returnCode = await exportContext(
this.parsed.contextName as string,
sasjs,
authConfig.access_token
)
.then(() => {
return ReturnCode.Success
})
.catch(() => {
return ReturnCode.InternalError
})
.then(() => ReturnCode.Success)
.catch(() => ReturnCode.InternalError)

return returnCode
}

async executeListContext() {
const { target } = await this.getTargetInfo()

if (target.serverType !== ServerType.SasViya) {
process.logger?.error(
`'context list' command is only supported for SAS Viya build targets.\nPlease try again with a different target.`
)

return ReturnCode.InternalError
}

const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
'Unable to create context. Error fetching auth config: ',
err
)

return null
})
if (!authConfig) {
return ReturnCode.InternalError
}

if (!authConfig) return ReturnCode.InternalError

const returnCode = await list(target, sasjs, authConfig)
.then(() => {
return ReturnCode.Success
})
.catch(() => {
return ReturnCode.InternalError
})
.then(() => ReturnCode.Success)
.catch(() => ReturnCode.InternalError)

return returnCode
}
Expand Down
10 changes: 2 additions & 8 deletions src/commands/flow/flowCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AuthConfig, Target } from '@sasjs/utils'
import SASjs from '@sasjs/adapter/node'
import { CommandExample, ReturnCode } from '../../types/command'
import { TargetCommand } from '../../types/command/targetCommand'
import { displayError, getAuthConfig } from '../../utils'
import { displayError, getAuthConfig, getSASjs } from '../../utils'
import { execute } from './execute'

enum FlowSubCommand {
Expand Down Expand Up @@ -67,13 +67,7 @@ export class FlowCommand extends TargetCommand {

public async execute() {
const { target } = await this.getTargetInfo()

const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target as Target).catch((err) => {
displayError(err, 'Error while getting access token.')
Expand Down
16 changes: 3 additions & 13 deletions src/commands/folder/folderCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SASjs from '@sasjs/adapter/node'
import { AuthConfig, ServerType, Target } from '@sasjs/utils'
import { CommandExample, ReturnCode } from '../../types/command'
import { TargetCommand } from '../../types/command/targetCommand'
import { getAuthConfig } from '../../utils'
import { getAuthConfig, getSASjs } from '../../utils'
import { prefixAppLoc } from '../../utils/prefixAppLoc'
import { create } from './create'
import { deleteFolder } from './delete'
Expand Down Expand Up @@ -74,12 +74,7 @@ export class FolderCommand extends TargetCommand {
return ReturnCode.InternalError
}

const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
Expand Down Expand Up @@ -185,12 +180,7 @@ export class FolderCommand extends TargetCommand {
return ReturnCode.InternalError
}

const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType
})
const sasjs = getSASjs(target)

const authConfig = await getAuthConfig(target).catch((err) => {
process.logger?.error(
Expand Down
20 changes: 8 additions & 12 deletions src/commands/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import {
getAbsolutePath,
generateTimestamp
} from '@sasjs/utils'
import { displayError, displaySuccess } from '../../utils/displayResult'
import { AuthConfig, ServerType, Target } from '@sasjs/utils/types'
import { isSASjsProject } from '../../utils/utils'
import { saveLog } from '../../utils/saveLog'
import {
isSASjsProject,
saveLog,
displayError,
displaySuccess,
getSASjs
} from '../../utils'

export async function runSasJob(
target: Target,
Expand Down Expand Up @@ -80,15 +84,7 @@ export async function runSasJob(
configJson.password = decodeFromBase64(configJson.password)
}

const sasjs = new SASjs({
serverUrl: target.serverUrl,
httpsAgentOptions: target.httpsAgentOptions,
appLoc: target.appLoc,
serverType: target.serverType,
contextName: target.contextName,
useComputeApi: false,
debug: true
})
const sasjs = getSASjs(target)

let result

Expand Down
Loading

0 comments on commit 65fd688

Please sign in to comment.