/**
 * Created by axetroy on 17-3-23.
 */
const Promise = require('bluebird');
const co = require('co');
const _ = require('lodash');
const fs = Promise.promisifyAll(require('fs-extra'));
const storage = require('node-persist');

const config = require('./config');

class Registry {
  constructor() {
    this.key = 'repositories';
    this[this.key] = [];
  }

  init() {
    const self = this;
    return co(function*() {
      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]
      });

      self[self.key] = (yield storage.getItem(self.key)) || [];
    });
  }

  add(repo) {
    const self = this;
    return co(function*() {
      self[self.key].push(repo);
      yield storage.set(self.key, self[self.key]);
    });
  }

  remove() {

  }

}

module.exports = new Registry();

@@ -222,16 +222,91 @@
 sist');%0A
+const fuzzy = require('fuzzy');%0Aconst %7BnormalizePath%7D = require('./utils');
 %0Aconst c
@@ -413,23 +413,92 @@
 this
-%5Bthis.key%5D = %5B%5D
+.repositories = %5B%5D;%0A  %7D%0A%0A  get isEmpty() %7B%0A    return this.repositories.length === 0
 ;%0A  
@@ -1350,26 +1350,29 @@
     self
-%5Bself.key%5D
+.repositories
  = (yiel
@@ -1495,26 +1495,29 @@
     self
-%5Bself.key%5D
+.repositories
 .push(re
@@ -1563,47 +1563,1303 @@
 self
-%5Bself.key%5D);%0A    %7D);%0A  %7D%0A%0A  remove() %7B%0A
+.repositories);%0A    %7D);%0A  %7D%0A%0A  remove() %7B%0A%0A  %7D%0A%0A  find(key) %7B%0A    const searchResult = fuzzy%0A      .filter(key, this.repositories.map(repo =%3E repo.owner + '/' + repo.name))%0A      .map(v =%3E v.string);%0A    return this.repositories.filter((repo) =%3E _.includes(searchResult, repo.owner + '/' + repo.name));%0A  %7D%0A%0A  toJson(options = %7B%7D) %7B%0A    const output = %7B%7D;%0A    const repositories = this.repositories.slice();%0A    while (repositories.length) %7B%0A      const repo = repositories.shift();%0A      const sourceKey = options.color ? repo.source.red : repo.source;%0A      const ownerKey = options.color ? repo.owner.yellow : repo.owner;%0A      const nameKey = options.color ? repo.name.green : repo.name;%0A      const source = output%5BsourceKey%5D = output%5BsourceKey%5D %7C%7C %7B%7D;%0A      const owner = source%5BownerKey%5D = source%5BownerKey%5D %7C%7C %7B%7D;%0A      const project = owner%5BnameKey%5D;%0A%0A      let projectPath = normalizePath(repo.path, options);%0A      projectPath = options.color ? projectPath.white : projectPath;%0A%0A      if (!_.isEmpty(project)) %7B%0A        if (_.isString(project)) %7B%0A          owner%5BnameKey%5D = %5Bproject%5D.concat(projectPath);%0A        %7D else if (_.isArray(project)) %7B%0A          owner%5BnameKey%5D.push(projectPath);%0A        %7D%0A      %7D else %7B%0A        owner%5BnameKey%5D = projectPath;%0A      %7D%0A    %7D%0A    return output;
 %0A  %7D

complete registry class