-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
206 lines (165 loc) · 5.12 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
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
var rimraf = require('rimraf'),
pump = require('pump'),
colors = require('ansi-colors'),
argv = require('minimist')(process.argv.slice(2)),
log = require('fancy-log');
var gulp = require('gulp'),
noop = require('gulp-noop'),
changed = require('gulp-changed'),
cache = require('gulp-cached'),
progeny = require('gulp-progeny'),
filter = require('gulp-filter'),
rename = require('gulp-rename'),
sourcemaps = require('gulp-sourcemaps'),
stylus = require('gulp-stylus'),
autoprefixer = require('gulp-autoprefixer'),
uglify = require('gulp-uglify'),
jshint = require('gulp-jshint');
// ENV Block
var Prod = argv.p || argv.prod;
var Lint = argv.l || argv.lint;
var Maps = argv.m || argv.maps;
var Force = argv.f || argv.force;
var Reset = argv.reset;
if (!Force && !Reset) log([
'Lint ',
(Lint ? colors.green('enabled') : colors.red('disabled')),
', sourcemaps ',
(Maps ? colors.green('enabled') : colors.yellow('disabled')),
', build in ',
(Prod ? colors.underline.green('production') : colors.underline.yellow('development')),
' mode.',
].join(''));
// Flags Block
var build_flags = {
'-p --prod': 'Builds in ' + colors.underline.green('production') + ' mode (minification, etc).',
'-d --dev': 'Builds in ' + colors.underline.yellow('development') + ' mode (default).',
'-l --lint': 'Lint JavaScript code.',
'-m --maps': 'Generate sourcemaps files.'
};
var clean_flags = {
'-f --force': 'Force clean public data.',
'--reset': 'Reset project to initial state.'
};
// Handlers Block
var errorLogger = function(err) {
if (err) log([
'',
colors.bold.inverse.red('---------- ERROR MESSAGE START ----------'),
'',
(colors.red(err.name) + ' in ' + colors.yellow(err.plugin)),
'',
err.message,
colors.bold.inverse.red('----------- ERROR MESSAGE END -----------'),
''
].join('\n'));
};
var watchLogger = function(e_type) {
return function(path, stats) {
log([
'File ',
colors.green(path.replace(__dirname + '/', '')),
' was ',
colors.yellow(e_type),
', running tasks...'
].join(''));
};
};
var cacheClean = function(path) {
delete cache.caches.scripts[path];
delete cache.caches.styles[path];
};
// Paths Block
var paths = {
styles: {
src: 'apps/**/src/styl/**/*.styl',
dest: 'public/build'
},
scripts: {
src: 'apps/**/src/js/**/*.js',
dest: 'public/build'
},
stuff: {
src: 'apps/**/stuff/**',
dest: 'public/stuff'
},
clean: {
base: ['public/build/**', 'public/stuff/**'],
force: ['public/preview/**/*', 'uploads/**/*'],
reset: ['node_modules/**', 'public/cdn/**']
}
};
// Tasks Block
function clean(callback) {
var clean = paths.clean.base;
if (Force) clean = clean.concat(paths.clean.force);
if (Reset) clean = [].concat(paths.clean.base, paths.clean.force, paths.clean.reset);
return rimraf('{' + clean.join(',') + '}', callback);
}
function stuff() {
return pump([
gulp.src(paths.stuff.src),
changed(paths.stuff.dest),
rename(function(path) { path.dirname = path.dirname.replace('/stuff', ''); }),
gulp.dest(paths.stuff.dest)
], errorLogger);
}
function styles() {
return pump([
gulp.src(paths.styles.src),
cache('styles'),
progeny(),
filter(['**/*.styl', '!**/_*.styl']),
Maps ? sourcemaps.init({ loadMaps: true }) : noop(),
stylus({ compress: Prod }),
autoprefixer({
overrideBrowserslist: ['last 12 versions'],
cascade: !Prod
}),
Maps ? sourcemaps.write('.') : noop(),
rename(function(path) { path.dirname = path.dirname.replace('/src/styl', '/css'); }),
gulp.dest(paths.styles.dest)
], errorLogger);
}
function scripts() {
return pump([
gulp.src(paths.scripts.src),
cache('scripts'),
Lint ? jshint({ laxbreak: true, expr: true, '-W041': false }) : noop(),
Lint ? jshint.reporter('jshint-stylish') : noop(),
Maps ? sourcemaps.init({ loadMaps: true }) : noop(),
Prod ? uglify() : noop(),
Maps ? sourcemaps.write('.', { mapSources: function(path) { return path.split('/').slice(-1)[0]; } }) : noop(),
rename(function(path) { path.dirname = path.dirname.replace('/src/js', '/js'); }),
gulp.dest(paths.scripts.dest)
], errorLogger);
}
function watch() {
gulp.watch(paths.scripts.src, scripts)
.on('unlink', cacheClean)
.on('change', watchLogger('changed'))
.on('add', watchLogger('added'))
.on('unlink', watchLogger('removed'));
gulp.watch(paths.styles.src, styles)
.on('unlink', cacheClean)
.on('change', watchLogger('changed'))
.on('add', watchLogger('added'))
.on('unlink', watchLogger('removed'));
gulp.watch(paths.stuff.src, stuff)
.on('change', watchLogger('changed'))
.on('add', watchLogger('added'))
.on('unlink', watchLogger('removed'));
}
// Exports Block
var task_clean = clean;
clean.description = 'Clean project folders';
clean.flags = clean_flags;
var task_build = gulp.series(clean, gulp.parallel(styles, scripts, stuff));
task_build.description = 'Build all...';
task_build.flags = build_flags;
var task_default = gulp.series(clean, gulp.parallel(styles, scripts, stuff), watch);
task_default.description = 'Build and start watching';
task_default.flags = build_flags;
exports.build = task_build;
exports.default = task_default;
exports.clean = task_clean;