All files / src config.js

55.56% Statements 15/27
0% Branches 0/5
0% Functions 0/2
55.56% Lines 15/27

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 561x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x                 1x                                                             1x 1x  
const homedir = require('homedir')
const path = require('path')
const Utils = require('./utils')
const fs = require('./utils/fs')
const pkg = require('../package')
 
const TRUE = 1
const STRING = 2
const ARRAY = 3
const NUMBER = 4
const BOOL = 5
const OBJECT = 6
 
const completionTypes = {
  TRUE,
  STRING,
  ARRAY,
  NUMBER,
  BOOL,
  OBJECT
}
 
const config = {
  completionTypes,
  completion: {
    help: {}
  },
  getVirtualDir: function (dir) {
    if (dir.indexOf(config.dataPath) === 0) {
      return dir.split(config.dataPath)[1]
    } else {
      throw new Error('The directory does not exist in Secrez.')
    }
  }
}
 
function setPaths(parentDir = homedir()) {
  config.root = '.secrez'
  config.rootPath = path.join(parentDir, config.root)
  config.dataPath = path.join(config.rootPath, 'data')
  config.workingDir = '/'
  config.confPath = path.join(config.dataPath, '.key')
  fs.ensureDirSync(config.dataPath)
  let readmePath = path.join(config.rootPath, 'README')
  if (!fs.existsSync(readmePath)) {
    fs.writeFileSync(readmePath, `
This folder has been generated by ${Utils.capitalize(pkg.name)} v${pkg.version}.
It contains your secret database.
Be careful and don't touch anything :o)
`, 'utf-8')
  }
}
 
module.exports = config
module.exports.setPaths = setPaths