-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (82 loc) · 2.66 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
require('colors');
let gulp = require('gulp');
let minimist = require('minimist');
let dotenv = require('dotenv');
let plumber = require('gulp-plumber');
let notifier = require('node-notifier');
let through = require('through2');
let browserSync = require('browser-sync').create();
let projectRoot = process.cwd();
let args = minimist(process.argv.slice(2));
let env = dotenv.config().parsed;
let isWatching = args._.includes('watch');
if ( !env ) throw new Error('A .env file is required!'.red);
// Environments
let isLocal = /^(local)$/i.test(env.ENV);
let isDevelopment = /(dev|development)/i.test(env.ENV);
let isStaging = /s(taging)/i.test(env.ENV);
let isProduction = !env.ENV || /(production)/i.test(env.ENV) ? true : false;
let tasks = [];
/**
* --------------------------------------------------------------------------------------------
* EXPORTS
* --------------------------------------------------------------------------------------------
*/
module.exports = {
args,
env,
isWatching,
browserSync,
// Environments
isLocal,
isDevelopment,
isStaging,
isProduction,
get tasks() { return tasks },
projectRoot: (path = '') => `${projectRoot}/${path.trim('/')}`,
onStreamError: function (title = null) {
return plumber({
errorHandler: error => {
notifier.notify({
'title': title || 'Error in Pipe!',
'message': error.message,
});
console.log(title.red);
console.log(error.message);
console.log(error.stack.gray);
}
});
},
gulpif: function ( condition = false, ifStream, elseStream ) {
elseStream = typeof elseStream === 'function' ? elseStream :
through.obj(function (file, enc, callback) {
this.push(file);
callback();
});
return condition ? ifStream : elseStream;
},
registerTasks: function ( tasksList = [], tasksDirectory = 'gulp' ) {
// Parse task files
tasks = tasksList.map( taskName => {
tasksDirectory = tasksDirectory.trim('/');
let task = require(`${projectRoot}/${tasksDirectory}/${taskName}.js`);
return {
name: taskName,
fn: task.fn,
watchFiles: task.watchFiles,
watchFn: task.watchFn,
deps: task.deps,
};
});
// Register to gulp
for ( let task of tasks ) {
if ( !task.fn ) continue;
task.deps = Array.isArray(task.deps) ? task.deps : [];
gulp.task(task.name, task.deps, task.fn)
}
// Default task
gulp.task('default', tasks.map( task => task.name ));
// Watch task
if ( isWatching ) require(`${projectRoot}/${tasksDirectory}/watch.js`);
},
};