-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
298 lines (265 loc) · 9.36 KB
/
gulpfile.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
var gulp = require('gulp');
var sass = require('gulp-sass');
var ts = require('gulp-typescript');
var util = require('gulp-util');
var fs = require("fs");
var fse = require("fs-extra");
var filter = require("gulp-filter");
var multimatch = require("multimatch");
var path = require("path");
var watch = require("gulp-watch");
var sourcemaps = require("gulp-sourcemaps");
var through2 = require("through2");
var argv = require("yargs").argv;
var plumber = require("gulp-plumber");
const contentDirName = 'WEB-CONTENT';
const webResourceDescFileName = 'OSGI-INF/web-resources.json';
var modulesCfg = fse.readJsonSync("./modules.json");
var aliases = modulesCfg.aliases;
var moduleMaps = getModuleMaps();
function getModuleMaps(moduleName) {
var moduleMaps = [];
modulesCfg.modules.forEach(module => {
if (!moduleName || module.name === moduleName) {
var modulePath = replaceAlias(module.path);
module.content.forEach(contentElement => {
var contentElementMap = contentElement.map;
for (var mapPath in contentElementMap) {
var mapValue = contentElementMap[mapPath];
var normalizedMapValue = replaceAliases(mapValue);
var destPath = path.resolve(modulePath, contentDirName, replaceAlias(mapPath));
moduleMaps.push({ "destPath": destPath, "pattern": normalizedMapValue });
}
});
}
});
return moduleMaps;
}
function flattenMaps(moduleMaps) {
var flattenedPatterns = [];
moduleMaps.forEach(moduleMap => {
var pattern = moduleMap.pattern;
if (Array.isArray(pattern)) {
flattenedPatterns = flattenedPatterns.concat(pattern);
} else {
flattenedPatterns.push(pattern);
}
});
return flattenedPatterns;
}
function replaceAliases(aliasedPaths) {
if (Array.isArray(aliasedPaths)) {
var normalizedPaths = [];
aliasedPaths.forEach(aliasedPath => {
var normalizedPath = replaceAlias(aliasedPath);
normalizedPaths.push(normalizedPath);
});
return normalizedPaths;
} else {
return replaceAlias(aliasedPaths);
}
}
function replaceAlias(aliasedPath) {
var matched = aliasedPath.match(/!?(@[^\/]*)/);
if (!matched)
return aliasedPath;
var matchedAlias = matched[1];
if (!matchedAlias)
return aliasedPath;
var substitution = aliases[matchedAlias];
if (!substitution)
return aliasedPath;
return aliasedPath.replace(matchedAlias, substitution);
}
function getFileModuleMaps(fileName) {
var fileModuleMaps = [];
moduleMaps.forEach(moduleMap => {
var pattern = moduleMap.pattern;
var matched = multimatch(fileName, pattern, { matchBase: true });
if (matched.length > 0) {
fileModuleMaps.push(moduleMap);
}
});
return fileModuleMaps;
}
function testFileEtension(file, extension) {
var ext = path.extname(file.path);
return ext === extension;
}
function pipeBuildTask(stream) {
const tsProject = ts.createProject('tsconfig.json');
const tsFilter = filter(file =>testFileEtension(file, '.ts'), { restore: true });
const scssFilter = filter(file => testFileEtension(file, '.scss'), { restore: true });
var result = stream;
result = result.pipe(plumber())
.pipe(tsFilter)
.pipe(sourcemaps.init())
.pipe(tsProject())
.pipe(sourcemaps.write())
.pipe(tsFilter.restore)
.pipe(scssFilter)
.pipe(sass({includePaths: [' c:/workspaces/com.ebase.eox/']}))
.pipe(scssFilter.restore)
.pipe(gulp.dest(file => {
var relPath = path.relative(file.cwd, file.path);
var fileModuleMaps = getFileModuleMaps(relPath);
return fileModuleMaps[0].destPath;
}));
return result;
}
function getBuildTask(pattern) {
var result = gulp.src(pattern);
result = pipeBuildTask(result);
return result;
}
function cleanModule(module) {
var modulePath = replaceAlias(module.path);
var webResourceDescriptions = [];
var webContentPath = path.resolve(modulePath, contentDirName);
var webResourceDescFilePath = path.resolve(modulePath, webResourceDescFileName);
try {
util.log('deleting web resource description: ' + webResourceDescFilePath);
fse.unlinkSync(webResourceDescFilePath);
util.log("emptying content: " + webContentPath);
fse.emptyDirSync(webContentPath);
fse.removeSync(webContentPath);
} catch (error) {
util.log(error);
}
}
// Deletes whole content of modules
gulp.task('clean', function () {
var moduleName = argv.module;
var modules = getModulesByName(moduleName);
if (!modules.length)
return;
util.log("Task 'clean': started...");
modules.forEach(function (module) {
if (!moduleName || module.name === moduleName) {
cleanModule(module);
}
})
util.log("Task 'clean': done.");
});
function createWebResourceDescForModule(module) {
var modulePath = replaceAlias(module.path);
var webResourceDescriptions = [];
module.content.forEach(function (contentElement) {
var webResourcePath = path.posix.resolve("/", contentDirName, contentElement.path);
var webResourceDescription = {
"path": webResourcePath,
"pattern": contentElement.pattern,
"contextName": contentElement.contextName
};
webResourceDescriptions.push(webResourceDescription);
});
var webResourceDescFilePath = path.resolve(modulePath, webResourceDescFileName);
util.log('creating web resource description: ' + webResourceDescFilePath);
try {
fse.mkdirpSync(path.dirname(webResourceDescFilePath));
fs.writeFileSync(webResourceDescFilePath, JSON.stringify(webResourceDescriptions));
} catch (error) {
util.log(error);
}
}
function getModulesByName(moduleName) {
return modulesCfg.modules.filter(function (module) {
return (!moduleName || (module.name === moduleName))
})
}
gulp.task('web-resources', function () {
var moduleName = argv.module;
var modules = getModulesByName(moduleName);
if (!modules.length)
return;
util.log("Task 'web-resources': started...");
modules.forEach(function (module) {
if (!moduleName || module.name === moduleName) {
createWebResourceDescForModule(module);
}
})
util.log("Task 'web-resources': done.");
});
gulp.task('build', ['clean', 'web-resources'], function () {
var moduleName = argv.module;
var maps = getModuleMaps(moduleName);
const flattenedPattern = flattenMaps(maps);
if (!flattenedPattern.length)
return;
return getBuildTask(flattenedPattern);
})
Array.prototype.unique = function () {
var seen = {};
var out = [];
var len = this.length;
var j = 0;
for (var i = 0; i < len; i++) {
var item = this[i];
if (seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
return out;
};
function batchUnlink() {
var moduleMapsToUnlink = [];
var holdOn;
var timeout;
var event;
function setupFlushTimeout() {
if (!holdOn && moduleMapsToUnlink.length) {
util.log('Timer started: ' + JSON.stringify(moduleMapsToUnlink));
timeout = setTimeout(flush, 100);
}
}
function flush() {
holdOn = true;
var currentModuleMapsToUnlink = moduleMapsToUnlink;
moduleMapsToUnlink = [];
currentModuleMapsToUnlink.forEach(function (fileModuleMap) {
util.log('deleting ' + fileModuleMap.destPath);
fse.emptyDirSync(fileModuleMap.destPath);
});
const flattenedFilePattern = flattenMaps(currentModuleMapsToUnlink);
util.log('rebuild ' + JSON.stringify(flattenedFilePattern));
var task = gulp.add('rebuild_task', function () { return getBuildTask(flattenedFilePattern) });
if (!event) {
event = task.on('task_stop', function (e) {
util.log('task done');
holdOn = false;
setupFlushTimeout();
});
}
gulp.start('rebuild_task');
}
function addModuleToUnlink(file, enc, cb) {
util.log('Unlink file: ' + file);
if (file.event !== 'unlink')
return;
var relPath = path.relative(file.cwd, file.path);
util.log('Unlink file: ' + relPath);
var fileModuleMaps = getFileModuleMaps(relPath);
util.log('found mudule maps: ' + JSON.stringify(fileModuleMaps));
moduleMapsToUnlink = moduleMapsToUnlink.concat(fileModuleMaps).unique();
util.log('moduleMapsToUnlink: ' + JSON.stringify(moduleMapsToUnlink));
if (timeout) { clearTimeout(timeout); }
setupFlushTimeout();
cb(null, file);
}
var stream = through2.obj(addModuleToUnlink);
return stream;
}
gulp.task('development', ['build'], function () {
const flattenedPatterns = flattenMaps(moduleMaps);
const unlinkFilter = filter(file => {
var result = (file.event === 'unlink');
return result;
}, { restore: true });
var stream = watch(flattenedPatterns, { ignoreInitial: true })
.pipe(unlinkFilter)
.pipe(batchUnlink())
.pipe(unlinkFilter.restore);
stream = pipeBuildTask(stream);
return stream;
});