fileoverview import { workspace } from 'vscode' ; import { FILENAME_AND_PATH , IMPORTED_FILES , REQUIRED_FILES , REQUIRED_PATH_JOIN , CODE_FILE , NEW_LINE , } from '../../constants/expressPatterns' ; const fs = require ( 'fs' ) ; const pathUtil = require ( 'path' ) ; param { File } file return { string } export const readFile = ( file ) : string => { const { path , fileName } = file ; return fs . readFileSync ( path + fileName , { encoding : 'utf8' , flag : 'r' } ) ; } ; param { string [ ] } fileList return { File [ ] } const createFileObjects = ( fileList ) => { const output = [ ] ; fileList . forEach ( ( path ) => { const PATH_SPLIT = path . match ( FILENAME_AND_PATH ) ; if ( PATH_SPLIT ) output . push ( { path : PATH_SPLIT [ 1 ] , fileName : PATH_SPLIT [ 2 ] , contents : '' , } ) ; } ) ; return output ; } ; param { string } filePath param { string } line return { string } const parseLineForImport = ( filePath , line ) : string => { const FILE_IMPORTED = line . match ( IMPORTED_FILES ) ; if ( FILE_IMPORTED ) return pathUtil . join ( filePath , FILE_IMPORTED [ 1 ] ) . replace ( /\\/g , '/' ) ; const FILE_REQUIRED = line . match ( REQUIRED_FILES ) ; if ( FILE_REQUIRED ) return pathUtil . join ( filePath , FILE_REQUIRED [ 1 ] ) . replace ( /\\/g , '/' ) ; const JOIN_FILE_REQUIRED = line . match ( REQUIRED_PATH_JOIN ) ; if ( JOIN_FILE_REQUIRED ) return pathUtil . join ( filePath , JOIN_FILE_REQUIRED [ 1 ] ) . replace ( /\\/g , '/' ) ; return '' ; } ; param { string } path return { string [ ] } const findCodeFiles = ( path ) : string [ ] => { const codeFiles = [ ] ; const DIR_CONTENTS = fs . readdirSync ( path ) ; let files = DIR_CONTENTS . map ( ( content ) => { const resource = pathUtil . resolve ( path , content ) . replace ( /\\/g , '/' ) ; return fs . statSync ( resource ) . isDirectory ( ) ? findCodeFiles ( resource ) : resource ; } ) ; files = files . flat ( Infinity ) ; files . forEach ( ( file ) => { if ( file . match ( CODE_FILE ) ) codeFiles . push ( file ) ; } ) ; return codeFiles ; } ; param { string } path return { string [ ] } export const resolvePath = ( path ) : string [ ] => { if ( fs . existsSync ( path ) ) { return fs . statSync ( path ) . isDirectory ( ) ? findCodeFiles ( path ) : [ path ] ; } const JS_PATH = path . concat ( '.js' ) ; if ( fs . existsSync ( JS_PATH ) ) { return fs . statSync ( JS_PATH ) . isDirectory ( ) ? findCodeFiles ( JS_PATH ) : [ JS_PATH ] ; } const TS_PATH = path . concat ( '.ts' ) ; if ( fs . existsSync ( TS_PATH ) ) { return fs . statSync ( TS_PATH ) . isDirectory ( ) ? findCodeFiles ( TS_PATH ) : [ TS_PATH ] ; } return [ ] ; } ; param { File } file return { File [ ] } export const findImportedFiles = ( file ) : File [ ] => { let output = [ ] ; file . contents . split ( NEW_LINE ) . forEach ( ( line ) => { const IMPORT_PATH = parseLineForImport ( file . path , line ) ; if ( IMPORT_PATH !== '' ) { const FILE_LIST = resolvePath ( IMPORT_PATH ) ; output = output . concat ( createFileObjects ( FILE_LIST ) ) ; } } ) ; return output ; } ; param { string } fullPath return { string } export const getLocalPath = ( fullPath ) => { let basePath = workspace . rootPath ; if ( basePath ) { basePath = basePath . replace ( /\\/g , '/' ) ; const PATH_FOUND = fullPath . match ( new RegExp ( `${ basePath }\\/(\\S*)` ) ) ; if ( PATH_FOUND ) return PATH_FOUND [ 1 ] ; } return fullPath ; } ; param { string } fullRoute return { string } export const getLocalRoute = ( fullRoute ) => { const ROUTE_FOUND = fullRoute . match ( /http:\/\/(\S*)/ ) ; if ( ROUTE_FOUND ) return ROUTE_FOUND [ 1 ] ; return fullRoute ; } ;