1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const glob = require('glob');
|
4 |
|
5 |
|
6 | function Bundle(dir, type) {
|
7 | this.type = type;
|
8 | this.dir = dir;
|
9 | this.list = glob.sync(path.join(dir, '**/*'), {
|
10 | dot: true
|
11 | });
|
12 | this.categories = null;
|
13 | }
|
14 |
|
15 | Bundle.prototype.getCategory = function (category) {
|
16 | if (this.categories && this.categories[category]) return this.categories[category];
|
17 | const categories = this.categories = {
|
18 | file: [],
|
19 | directory: []
|
20 | };
|
21 | if (!categories[category]) return [];
|
22 | this.list = this.list.map(fsPath => {
|
23 | if (!fs.existsSync(fsPath)) return;
|
24 | const stat = fs.statSync(fsPath);
|
25 | const item = {
|
26 | bundle: this,
|
27 | absolute: fsPath,
|
28 | path: fsPath.replace(this.dir, ''),
|
29 | stat: stat
|
30 | };
|
31 | if (stat.isDirectory()) {
|
32 | categories.directory.push(item)
|
33 | } else if (stat.isFile()) {
|
34 | categories.file.push(item)
|
35 | }
|
36 | return item;
|
37 | }).filter(arg => !!arg);
|
38 | return categories[category];
|
39 | };
|
40 |
|
41 | Bundle.prototype.getFile = function () {
|
42 | return this.getCategory('file');
|
43 | };
|
44 |
|
45 | Bundle.prototype.getDirectory = function () {
|
46 | return this.getCategory('directory');
|
47 | };
|
48 |
|
49 |
|
50 | exports = module.exports = Bundle;
|