Skip to content

Commit

Permalink
Merge pull request #226 from sasjs/add-function-listSasFilesInFolder
Browse files Browse the repository at this point in the history
feat: add a new method listSasFilesInFolder
  • Loading branch information
allanbowe authored Jan 4, 2023
2 parents a833a53 + be2d7d9 commit 95bbcaf
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 9 deletions.
46 changes: 37 additions & 9 deletions src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ export async function listIniFilesInFolder(folderName: string) {
name.endsWith('.ini')
)
}
/**
* This function returns a list of all SAS files in a folder
*
* @param folderName a string that contains the folder path
* @param recurse (optional) a boolean that identifies the searching of sas files in nested folders recursively
* @param ignoredFolders (optional) a string array that contains the folders to be ignored in recursive search
* @returns a string array containing sas files paths relatived to folderName
*/
export async function listSasFilesInFolder(
folderName: string,
recurse: boolean = false,
ignoredFolders: string[] = []
) {
const filesAndFolders = await listFilesAndSubFoldersInFolder(
folderName,
recurse,
ignoredFolders
)
const sasFiles = filesAndFolders.filter((f) => f.endsWith('.sas'))
return sasFiles
}

export async function listSubFoldersInFolder(
folderName: string
Expand All @@ -58,7 +79,8 @@ export async function listSubFoldersInFolder(

export async function listFilesAndSubFoldersInFolder(
folderName: string,
recurse: boolean = true
recurse: boolean = true,
ignoredFolders: string[] = []
): Promise<string[]> {
return fs.promises
.readdir(folderName, { withFileTypes: true })
Expand All @@ -73,14 +95,20 @@ export async function listFilesAndSubFoldersInFolder(
list.filter((f) => f.isDirectory()),
async (f) => {
const subFolder = f.name
const subPath = path.join(folderName, subFolder)

subFoldersFilesAndFolders = [
...subFoldersFilesAndFolders,
...(await listFilesAndSubFoldersInFolder(subPath)).map((f) =>
path.join(subFolder, f)
)
]
if (!ignoredFolders.includes(subFolder)) {
const subPath = path.join(folderName, subFolder)

subFoldersFilesAndFolders = [
...subFoldersFilesAndFolders,
...(
await listFilesAndSubFoldersInFolder(
subPath,
recurse,
ignoredFolders
)
).map((f) => path.join(subFolder, f))
]
}
}
)

Expand Down
1 change: 1 addition & 0 deletions src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
readFileBinary,
listFilesInFolder,
listIniFilesInFolder,
listSasFilesInFolder,
listSubFoldersInFolder,
listFilesAndSubFoldersInFolder,
createFile,
Expand Down
78 changes: 78 additions & 0 deletions src/file/spec/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getRelativePath,
createFolder,
listFilesInFolder,
listSasFilesInFolder,
listIniFilesInFolder,
listSubFoldersInFolder,
listFilesAndSubFoldersInFolder,
Expand Down Expand Up @@ -257,6 +258,83 @@ describe('getRelativePath', () => {
})
})

describe('listSasFilesInFolder', () => {
const timestamp = new Date().valueOf()

const fileName = `test-create-file-${timestamp}.sas`
const subFolderFileName = `test-create-sub-file-${timestamp}.sas`
const subSubFolderFileName = `test-create-sub-sub-file-${timestamp}.sas`

const testFolderName = `test-create-folder-${timestamp}`
const subFolderName = `test-create-sub-folder-${timestamp}`
const subSubFolderName = `test-create-sub-sub-folder-${timestamp}`

const createTestFoldersAndFiles = async () => {
const testFolderPath = path.join(__dirname, testFolderName)
await createFolder(testFolderPath)

const testSubFolderPath = path.join(testFolderPath, subFolderName)
await createFolder(testSubFolderPath)

const testSubSubFolderPath = path.join(testSubFolderPath, subSubFolderName)
await createFolder(testSubSubFolderPath)

await createFile(path.join(testFolderPath, fileName), content)
await createFile(path.join(testSubFolderPath, subFolderFileName), content)
await createFile(
path.join(testSubSubFolderPath, subSubFolderFileName),
content
)

return testFolderPath
}

it('should return a list of sas files present at all levels', async () => {
const testFolderPath = await createTestFoldersAndFiles()

const expectedResult = [
fileName,
path.join(subFolderName, subFolderFileName),
path.join(subFolderName, subSubFolderName, subSubFolderFileName)
]

await expect(listSasFilesInFolder(testFolderPath, true)).resolves.toEqual(
expectedResult
)

await deleteFolder(testFolderPath)
})

it('should return a list of sas files in current folder only', async () => {
const testFolderPath = await createTestFoldersAndFiles()

const expectedResult = [fileName]

await expect(listSasFilesInFolder(testFolderPath, false)).resolves.toEqual(
expectedResult
)

await deleteFolder(testFolderPath)
})

it('should return a list of sas files present at all levels except a specific folder', async () => {
const testFolderPath = await createTestFoldersAndFiles()

const expectedResult = [
fileName,
path.join(subFolderName, subFolderFileName)
]

const ignoredFolders = [subSubFolderName]

await expect(
listSasFilesInFolder(testFolderPath, true, ignoredFolders)
).resolves.toEqual(expectedResult)

await deleteFolder(testFolderPath)
})
})

describe('listFilesAndSubFoldersInFolder', () => {
const timestamp = new Date().valueOf()

Expand Down

0 comments on commit 95bbcaf

Please sign in to comment.