all files / src/commitizen/ cache.js

80% Statements 4/5
100% Branches 0/0
66.67% Functions 2/3
80% Lines 4/5
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                                                                               
import fs from 'fs';
import _ from 'lodash';
 
export {
  getCacheValueSync,
  readCacheSync,
  setCacheValueSync,
};
 
/**
 * Reads the entire cache
 */
function readCacheSync(cachePath) {
  return JSON.parse(fs.readFileSync(cachePath, 'utf8'));
}
 
/**
 * Sets a cache value and writes the file to disk
 */
function setCacheValueSync(cachePath, key, value) {
  var originalCache;
  try {
    originalCache = readCacheSync(cachePath); 
  } catch (e) {
    originalCache = {};
  }
  var newCache = _.assign(originalCache, {
    [key]: value
  });
  fs.writeFileSync(cachePath, JSON.stringify(newCache, null, '  '));
  return newCache;
}
 
/**
 * Gets a single value from the cache given a key
 */
function getCacheValueSync(cachePath, repoPath) {
  try {
    let cache = readCacheSync(cachePath);
    return cache[repoPath];
  } catch(e) {
    return;
  }
}