-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPonyContainer.js
38 lines (32 loc) · 1.01 KB
/
PonyContainer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
const Path = require('path');
const fs = require('fs').promises;
const {ForEach} = require('./promiseUtils');
const Pony = require('./Pony');
class PonyContainer {
constructor() {
this.ponylist=[];
// this.loadPonies();
}
get ponies() { return this.ponylist; }
find(ponyName) { return this.ponies.find( p => p.name == ponyName); }
loadPonies() {
let dir = Path.join(__dirname, 'pony-data');
return fs.readdir(dir)
.then( ForEach( this.loadPony(this) ) )
.catch( (err) => { console.log(err); });
}
loadPony = (that) => (ponydir) => {
return Promise.resolve(ponydir)
.then( dir => {
let ponyfile = Path.join(__dirname, 'pony-data', dir, 'Pony.ini');
return new Pony(ponyfile);
})
.then( p => p.loadPony() )
.then( p => {
that.ponylist.push(p);
return p;
});
}
}
module.exports = PonyContainer;