All files / src InternalFileSystemleSystem.js

17.39% Statements 8/46
0% Branches 0/28
11.11% Functions 1/9
17.39% Lines 8/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1021x 1x 1x 1x 1x         1x   1x                                                                                                                                                                                 1x  
const _ = require('lodash')
const fs = require('./utils/fs')
const path = require('path')
const config = require('./config')
const Logger = require('./utils/Logger')
 
class InternalFileSystem {
 
  constructor(secrez) {
    this.secrez = secrez
 
    this.dirTree = this.getInitialDirTree()
 
  }
 
  async getAllDirs(dir) {
    let list = []
    let files = fs.readdirSync(dir)
    for (let file of files) {
      let filePath = path.join(dir, file)
      let stat = fs.lstatSync(filePath)
      if (stat.isDirectory()) {
        list.push(file)
        list = list.concat(await this.getAllDirs(filePath))
      }
    }
    return list
  }
 
  getWorkingDir() {
    let dir = config.workingDir
    if (!dir || dir === '/') {
      return '~'
    } else {
      return path.basename(dir)
    }
  }
 
  cd(dir) {
    // console.log(config.workingDir)
    dir = dir.replace(/^~/, '')
    if (!dir) {
      dir = '/'
    }
    let newWorkingDir = path.normalize(path.resolve(config.workingDir || '/', dir))
    // console.log(newWorkingDir)
    if (this.isDir(newWorkingDir)) {
      config.workingDir = path.normalize(path.resolve(config.workingDir || '/', dir))
      // console.log(config.workingDir)
    } else {
      Logger.red('No such file or directory')
    }
  }
 
  ls(dir) {
    // console.log(config.workingDir)
    dir = dir.replace(/^~/, '')
    if (!dir) {
      dir = '/'
    }
    let newWorkingDir = path.normalize(path.resolve(config.workingDir || '/', dir))
    // console.log(newWorkingDir)
    if (this.isDir(newWorkingDir)) {
      config.workingDir = path.normalize(path.resolve(config.workingDir || '/', dir))
      // console.log(config.workingDir)
    } else {
      Logger.red('No such file or directory')
    }
  }
 
 
  realPath(p) {
    if (p === '~') {
      p = ''
    }
    return path.join(config.dataPath, './' + p).replace(/\/+$/, '')
  }
 
  isDir(dir) {
    let dirPath = this.realPath(dir)
    if (fs.existsSync(dirPath)) {
      return fs.lstatSync(dirPath).isDirectory()
    }
    return false
  }
 
  isFile(fn) {
    let dirPath = this.realPath(fn)
    if (fs.existsSync(dirPath)) {
      return fs.lstatSync(dirPath).isFile()
    }
    return false
  }
 
  readFile(file) {
 
  }
 
}
 
module.exports = InternalFileSystem