Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add utils for sasjs fs command #208

Merged
merged 19 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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