From 281a145beffab01ed16f0a679b853aaf04daa2a9 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Fri, 23 Jul 2021 22:24:41 +0100 Subject: [PATCH] fix(node): only create and write file stream if running in node --- src/api/viya/getFileStream.ts | 16 +++++++++++++ src/api/viya/pollJobState.ts | 44 +++++++++++++---------------------- 2 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 src/api/viya/getFileStream.ts diff --git a/src/api/viya/getFileStream.ts b/src/api/viya/getFileStream.ts new file mode 100644 index 0000000..620ddc0 --- /dev/null +++ b/src/api/viya/getFileStream.ts @@ -0,0 +1,16 @@ +import { isFolder } from '@sasjs/utils/file' +import { generateTimestamp } from '@sasjs/utils/time' +import { Job } from '../../types' + +export const getFileStream = async (job: Job, filePath?: string) => { + const { createWriteStream } = require('@sasjs/utils/file') + const logPath = filePath || process.cwd() + const isFolderPath = await isFolder(logPath) + if (isFolderPath) { + const logFileName = `${job.name || 'job'}-${generateTimestamp()}.log` + const logFilePath = `${filePath || process.cwd()}/${logFileName}` + return await createWriteStream(logFilePath) + } else { + return await createWriteStream(logPath) + } +} diff --git a/src/api/viya/pollJobState.ts b/src/api/viya/pollJobState.ts index c69546e..c4b05d0 100644 --- a/src/api/viya/pollJobState.ts +++ b/src/api/viya/pollJobState.ts @@ -3,11 +3,8 @@ import { Job, PollOptions } from '../..' import { getTokens } from '../../auth/getTokens' import { RequestClient } from '../../request/RequestClient' import { JobStatePollError } from '../../types/errors' -import { generateTimestamp } from '@sasjs/utils/time' -import { saveLog } from './saveLog' -import { createWriteStream, isFolder } from '@sasjs/utils/file' -import { WriteStream } from 'fs' -import { Link } from '../../types' +import { Link, WriteStream } from '../../types' +import { isNode } from '../../utils' export async function pollJobState( requestClient: RequestClient, @@ -55,21 +52,9 @@ export async function pollJobState( } let logFileStream - if (pollOptions.streamLog) { - const logPath = pollOptions?.logFolderPath || process.cwd() - const isFolderPath = await isFolder(logPath) - if (isFolderPath) { - const logFileName = `${ - postedJob.name || 'job' - }-${generateTimestamp()}.log` - const logFilePath = `${ - pollOptions?.logFolderPath || process.cwd() - }/${logFileName}` - - logFileStream = await createWriteStream(logFilePath) - } else { - logFileStream = await createWriteStream(logPath) - } + if (pollOptions.streamLog && isNode()) { + const { getFileStream } = require('./getFileStream') + logFileStream = await getFileStream(postedJob, pollOptions.logFolderPath) } // Poll up to the first 100 times with the specified poll interval @@ -230,14 +215,17 @@ const doPoll = async ( const endLogLine = job.logStatistics?.lineCount ?? 1000000 - await saveLog( - postedJob, - requestClient, - startLogLine, - endLogLine, - logStream, - authConfig?.access_token - ) + const { saveLog } = isNode() ? require('./saveLog') : { saveLog: null } + if (saveLog) { + await saveLog( + postedJob, + requestClient, + startLogLine, + endLogLine, + logStream, + authConfig?.access_token + ) + } startLogLine += endLogLine }