forked from itsjavi/bootstrap-colorpicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
213 lines (179 loc) · 5.95 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
'use strict';
const gulp = require('gulp');
const del = require('del');
const webpack = require('webpack-stream');
const sass = require('gulp-sass');
const ghPages = require('gh-pages');
const handlebars = require('gulp-compile-handlebars');
const handlebarsLayouts = require('handlebars-layouts');
const rename = require('gulp-rename');
const header = require('gulp-header');
const shell = require('gulp-shell');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const cleanCss = require('gulp-clean-css');
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('./package.json'));
const replace = require('gulp-string-replace');
handlebars.Handlebars.registerHelper(handlebarsLayouts(handlebars.Handlebars));
let banner = `/*!
* Bootstrap Colorpicker - <%= pkg.description %>
* @package <%= pkg.name %>
* @version v<%= pkg.version %>
* @license <%= pkg.license %>
* @link <%= pkg.homepage %>
* @link <%= pkg.repository.url %>
*/
`;
let distDir = 'dist', buildDir = 'build', docsDir = buildDir + '/docs';
/**
* Returns a callback that runs the given tasks programmatically
* @returns {Function}
*/
let tasksCb = function () {
let tasks = Array.prototype.slice.call(arguments);
return function () {
return gulp.start(tasks);
};
};
gulp.task('clean', function () {
return del([
distDir + '/**/*',
buildDir + '/**/*'
]);
});
// ##########################
// JS files (webpack + babel)
// ##########################
gulp.task('js:clean', function () {
return del([distDir + '/js/**/*']);
});
gulp.task('js:compile', function () {
return gulp.src([])
.pipe(webpack(require('./webpack.config.js')))
.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest(distDir + '/js/'));
});
gulp.task('js', ['js:clean'], tasksCb('js:compile'));
// ##########################
// SASS files
// ##########################
gulp.task('css:clean', function () {
return del([distDir + '/css/**/*']);
});
gulp.task('css:compile', function () {
return gulp.src('./src/sass/colorpicker.scss')
.pipe(sourcemaps.init())
.pipe(header(banner, {pkg: pkg}))
.pipe(sass({style: 'expanded'}).on('error', sass.logError))
.pipe(autoprefixer())
.pipe(rename('bootstrap-colorpicker.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(distDir + '/css/'));
});
gulp.task('css:minify', ['css:compile'], function () {
return gulp.src(distDir + '/css/bootstrap-colorpicker.css')
.pipe(sourcemaps.init())
.pipe(cleanCss())
.pipe(rename({extname: '.min.css'}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(distDir + '/css/'));
});
gulp.task('css', ['css:clean'], tasksCb('css:minify'));
// ##########################
// Examples written in handlebars
// ##########################
gulp.task('examples:compile', function () {
let data = {banner: banner, package: pkg};
let options = {
batch: ['./src/docs', './src/docs/examples'],
helpers: {
code: function (hbsOptions) {
let entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
};
return String(hbsOptions.fn(this)).trim().replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
}).replace(/^ {2,8}/mgi, '');
}
}
};
gulp.src('src/docs/examples.json')
.pipe(gulp.dest(buildDir + '/examples'));
return gulp.src('src/docs/examples/**/*.hbs')
.pipe(handlebars(data, options))
.pipe(rename({extname: '.html'}))
.pipe(gulp.dest(buildDir + '/examples'));
});
// ##########################
// JSDoc documentation
// ##########################
gulp.task('docs:clean', function () {
return del([docsDir + '/*']);
});
gulp.task('docs:replace-data', function () {
gulp.src(['./README.md'])
.pipe(
replace(
new RegExp('<!--version-->', 'g'),
'<!--googleoff: index--><h2 class="pkg-version">v' + pkg.version + '</h2><!--googleon: index-->'
))
.pipe(gulp.dest('./build'));
});
gulp.task('docs:compile', ['examples:compile', 'docs:replace-data'], shell.task([
'echo "Compiling docs..."',
'node_modules/.bin/jsdoc --configure .jsdoc.json --verbose'
]));
gulp.task('docs:add-dist', shell.task([
'echo "Adding dist files to docs..."',
`mkdir -p ${distDir} && mkdir -p ${docsDir}`,
`cp -R ${distDir} ${docsDir}/dist`,
`mkdir -p ${docsDir}/src && cp -R src/js ${docsDir}/src/js`
]));
gulp.task('docs:add-v2-docs', shell.task([
'echo "Adding v2 docs..."',
'mkdir -p build/tmp',
'rm -rf build/tmp/* build/docs/v2/*',
`git clone ${pkg.repository.url} build/tmp/v2`,
'cd build/tmp/v2 && git checkout v2.x && cd ../../../',
'mkdir -p build/docs/v2 && cp build/tmp/v2/index.html build/docs/v2/index.html',
'mkdir -p build/docs/v2/dist && cp -R build/tmp/v2/dist/* build/docs/v2/dist',
'mkdir -p build/docs/v2/docs/assets && cp -R build/tmp/v2/docs/assets/* build/docs/v2/docs/assets',
'rm -rf build/tmp/*'
]));
gulp.task('docs', ['docs:clean'], tasksCb('docs:compile', 'docs:add-dist'));
// ##########################
// Publish tools
// ##########################
gulp.task('publish-gh-pages', function () {
// WARNING! You won't be able to publish unless you have write permissions on the repo.
// Check the gh-pages npm package documentation.
ghPages.publish('build/docs',
{
message: 'Update build with latest master changes'
},
function () {
console.info('The gh-pages branch is updated and pushed 📦.');
}
);
});
gulp.task('npm-prepublish', shell.task([
'cp LICENSE dist/LICENSE',
'cp README.md dist/README.md'
]));
// ##########################
// Default
// ##########################
gulp.task('watch', ['default'], function () {
gulp.watch('src/docs/**/*.{hbs,tmpl,css,js}', ['docs']);
gulp.watch('src/sass/**/*.scss', ['css']);
gulp.watch('src/js/**/*.js', ['js']);
});
gulp.task('dist', ['js', 'css']);
gulp.task('default', ['dist']);
// To list all tasks run "gulp --tasks" or "gulp --tasks-simple"