Skip to content

Commit

Permalink
feat: add utility findResourcesNotPresentLocally
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhas committed Oct 24, 2022
1 parent dc125b6 commit c528009
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/fs/generateCompileProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
getCompiledMacrosCode,
generateCodeForFileCreation
} from './internal/helper'
import { HashedFolder } from '../types'

export const generateCompileProgram = async (folderPath: string) => {
const compiledMacrosCode = await getCompiledMacrosCode(['mf_mkdir.sas'])
Expand Down
3 changes: 3 additions & 0 deletions src/fs/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const getHash = async (folderPath: string): Promise<HashedFolder> => {
return await hashFolder(folderPath)
}

/**
* It returns a hashed folder tree that contains the local directory resources that are not synced with remote
*/
export async function compareHashes(
localHash: HashedFolder,
remoteHashMap: { [key: string]: string }
Expand Down
3 changes: 2 additions & 1 deletion src/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export { getHash, compareHashes } from './hash'
export { generateCompileProgram } from './generateCompileProgram'
export {
generateProgramToGetRemoteHash,
generateProgramToSyncHashDiff
generateProgramToSyncHashDiff,
findResourcesNotPresentLocally
} from './sync'
40 changes: 36 additions & 4 deletions src/fs/sync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import path from 'path'
import { getNodeModulePath } from '../utils/getNodeModulePath'
import { readFile } from '../file'
import {
getInitialCode,
getCompiledMacrosCode,
Expand Down Expand Up @@ -33,9 +31,13 @@ export const generateProgramToSyncHashDiff = async (

const initialProgramContent = getInitialCode()

const folderCreationCode = generateCodeForFolderCreation(
const pathRelativeTo = hashedFolder.absolutePath.endsWith(path.sep)
? hashedFolder.absolutePath.slice(0, -1)
: hashedFolder.absolutePath

const folderCreationCode = await generateCodeForFolderCreation(
hashedFolder,
hashedFolder.absolutePath
pathRelativeTo
)

const codeForHashCreation = getCodeForHashCreation()
Expand All @@ -48,6 +50,16 @@ export const generateProgramToSyncHashDiff = async (
return setTargetAtStart(code, remotePath)
}

export const findResourcesNotPresentLocally = async (
localHash: HashedFolder,
remoteHashMap: { [key: string]: string }
) => {
const localHashedArray = convertHashFolderTreeToArray(localHash)
const remoteHashedArray = Object.keys(remoteHashMap)

return remoteHashedArray.filter((item) => !localHashedArray.includes(item))
}

const generateCodeForFolderCreation = async (
hashedFolder: HashedFolder,
pathRelativeTo: string,
Expand Down Expand Up @@ -100,3 +112,23 @@ run;
const setTargetAtStart = (code: string, target: string) => {
return `%let fsTarget=${target};\n${code}`
}

/**
* convert hash folder tree to an array of relative paths,
* the returned array will be used to check the resources that are present on remote but not local
*/

const convertHashFolderTreeToArray = (
hashedFolder: HashedFolder,
array: string[] = []
) => {
if (hashedFolder.isFile) return [...array, hashedFolder.relativePath]

for (const member of hashedFolder.members) {
array = convertHashFolderTreeToArray(member as HashedFolder, array)
}

array.push(hashedFolder.relativePath)

return array
}

0 comments on commit c528009

Please sign in to comment.