diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index d195921..c509f87 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -16,3 +16,7 @@ What code changes have been made to achieve the intent. - [ ] All unit tests are passing (`npm test`). - [ ] All `sasjs-cli` unit tests are passing (`npm test`). - [ ] Reviewer is assigned. + +### Reviewer checks + +- [ ] Any new code is documented. diff --git a/src/file/file.ts b/src/file/file.ts index 95b5857..b3a7da0 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -3,6 +3,7 @@ import rimraf from 'rimraf' import path from 'path' import { asyncForEach } from '../utils' import * as file from '.' +import { LineEndings } from '../types' export async function fileExists(filePath: string): Promise { return fs.promises @@ -268,3 +269,11 @@ export const createReadStream = async (filePath: string) => export const testFileRegExp = /\.test\.(\d+\.)?sas$/i export const isTestFile = (fileName: string) => testFileRegExp.test(fileName) + +/** + * Returns end of line sequence + * @param content to get end of line sequence. + * @returns CRLF(\r\n) or LF(\n) end of line sequence + */ +export const getLineEnding = (content: string) => + new RegExp(LineEndings.CRLF).test(content) ? LineEndings.CRLF : LineEndings.LF diff --git a/src/file/index.ts b/src/file/index.ts index 78c64d0..18436d0 100644 --- a/src/file/index.ts +++ b/src/file/index.ts @@ -24,7 +24,8 @@ export { base64EncodeFile, getRealPath, isTestFile, - testFileRegExp + testFileRegExp, + getLineEnding } from './file' export { updateCsv, createCsv, readCsv } from './csvFile' diff --git a/src/file/spec/file.spec.ts b/src/file/spec/file.spec.ts index d1b6b4e..f0a9805 100644 --- a/src/file/spec/file.spec.ts +++ b/src/file/spec/file.spec.ts @@ -22,12 +22,14 @@ import { base64EncodeImageFile, getRealPath, createWriteStream, - createReadStream + createReadStream, + getLineEnding } from '../file' import * as fileModule from '../file' import { generateTimestamp } from '../../time' import { svgBase64EncodedUnix, svgBase64EncodedWin } from './expectedOutputs' import { isWindows } from '../../utils' +import { LineEndings } from '../../types' const content = 'test content' @@ -500,7 +502,7 @@ describe('createReadStream', () => { jest.mock('../file') jest .spyOn(fs, 'createReadStream') - .mockImplementation(() => ({} as unknown as ReadStream)) + .mockImplementation(() => ({}) as unknown as ReadStream) jest .spyOn(fileModule, 'createFile') .mockImplementation(() => Promise.resolve()) @@ -525,7 +527,7 @@ describe('createWriteStream', () => { jest.mock('../file') jest .spyOn(fs, 'createWriteStream') - .mockImplementation(() => ({} as unknown as WriteStream)) + .mockImplementation(() => ({}) as unknown as WriteStream) jest .spyOn(fileModule, 'createFile') .mockImplementation(() => Promise.resolve()) @@ -606,3 +608,17 @@ describe('deleteFolder', () => { expect(isFolderPresent).toBeFalsy() }) }) + +describe('getLineEnding', () => { + it('should return correct line ending', () => { + let lineEnding = LineEndings.CRLF + let line: LineEndings = lineEnding + + expect(getLineEnding(line)).toEqual(lineEnding) + + lineEnding = LineEndings.LF + line = lineEnding + + expect(getLineEnding(line)).toEqual(lineEnding) + }) +}) diff --git a/src/types/file.ts b/src/types/file.ts new file mode 100644 index 0000000..10f4c31 --- /dev/null +++ b/src/types/file.ts @@ -0,0 +1,4 @@ +export enum LineEndings { + CRLF = `\r\n`, + LF = `\n` +} diff --git a/src/types/index.ts b/src/types/index.ts index 4c322c6..09c1ac6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -12,3 +12,4 @@ export * from './serverError' export * from './serverType' export * from './servicePack' export * from './target' +export * from './file'