From c5280098477927c41c032d1d8f77d6c5308c88a2 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Mon, 24 Oct 2022 11:01:21 +0500 Subject: [PATCH] feat: add utility findResourcesNotPresentLocally --- src/fs/generateCompileProgram.ts | 1 - src/fs/hash.ts | 3 +++ src/fs/index.ts | 3 ++- src/fs/sync.ts | 40 ++++++++++++++++++++++++++++---- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/fs/generateCompileProgram.ts b/src/fs/generateCompileProgram.ts index 8e63926..9c46030 100644 --- a/src/fs/generateCompileProgram.ts +++ b/src/fs/generateCompileProgram.ts @@ -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']) diff --git a/src/fs/hash.ts b/src/fs/hash.ts index 8d9be01..6387205 100644 --- a/src/fs/hash.ts +++ b/src/fs/hash.ts @@ -26,6 +26,9 @@ export const getHash = async (folderPath: string): Promise => { 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 } diff --git a/src/fs/index.ts b/src/fs/index.ts index a562645..0bee460 100644 --- a/src/fs/index.ts +++ b/src/fs/index.ts @@ -2,5 +2,6 @@ export { getHash, compareHashes } from './hash' export { generateCompileProgram } from './generateCompileProgram' export { generateProgramToGetRemoteHash, - generateProgramToSyncHashDiff + generateProgramToSyncHashDiff, + findResourcesNotPresentLocally } from './sync' diff --git a/src/fs/sync.ts b/src/fs/sync.ts index 5def1b6..cc49508 100644 --- a/src/fs/sync.ts +++ b/src/fs/sync.ts @@ -1,6 +1,4 @@ import path from 'path' -import { getNodeModulePath } from '../utils/getNodeModulePath' -import { readFile } from '../file' import { getInitialCode, getCompiledMacrosCode, @@ -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() @@ -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, @@ -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 +}