import path from 'path' ; export const regexRemoveExtension = /\.[^.]*$/ ; export const getRelativePathnameFromConfig = ( config ) => { const toPackage = path . join ( config . toPackage , './src' ) ; const relativePathname = path . relative ( toPackage , config . to ) ; const relative = `./${ relativePathname }` ; return relative . replace ( regexRemoveExtension , '' ) ; } ; export const getRelativePathname = ( from , to , removeExtension = true ) => { const fromDir = path . dirname ( from ) ; const relativePathname = path . relative ( fromDir , to ) ; const importSource = removeExtension ? relativePathname . replace ( regexRemoveExtension , '' ) : relativePathname ; if ( ! importSource . includes ( './' ) || ! importSource . includes ( '../' ) ) { return `./${ importSource }` ; } return importSource ; } ; const getValidImportPaths = ( importPath ) : string [ ] => { const indexEndRegex = /\/index$/ ; if ( ! indexEndRegex . test ( importPath ) ) { return [ importPath ] ; } return [ importPath , path . dirname ( importPath ) ] ; } ; export const isSameImport = ( from , to ) : boolean => { const fromPaths = getValidImportPaths ( from ) ; const toPaths = getValidImportPaths ( to ) ; for ( const f of fromPaths ) { for ( const t of toPaths ) { if ( f === t ) { return true ; } } } return false ; } ;