<commit_before>// Populates N.stores tree
//


"use strict";


/*global N, underscore*/


// stdlib
var path = require("path");


// 3rd-party
var _       = underscore;
var async   = require("async");
var fstools = require("fs-tools");


// internal
var apify       = require("./utils/apify");
var stopwatch   = require("./utils/stopwatch");
var expandTree  = require("./utils/expand_tree");
var Settings    = require("./stores/settings");
var Store       = require("./stores/store");


////////////////////////////////////////////////////////////////////////////////


module.exports = function (callback) {
  var timer = stopwatch();

  N.hooks.init.run("stores", function (next) {
    N.settings  = new Settings();
    N.stores    = {};

    // KLUDGE: Probably it's better to provide Store right into store init func
    N.settings.createStore = function (schema) {
      return new Store(schema);
    };

    async.forEach(N.runtime.apps, function (app, next) {
      var storesRoot = path.join(app.root, 'stores');

      fstools.walk(storesRoot, /\.js$/, function (file, stat, next) {
        var apiPath, init, store;

        // skip:
        //
        // - filename starts with underscore, e.g.: /foo/bar/_baz.js
        // - dirname of file starts with underscore, e.g. /foo/_bar/baz.js
        if (file.match(/(^|\/)_/)) {
          next();
          return;
        }

        apiPath = apify(file, storesRoot, /\.js$/);
        init    = require(file);
        store   = init(N, apiPath);

        if (!store) {
          next(apiPath + " store constructor returns nothing (in: " + file + ")");
          return;
        }

        try {
          // register known keys and theirs default values
          _.each(N.config.setting_schemas[name], function (config, key) {
            store.addKnownKey(key, config['type'], config['default']);
          });

          // register store
          N.settings.addStore(name, store);
        } catch (err) {
          next(err);
          return;
        }

        N.stores[apiPath] = store;
        next();<commit_msg>Fix for stores init<commit_after>// Populates N.stores tree
//


"use strict";


/*global N, underscore*/


// stdlib
var path = require("path");


// 3rd-party
var _       = underscore;
var async   = require("async");
var fstools = require("fs-tools");


// internal
var apify       = require("./utils/apify");
var stopwatch   = require("./utils/stopwatch");
var expandTree  = require("./utils/expand_tree");
var Settings    = require("./stores/settings");
var Store       = require("./stores/store");


////////////////////////////////////////////////////////////////////////////////


module.exports = function (callback) {
  var
  timer   = stopwatch(),
  schemas = N.config.setting_schemas || {};

  N.hooks.init.run("stores", function (next) {
    N.settings  = new Settings();
    N.stores    = {};

    // KLUDGE: Probably it's better to provide Store right into store init func
    N.settings.createStore = function (schema) {
      return new Store(schema);
    };

    async.forEach(N.runtime.apps, function (app, next) {
      var storesRoot = path.join(app.root, 'stores');

      fstools.walk(storesRoot, /\.js$/, function (file, stat, next) {
        var apiPath, init, store;

        // skip:
        //
        // - filename starts with underscore, e.g.: /foo/bar/_baz.js
        // - dirname of file starts with underscore, e.g. /foo/_bar/baz.js
        if (file.match(/(^|\/)_/)) {
          next();
          return;
        }

        apiPath = apify(file, storesRoot, /\.js$/);
        init    = require(file);
        store   = init(N, apiPath);

        if (!store) {
          next(apiPath + " store constructor returns nothing (in: " + file + ")");
          return;
        }

        try {
          // register known keys and theirs default values
          _.each(schemas[apiPath], function (config, key) {
            store.addKnownKey(key, config['type'], config['default']);
          });

          // register store
          N.settings.addStore(apiPath, store);
        } catch (err) {
          next(err);
          return;
        }

        N.stores[apiPath] = store;
        next();
{'fraction_non_alphanumeric': 0.18605757792053296, 'fraction_numerical': 0.0004758505829169641, 'mean_word_length': 1.8045363575717144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1748525', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1217, 'n_words': 306}