forked from thirtybees/niara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
191 lines (158 loc) · 4.35 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
var gulp = require('gulp');
var del = require('del');
var mkdirp = require('mkdirp');
var glob = require('glob-all');
var fs = require('fs-extra');
var zip = require('gulp-zip');
var jscs = require('gulp-jscs');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var notify = require('gulp-notify');
var bourbon = require('node-bourbon');
var gulpif = require('gulp-if');
var rename = require('gulp-rename');
/** @var {{ themeName, themeModulePrefix, sourcemaps }} options **/
var options = require('./package.json').options;
var createFolders = [
'./cache/',
'./pdf/',
'.pdf/lang/'
];
var copyIndexIgnore = [];
var cleanUp = [
'..sass-cache/',
'.cache/*',
'.css/**/*.css.map'
];
gulp.task('create-folders', function(callback) {
var total = createFolders.length;
var done = 0;
if (total < 1 && callback) {
callback();
}
createFolders.forEach(function(path) {
mkdirp(path, function(err) {
if (err) {
console.error(err);
} else {
console.log('Created folder : ' + path);
}
done++;
if (done == total && callback) {
callback();
}
});
});
});
function displayNotification(msg) {
return notify(msg);
}
gulp.task('compile-css', function() {
return gulp.src('./sass/**/*.scss')
.pipe(gulpif(options.sourcemaps, sourcemaps.init()))
.pipe(
sass({
includePaths: bourbon.includePaths,
outputStyle: 'expanded',
precision: 8
}).on('error', sass.logError)
)
.pipe(gulpif(options.sourcemaps, sourcemaps.write('./')))
.pipe(gulp.dest('./css/'))
.pipe(displayNotification({
message: 'Theme CSS compilation successful for ' + options.themeName,
onLast: true
}));
});
gulp.task('watch-sass', function() {
gulp.watch('./sass/**/*.scss', ['compile-css']);
});
gulp.task('clean-up', function() {
return del(cleanUp).then(function() {
console.log('Deleted files and folders:\n', cleanUp.join('\n'));
});
});
gulp.task('copy-index', function(callback) {
var total;
var done = 0;
glob(['**/'], {ignore: copyIndexIgnore}, function(err, folders) {
total = folders.length;
if (total < 1 && callback) {
callback();
}
// console.log('Copy to folders: \n', folders.join('\n'));
folders.forEach(function(folder) {
fs.copy('index.php.copy', folder + '/index.php', function(err) {
if (err) {
return console.error(err);
}
done++;
if (done == total && callback) {
callback();
}
});
});
});
});
gulp.task('format-js', function() {
return gulp.src([
'./gulpfile.js',
'./js/**/*.js',
'!./js/**/*.min.js',
'!./js/autoload/**/*.js',
'!./js/debug/**/*.js'
])
.pipe(jscs({fix: false}))
.pipe(gulp.dest('./js/'));
});
gulp.task('scan-translations', function(cb) {
glob(['**/*.tpl'], [], function(er, files) {
var brokenTranslations = [];
var totalFiles = files.length;
var scannedFiles = 0;
files.forEach(function(file) {
var translationContext = getTemplateContext(file);
fs.readFile(file, 'utf-8', function(err, contents) {
var brokenTranslationsInFile = listBrokenTranslationStrings(contents, translationContext);
if (brokenTranslationsInFile.length) {
brokenTranslationsInFile.unshift(file);
brokenTranslations.push(brokenTranslationsInFile);
}
if (++scannedFiles == totalFiles) {
console.log('Broken Smarty translation strings in theme templates:');
console.log(brokenTranslations);
cb && cb();
}
});
});
});
});
function getTemplateContext(templateFilePath) {
var bits = templateFilePath.split('/');
// modules/mymodule/views/templates/hook/block.tpl
if (typeof bits[0] == 'string' && bits[0] == 'modules') {
return bits[1];
}
// 'themes/themename/modules/blockwishlist/my-account.tpl',
if (bits[0] == 'themes' && typeof bits[3] == 'string') {
return bits[3];
}
// 'themes/themename/product.tpl
return '';
}
function listBrokenTranslationStrings(smartyTplCode, translationContext) {
var translations = smartyTplCode.match(/\{l\s+s=['"].+?}/g);
if (!translations) {
return [];
}
return translations.filter(function(t) {
if (translationContext.length) {
// Doesn't have correct context
return !((new RegExp('mod=[\'"]' + translationContext + '[\'"]')).test(t));
} else {
// Has context even though it shouldn't
return !!/mod=['"]/.test(t);
}
});
}
gulp.task('default', ['compile-css']);