Skip to content

Commit 3da456e

Browse files
authored
Merge pull request #409 from sasjs/print-support
feat(execute): save webout, log, print and code after completion
2 parents 96cb0a3 + dde7aee commit 3da456e

File tree

6 files changed

+70
-38
lines changed

6 files changed

+70
-38
lines changed

package-lock.json

Lines changed: 19 additions & 17 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
@@ -250,7 +250,7 @@
250250
"vscode-test": "^1.6.1"
251251
},
252252
"dependencies": {
253-
"@sasjs/adapter": "4.1.2",
253+
"@sasjs/adapter": "4.3.3",
254254
"@sasjs/cli": "4.1.1",
255255
"@sasjs/lint": "2.3.1",
256256
"@sasjs/utils": "3.2.0",

src/commands/execute-code/ExecuteCodeCommand.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { window, ExtensionContext, commands } from 'vscode'
22
import { getEditorContent } from '../../utils/editor'
3-
import { selectTarget } from '../../utils/target'
4-
import { createAndOpenLogFile, handleErrorResponse } from '../../utils/utils'
3+
import {
4+
createAndOpenLogFile,
5+
handleErrorResponse,
6+
getTimestamp
7+
} from '../../utils/utils'
58
import { executeCode } from '../../utils/executeCode'
6-
import { updateSasjsConstants } from '../../utils/setConstants'
79
import { TargetCommand } from '../../types/commands/targetCommand'
10+
import { ScriptExecutionResult } from '@sasjs/adapter'
11+
import { createFile, createFolder } from '../../utils/file'
12+
import * as path from 'path'
13+
14+
interface ExecutionArtifacts extends ScriptExecutionResult {
15+
code?: string
16+
}
817

918
export class ExecuteCodeCommand extends TargetCommand {
1019
constructor(private context: ExtensionContext) {
@@ -29,17 +38,21 @@ export class ExecuteCodeCommand extends TargetCommand {
2938
}
3039

3140
const execFilePath = window.activeTextEditor?.document.fileName
32-
3341
const sasCodeInjection = `options set=SAS_EXECFILEPATH "${execFilePath}";`
34-
35-
const currentFileContent = `${sasCodeInjection}\n${getEditorContent()}`
42+
const editorContent = getEditorContent()
43+
const currentFileContent = `${sasCodeInjection}\n${editorContent}`
3644

3745
commands.executeCommand('setContext', 'isSasjsCodeExecuting', true)
3846

3947
await executeCode(target, currentFileContent || '')
40-
.then(async ({ log }) => {
48+
.then(async (res) => {
4149
process.outputChannel.appendLine('SASjs: Code executed successfully!')
42-
await handleSuccessResponse(log)
50+
51+
if (typeof res.log === 'object') {
52+
res.log = JSON.stringify(res.log, null, 2)
53+
}
54+
55+
await this.saveExecutionArtifacts({ ...res, code: editorContent })
4356
})
4457
.catch(async (err) => {
4558
await handleErrorResponse(err, 'Error executing code')
@@ -48,14 +61,27 @@ export class ExecuteCodeCommand extends TargetCommand {
4861
commands.executeCommand('setContext', 'isSasjsCodeExecuting', false)
4962
})
5063
}
51-
}
5264

53-
const handleSuccessResponse = async (log: any) => {
54-
if (typeof log === 'object') {
55-
return await createAndOpenLogFile(JSON.stringify(log, null, 2))
56-
}
65+
private saveExecutionArtifacts = async (result: ExecutionArtifacts) => {
66+
const { buildDestinationResultsFolder: resultsFolder } =
67+
process.sasjsConstants
68+
const timestamp = getTimestamp()
69+
const folderPath = path.join(resultsFolder, timestamp)
70+
71+
await createFolder(folderPath)
72+
73+
const { log, webout, printOutput, code } = result
74+
75+
if (webout) {
76+
await createFile(path.join(folderPath, 'webout.txt'), webout)
77+
}
78+
if (printOutput) {
79+
await createFile(path.join(folderPath, 'print.lst'), printOutput)
80+
}
81+
if (code) {
82+
await createFile(path.join(folderPath, 'code.sas'), code)
83+
}
5784

58-
if (typeof log === 'string') {
59-
return await createAndOpenLogFile(log)
85+
await createAndOpenLogFile(log, path.join(folderPath, 'log.log'))
6086
}
6187
}

src/utils/executeCode.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Target, ServerType, decodeFromBase64 } from '@sasjs/utils'
22
import { getAuthConfig, getAuthConfigSas9 } from './config'
33
import { getSASjs } from './getSASjs'
4+
import { ScriptExecutionResult } from '@sasjs/adapter'
45

56
export const executeCode = async (target: Target, code: string) => {
67
if (target.serverType === ServerType.SasViya) {
@@ -55,11 +56,11 @@ const executeOnSasJS = async (target: Target, code: string) => {
5556
const sasjs = getSASjs(target)
5657
const authConfig = await getAuthConfig(target)
5758

58-
const executionResult = await sasjs.executeScript({
59+
const executionResult: ScriptExecutionResult = await sasjs.executeScript({
5960
linesOfCode: code.split('\n'),
6061
runTime: 'sas',
6162
authConfig
6263
})
6364

64-
return { log: executionResult }
65+
return executionResult
6566
}

src/utils/file.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export async function readFile(filePath: string) {
99
.readFile(Uri.file(filePath))
1010
.then((content) => Buffer.from(content).toString('utf8'))
1111
}
12+
13+
export async function createFolder(filePath: string) {
14+
return await workspace.fs.createDirectory(Uri.file(filePath))
15+
}

src/utils/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { window, workspace, ViewColumn } from 'vscode'
55
import {
66
Target,
77
timestampToYYYYMMDDHHMMSS,
8-
fileExists,
98
folderExists,
109
copy
1110
} from '@sasjs/utils'
@@ -103,12 +102,12 @@ export const handleErrorResponse = async (e: any, message: string) => {
103102
}
104103
}
105104

106-
export const createAndOpenLogFile = async (log: string) => {
105+
export const createAndOpenLogFile = async (log: string, filePath?: string) => {
107106
const { buildDestinationResultsFolder: resultsFolder } =
108107
process.sasjsConstants
109108

110109
const timestamp = getTimestamp()
111-
const resultsPath = path.join(resultsFolder, `${timestamp}.log`)
110+
const resultsPath = filePath || path.join(resultsFolder, `${timestamp}.log`)
112111

113112
process.outputChannel.appendLine(
114113
`SASjs: Attempting to create log file at ${resultsPath}.`

0 commit comments

Comments
 (0)