Skip to content

Commit 580a6a3

Browse files
authored
Merge pull request #1376 from sasjs/job-command-improvements
Job command improvements
2 parents 39adb7f + f45a2fc commit 580a6a3

File tree

15 files changed

+86
-171
lines changed

15 files changed

+86
-171
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = {
4646
statements: 73.51,
4747
branches: 60.57,
4848
functions: 73.56,
49-
lines: 74.17
49+
lines: 74.12
5050
}
5151
},
5252

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"access": "public"
5252
},
5353
"dependencies": {
54-
"@sasjs/adapter": "4.10.0",
54+
"@sasjs/adapter": "4.10.1",
5555
"@sasjs/core": "4.46.3",
5656
"@sasjs/lint": "2.3.1",
5757
"@sasjs/utils": "3.4.0",

src/commands/add/addCredential.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from 'path'
2-
import { LogLevel } from '@sasjs/utils/logger'
32
import { ServerType, Target, HttpsAgentOptions } from '@sasjs/utils/types'
4-
5-
import SASjs from '@sasjs/adapter/node'
6-
import { getNewAccessToken } from '../../utils/auth'
7-
import { isSasJsServerInServerMode } from '../../utils'
3+
import {
4+
getNewAccessToken,
5+
isSasJsServerInServerMode,
6+
getSASjs
7+
} from '../../utils'
88
import { createFile } from '@sasjs/utils'
99
import {
1010
getAndValidateServerUrl,
@@ -54,8 +54,7 @@ export const addCredential = async (
5454
const { access_token, refresh_token } = await getTokens(
5555
target,
5656
client,
57-
secret,
58-
httpsAgentOptions
57+
secret
5958
).catch((err) => {
6059
throw err
6160
})
@@ -108,8 +107,7 @@ export const addCredential = async (
108107
const { access_token, refresh_token } = await getTokens(
109108
target,
110109
client,
111-
'',
112-
httpsAgentOptions
110+
''
113111
)
114112

115113
if (targetScope === TargetScope.Local) {
@@ -159,15 +157,9 @@ export const validateTargetName = (targetName: string): string => {
159157
export const getTokens = async (
160158
target: Target,
161159
client: string,
162-
secret: string,
163-
httpsAgentOptions?: HttpsAgentOptions
160+
secret: string
164161
) => {
165-
const adapter = new SASjs({
166-
serverUrl: target.serverUrl,
167-
serverType: target.serverType,
168-
httpsAgentOptions,
169-
debug: process.logger?.logLevel === LogLevel.Debug
170-
})
162+
const adapter = getSASjs(target)
171163
const { access_token, refresh_token } = await getNewAccessToken(
172164
adapter,
173165
client,
@@ -189,7 +181,9 @@ export const createEnvFileForViya = async (
189181
): Promise<void> => {
190182
const envFileContent = `CLIENT=${client}\nSECRET=${secret}\nACCESS_TOKEN=${accessToken}\nREFRESH_TOKEN=${refreshToken}\n`
191183
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)
184+
192185
await createFile(envFilePath, envFileContent)
186+
193187
process.logger?.success(`Environment file saved at ${envFilePath}`)
194188
}
195189

@@ -200,7 +194,9 @@ export const createEnvFileForSas9 = async (
200194
): Promise<void> => {
201195
const envFileContent = `SAS_USERNAME=${userName}\nSAS_PASSWORD=${password}\n`
202196
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)
197+
203198
await createFile(envFilePath, envFileContent)
199+
204200
process.logger?.success(`Environment file saved at ${envFilePath}`)
205201
}
206202

@@ -212,6 +208,8 @@ export const createEnvFileForSasjs = async (
212208
): Promise<void> => {
213209
const envFileContent = `CLIENT=${client}\nACCESS_TOKEN=${accessToken}\nREFRESH_TOKEN=${refreshToken}\n`
214210
const envFilePath = path.join(process.projectDir, `.env.${targetName}`)
211+
215212
await createFile(envFilePath, envFileContent)
213+
216214
process.logger?.success(`Environment file saved at ${envFilePath}`)
217215
}

src/commands/add/internal/input.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import {
55
getUrl
66
} from '@sasjs/utils/input'
77
import { Target, TargetJson, ServerType } from '@sasjs/utils/types'
8-
import { LogLevel } from '@sasjs/utils/logger'
98
import { encodeToBase64 } from '@sasjs/utils'
109
import path from 'path'
1110
import dotenv from 'dotenv'
12-
import SASjs from '@sasjs/adapter/node'
1311
import { TargetScope } from '../../../types/targetScope'
1412
import { CommonFields } from '../../../types/commonFields'
15-
import { findTargetInConfiguration } from '../../../utils/config'
13+
import { findTargetInConfiguration, getSASjs } from '../../../utils'
1614

1715
export async function getCommonFields(): Promise<CommonFields> {
1816
const scope = await getAndValidateScope()
@@ -207,12 +205,7 @@ export async function getAndValidateSasViyaFields(
207205
}
208206
: target.httpsAgentOptions
209207

210-
const sasjs = new SASjs({
211-
serverUrl,
212-
serverType: ServerType.SasViya,
213-
httpsAgentOptions,
214-
debug: process.logger?.logLevel === LogLevel.Debug
215-
})
208+
const sasjs = getSASjs(target)
216209
let contexts: any[] = []
217210
if (scope === TargetScope.Local) {
218211
dotenv.config({

src/commands/context/contextCommand.ts

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import SASjs from '@sasjs/adapter/node'
21
import { readFile, ServerType } from '@sasjs/utils'
32
import { CommandExample, ReturnCode } from '../../types/command'
43
import { TargetCommand } from '../../types/command/targetCommand'
5-
import { getAuthConfig } from '../../utils'
4+
import { getAuthConfig, getSASjs } from '../../utils'
65
import { create } from './create'
76
import { deleteContext } from './delete'
87
import { edit } from './edit'
@@ -145,12 +144,7 @@ export class ContextCommand extends TargetCommand {
145144

146145
async executeCreateContext() {
147146
const { target } = await this.getTargetInfo()
148-
const sasjs = new SASjs({
149-
serverUrl: target.serverUrl,
150-
httpsAgentOptions: target.httpsAgentOptions,
151-
appLoc: target.appLoc,
152-
serverType: target.serverType
153-
})
147+
const sasjs = getSASjs(target)
154148
const config = await this.getConfig().catch((err) => {
155149
process.logger?.error(
156150
`Unable to create context. Error fetching context configuration from ${this.parsed.source}: `,
@@ -186,12 +180,7 @@ export class ContextCommand extends TargetCommand {
186180

187181
async executeDeleteContext() {
188182
const { target } = await this.getTargetInfo()
189-
const sasjs = new SASjs({
190-
serverUrl: target.serverUrl,
191-
httpsAgentOptions: target.httpsAgentOptions,
192-
appLoc: target.appLoc,
193-
serverType: target.serverType
194-
})
183+
const sasjs = getSASjs(target)
195184

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

222211
async executeEditContext() {
223212
const { target } = await this.getTargetInfo()
224-
const sasjs = new SASjs({
225-
serverUrl: target.serverUrl,
226-
httpsAgentOptions: target.httpsAgentOptions,
227-
appLoc: target.appLoc,
228-
serverType: target.serverType
229-
})
213+
214+
const sasjs = getSASjs(target)
230215
const config = await this.getConfig().catch((err) => {
231216
process.logger?.error(
232217
`Unable to edit context. Error fetching context configuration from ${this.parsed.source}: `,
233218
err
234219
)
220+
235221
return null
236222
})
237-
if (!config) {
238-
return ReturnCode.InternalError
239-
}
223+
224+
if (!config) return ReturnCode.InternalError
240225

241226
const authConfig = await getAuthConfig(target).catch((err) => {
242227
process.logger?.error(
243228
'Unable to edit context. Error fetching auth config: ',
244229
err
245230
)
231+
246232
return null
247233
})
248-
if (!authConfig) {
249-
return ReturnCode.InternalError
250-
}
234+
235+
if (!authConfig) return ReturnCode.InternalError
251236

252237
const returnCode = await edit(
253238
this.parsed.contextName as string,
254239
config,
255240
sasjs,
256241
authConfig.access_token
257242
)
258-
.then(() => {
259-
return ReturnCode.Success
260-
})
261-
.catch(() => {
262-
return ReturnCode.InternalError
263-
})
243+
.then(() => ReturnCode.Success)
244+
.catch(() => ReturnCode.InternalError)
264245

265246
return returnCode
266247
}
267248

268249
async executeExportContext() {
269250
const { target } = await this.getTargetInfo()
270-
const sasjs = new SASjs({
271-
serverUrl: target.serverUrl,
272-
httpsAgentOptions: target.httpsAgentOptions,
273-
appLoc: target.appLoc,
274-
serverType: target.serverType
275-
})
251+
const sasjs = getSASjs(target)
276252

277253
const authConfig = await getAuthConfig(target).catch((err) => {
278254
process.logger?.error(
279255
'Unable to create context. Error fetching auth config: ',
280256
err
281257
)
258+
282259
return null
283260
})
284-
if (!authConfig) {
285-
return ReturnCode.InternalError
286-
}
261+
262+
if (!authConfig) return ReturnCode.InternalError
287263

288264
const returnCode = await exportContext(
289265
this.parsed.contextName as string,
290266
sasjs,
291267
authConfig.access_token
292268
)
293-
.then(() => {
294-
return ReturnCode.Success
295-
})
296-
.catch(() => {
297-
return ReturnCode.InternalError
298-
})
269+
.then(() => ReturnCode.Success)
270+
.catch(() => ReturnCode.InternalError)
299271

300272
return returnCode
301273
}
302274

303275
async executeListContext() {
304276
const { target } = await this.getTargetInfo()
277+
305278
if (target.serverType !== ServerType.SasViya) {
306279
process.logger?.error(
307280
`'context list' command is only supported for SAS Viya build targets.\nPlease try again with a different target.`
308281
)
282+
309283
return ReturnCode.InternalError
310284
}
311285

312-
const sasjs = new SASjs({
313-
serverUrl: target.serverUrl,
314-
httpsAgentOptions: target.httpsAgentOptions,
315-
appLoc: target.appLoc,
316-
serverType: target.serverType
317-
})
286+
const sasjs = getSASjs(target)
318287

319288
const authConfig = await getAuthConfig(target).catch((err) => {
320289
process.logger?.error(
321290
'Unable to create context. Error fetching auth config: ',
322291
err
323292
)
293+
324294
return null
325295
})
326-
if (!authConfig) {
327-
return ReturnCode.InternalError
328-
}
296+
297+
if (!authConfig) return ReturnCode.InternalError
329298

330299
const returnCode = await list(target, sasjs, authConfig)
331-
.then(() => {
332-
return ReturnCode.Success
333-
})
334-
.catch(() => {
335-
return ReturnCode.InternalError
336-
})
300+
.then(() => ReturnCode.Success)
301+
.catch(() => ReturnCode.InternalError)
337302

338303
return returnCode
339304
}

src/commands/flow/flowCommand.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AuthConfig, Target } from '@sasjs/utils'
33
import SASjs from '@sasjs/adapter/node'
44
import { CommandExample, ReturnCode } from '../../types/command'
55
import { TargetCommand } from '../../types/command/targetCommand'
6-
import { displayError, getAuthConfig } from '../../utils'
6+
import { displayError, getAuthConfig, getSASjs } from '../../utils'
77
import { execute } from './execute'
88

99
enum FlowSubCommand {
@@ -67,13 +67,7 @@ export class FlowCommand extends TargetCommand {
6767

6868
public async execute() {
6969
const { target } = await this.getTargetInfo()
70-
71-
const sasjs = new SASjs({
72-
serverUrl: target.serverUrl,
73-
httpsAgentOptions: target.httpsAgentOptions,
74-
appLoc: target.appLoc,
75-
serverType: target.serverType
76-
})
70+
const sasjs = getSASjs(target)
7771

7872
const authConfig = await getAuthConfig(target as Target).catch((err) => {
7973
displayError(err, 'Error while getting access token.')

0 commit comments

Comments
 (0)