This repository was archived by the owner on Oct 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
257 lines (237 loc) · 7.05 KB
/
handler.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
const { parse_args, current_path } = require( './functions' );
const $gulp = require( 'gulp' );
const $webpack = require( 'webpack-stream' );
const $revert_path = require( 'gulp-revert-path' );
const supportsColor = require( 'supports-color' );
const fancyLog = require( 'fancy-log' );
const $rename = require( 'gulp-rename' );
const $scss = require( 'gulp-sass' );
const $minify_css = require( 'gulp-clean-css' );
const $autoprefixer = require( 'gulp-autoprefixer' );
const $uglify = require( 'gulp-uglify' );
const $babel = require( 'gulp-babel' );
const $combine_files = require( 'gulp-combine-files' );
const $concat = require( 'gulp-concat' );
/**
* Gulp Handler.
*/
class GulpHandler {
/**
*
* @param $src
* @param $file_config
* @param $global_config
*/
constructor( $src, $file_config, $global_config ) {
this.src = $src;
this.config = $file_config;
this.global = $global_config;
this.instance = $gulp.src( this.src );
}
/**
* Returns Config Values.
* @param $type
* @param $config
* @returns {null|JS_Parse_Args|*}
*/
get_config( $type = '', $config = {} ) {
if( typeof this.global[ $type ] !== 'undefined' ) {
$config = ( true === $config ) ? {} : $config;
if( typeof $config === 'string' ) {
return ( typeof this.global[ $config ] !== 'undefined' ) ? this.global[ $config ] : $config;
} else {
let $global_config = this.global[ $type ];
return parse_args( $config, $global_config, false );
}
}
return ( true === $config ) ? null : $config;
}
/**
* Inits Workout.
* @returns {Promise<void>}
*/
async init() {
fancyLog( ' ' );
fancyLog( this.src + ' Started ' );
for( let $id in this.config ) {
if( this.config.hasOwnProperty( $id ) && this.config[ $id ] !== false && typeof GulpHandler.prototype[ $id ] === 'function' ) {
await this[ $id ]( this.config[ $id ] );
}
}
await this.save();
fancyLog( ' ' );
}
/**
* Saves Output.
* @returns {Promise<*>}
*/
async save() {
return new Promise( ( resolve ) => {
if( typeof this.config.rename !== 'undefined' ) {
fancyLog( 'Renaming To ' + this.config.rename );
this.instance = this.instance.pipe( $rename( this.config.rename ) );
}
this.instance = this.instance.pipe( $gulp.dest( current_path + '/' + this.config.dist ) ).on( 'end', () => {
fancyLog( 'File Saved In ' + this.config.dist );
fancyLog( '' );
resolve();
} );
} );
}
/**
* Triggers Webpack.
* @param $arg
* @returns {Promise<*>}
*/
async webpack( $arg ) {
fancyLog( '---> Webpack Start' );
let $options = this.get_config( 'webpack', $arg );
let webpack_done = function( err, stats ) {
if( err ) {
// The err is here just to match the API but isnt used
return;
}
stats = stats || {};
fancyLog( stats.toString( {
colors: supportsColor.stdout.hasBasic,
hash: false,
timings: false,
chunks: false,
chunkModules: false,
modules: false,
children: true,
version: true,
cached: false,
cachedAssets: false,
reasons: false,
source: false,
errorDetails: false
} ) );
};
return new Promise( ( resolve ) => {
this.instance = this.instance.pipe( $revert_path() );
this.instance = this.instance.pipe( $webpack( $options, null, ( err, stats ) => {
webpack_done( err, stats );
fancyLog( '---> Webpack End' );
resolve();
} ) );
this.instance = this.instance.pipe( $revert_path() );
} );
}
/**
* Triggers SCSS Compile.
* @param $arg
* @returns {*}
*/
async scss( $arg ) {
return new Promise( ( resolve ) => {
fancyLog( '---> SCSS Start' );
this.instance = this.instance.pipe( $scss( this.get_config( 'scss', $arg ) ).on( 'error', fancyLog ) );
fancyLog( '---> SCSS End' );
resolve();
} );
}
/**
* Triggers minify Compile.
* @param $arg
* @returns {*}
*/
async minify( $arg ) {
return new Promise( ( resolve ) => {
fancyLog( '---> Minify Start' );
let $options = this.get_config( 'minify', $arg );
this.instance = this.instance.pipe( $minify_css( $options.options.args, $options.options.callback ) )
.on( 'error', fancyLog );
fancyLog( '---> Minify End' );
resolve();
} );
}
/**
* Triggers Autoprefixer Compile.
* @param $arg
* @returns {*}
*/
async autoprefixer( $arg ) {
return new Promise( ( resolve ) => {
fancyLog( '---> Autoprefixer Start' );
let $options = this.get_config( 'autoprefixer', $arg );
this.instance = this.instance.pipe( $autoprefixer( $options ) )
.on( 'error', fancyLog );
fancyLog( '---> Autoprefixer End' );
resolve();
} );
}
/**
* Triggers uglify Compile.
* @param $arg
* @returns {*}
*/
async uglify( $arg ) {
return new Promise( ( resolve ) => {
fancyLog( '---> Uglify Start' );
let $options = this.get_config( 'uglify', $arg );
this.instance = this.instance.pipe( $uglify( $options ) )
.on( 'error', fancyLog );
fancyLog( '---> Uglify End' );
resolve();
} );
}
/**
* Triggers babel Compile.
* @param $arg
* @returns {*}
*/
async babel( $arg ) {
return new Promise( ( resolve ) => {
fancyLog( '---> Bable Start' );
let $options = this.get_config( 'babel', $arg );
this.instance = this.instance.pipe( $babel( $options ) )
.on( 'error', fancyLog );
fancyLog( '---> Babel End' );
resolve();
} );
}
/**
* Triggers combine_files Compile.
* @param $arg
* @returns {*}
*/
async combine_files( $arg ) {
return new Promise( ( resolve ) => {
fancyLog( '---> Combine Files Start' );
let $options = this.get_config( 'combine_files', $arg );
this.instance = this.instance.pipe( $combine_files( $options ) );
fancyLog( '---> Combine Files End' );
resolve();
} );
}
/**
* Triggers Concat Compile.
* @param $arg
* @returns {*}
*/
async concat( $arg ) {
return new Promise( ( resolve ) => {
fancyLog( '---> Concat Start' );
let $options = this.get_config( 'concat', $arg );
if( typeof $options.options === 'object' && typeof $options.options.filename !== 'undefined' ) {
if( $options.src ) {
this.instance = this.instance.pipe( $gulp.src( $options.src ) )
.pipe( $concat( $options.options.filename, $options.options ) )
.on( 'error', fancyLog );
} else {
this.instance = this.instance.pipe( $concat( $options.options.filename, $options.options ) )
.on( 'error', fancyLog );
}
} else if( typeof $options === 'string' ) {
this.instance = this.instance.pipe( $concat( $options ) )
.on( 'error', fancyLog );
}
fancyLog( '---> Concat End' );
resolve();
} );
}
}
module.exports = {
handler: GulpHandler,
};