All files / lib config.js

64.07% Statements 66/103
72.72% Branches 8/11
50% Functions 4/8
64.07% Lines 66/103

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 102 103 10432x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 138x 1242x 1242x 138x 32x 10x 10x 10x 10x     10x 10x 32x 59x 59x 59x 59x     59x 59x 59x 32x                   32x 32x                       32x               32x             32x 69x 69x 69x 69x 69x 69x 69x 69x  
'use strict'
 
const Configstore = require('configstore')
const pkg = require('../package.json')
const conf = new Configstore(pkg.name)
 
const api = process.env.NCM_API || 'https://api.nodesource.com'
 
const keys = {
  installCmd: 'install',
  installBin: 'npm',
  token: ' ',
  refreshToken: ' ',
  org: 'default',
  orgId: '1',
  policy: ' ',
  policyId: ' ',
  cachedPublicPackages: []
}
 
module.exports = {
  setValue,
  getValue,
  delValue,
  popValue,
  resetState,
  setTokens,
  getTokens,
  api,
  keyNames: Object.keys(keys)
}
 
const validateState = () => {
  for (const key in keys) {
    if (!conf.get(key)) conf.set(key, keys[key])
  }
}
 
function setValue (key, value) {
  validateState()
 
  if (!keys[key]) {
    throw new Error(`Attempted to set invalid config key "${key}"`)
  }
  conf.set(key, value)
}
 
function getValue (key) {
  validateState()
 
  if (!keys[key]) {
    throw new Error(`Attempted to get invalid config key "${key}"`)
  }
 
  return conf.get(key)
}
 
function delValue (key) {
  validateState()

  if (!keys[key]) {
    throw new Error(`Attempted to delete invalid config key "${key}"`)
  }

  conf.set(key, keys[key])
}
 
// get and delete
function popValue (key) {
  validateState()

  if (!keys[key]) {
    throw new Error(`Attempted to pop invalid config key "${key}"`)
  }

  const val = conf.get(key)
  conf.set(key, keys[key])
  return val
}
 
function resetState () {
  validateState()

  for (const key in keys) {
    conf.set(key, keys[key])
  }
}
 
function setTokens ({ session, refreshToken }) {
  validateState()

  conf.set('token', session)
  conf.set('refreshToken', refreshToken)
}
 
function getTokens () {
  validateState()
 
  return {
    token: process.env.NCM_TOKEN || conf.get('token'),
    refreshToken: conf.get('refreshToken')
  }
}