-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
149 lines (135 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
'use strict';
var gulp = require('gulp'),
fs = require('fs'),
$ = require('gulp-load-plugins')(),
config = {
dest: 'dist',
script: 'scripts.min.js',
style: 'styles.min.css',
translations: 'translations.js',
templates: 'templates.js'
};
config.get = function(key) {
return (config.dest !== '.' ? config.dest + '/' : '') + config[key];
}
gulp.task('pot', function () {
return gulp.src(['*.html', '*.js', 'views/*.html'])
.pipe($.angularGettext.extract('classic.pot', {}))
.pipe(gulp.dest('po/'));
});
// copy the vendors needed files in the destination folder.
gulp.task('copy_vendors', function () {
return gulp.src(['styles/default-skin/*.*'])
.pipe(gulp.dest(config.dest));
});
gulp.task('translations', function () {
return gulp.src('po/*.po')
.pipe($.angularGettext.compile({format: 'json'}))
.pipe($.extend('translations.json')) // use .json extension for wrap plugin to load content
.pipe($.wrap(
'\'use strict\';\n\n' +
'angular.module(\'gettext\').run([\'gettextCatalog\', function (gettextCatalog) {\n' +
'/* jshint -W100,-W109 */\n' +
'<% _.forEach(contents, function(translations, lang) { %> gettextCatalog.setStrings(\'<%= lang %>\', <%= JSON.stringify(translations, undefined, 2) %>);\n<% }); %>' +
'/* jshint +W100,+W109 */\n' +
'}]);'))
.pipe($.rename(config.translations))
.pipe(gulp.dest(config.dest));
});
gulp.task('templates', function () {
return gulp.src(['views/*.html'])
.pipe($.htmlmin({
collapseWhitespace: true,
removeComments: true,
})).pipe($.angularTemplatecache({
root:'views/',
module: 'theme'
}))
.pipe($.rename(config.templates))
.pipe(gulp.dest(config.dest));
});
// build `scripts` and `styles` for theme
// excludes external links, and runs `translations` and `templates` tasks before running build.
gulp.task('build', ['translations', 'templates', 'copy_vendors'], function() {
var externalUrlCheck = /^(http(s)?:)?\/\//;
var theme = JSON.parse(fs.readFileSync('./theme.json')),
build = {
from: {
scripts: [],
styles: []
}, to: {
scripts: [],
styles: []
}
};
if(theme.devScripts && theme.devScripts.length) {
theme.devScripts.forEach(function(script) {
// check if it is an external url, if so add it like that in final scripts.
if(externalUrlCheck.test(script) || (script === 'parent-iframe.js')) {
build.to.scripts.push(script);
} else {
build.from.scripts.push(script);
}
});
if(build.from.scripts.indexOf(config.get('translations')) === -1) {
build.from.scripts.push(config.get('translations'));
}
if(build.from.scripts.indexOf(config.get('templates')) === -1) {
build.from.scripts.push(config.get('templates'));
}
gulp.src(build.from.scripts)
.pipe($.concat(config.script))
.pipe($.uglify())
.pipe(gulp.dest(config.dest));
build.to.scripts.push(config.get('script'));
theme.scripts = build.to.scripts;
}
if(theme.devStyles && theme.devStyles.length) {
theme.devStyles.forEach(function(style) {
// check if it is an external url, if so add it like that in final scripts.
if(externalUrlCheck.test(style)) {
build.to.styles.push(style);
} else {
build.from.styles.push(style);
}
});
gulp.src(build.from.styles)
.pipe($.concat(config.style))
.pipe($.cleanCss())
.pipe(gulp.dest(config.dest));
build.to.styles.push(config.get('style'));
theme.styles = build.to.styles;
}
// automatic `patch` increase in `version`.
if(theme.version) {
var version = theme.version.split('.');
if(version[2]) {
version[2] = parseInt(version[2], 10) + 1;
}
theme.version = version.join('.');
}
fs.writeFileSync('./theme.json', JSON.stringify(theme, null, 4));
});
var zipTask = function(){
var theme = JSON.parse(fs.readFileSync('./theme.json')),
name = 'lb-theme';
if(theme.name) {
name = name + '-' + theme.name;
}
if(theme.version) {
name = name + '-' + theme.version;
}
var zipdest = '..', i = process.argv.indexOf("--zipdest");
if(i>-1) {
zipdest = process.argv[i+1];
}
return gulp.src(['./**','!./node_modules','!./node_modules/**', '!./.git/**', '!./__MACOSX', '!./__MACOSX/**', '!./.DS_Store'])
// in this way it goes from 7sec to 300ms zip time,
// but the folder structure isn't keept.
// @TODO: check more.
// return gulp.src(['./dist/**','./images/**', './po/**','./styles/**','./vendors/**', './views/**', '*.*'])
.pipe($.zip(name + '.zip'))
.pipe(gulp.dest(zipdest));
};
gulp.task('zip', zipTask);
gulp.task('make',['build'], zipTask);