-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (144 loc) · 4.95 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
var baseConfig = require('./config'),
bundlerMaker = require('./lib/bundler'),
builderMaker = require('./lib/builder'),
packagerMaker = require('./lib/packager'),
packageManagerMaker = require('./lib/package-manager'),
utils = require('./lib/utils'),
path = require('path'),
extend = require('extend'),
write = process.stdout.write.bind(process.stdout); // not using console.log because i want control over new lines
module.exports = function(config) {
config = Object.freeze(extend({}, baseConfig, config));
var bundler = bundlerMaker(config),
builder = builderMaker(config),
packager = packagerMaker(config),
packageManager = packageManagerMaker(config);
return {
host: function(options) {
return createJSLib('host', options);
},
ext: function(options) {
return createJSLib('ext', options);
},
iframe: function(options) {
return createIframe(options);
}
};
function createJSLib(target, options) {
options = options || {};
var plugins = options.plugins || [];
var allowedDomains = options.allowedDomains || [];
var minify = !!options.minify;
// 1. install target library
write('Installing Gardr ' + target + ' at version ' + config.VERSIONS[target.toUpperCase()] + ' ...');
return packageManager.install('gardr-' + target + '@' + config.VERSIONS[target.toUpperCase()])
// lib not found?
.catch(utils.thrower)
// 2. install plugins
.then(function () {
write('done\n');
write('Installing plugins... ');
return packageManager.install(plugins);
})
// plugins file not found?
.catch(utils.thrower)
// 3. create bundle file
.then(function () {
write('done\n');
write('Creating bundle... ');
return bundler.bundle({
type: target,
plugins: plugins,
allowedDomains: allowedDomains
});
})
// domains file not found?
.catch(utils.thrower)
// 4. build gardr file
.then(function (bundle) {
write('done\n');
write('Building file... ');
return builder.build({
type: target,
bundlePath: bundle.filePath
});
})
// 5. minify and package
.then(function (built) {
write('done\n');
write('File ready at ' + path.resolve(built.filePath) + '\n');
if (minify) {
write('Minifying file... ');
return packager.minifyJS({
filePath: built.filePath,
outputFileName: config.PACKAGE_PREFIX + target + '-min.js'
});
}
else {
return {
filePath: built.filePath
};
}
})
// 6. expose file path
.then(function (minified) {
if (minify) {
write('done\n');
write('Minified file ready at ' + path.resolve(minified.filePath) + '\n');
}
return {
filePath: minified.filePath
};
});
}
function createIframe(options) {
options = options || {};
var minify = !!options.minify;
// 1. install ext library
write('Installing Gardr ext at version ' + config.VERSIONS.EXT + ' ...');
return packageManager.install('gardr-ext@' + config.VERSIONS.EXT)
// lib not found?
.catch(utils.thrower)
// 2. copy iframe from module
.then(function() {
write('done\n');
write('Copying iframe... ');
return builder.copy({
source: config.IFRAME_ORIGINAL_PATH,
dest: config.IFRAME_DESTINATION_PATH
});
})
// error while copying file?
.catch(utils.thrower)
// 3. minify
.then(function(copied) {
write('done\n');
write('File ready at ' + path.resolve(copied.destFilePath) + '\n');
if(minify) {
write('Minifying iframe... ');
return packager.minifyHTML({
filePath: copied.destFilePath,
outputFileName: 'iframe-min.html'
});
}
else {
return {
filePath: copied.destFilePath
};
}
})
// error while minifying file?
.catch(utils.thrower)
// 4. expose file path
.then(function(minified) {
if(minify) {
write('done\n');
write('Minified file ready at ' + path.resolve(minified.filePath) + '\n');
}
return {
filePath: minified.filePath
};
});
}
};