|
3 | 3 | 'use strict'; |
4 | 4 |
|
5 | 5 | const fs = require('fs'); |
| 6 | + const path = require('path'); |
6 | 7 |
|
7 | 8 | const NDDB = require('NDDB'); |
8 | 9 |
|
|
11 | 12 | /** |
12 | 13 | * ### NDDB.loadDirSync |
13 | 14 | * |
14 | | - * Load all files matching the criteria into db synchronously |
| 15 | + * Synchronously load all files matching the criteria from a folder |
15 | 16 | * |
16 | 17 | * @param {string} dir The directory to search recursively |
| 18 | + * @param {object} opts The options to search and load files |
| 19 | + * |
| 20 | + * @returns {NDDB} The NDDB instance for chaining |
17 | 21 | * |
18 | 22 | * @see NDDB.loadSync |
19 | 23 | */ |
20 | 24 | NDDB.prototype.loadDirSync = function(dir, opts) { |
21 | 25 |
|
22 | 26 | decorateLoadDirOpts(opts); |
23 | 27 |
|
24 | | - getFilesSync(dir, opts).forEach(file => this.loadSync(file, opts)); |
25 | | - |
| 28 | + getFilesSync(dir, opts).forEach(file => { |
| 29 | + try { |
| 30 | + this.loadSync(file, opts); |
| 31 | + } |
| 32 | + catch(e) { |
| 33 | + console.log(`\n (!) NDDB.loadDirSync: An error occurred ` + |
| 34 | + `in file: ${file}\n`); |
| 35 | + throw e; |
| 36 | + } |
| 37 | + }); |
26 | 38 | return this; |
27 | 39 | }; |
28 | 40 |
|
29 | 41 | /** |
30 | | - * ### NDDB.loadDir |
| 42 | + * ### NDDB.loadDirSync |
31 | 43 | * |
32 | | - * Load in the specified format and loads them into db synchronously |
| 44 | + * Asynchronously load all files matching the criteria from a folder |
33 | 45 | * |
34 | | - * @see NDDB.loadSync |
| 46 | + * @param {string} dir The directory to search recursively |
| 47 | + * @param {object} opts The options to search and load files |
| 48 | + * @param {function} cb A callback executed when all files are loaded |
| 49 | + * |
| 50 | + * @returns {NDDB} The NDDB instance for chaining |
| 51 | + * |
| 52 | + * @see NDDB.load |
35 | 53 | */ |
36 | 54 | NDDB.prototype.loadDir = function(dir, opts, cb) { |
37 | 55 |
|
|
41 | 59 | let files = getFilesSync(dir, opts); |
42 | 60 | let filesLeft = files.length; |
43 | 61 |
|
44 | | - files.forEach(file => this.load(file, (err) => { |
45 | | - if (err) this.throwErr('Error', 'load', err); |
46 | | - if (--filesLeft <= 0 && cb) cb(this); |
47 | | - )}); |
| 62 | + files.forEach(file => { |
| 63 | + try { |
| 64 | + this.load(file, (err) => { |
| 65 | + if (err) this.throwErr('Error', 'load', err); |
| 66 | + if (--filesLeft <= 0 && cb) cb(this); |
| 67 | + }); |
| 68 | + } |
| 69 | + catch(e) { |
| 70 | + console.log(`\n (!) NDDB.loadDir: An error occurred in file: ` + |
| 71 | + `${file}\n`); |
| 72 | + console.log(e.message); |
| 73 | + throw e; |
| 74 | + } |
| 75 | + }); |
48 | 76 |
|
49 | 77 | return this; |
50 | 78 | }; |
|
0 commit comments