forked from themeum/tutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
209 lines (175 loc) · 5.77 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
var gulp = require("gulp"),
sass = require("gulp-sass"),
sourcemaps = require('gulp-sourcemaps'),
rename = require("gulp-rename"),
prefix = require("gulp-autoprefixer"),
plumber = require("gulp-plumber"),
notify = require("gulp-notify"),
wpPot = require('gulp-wp-pot'),
clean = require("gulp-clean"),
zip = require("gulp-zip"),
fs = require('fs'),
path = require('path'),
build_name = 'tutor-' + require('./package.json').version + '.zip';
var onError = function (err) {
notify.onError({
title: "Gulp",
subtitle: "Failure!",
message: "Error: <%= error.message %>",
sound: "Basso",
})(err);
this.emit("end");
};
var prefixerOptions = {
overrideBrowserslist: ["last 2 versions"],
};
var scss_blueprints = {
tutor_front: {src: "assets/scss/front/index.scss", mode: 'expanded', destination: 'tutor-front.css'},
tutor_front_min: {src: "assets/scss/front/index.scss", mode: 'compressed', destination: 'tutor-front.min.css'},
tutor_admin: {src: "assets/scss/admin-dashboard/index.scss", mode: 'expanded', destination: 'tutor-admin.css'},
tutor_admin_min: {src: "assets/scss/admin-dashboard/index.scss", mode: 'compressed', destination: 'tutor-admin.min.css'},
tutor_setup: {src: "assets/scss/admin-dashboard/tutor-setup.scss", mode: 'expanded', destination: 'tutor-setup.css'},
tutor_setup_min: {src: "assets/scss/admin-dashboard/tutor-setup.scss", mode: 'compressed', destination: 'tutor-setup.min.css'},
tutor_front_dashboard: {src: "assets/scss/frontend-dashboard/index.scss", mode: 'expanded', destination: 'tutor-frontend-dashboard.css'},
tutor_front_dashboard_min: {src: "assets/scss/frontend-dashboard/index.scss", mode: 'compressed', destination: 'tutor-frontend-dashboard.min.css'},
tutor_course_builder: {src: "assets/scss/course-builder/index.scss", mode: 'expanded', destination: 'tutor-course-builder.css'},
tutor_course_builder_min: {src: "assets/scss/course-builder/index.scss", mode: 'compressed', destination: 'tutor-course-builder.min.css'},
};
var task_keys = Object.keys(scss_blueprints);
for(let task in scss_blueprints) {
let blueprint = scss_blueprints[task];
gulp.task(task, function () {
return gulp.src(blueprint.src)
.pipe(plumber({errorHandler: onError}))
.pipe(sourcemaps.init({loadMaps: true, largeFile:true}))
.pipe(sass({outputStyle: blueprint.mode}))
.pipe(prefix(prefixerOptions))
.pipe(rename(blueprint.destination))
.pipe(sourcemaps.write('.', {addComment: process.env._GULP_ENV!='build'}))
.pipe(gulp.dest(blueprint.dest_path || "assets/css"));
});
}
var added_texts = [];
const regex = /__\(\s*(['"])((?:(?!(?<!\\)\1).)+)\1(?:,\s*(['"])((?:(?!(?<!\\)\3).)+)\3)?\s*\)/ig;
const js_files = ['tutor-front', 'tutor-admin', 'tutor-course-builder', 'tutor-setup'].map(f=>'assets/js/'+f+'.js:1').join(', ');
function i18n_makepot(callback, target_dir) {
const parent_dir = target_dir || __dirname;
var translation_texts = '';
// Loop through JS files inside js directory
fs.readdirSync(parent_dir).forEach( function(file_name) {
if(file_name=='node_modules' || file_name.indexOf('.')===0) {
return;
}
var full_path = parent_dir+'/'+file_name;
var stat = fs.lstatSync(full_path);
if(stat.isDirectory()) {
i18n_makepot(null, full_path);
return;
}
// Make sure only js extension file to process
if(stat.isFile() && path.extname(file_name)=='.js' && full_path.indexOf('assets/react')>-1)
{
var codes = fs.readFileSync(full_path).toString();
var lines = codes.split('\n');
// Loop through every single line in the JS file
for(var i=0; i<lines.length; i++) {
var found = lines[i].match(regex);
!Array.isArray(found) ? found=[] : 0;
// Loop through found translations
for(var n=0; n<found.length; n++) {
// Parse the string
var string = found[n];
var first_quote = string.indexOf("'")+1;
var second_quote = string.indexOf("'", first_quote);
var text = string.slice(first_quote, second_quote);
if(added_texts.indexOf(text)>-1) {
// Avoid duplicate entry
continue;
}
added_texts.push(text);
translation_texts+=
'\n#: ' + js_files
+ '\nmsgid "'+text
+'"\nmsgstr ""'
+ '\n';
}
}
}
});
// Finally append the texts to the pot file
var text_domain = path.basename(__dirname);
fs.appendFileSync(__dirname + '/languages/' + text_domain.toLowerCase() + '.pot', translation_texts);
callback ? callback() : 0;
}
gulp.task("watch", function () {
gulp.watch("./**/*.scss", gulp.series(...task_keys));
});
gulp.task('makepot', function () {
return gulp
.src('**/*.php')
.pipe(plumber({
errorHandler: onError
}))
.pipe(wpPot({
domain: 'tutor',
package: 'Tutor LMS'
}))
.pipe(gulp.dest('languages/tutor.pot'));
});
/**
* Build
*/
gulp.task("clean-zip", function () {
return gulp.src("./"+build_name, {
read: false,
allowEmpty: true
}).pipe(clean());
});
gulp.task("clean-build", function () {
return gulp.src("./build", {
read: false,
allowEmpty: true
}).pipe(clean());
});
gulp.task("copy", function () {
return gulp
.src([
"./**/*.*",
"!./build/**",
"!./assets/**/*.map",
"!./assets/react/**",
"!./assets/scss/**",
"!./assets/.sass-cache",
"!./node_modules/**",
"!./v2-library/**",
"!./test/**",
"!./.docz/**",
"!./**/*.zip",
"!.github",
"!./readme.md",
"!.DS_Store",
"!./**/.DS_Store",
"!./LICENSE.txt",
"!./*.lock",
"!./*.js",
"!./*.json",
])
.pipe(gulp.dest("build/tutor/"));
});
gulp.task("make-zip", function () {
return gulp.src("./build/**/*.*").pipe(zip(build_name)).pipe(gulp.dest("./"));
});
/**
* Export tasks
*/
exports.build = gulp.series(
...task_keys,
"clean-zip",
"clean-build",
"makepot",
i18n_makepot,
"copy",
"make-zip"
);
exports.sass = gulp.series(...task_keys);
exports.default = gulp.series(...task_keys, "watch");