-
Notifications
You must be signed in to change notification settings - Fork 2
/
Glass.js
89 lines (75 loc) · 2.41 KB
/
Glass.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
'use strict';
const {
isString,
isPlainObject,
isArray,
isFunction,
} = require('lodash');
const glob = require('glob');
const gulp = require('gulp');
const path = require('path');
class Glass {
/**
* Constructor
*
* @param {Object?} configuration
* @returns {Glass}
*/
constructor(configuration) {
this.taskPaths = [];
this.filePattern = '**/*.js';
this.sort = undefined;
this.nameResolver = (file) => path.basename(file, path.extname(file));
if (isString(configuration)) {
this.taskPaths = [configuration];
} else if (isPlainObject(configuration)) {
if (isString(configuration.taskPaths)) {
this.taskPaths = [configuration.taskPaths];
}
if (isArray(configuration.taskPaths)) {
this.taskPaths = configuration.taskPaths;
}
if (isString(configuration.filePattern)) {
this.filePattern = configuration.filePattern;
}
if (isFunction(configuration.nameResolver)) {
this.nameResolver = configuration.nameResolver;
}
if (isFunction(configuration.sort)) {
this.sort = configuration.sort;
}
}
}
/**
* Loads all gulp tasks
*
* @returns {gulp}
*/
loadTasks() {
// Consolidating file list from task paths
let files = this.taskPaths.reduce((accumulatedFiles, taskPath) => {
const filesInPath = glob.sync(this.filePattern, { cwd: taskPath });
return [
...accumulatedFiles,
...filesInPath.map((file) => path.join(path.resolve(taskPath), file)),
];
}, []);
// Sorting files
if (this.sort) {
files = files.sort(this.sort);
}
// Registering tasks
files.forEach((file) => {
// eslint-disable-next-line global-require,import/no-dynamic-require
const taskFunction = require(file);
const taskNames = isArray(taskFunction.alias)
? taskFunction.alias
: isString(taskFunction.alias)
? [taskFunction.alias]
: [this.nameResolver(file)];
taskNames.forEach((taskName) => gulp.task(taskName, taskFunction));
});
return gulp;
}
}
module.exports = Glass;