const micromatch = require ( "micromatch" ) ; import { rootRemove , rootPrepend } from "./utility" ; import { parse , resolve , relative , dirname } from "path" ; import { invert } from "lodash/fp" ; import { fromGlobsToFilesArray } from "./globs" ; import { workspaceConfig as config } from "./config" ; const patternSrcObject = config . get ( "source.pattern" ) ; function getObjectTypeFromSrcPath ( path ) { const foundPattern = Object . keys ( patternSrcObject ) . filter ( ( element ) => { const pattern = patternSrcObject [ element ] ; const globPattern = pattern . replace ( /{schema-name}|{object-name}/gi , "*" ) ; return micromatch . isMatch ( path , globPattern , { format : rootRemove , nocase : true , } ) ; } ) ; return foundPattern [ 0 ] ; } const patternPckOutput = config . get ( "package.output" ) ; function isDeployPath ( path ) { const globPattern = patternPckOutput . replace ( /{schema-name}|{object-name}/gi , "*" ) ; return micromatch . isMatch ( path , globPattern , { format : rootRemove , nocase : true , } ) ; } export function matchOutputFiles ( outputFilePattern , options ? ) { const globPattern = outputFilePattern . replace ( /{schema-name}|{object-name}/gi , "*" ) ; const files = fromGlobsToFilesArray ( globPattern , options ) ; return files ; } const mapToOraObjectType = { packageSpec : "PACKAGE" , procedure : "PROCEDURE" , function : "FUNCTION" , packageBody : "PACKAGE BODY" , view : "VIEW" , trigger : "TRIGGER" , typeSpec : "TYPE" , typeBody : "TYPE BODY" , table : "TABLE" , synonym : "SYNONYM" , apex : "APEX" , } ; const mapToOraObjectTypeAlt = { packageSpec : "PACKAGE_SPEC" , packageBody : "PACKAGE_BODY" , typeSpec : "TYPE_SPEC" , typeBody : "TYPE_BODY" , } ; const mapfromOraObjectType = invert ( mapToOraObjectType ) ; typedef { { owner : string , objectType ? : string , objectType1 ? : string , objectName ? : string , isSource ? : string , isScript ? : string } } ObjectDefinition param { string } path returns { ObjectDefinition } export function getObjectInfoFromPath ( path ) { if ( ! path ) { return { owner : undefined } ; } let schema , objectName , objectType ; const absPath = resolve ( path ) ; const base = resolve ( "./" ) ; const relPath = relative ( base , absPath ) ; const pathPosix = rootPrepend ( relPath . replace ( /\\/gi , "/" ) ) ; objectType = getObjectTypeFromSrcPath ( pathPosix ) ; if ( objectType ) { const patternSrc = patternSrcObject [ objectType ] ; /** extact schema*/ const schemaSplit = patternSrc . split ( "{schema-name}" ) ; const schemaRegex = schemaSplit . map ( ( v ) => new RegExp ( v . replace ( "{object-name}" , "\\w+" ) , "gi" ) ) ; schema = schemaRegex . reduce ( ( acc , val ) => acc . replace ( val , "" ) , pathPosix ) ; /** extact object-name*/ const objectSplit = patternSrc . split ( "{object-name}" ) ; const objectRegex = objectSplit . map ( ( v ) => new RegExp ( v . replace ( "{schema-name}" , "\\w+" ) , "gi" ) ) ; objectName = objectRegex . reduce ( ( acc , val ) => acc . replace ( val , "" ) , pathPosix ) ; const objectTypeOra = mapToOraObjectType [ objectType ] || objectType ; const objectTypeOraAlt = mapToOraObjectTypeAlt [ objectType ] || objectTypeOra ; return { owner : schema , objectName , objectType : objectTypeOra , objectType1 : objectTypeOraAlt , isSource : true , isScript : false , } ; } const isDeploy = isDeployPath ( pathPosix ) ; if ( isDeploy ) { const patternPck = patternPckOutput ; /** extact schema*/ const schemaSplit = patternPck . split ( "{schema-name}" ) ; schema = pathPosix . replace ( new RegExp ( schemaSplit [ 0 ] , "gi" ) , "" ) . replace ( new RegExp ( schemaSplit [ 1 ] , "gi" ) , "" ) ; objectName = parse ( absPath ) . name ; return { owner : schema , objectType : "deployScript" , objectType1 : "deployScript" , objectName , isSource : false , isScript : true , } ; } if ( ! objectType ) { const pathSplit = pathPosix . split ( "/" ) ; schema = pathSplit . length > 3 ? pathSplit [ 2 ] : undefined ; objectName = parse ( absPath ) . name ; return { owner : schema , objectType : "script" , objectType1 : "script" , objectName , isSource : false , isScript : true , } ; } } export function replaceVarsInPattern ( pattern = "" , owner , name ? ) { return pattern . replace ( "{schema-name}" , owner ) . replace ( "{object-name}" , name ) ; } param { string } owner param { string } oraType param { string } name returns { string } export function getPathFromObjectInfo ( owner , oraType , name ) { const type = mapfromOraObjectType [ oraType ] || oraType ; const pattern = patternSrcObject [ type ] ; const path = replaceVarsInPattern ( pattern , owner , name ) ; return path ; } export function getPackageOutputPath ( { owner } ) { const path = replaceVarsInPattern ( patternPckOutput , owner ) ; return path ; } returns { Array } export function getSourceStructure ( ) { return Object . keys ( patternSrcObject ) . map ( ( el ) => dirname ( patternSrcObject [ el ] ) ) ; } export function getObjectTypes ( ) { return Object . values ( mapToOraObjectType ) ; } returns { Array }