11import { window , ExtensionContext , commands } from 'vscode'
22import { 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'
58import { executeCode } from '../../utils/executeCode'
6- import { updateSasjsConstants } from '../../utils/setConstants'
79import { 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
918export 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}
0 commit comments