/
compiled-helpers.js
107 lines (95 loc) · 3.06 KB
/
compiled-helpers.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
/**
* Additional functions for compiled modules build
* @author Kolbeshin F.A.
*/
'use strict';
const { path } = require('../../lib/platform/path');
const fs = require('fs-extra');
const gulp = require('gulp');
function getCompiledPath(taskParameters, moduleInfo) {
const { compiled } = taskParameters.config;
// if path was transmitted as sources path and common compiled folder
// is selected in current build, get path from compiled folder instead of
// sources
if (compiled && moduleInfo.path.startsWith(taskParameters.config.sourcesDirectory)) {
return path.join(compiled, moduleInfo.outputName);
}
return moduleInfo.path;
}
function generateTaskForSymlinkCompiled(taskParameters, moduleInfo, moduleOutput) {
return async function symlinkCompiledModule() {
const compiledPath = getCompiledPath(taskParameters, moduleInfo);
try {
const stats = await fs.lstat(moduleOutput);
if (stats.isSymbolicLink()) {
await fs.unlink(moduleOutput);
} else {
await fs.remove(moduleOutput);
}
} catch (e) {
// nothing to do
}
await fs.ensureSymlink(compiledPath, moduleOutput);
};
}
/**
* Get tasks types by interface modules meta
* should it be skipped or copied, or should be built
* @param{Array} modules list of modules of current project
* @param{boolean} needToSymlinkCompiled select whether compiled module should be skipped or created symlink to compiled
* needed for tasks, that works with already built or copied interface module
*/
function getTasksTypesByModules(modules, needToSymlinkCompiled) {
const meta = {
skip: [],
build: [],
symlink: []
};
for (const moduleInfo of modules) {
if (moduleInfo.compiled && typeof moduleInfo.compiled === 'boolean') {
if (needToSymlinkCompiled) {
meta.symlink.push(moduleInfo);
} else {
meta.skip.push(moduleInfo);
}
} else {
meta.build.push(moduleInfo);
}
}
return meta;
}
/**
* gets gulp order of tasks to execute.
* Returns series of parallel flows of tasks(symlink/skip first, second is build),
* if there are any skip/symlink tasks to execute, otherwise returns parallel
* flow of build tasks
* @param{Array} firstQueue first queue of tasks to execute
* @param{Array} secondQueue second queue of tasks to execute
* @returns {*}
*/
function getParallelTasksOrderByQueue(firstQueue, secondQueue) {
if (firstQueue.length > 0) {
return gulp.series(
gulp.parallel(firstQueue),
gulp.parallel(secondQueue)
);
}
return gulp.parallel(secondQueue);
}
function fillEmptyTasksFlows(tasksFlows) {
const result = {};
Object.keys(tasksFlows).forEach((current) => {
if (tasksFlows[current].length === 0) {
result[current] = [done => done()];
} else {
result[current] = tasksFlows[current];
}
});
return result;
}
module.exports = {
generateTaskForSymlinkCompiled,
getTasksTypesByModules,
getParallelTasksOrderByQueue,
fillEmptyTasksFlows
};