This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
ui_bundle_collection.js
99 lines (82 loc) · 2.1 KB
/
ui_bundle_collection.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import UiBundle from './ui_bundle';
import appEntryTemplate from './app_entry_template';
import { transform, pluck } from 'lodash';
import { promisify } from 'bluebird';
import { makeRe } from 'minimatch';
const mkdirp = promisify(require('mkdirp'));
class UiBundleCollection {
constructor(bundlerEnv, filter) {
this.each = [];
this.env = bundlerEnv;
this.filter = makeRe(filter || '*', {
noglobstar: true,
noext: true,
matchBase: true
});
}
add(bundle) {
if (!(bundle instanceof UiBundle)) {
throw new TypeError('expected bundle to be an instance of UiBundle');
}
if (this.filter.test(bundle.id)) {
this.each.push(bundle);
}
}
addApp(app) {
this.add(new UiBundle({
id: app.id,
modules: app.getModules(),
template: appEntryTemplate,
env: this.env
}));
}
desc() {
switch (this.each.length) {
case 0:
return '0 bundles';
case 1:
return `bundle for ${this.each[0].id}`;
default:
const ids = this.getIds();
const last = ids.pop();
const commas = ids.join(', ');
return `bundles for ${commas} and ${last}`;
}
}
async ensureDir() {
await mkdirp(this.env.workingDir);
}
async writeEntryFiles() {
await this.ensureDir();
for (const bundle of this.each) {
const existing = await bundle.readEntryFile();
const expected = bundle.renderContent();
if (existing !== expected) {
await bundle.writeEntryFile();
await bundle.clearBundleFile();
}
}
}
async getInvalidBundles() {
const invalids = new UiBundleCollection(this.env);
for (const bundle of this.each) {
const exists = await bundle.checkForExistingOutput();
if (!exists) {
invalids.add(bundle);
}
}
return invalids;
}
toWebpackEntries() {
return transform(this.each, function (entries, bundle) {
entries[bundle.id] = bundle.entryPath;
}, {});
}
getIds() {
return pluck(this.each, 'id');
}
toJSON() {
return this.each;
}
}
module.exports = UiBundleCollection;