import { dirname , basename } from "path" ; import { INormalizedModules , IModule , ISandboxFile , ISandboxDirectory , IBinaryModule , } from "codesandbox-import-util-types" ; import { generate as generateShortid } from "shortid" ; import { getDirectoryPaths } from "../../create-sandbox/utils/resolve" ; function generateSandboxFile ( module , path , parentDirectoryShortid ? ) { const sandboxFile = { shortid : generateShortid ( ) , code : module . content , directoryShortid : parentDirectoryShortid , title : basename ( path ) , uploadId : module . uploadId , isBinary : module . isBinary , sha : module . sha , } ; if ( "binaryContent" in module ) { sandboxFile . binaryContent = module . binaryContent ; } return sandboxFile ; } function createDirectoryRecursively ( path , directories ) { if ( directories [ path ] ) { return directories [ path ] ; } const parentDir = dirname ( path ) ; if ( parentDir === "." ) { directories [ path ] = generateSandboxDirectory ( path , undefined ) ; return ; } if ( ! directories [ parentDir ] ) { createDirectoryRecursively ( parentDir , directories ) ; } directories [ path ] = generateSandboxDirectory ( basename ( path ) , directories [ parentDir ] . shortid ) ; } function generateSandboxDirectory ( title , parentDirectoryShortid ? ) { return { shortid : generateShortid ( ) , directoryShortid : parentDirectoryShortid , title , } ; } export default function denormalize ( paramFiles , existingDirs = [ ] ) { const existingDirPathsParams = getDirectoryPaths ( existingDirs ) ; let existingDirPaths = { } ; Object . keys ( existingDirPathsParams ) . forEach ( ( path ) => { existingDirPaths [ path . replace ( /^\// , "" ) ] = existingDirPathsParams [ path ] ; } ) ; let files = { } ; Object . keys ( paramFiles ) . forEach ( ( path ) => { files [ path . replace ( /^\// , "" ) ] = paramFiles [ path ] ; } ) ; const directories = new Set ( ) ; Object . keys ( files ) . forEach ( ( path ) => { const dir = dirname ( path ) ; if ( dir !== "." && ! existingDirPaths [ "/" + dir ] ) { directories . add ( dirname ( path ) ) ; } const file = files [ path ] ; if ( file . type === "directory" ) { directories . add ( path ) ; } } ) ; const sandboxDirectories = { ... existingDirPaths } ; Array . from ( directories ) . forEach ( ( dirPath ) => { createDirectoryRecursively ( dirPath , sandboxDirectories ) ; } ) ; const sandboxModules = Object . keys ( files ) . map ( ( path ) => { const dir = sandboxDirectories [ dirname ( path ) ] ; const parentShortid = dir ? dir . shortid : undefined ; const fileOrDirectory = files [ path ] ; if ( fileOrDirectory . type === "directory" ) { return ; } else { return generateSandboxFile ( fileOrDirectory , path , parentShortid ) ; } } ) . filter ( ( x ) : x is ISandboxFile => x !== undefined ) ; const dirs = Object . keys ( sandboxDirectories ) . map ( ( s ) => ! existingDirPaths [ s ] && sandboxDirectories [ s ] ) . filter ( Boolean ) ; return { modules : sandboxModules , directories : dirs as ISandboxDirectory [ ] , } ; }