import { dirname } from 'path' ; import { commandsTrie , Util } from '@kui-shell/core' ; import { VFS } from '..' ; import { TrieVFS , Leaf , Directory , BaseEntry } from './TrieVFS' ; class CommandsFS extends TrieVFS < string > { public constructor ( ) { super ( '/' , commandsTrie ) ; } protected isLeaf ( entry ) { return super . isLeaf ( entry ) || ! entry . isDirectory ; } protected loadAsString ( ) { return '' ; } protected trieGet ( filepath ) { return this . fillInParents ( filepath , Array . from ( new Set ( this . trie . get ( filepath ) ) ) . map ( this . direntry ) ) ; } private parentsOf ( mountPath , enclosing ) { const parents = [ ] ; mountPath = dirname ( mountPath ) ; while ( mountPath . length >= enclosing . length ) { parents . push ( { mountPath : mountPath + '/' , isDirectory : true } ) ; if ( mountPath === '/' ) { break ; } mountPath = dirname ( mountPath ) ; } return parents ; } private fillInParents ( filepath , entries ) { const parents = Util . flatten ( entries . map ( _ => this . parentsOf ( _ . mountPath , filepath . replace ( /\/$/ , '' ) ) ) ) ; return entries . concat ( parents ) ; } private direntry ( mountPath ) { return { mountPath , isDirectory : false , isExecutable : true } ; } protected viewer ( ) { return '' ; } public async ls ( args , filepaths ) { const entries = await super . ls ( args , filepaths ) ; if ( entries . length > 0 ) { const seen = { } ; return entries . filter ( _ => { if ( ! seen [ _ . path ] ) { seen [ _ . path ] = true ; return true ; } else { return false ; } } ) ; } else { return entries ; } } } export default new CommandsFS ( ) ;