1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-10 13:50:05 +00:00

Merge pull request #485 from sasjs/log-file-paths

fix(stream-log): use filepath if provided
This commit is contained in:
Krishna Acondy
2021-07-22 09:57:31 +01:00
committed by GitHub
5 changed files with 70 additions and 45 deletions

View File

@@ -13,7 +13,7 @@ jobs:
strategy: strategy:
matrix: matrix:
node-version: [12.x] node-version: [15.x]
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2

58
package-lock.json generated
View File

@@ -7,7 +7,7 @@
"name": "@sasjs/adapter", "name": "@sasjs/adapter",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@sasjs/utils": "^2.25.4", "@sasjs/utils": "^2.27.1",
"axios": "^0.21.1", "axios": "^0.21.1",
"axios-cookiejar-support": "^1.0.1", "axios-cookiejar-support": "^1.0.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
@@ -41,6 +41,9 @@
"typescript": "^4.3.5", "typescript": "^4.3.5",
"webpack": "^5.44.0", "webpack": "^5.44.0",
"webpack-cli": "^4.7.2" "webpack-cli": "^4.7.2"
},
"engines": {
"node": ">=15"
} }
}, },
"node_modules/@babel/code-frame": { "node_modules/@babel/code-frame": {
@@ -1187,9 +1190,9 @@
} }
}, },
"node_modules/@sasjs/utils": { "node_modules/@sasjs/utils": {
"version": "2.25.4", "version": "2.27.1",
"resolved": "https://registry.npmjs.org/@sasjs/utils/-/utils-2.25.4.tgz", "resolved": "https://registry.npmjs.org/@sasjs/utils/-/utils-2.27.1.tgz",
"integrity": "sha512-LTWExtHp4g3VcLLCUMyeeyTXEAZawSQngmJ3/2Z93ysxpeu2/NS7lGG/ERGCQb2snbqmXK8dkZmfg44Tn4Qebw==", "integrity": "sha512-CYTQwEj89cc7H3tGiQQcyDkZYaWRc1HZJpOF8o2RHYS37fIAOy0SyyJdq6mcQ74Nb1u5AmFXPFIvnRCMEcTYeQ==",
"dependencies": { "dependencies": {
"@types/fs-extra": "^9.0.11", "@types/fs-extra": "^9.0.11",
"@types/prompts": "^2.0.13", "@types/prompts": "^2.0.13",
@@ -1199,7 +1202,11 @@
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
"jwt-decode": "^3.1.2", "jwt-decode": "^3.1.2",
"prompts": "^2.4.1", "prompts": "^2.4.1",
"rimraf": "^3.0.2",
"valid-url": "^1.0.9" "valid-url": "^1.0.9"
},
"engines": {
"node": ">=15"
} }
}, },
"node_modules/@semantic-release/commit-analyzer": { "node_modules/@semantic-release/commit-analyzer": {
@@ -2157,7 +2164,6 @@
}, },
"node_modules/balanced-match": { "node_modules/balanced-match": {
"version": "1.0.2", "version": "1.0.2",
"dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/base": { "node_modules/base": {
@@ -2259,7 +2265,6 @@
}, },
"node_modules/brace-expansion": { "node_modules/brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"dev": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
@@ -2741,7 +2746,6 @@
}, },
"node_modules/concat-map": { "node_modules/concat-map": {
"version": "0.0.1", "version": "0.0.1",
"dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/consola": { "node_modules/consola": {
@@ -4113,7 +4117,6 @@
}, },
"node_modules/fs.realpath": { "node_modules/fs.realpath": {
"version": "1.0.0", "version": "1.0.0",
"dev": true,
"license": "ISC" "license": "ISC"
}, },
"node_modules/fsevents": { "node_modules/fsevents": {
@@ -4243,7 +4246,6 @@
}, },
"node_modules/glob": { "node_modules/glob": {
"version": "7.1.7", "version": "7.1.7",
"dev": true,
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"fs.realpath": "^1.0.0", "fs.realpath": "^1.0.0",
@@ -4657,7 +4659,6 @@
}, },
"node_modules/inflight": { "node_modules/inflight": {
"version": "1.0.6", "version": "1.0.6",
"dev": true,
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"once": "^1.3.0", "once": "^1.3.0",
@@ -4666,7 +4667,6 @@
}, },
"node_modules/inherits": { "node_modules/inherits": {
"version": "2.0.4", "version": "2.0.4",
"dev": true,
"license": "ISC" "license": "ISC"
}, },
"node_modules/ini": { "node_modules/ini": {
@@ -7610,7 +7610,6 @@
}, },
"node_modules/minimatch": { "node_modules/minimatch": {
"version": "3.0.4", "version": "3.0.4",
"dev": true,
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
@@ -11007,7 +11006,6 @@
}, },
"node_modules/once": { "node_modules/once": {
"version": "1.4.0", "version": "1.4.0",
"dev": true,
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"wrappy": "1" "wrappy": "1"
@@ -11242,7 +11240,6 @@
}, },
"node_modules/path-is-absolute": { "node_modules/path-is-absolute": {
"version": "1.0.1", "version": "1.0.1",
"dev": true,
"license": "MIT", "license": "MIT",
"engines": { "engines": {
"node": ">=0.10.0" "node": ">=0.10.0"
@@ -11776,7 +11773,6 @@
}, },
"node_modules/rimraf": { "node_modules/rimraf": {
"version": "3.0.2", "version": "3.0.2",
"dev": true,
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"glob": "^7.1.3" "glob": "^7.1.3"
@@ -13853,7 +13849,6 @@
}, },
"node_modules/wrappy": { "node_modules/wrappy": {
"version": "1.0.2", "version": "1.0.2",
"dev": true,
"license": "ISC" "license": "ISC"
}, },
"node_modules/write-file-atomic": { "node_modules/write-file-atomic": {
@@ -14791,9 +14786,9 @@
} }
}, },
"@sasjs/utils": { "@sasjs/utils": {
"version": "2.25.4", "version": "2.27.1",
"resolved": "https://registry.npmjs.org/@sasjs/utils/-/utils-2.25.4.tgz", "resolved": "https://registry.npmjs.org/@sasjs/utils/-/utils-2.27.1.tgz",
"integrity": "sha512-LTWExtHp4g3VcLLCUMyeeyTXEAZawSQngmJ3/2Z93ysxpeu2/NS7lGG/ERGCQb2snbqmXK8dkZmfg44Tn4Qebw==", "integrity": "sha512-CYTQwEj89cc7H3tGiQQcyDkZYaWRc1HZJpOF8o2RHYS37fIAOy0SyyJdq6mcQ74Nb1u5AmFXPFIvnRCMEcTYeQ==",
"requires": { "requires": {
"@types/fs-extra": "^9.0.11", "@types/fs-extra": "^9.0.11",
"@types/prompts": "^2.0.13", "@types/prompts": "^2.0.13",
@@ -14803,6 +14798,7 @@
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
"jwt-decode": "^3.1.2", "jwt-decode": "^3.1.2",
"prompts": "^2.4.1", "prompts": "^2.4.1",
"rimraf": "^3.0.2",
"valid-url": "^1.0.9" "valid-url": "^1.0.9"
} }
}, },
@@ -15504,8 +15500,7 @@
} }
}, },
"balanced-match": { "balanced-match": {
"version": "1.0.2", "version": "1.0.2"
"dev": true
}, },
"base": { "base": {
"version": "0.11.2", "version": "0.11.2",
@@ -15570,7 +15565,6 @@
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"dev": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@@ -15892,8 +15886,7 @@
"dev": true "dev": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1"
"dev": true
}, },
"consola": { "consola": {
"version": "2.15.3" "version": "2.15.3"
@@ -16846,8 +16839,7 @@
} }
}, },
"fs.realpath": { "fs.realpath": {
"version": "1.0.0", "version": "1.0.0"
"dev": true
}, },
"fsevents": { "fsevents": {
"version": "2.3.2", "version": "2.3.2",
@@ -16938,7 +16930,6 @@
}, },
"glob": { "glob": {
"version": "7.1.7", "version": "7.1.7",
"dev": true,
"requires": { "requires": {
"fs.realpath": "^1.0.0", "fs.realpath": "^1.0.0",
"inflight": "^1.0.4", "inflight": "^1.0.4",
@@ -17186,15 +17177,13 @@
}, },
"inflight": { "inflight": {
"version": "1.0.6", "version": "1.0.6",
"dev": true,
"requires": { "requires": {
"once": "^1.3.0", "once": "^1.3.0",
"wrappy": "1" "wrappy": "1"
} }
}, },
"inherits": { "inherits": {
"version": "2.0.4", "version": "2.0.4"
"dev": true
}, },
"ini": { "ini": {
"version": "1.3.8", "version": "1.3.8",
@@ -19219,7 +19208,6 @@
}, },
"minimatch": { "minimatch": {
"version": "3.0.4", "version": "3.0.4",
"dev": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
@@ -21534,7 +21522,6 @@
}, },
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"dev": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@@ -21688,8 +21675,7 @@
"dev": true "dev": true
}, },
"path-is-absolute": { "path-is-absolute": {
"version": "1.0.1", "version": "1.0.1"
"dev": true
}, },
"path-key": { "path-key": {
"version": "3.1.1", "version": "3.1.1",
@@ -22020,7 +22006,6 @@
}, },
"rimraf": { "rimraf": {
"version": "3.0.2", "version": "3.0.2",
"dev": true,
"requires": { "requires": {
"glob": "^7.1.3" "glob": "^7.1.3"
} }
@@ -23403,8 +23388,7 @@
} }
}, },
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2"
"dev": true
}, },
"write-file-atomic": { "write-file-atomic": {
"version": "3.0.3", "version": "3.0.3",

View File

@@ -67,11 +67,14 @@
}, },
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"@sasjs/utils": "^2.25.4", "@sasjs/utils": "^2.27.1",
"axios": "^0.21.1", "axios": "^0.21.1",
"axios-cookiejar-support": "^1.0.1", "axios-cookiejar-support": "^1.0.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"https": "^1.0.0", "https": "^1.0.0",
"tough-cookie": "^4.0.0" "tough-cookie": "^4.0.0"
},
"engines": {
"node": ">=15"
} }
} }

View File

@@ -5,7 +5,7 @@ import { RequestClient } from '../../request/RequestClient'
import { JobStatePollError } from '../../types/errors' import { JobStatePollError } from '../../types/errors'
import { generateTimestamp } from '@sasjs/utils/time' import { generateTimestamp } from '@sasjs/utils/time'
import { saveLog } from './saveLog' import { saveLog } from './saveLog'
import { createWriteStream } from '@sasjs/utils/file' import { createWriteStream, isFolder } from '@sasjs/utils/file'
import { WriteStream } from 'fs' import { WriteStream } from 'fs'
import { Link } from '../../types' import { Link } from '../../types'
@@ -53,12 +53,20 @@ export async function pollJobState(
let logFileStream let logFileStream
if (pollOptions?.streamLog) { if (pollOptions?.streamLog) {
const logFileName = `${postedJob.name || 'job'}-${generateTimestamp()}.log` const logPath = pollOptions?.logFolderPath || process.cwd()
const logFilePath = `${ const isFolderPath = await isFolder(logPath)
pollOptions?.logFolderPath || process.cwd() if (isFolderPath) {
}/${logFileName}` const logFileName = `${
postedJob.name || 'job'
}-${generateTimestamp()}.log`
const logFilePath = `${
pollOptions?.logFolderPath || process.cwd()
}/${logFileName}`
logFileStream = await createWriteStream(logFilePath) logFileStream = await createWriteStream(logFilePath)
} else {
logFileStream = await createWriteStream(logPath)
}
} }
let result = await doPoll( let result = await doPoll(

View File

@@ -1,4 +1,5 @@
import { Logger, LogLevel } from '@sasjs/utils' import { Logger, LogLevel } from '@sasjs/utils'
import * as path from 'path'
import * as fileModule from '@sasjs/utils/file' import * as fileModule from '@sasjs/utils/file'
import { RequestClient } from '../../../request/RequestClient' import { RequestClient } from '../../../request/RequestClient'
import { mockAuthConfig, mockJob } from './mockResponses' import { mockAuthConfig, mockJob } from './mockResponses'
@@ -85,6 +86,35 @@ describe('pollJobState', () => {
expect(saveLogModule.saveLog).toHaveBeenCalledTimes(2) expect(saveLogModule.saveLog).toHaveBeenCalledTimes(2)
}) })
it('should use the given log path if it points to a file', async () => {
mockSimplePoll()
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
...defaultPollOptions,
streamLog: true,
logFolderPath: path.join(__dirname, 'test.log')
})
expect(fileModule.createWriteStream).toHaveBeenCalledWith(
path.join(__dirname, 'test.log')
)
})
it('should generate a log file path with a timestamp if it points to a folder', async () => {
mockSimplePoll()
await pollJobState(requestClient, mockJob, false, mockAuthConfig, {
...defaultPollOptions,
streamLog: true,
logFolderPath: path.join(__dirname)
})
expect(fileModule.createWriteStream).not.toHaveBeenCalledWith(__dirname)
expect(fileModule.createWriteStream).toHaveBeenCalledWith(
expect.stringContaining(__dirname + '/test job-20')
)
})
it('should not attempt to fetch and save the log after each poll when streamLog is false', async () => { it('should not attempt to fetch and save the log after each poll when streamLog is false', async () => {
mockSimplePoll() mockSimplePoll()