import { SchematicsException , Tree } from '@angular-devkit/schematics' ; export const getFileDirectoryPath = ( filePath ) => filePath . substring ( 0 , filePath . lastIndexOf ( '/' ) ) ; export function getModuleName ( moduleFilePath ) { const pathSegments = moduleFilePath . split ( '/' ) ; return pathSegments [ pathSegments . length - 2 ] ; } export function convertModulePathToPublicAPIImport ( modulePath ) { const regex = /\/projects\/(.*)(\/)/ ; const pathSegments = regex . exec ( modulePath ) ; return pathSegments && pathSegments . length ? pathSegments [ 1 ] : '' ; } export function getSourceRootPath ( tree , projectName ? ) { const workspaceAsBuffer = tree . read ( 'angular.json' ) ; if ( ! workspaceAsBuffer ) { throw new SchematicsException ( 'Not and Angular CLI workspace' ) ; } const workspace = JSON . parse ( workspaceAsBuffer . toString ( ) ) ; const project = workspace . projects [ projectName || workspace . defaultProject ] ; if ( project . projectType === 'application' ) { throw new SchematicsException ( 'Ng-samurai works only for the "library" projects, please specify correct project using --project flag' ) ; } return project . sourceRoot ; } export function getLibRootPath ( tree , projectName ? ) { return `${ getSourceRootPath ( tree , projectName ) }/lib` ; } export function getFolderPath ( filePath ) { return filePath . substring ( 0 , filePath . lastIndexOf ( '/' ) ) ; } export function convertToAbsolutPath ( filePath , importStringLiteral ) { const levelsUp = getLevels ( importStringLiteral ) ; const filePathSegments = filePath . split ( '/' ) ; const folderPathAfterLevelsMove = filePathSegments . slice ( 0 , filePathSegments . length - levelsUp - 1 ) . join ( '/' ) ; const pathAfterRelativeSegment = importStringLiteral . match ( /\/[a-zA-Z0-9].*/ ) ; return `${ folderPathAfterLevelsMove }${ pathAfterRelativeSegment }` ; } export function resolvePath ( filePath , pathChange ) { const levelsUp = getLevels ( pathChange ) ; const filePathSegments = filePath . split ( '/' ) ; return filePathSegments . slice ( 0 , filePathSegments . length - levelsUp ) . join ( '/' ) ; } function getLevels ( importStringLiteral ) { const numberOfDots = importStringLiteral . match ( /[^a-zA-Z0-9]*/ ) [ 0 ] . match ( /\./g ) ?. length ; return Math . floor ( numberOfDots / 2 ) ; }