import * as path from 'path' ; import { Moniker , PackageInformation , MonikerKind , UniquenessLevel } from 'lsif-protocol' ; import { EmitterContext } from '../common/graph' ; import { NpmMoniker } from '../common/moniker' ; import * as paths from '../common/paths' ; import { PackageJson } from './package' ; export class ExportMonikers { private readonly emitter : EmitterContext ; private readonly workspaceRoot : string ; private readonly packageJson : PackageJson ; private readonly pathPrefix : string ; private _packageInformation : PackageInformation | undefined ; constructor ( emitter , workspaceRoot , packageJson ) { this . emitter = emitter ; this . workspaceRoot = workspaceRoot ; this . packageJson = packageJson ; this . pathPrefix = packageJson . $location ; if ( this . pathPrefix [ this . pathPrefix . length - 1 ] !== '/' ) { this . pathPrefix = `${ this . pathPrefix }/` ; } } public attachMoniker ( tscMoniker , filePath , exportParts ) { if ( ! this . isPackaged ( path . join ( this . workspaceRoot , filePath ) ) ) { return undefined ; } const exportPath = typeof exportParts === 'string' ? exportParts : `[${ exportParts . join ( ',' ) }]` ; const npmFilePath = this . getNpmFilePath ( this . workspaceRoot , filePath ) ; let npmIdentifier ; if ( this . packageJson . main === npmFilePath || this . packageJson . typings === npmFilePath ) { npmIdentifier = NpmMoniker . create ( this . packageJson . name , undefined , exportPath ) ; } else { npmIdentifier = NpmMoniker . create ( this . packageJson . name , npmFilePath , exportPath ) ; } const npmMoniker = this . emitter . vertex . moniker ( NpmMoniker . scheme , npmIdentifier , UniquenessLevel . scheme , MonikerKind . export ) ; this . emitter . emit ( npmMoniker ) ; this . emitter . emit ( this . emitter . edge . packageInformation ( npmMoniker , this . packageInformation ) ) ; this . emitter . emit ( this . emitter . edge . attach ( npmMoniker , tscMoniker ) ) ; } private isPackaged ( _uri ) { return true ; } private get packageInformation ( ) : PackageInformation { if ( this . _packageInformation === undefined ) { this . _packageInformation = this . emitter . vertex . packageInformation ( this . packageJson . name , 'npm' ) ; this . _packageInformation . version = this . packageJson . version ; this . emitter . emit ( this . _packageInformation ) ; } return this . _packageInformation ; } private getNpmFilePath ( projectRoot , filePath ) { const fullPath = path . posix . join ( projectRoot , filePath ) ; if ( paths . isParent ( this . pathPrefix , fullPath ) ) { return path . posix . relative ( this . pathPrefix , fullPath ) ; } return filePath ; } }