all files / src/ dir-array.js

100% Statements 11/11
100% Branches 2/2
100% Functions 3/3
100% Lines 11/11
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                                                
const fs = require('fs');
const path = require('path');
const DIR = Symbol('DirArray.Dir');
 
class DirArray extends Array {
    // Load directory structure if it exists
    constructor(dir) {
        super();
 
        this[DIR] = path.resolve(dir);
 
        if (fs.existsSync(this[DIR])) {
            this.push(...fs.readdirSync(this[DIR]));
        }
    }
 
    // Create Directory array from relative path
    // supports '..' for lookup
    dir(dir) {
        return new this.constructor(
            path.resolve(
                this[DIR], dir
            )
        );
    }
 
    toString() {
        return this[DIR];
    }
}
 
module.exports = DirArray;
 
DirArray.Dir = DIR;