Skip to content

Commit

Permalink
Merge pull request #208 from sasjs/issue-203
Browse files Browse the repository at this point in the history
feat: add utils for sasjs fs command
  • Loading branch information
allanbowe authored Oct 26, 2022
2 parents 4303246 + 4522abb commit 1b632d4
Show file tree
Hide file tree
Showing 32 changed files with 878 additions and 60 deletions.
192 changes: 140 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/sasjs/utils#readme",
"devDependencies": {
"@sasjs/core": "3.10.0",
"@sasjs/core": "4.41.0",
"@types/cli-table": "0.3.0",
"@types/find": "0.2.1",
"@types/jest": "27.4.0",
Expand Down
14 changes: 14 additions & 0 deletions src/file/spec/getAbsolutePath.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getAbsolutePath } from '../getAbsolutePath'
import path from 'path'

describe('getAbsolutePath', () => {
it('should return absolute path in normalized form', () => {
expect(getAbsolutePath('~/..', '')).toBeTruthy()
})

it('should return joined path when provided path is not absolute path', () => {
expect(getAbsolutePath('utils', 'sasjs')).toEqual(
path.join('sasjs', 'utils')
)
})
})
57 changes: 57 additions & 0 deletions src/fs/generateCompileProgram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import path from 'path'
import { isFolder, listFilesInFolder, listSubFoldersInFolder } from '../file'
import {
getInitialCode,
getCompiledMacrosCode,
generateCodeForFileCreation
} from './internal/helper'

export const generateCompileProgram = async (folderPath: string) => {
const compiledMacrosCode = await getCompiledMacrosCode(['mf_mkdir.sas'])

const initialProgramContent = getInitialCode()

const folderCreationCode = await fileAndDirectoryCreationCode(folderPath)

return compiledMacrosCode + initialProgramContent + folderCreationCode
}

const fileAndDirectoryCreationCode = async (
resourcePath: string,
pathRelativeTo: string = resourcePath,
resultCode: string = ''
) => {
if (!(await isFolder(resourcePath))) {
resultCode += await generateCodeForFileCreation(
resourcePath,
pathRelativeTo
)
return resultCode
}

const files = await listFilesInFolder(resourcePath)
for (const file of files) {
resultCode = await fileAndDirectoryCreationCode(
path.join(resourcePath, file),
pathRelativeTo,
resultCode
)
}

const subFolders = await listSubFoldersInFolder(resourcePath)
for (const folder of subFolders) {
const folderPath = path.join(resourcePath, folder)

resultCode = `${resultCode}
%mf_mkdir(&fsTarget${folderPath.replace(pathRelativeTo, '')})
`

resultCode = await fileAndDirectoryCreationCode(
folderPath,
pathRelativeTo,
resultCode
)
}

return resultCode
}
Loading

0 comments on commit 1b632d4

Please sign in to comment.