-
Notifications
You must be signed in to change notification settings - Fork 140
/
Gulpfile.js
244 lines (221 loc) · 6.11 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
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
/**
* Gulp file by nhamdv.
*/
const gulp = require( 'gulp' );
const cache = require( 'gulp-cache' );
//const lineec = require( 'gulp-line-ending-corrector' );
//const notify = require( 'gulp-notify' );
const notifier = require( 'node-notifier' );
const rename = require( 'gulp-rename' );
const sass = require( 'gulp-sass' )( require( 'sass' ) );
const replace = require( 'gulp-replace' );
const uglify = require( 'gulp-uglify-es' ).default;
const zip = require( 'gulp-vinyl-zip' );
const plumber = require( 'gulp-plumber' );
const uglifycss = require( 'gulp-uglifycss' );
const del = require( 'del' );
const readFile = require( 'read-file' );
const wpPot = require( 'gulp-wp-pot' );
const rtlcss = require( 'gulp-rtlcss' );
let currentVer = null;
const getCurrentVer = function( force ) {
if ( currentVer === null || force === true ) {
const current = readFile.sync( 'learnpress.php', { encoding: 'utf8' } ).match( /Version:\s*(.*)/ );
currentVer = current ? current[ 1 ] : null;
}
return currentVer;
};
const releasesFiles = [
'./**',
'assets/src/**',
'!assets/src/scss/**',
'!assets/src/app/**',
'!assets/**/*.js.map',
'!assets/**/*.dev.js',
'!assets/**/*bak*',
'!assets/**/*bk*',
'!assets/**/*.asset.php',
'!assets/**/*.deps.json',
'!releases/**',
'!tests/**',
'!tools/**',
'!node_modules/**',
'!vendor/**',
'!*.json',
'!*.js',
'!*.map',
'!*.xml',
'!*.sublime-project',
'!*.sublime-workspace',
'!*.log',
'!*.DS_Store',
'!*.gitignore',
'!TODO',
'!*.git',
'!*.ftppass',
'!*.DS_Store',
'!sftp.json',
'!composer.lock',
'!*.md',
'!package.lock',
'!*.dist',
'!*.xml',
'!editorconfig',
'!.travis.yml',
'!.babelrc',
'!inc/**/*.http',
'!packages/**',
'!languages/strings/**',
'!languages/learnpress-js.pot',
];
const errorHandler = ( err ) => {
console.error( 'Error:', err.message );
this.emit( 'end' );
};
// Clear cache.
gulp.task( 'clearCache', ( done ) => {
return cache.clearAll( done );
} );
gulp.task( 'styles', () => {
return gulp
.src( [ 'assets/src/scss/**/*.scss' ] )
.pipe( plumber( errorHandler ) )
// .pipe( sourcemaps.init() )
.pipe( sass.sync().on( 'error', sass.logError ) )
// .pipe( sourcemaps.write( './' ) )
//.pipe( lineec() )
.pipe( gulp.dest( 'assets/css' ) )
.pipe( rtlcss() )
.pipe( rename( { suffix: '-rtl' } ) )
.pipe( gulp.dest( 'assets/css' ) );
} );
// Watch sass
gulp.task( 'watch', gulp.series( 'clearCache', () => {
gulp.watch( [ 'assets/src/scss/**/*.scss' ], gulp.parallel( 'styles' ) );
} ) );
// Min CSS frontend.
gulp.task( 'mincss', () => {
return gulp
.src( [ 'assets/css/**/*.css', '!assets/css/**/*.min.css' ] )
.pipe( rename( { suffix: '.min' } ) )
.pipe( uglifycss() )
//.pipe( lineec() )
.pipe( gulp.dest( 'assets/css' ) );
} );
// Clear JS folder.
gulp.task( 'clearJsAdmin', () => {
return del( './assets/js/admin/**' );
} );
gulp.task( 'clearJsFrontend', () => {
return del( './assets/js/frontend/**' );
} );
// Min JS.
gulp.task( 'minJsAdmin', () => {
return gulp
.src(
[
'assets/src/js/admin/**/*.js',
'!assets/src/js/admin/admin.js',
'!assets/src/js/admin/admin-order.js',
'!assets/src/js/admin/admin-tools.js',
'!assets/src/js/admin/addons.js',
'!assets/src/js/admin/admin-statistic.js',
'!assets/src/js/admin/tools/assign-user-course.js',
]
)
.pipe(
rename( {
suffix: '.min',
} )
)
.pipe( uglify() )
//.pipe( lineec() )
.pipe( gulp.dest( 'assets/js/admin' ) );
} );
gulp.task( 'minJsFrontend', () => {
return gulp
.src( [ 'assets/src/js/frontend/**/*.js' ] )
.pipe(
rename( {
suffix: '.min',
} )
)
.pipe( uglify() )
//.pipe( lineec() )
.pipe( gulp.dest( 'assets/js/frontend' ) );
} );
// Clean folder to releases.
gulp.task( 'cleanReleases', () => {
return del( './releases/**' );
} );
// Copy folder to releases.
gulp.task( 'copyReleases', () => {
gulp.src( [ 'vendor/autoload.php' ] ).pipe( gulp.dest( './releases/learnpress/vendor' ) );
gulp.src( [ 'vendor/composer/**' ] ).pipe( gulp.dest( './releases/learnpress/vendor/composer' ) );
return gulp.src( releasesFiles ).pipe( gulp.dest( './releases/learnpress/' ) );
} );
// Update file Readme
gulp.task( 'updateReadme', () => {
return gulp.src( [ 'readme.txt' ] )
.pipe( replace( /Stable tag: (.*)/g, 'Stable tag: ' + getCurrentVer( true ) ) )
.pipe( gulp.dest( './releases/learnpress/', { overwrite: true } ) );
} );
// Zip learnpress in releases.
gulp.task( 'zipReleases', () => {
const version = getCurrentVer();
return gulp
.src( './releases/learnpress/**', { base: './releases/' } )
.pipe( zip.dest( './releases/learnpress.' + version + '.zip' ) );
} );
// Notice.
gulp.task( 'noticeReleases', () => {
const version = getCurrentVer();
return gulp.src( './' )
//.pipe( gulp.dest( './' ) )
.on( 'end', function() {
notifier.notify( {
title: 'LearnPress',
message: 'LearnPress version ' + version + ' build successfully!',
sound: 'Bottle',
} );
} );
} );
gulp.task( 'makepot', function() {
return gulp.src( [ './**/*.php', '!node_modules/**', '!releases/**', '!vendor/**' ] )
.pipe( wpPot( {
domain: 'learnpress',
package: 'learnpress',
} ) )
.pipe( gulp.dest( './languages/learnpress.pot' ) );
} );
gulp.task(
'build',
gulp.series(
'clearCache',
'clearJsAdmin',
//'clearJsFrontend',
'minJsAdmin',
//'minJsFrontend',
'mincss',
'cleanReleases',
'copyReleases',
'updateReadme',
'zipReleases',
( done ) => {
done();
}
)
);
gulp.task( 'release', gulp.series( 'build', 'noticeReleases', ( done ) => {
done();
} ) );
gulp.task( 'updatePot', () => {
return gulp
.src( [ './languages/learnpress.pot' ] )
.pipe( replace( /(assets\/.*\/frontend\/quiz)(.*)(.js)/g, 'assets/js/dist/frontend/quiz.min.js' ) )
.pipe( replace( /(assets\/.*\/frontend\/question)(.*)(.js)/g, 'assets/js/dist/frontend/quiz.min.js' ) )
.pipe( replace( /(assets\/.*\/frontend\/modal)(.*)(.js)/g, 'assets/js/dist/frontend/quiz.min.js' ) )
.pipe( replace( /(assets\/.*\/frontend\/profile)(.*)(.js)/g, 'assets/js/dist/frontend/profile.min.js' ) )
.pipe( replace( /(assets\/.*\/admin\/pages\/tools)(.*)(.js)/g, 'assets/js/dist/admin/pages/tools.min.js' ) )
.pipe( gulp.dest( './languages/' ) );
} );