<commit_before>'use strict';

var Datastore = require('nedb'),
    Parser = require('./parser.js'),
    events = require('events'),
    watcher = require('./watcher.js'),
    fs = require('fs'),
    db = new Datastore({ filename: 'db/bundles.db', autoload: true }),
    Q = require('q'),
    util = require('util'),
    log = require('../logger');

// Using a unique constraint with the bundle name
db.ensureIndex({ fieldName: 'name', unique: true }, function (err) {});

// Used in the "watcher" listener
var self = {};

function Bundles() {
    log.trace("[lib/bundles/index.js] Loading bundles");
    events.EventEmitter.call(this);
    self = this;

    // Do an initial scan of the bundles dir
    var bundlesDir = fs.readdirSync('bundles/');
    var discoveredBundles = [];

    bundlesDir.forEach(function(bundleFolderName) {
        var bundle = Parser.parse(bundleFolderName);

        if (bundle !== null) {
            log.trace("[lib/bundles/index.js] Discovered bundle %s", bundle.name);
            discoveredBundles.push(bundle);
        }
    });

    // Remove any entries that are no longer present on disk,
    // add/update the rest
    self.all()
        .then(function (currentBundles) {
            currentBundles.forEach(function(bundle) {
                var bundleName = bundle.name;
                var bundleDiscovered = false;

                discoveredBundles.forEach(function (discoveredBundle) {
                    if (discoveredBundle.name === bundleName) {
                        bundleDiscovered = true;
                    }
                });

                if (!bundleDiscovered) {
                    log.info("[lib/bundle/index.js] %s's nodecg.json was not discovered, assuming the bundle has been deleted or moved", bundle.name);
                    self.remove(bundle.name);
                }<commit_msg>Create bundles folder if it doesn't exist, fixes #43<commit_after>'use strict';

var Datastore = require('nedb'),
    Parser = require('./parser.js'),
    events = require('events'),
    watcher = require('./watcher.js'),
    fs = require('fs'),
    db = new Datastore({ filename: 'db/bundles.db', autoload: true }),
    Q = require('q'),
    util = require('util'),
    log = require('../logger');

// Using a unique constraint with the bundle name
db.ensureIndex({ fieldName: 'name', unique: true }, function (err) {});

// Used in the "watcher" listener
var self = {};

function Bundles() {
    log.trace("[lib/bundles/index.js] Loading bundles");
    events.EventEmitter.call(this);
    self = this;

    // Do an initial scan of the bundles dir
    if (!fs.existsSync('bundles/')) {
        fs.mkdirSync('bundles');
    }

    var bundlesDir = fs.readdirSync('bundles/');
    var discoveredBundles = [];

    bundlesDir.forEach(function(bundleFolderName) {
        var bundle = Parser.parse(bundleFolderName);

        if (bundle !== null) {
            log.trace("[lib/bundles/index.js] Discovered bundle %s", bundle.name);
            discoveredBundles.push(bundle);
        }
    });

    // Remove any entries that are no longer present on disk,
    // add/update the rest
    self.all()
        .then(function (currentBundles) {
            currentBundles.forEach(function(bundle) {
                var bundleName = bundle.name;
                var bundleDiscovered = false;

                discoveredBundles.forEach(function (discoveredBundle) {
                    if (discoveredBundle.name === bundleName) {
                        bundleDiscovered = true;
                    }
                });

                if (!bundleDiscovered) {
                    log.info("[lib/bundle/index.js] %s's nodecg.json was not discovered, assuming the bundle has been deleted or moved", bundle.name);
                    self.remove(bundle.name);
                }
{'fraction_non_alphanumeric': 0.13294948966239203, 'fraction_numerical': 0.0005234231876472127, 'mean_word_length': 2.211764705882353, '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': '73140', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1043, 'n_words': 271}