/**
 * Created by axetroy on 17-2-15.
 */

const _ = require('lodash');
const Promise = require('bluebird');
const co = require('co');
const fs = Promise.promisifyAll(require('fs-extra'));
const config = require('./config');
const storage = require('node-persist');
const globalConfig = require('./global-config');

/**
 1. make sure root directory has exist
 2. make sure [temp, config] has exist
 3. make sure [config] must be a json file even it's empty
 */
module.exports = function (options) {
  return co(function *() {
    yield fs.ensureDirAsync(config.paths.root);

    yield [
      fs.ensureDirAsync(config.paths.temp),
      yield globalConfig.init()
    ];

    yield storage.init({
      dir: config.paths.storage,
      stringify: JSON.stringify,
      parse: JSON.parse,
      encoding: 'utf8',
      logging: false,  // can also be custom logging function
      continuous: true, // continously persist to disk
      interval: false, // milliseconds, persist to disk on an interval
      ttl: false, // ttl* [NEW], can be true for 24h default or a number in MILLISECONDS
      expiredInterval: 7 * 24 * 3600 * 1000,     // clear cache in 7 days
      // in some cases, you (or some other service) might add non-valid storage files to your
      // storage dir, i.e. Google Drive, make this true if you'd like to ignore these files and not throw an error
      forgiveParseErrors: false // [NEW]
    });

  });
};

/**
 * Created by axetroy on 17-2-15.
 */

const Promise = require('bluebird');
const co = require('co');
const fs = Promise.promisifyAll(require('fs-extra'));
const config = require('./config');
const storage = require('node-persist');
const globalConfig = require('./global-config');

/**
 1. make sure root directory has exist
 2. make sure [temp, config] has exist
 3. make sure [config] must be a json file even it's empty
 */
module.exports = function (options) {
  return co(function *() {
    yield fs.ensureDirAsync(config.paths.root);
    yield [
      fs.ensureDirAsync(config.paths.temp),
      yield globalConfig.init(),
      storage.init({
        dir: config.paths.storage,
        stringify: JSON.stringify,
        parse: JSON.parse,
        encoding: 'utf8',
        logging: false,  // can also be custom logging function
        continuous: true, // continously persist to disk
        interval: false, // milliseconds, persist to disk on an interval
        ttl: false, // ttl* [NEW], can be true for 24h default or a number in MILLISECONDS
        expiredInterval: 7 * 24 * 3600 * 1000,     // clear cache in 7 days
        // in some cases, you (or some other service) might add non-valid storage files to your
        // storage dir, i.e. Google Drive, make this true if you'd like to ignore these files and not throw an error
        forgiveParseErrors: false // [NEW]
      })
    ];
  });
};

refactor prepare working