-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
177 lines (155 loc) · 3.94 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
// Include gulp
var
gulp = require('gulp'),
gulpif = require('gulp-if'),
gutil = require('gulp-util'),
gulpIgnore = require('gulp-ignore'),
// Read Files
fs = require("fs"),
header = require("gulp-header"),
// Add Config
config = {
baseDir: 'src/**/',
jsPattern: '*.js',
htmlPattern: '*.html',
sassPattern: '*.scss'
},
// To Detect Any File Change
patterns = [
config.baseDir + config.jsPattern,
config.baseDir + config.sassPattern,
config.baseDir + config.htmlPattern
],
// Include Our Plugins
sass = require('gulp-sass'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
jshint = require('gulp-jshint'),
prettify = require('gulp-jsbeautifier'),
// Use Strict
iife = require("gulp-iife"),
replace = require("gulp-replace");
/*******************************************
* GULP TASKS
*******************************************/
// Lint Task
gulp.task('lint', function() {
return gulp.src(config.baseDir + config.jsPattern)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// Compile Our Sass
gulp.task('sass', compileSASS);
// Concatenate & Minify JS
gulp.task('minify', function() {
return gulp.src(config.baseDir + config.jsPattern)
.pipe(concat('angular-checkbox-tree-grid.js'))
.pipe(replace(/'use strict';/g, ' '))
.pipe(iife())
.pipe(header(getHeader()))
.pipe(gulp.dest('dist'))
.pipe(rename('angular-checkbox-tree-grid.min.js'))
.pipe(uglify())
.pipe(header(getHeader()))
.pipe(gulp.dest('dist'));
});
//Watch Files For Changes
gulp.task('watch', function() {
gutil.log('Watching for changes!');
gulp.watch(patterns).on("change", prettifyFile);
});
//Prettify JS/HTML/SCSS files
gulp.task('prettify', function() {
gulp.src(patterns)
.pipe(gulpIgnore.exclude(IgnoreSass))
.pipe(prettify({
indent_size: 2
}))
.pipe(gulp.dest(function(file) {
return file.base;
}));
});
/*******************************************
* SHARED METHODS
*******************************************/
/**
* @description - Add licence header to the scripts.
*
* @return {string} - License verbiage
*/
function getHeader() {
return "" + fs.readFileSync('license.js', 'utf8');
}
/**
* @param {file} - file object
*
* @description - gulp ignore files wrapper.
*
* @return {boolean} - True if the file path
* need to be ignored.
*/
function IgnoreSass(file) {
if (file.path.indexOf('_variables.scss') > -1) {
return true;
}
return false;
}
/**
* @param {file} - file object
*
* @description - Checks the current file formats
* and gulp ignore.
*
* @return {boolean} - True if the file format is
* '.scss' and the current file is not included in
* gulp ignore list.
*/
function isSASS(file) {
var isSCSS = file.path.indexOf("scss") > -1;
var isExcluded = IgnoreSass(file);
if (isSCSS && !isExcluded) {
return true;
}
return false;
}
/**
* @param {file} - file object
*
* @description - Run formatter on the modified file
* and checks if the file format is '.scss' and executes
* compile sass task when the file has been prettified.
* Overwrites the existing file.
*/
function prettifyFile(file) {
var runSASS = isSASS(file);
gulp.src(file.path)
.pipe(gulpIgnore.exclude(IgnoreSass))
.pipe(prettify({
indent_size: 2
}))
.pipe(gulp.dest(function(file) {
return file.base;
}))
.on('end', function(e) {
if (runSASS) {
compileSASS();
}
});
}
/**
* @description - Compiles scss to css and moves it to
* 'assets/css/stylesheets' directory.
*/
function compileSASS() {
console.log("Compiling!");
gutil.log('SASS will be Compiled to src/css');
return gulp.src(config.baseDir + config.sassPattern)
.pipe(sass({
outputStyle: 'compressed'
})
.on('error', sass.logError))
.pipe(gulp.dest(config.baseDir + 'src/css'));
}
// Default Task
gulp.task('default', ['prettify']);