-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·404 lines (351 loc) · 12.2 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/******************************************************
* PATTERN LAB NODE
* EDITION-NODE-GULP
* The gulp wrapper around patternlab-node core, providing tasks to interact with the core library and move supporting frontend assets.
******************************************************/
var dist = {
"root": "./dist/",
"templates": "./dist/templates/",
"js": "./dist/js",
"images": "./dist/images",
"fonts": "./dist/fonts",
"css": "./dist/css",
"flags": "./dist/flags",
};
var templates = [
"02-organisms-00-global/*.*",
"02-organisms-00-global-footer/*.*",
"02-organisms-00-global-header/*.*",
"02-organisms-00-global-empty-header/*.*",
"02-organisms-00-global-mobile-header/*.*",
"02-organisms-00-global-black-header/*.*",
"02-organisms-00-global-white-header/*.*",
"02-organisms-00-global-cookie-banner/*.*",
"01-molecules-tech-spec/*.*",
"01-molecules-tech-spec-zuhause-tech-spec-table/*.*",
"01-molecules-tech-spec-copenhagen-tech-spec-table/*.*"
];
var localized_templates = [
"de/",
"en/"
];
var gulp = require('gulp'),
Promise = require("bluebird");
gutil = require('gulp-util'),
git = Promise.promisifyAll(require('gulp-git')),
sass = require('gulp-sass'),
sassLint = require('gulp-sass-lint'),
path = require('path'),
browserSync = require('browser-sync').create(),
argv = require('minimist')(process.argv.slice(2)),
merge = require('merge-stream');
function resolvePath(pathInput) {
return path.resolve(pathInput).replace(/\\/g, "/");
}
// SASS compilation
gulp.task('pl-sass', function () {
return gulp.src(path.resolve(paths().source.css, '**/*.scss'))
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(path.resolve(paths().source.css)));
});
/******************************************************
* COPY TASKS - stream assets from source to destination
******************************************************/
// JS copy
gulp.task('pl-copy:js', function () {
return gulp.src('**/*.js', { cwd: resolvePath(paths().source.js) })
.pipe(gulp.dest(resolvePath(paths().public.js)));
});
// JS copy dist
gulp.task('dist-copy:js', function () {
return gulp.src('**/*.js', { cwd: resolvePath(paths().public.js) })
.pipe(gulp.dest(resolvePath(dist.js)));
});
// Images copy
gulp.task('pl-copy:img', function () {
return gulp.src('**/*.*', { cwd: resolvePath(paths().source.images) })
.pipe(gulp.dest(resolvePath(paths().public.images)));
});
// Images copy dist
gulp.task('dist-copy:img', function () {
return gulp.src('**/*.*', { cwd: resolvePath(paths().public.images) })
.pipe(gulp.dest(resolvePath(dist.images)));
});
// Flags copy
gulp.task('pl-copy:flags', function () {
console.log(resolvePath(paths().source.flags));
console.log(resolvePath(paths().public.flags));
return gulp.src('**/*.*', { cwd: resolvePath(paths().source.flags) })
.pipe(gulp.dest(resolvePath(paths().public.flags)));
});
// Flags copy dist
gulp.task('dist-copy:flags', function () {
return gulp.src('**/*.*', { cwd: resolvePath(paths().public.flags) })
.pipe(gulp.dest(resolvePath(dist.flags)));
});
// Favicon copy
gulp.task('pl-copy:favicon', function () {
return gulp.src('favicon.ico', { cwd: resolvePath(paths().source.root) })
.pipe(gulp.dest(resolvePath(paths().public.root)));
});
// Favicon copy dist
gulp.task('dist-copy:favicon', function () {
return gulp.src('favicon.ico', { cwd: resolvePath(paths().public.root) })
.pipe(gulp.dest(resolvePath(dist.root)));
});
// Fonts copy
gulp.task('pl-copy:font', function () {
return gulp.src('**/*.*', { cwd: resolvePath(paths().source.fonts) })
.pipe(gulp.dest(resolvePath(paths().public.fonts)));
});
// Fonts copy dist
gulp.task('dist-copy:font', function () {
return gulp.src('**/*.*', { cwd: resolvePath(paths().public.fonts) })
.pipe(gulp.dest(resolvePath(dist.fonts)));
});
// CSS Copy
gulp.task('pl-copy:css', function () {
return gulp.src(resolvePath(paths().source.css) + '/*.css')
.pipe(gulp.dest(resolvePath(paths().public.css)))
.pipe(browserSync.stream());
});
// CSS Copy dist
gulp.task('dist-copy:css', function () {
return gulp.src(resolvePath(paths().public.css) + '/*.css')
.pipe(gulp.dest(resolvePath(dist.css)));
});
// Styleguide Copy everything but css
gulp.task('pl-copy:styleguide', function () {
return gulp.src(resolvePath(paths().source.styleguide) + '/**/!(*.css)')
.pipe(gulp.dest(resolvePath(paths().public.root)))
.pipe(browserSync.stream());
});
// Styleguide Copy and flatten css
gulp.task('pl-copy:styleguide-css', function () {
return gulp.src(resolvePath(paths().source.styleguide) + '/**/*.css')
.pipe(gulp.dest(function (file) {
//flatten anything inside the styleguide into a single output dir per http://stackoverflow.com/a/34317320/1790362
file.path = path.join(file.base, path.basename(file.path));
return resolvePath(path.join(paths().public.styleguide, '/css'));
}))
.pipe(browserSync.stream());
});
// Templates copy to dist from public
gulp.task('dist-copy:templates', function () {
return gulp.src(templates, { cwd: resolvePath(paths().public.patterns) })
.pipe(gulp.dest(resolvePath(dist.templates)));
});
// Templates copy localized files to dist from public
gulp.task('dist-copy:localized-templates', function () {
var folders = [];
localized_templates.forEach(function (locale) {
folders.push(
gulp.src(templates, { cwd: resolvePath(paths().public.patterns + locale) })
.pipe(gulp.dest(resolvePath(dist.templates + locale)))
);
});
return merge(folders);
});
/******************************************************
* PATTERN LAB CONFIGURATION - API with core library
******************************************************/
//read all paths from our namespaced config file
var config = require('./patternlab-config.json'),
patternlab = require('patternlab-node')(config);
function paths() {
return config.paths;
}
function getConfiguredCleanOption() {
return config.cleanPublic;
}
function build(done) {
patternlab.build(done, getConfiguredCleanOption());
}
gulp.task('pl-assets', gulp.series(
gulp.parallel(
'pl-copy:js',
'pl-copy:img',
'pl-copy:favicon',
'pl-copy:font',
gulp.series('pl-sass', 'pl-copy:css', function (done) { done(); }),
'pl-copy:styleguide',
'pl-copy:styleguide-css',
'pl-copy:flags'
),
function (done) {
done();
})
);
gulp.task('dist-assets',
gulp.parallel(
'dist-copy:js',
'dist-copy:img',
'dist-copy:css',
'dist-copy:favicon',
'dist-copy:font',
'dist-copy:flags'
)
);
gulp.task('patternlab:version', function (done) {
patternlab.version();
done();
});
gulp.task('patternlab:help', function (done) {
patternlab.help();
done();
});
gulp.task('patternlab:patternsonly', function (done) {
patternlab.patternsonly(done, getConfiguredCleanOption());
});
gulp.task('patternlab:liststarterkits', function (done) {
patternlab.liststarterkits();
done();
});
gulp.task('patternlab:loadstarterkit', function (done) {
patternlab.loadstarterkit(argv.kit, argv.clean);
done();
});
gulp.task('patternlab:build', gulp.series('pl-assets', build, function (done) {
done();
}));
gulp.task('patternlab:installplugin', function (done) {
patternlab.installplugin(argv.plugin);
done();
});
gulp.task('dist', gulp.series('patternlab:build', 'dist-assets', 'dist-copy:templates', 'dist-copy:localized-templates', function (done) {
done();
}));
/******************************************************
* SERVER AND WATCH TASKS
******************************************************/
// watch task utility functions
function getSupportedTemplateExtensions() {
var engines = require('./node_modules/patternlab-node/core/lib/pattern_engines');
return engines.getSupportedFileExtensions();
}
function getTemplateWatches() {
return getSupportedTemplateExtensions().map(function (dotExtension) {
return resolvePath(paths().source.patterns) + '/**/*' + dotExtension;
});
}
function reload() {
browserSync.reload();
}
function reloadCSS() {
browserSync.reload('*.css');
}
function watch() {
gulp.watch(resolvePath(paths().source.css) + '/**/*.css', { awaitWriteFinish: true }).on('change', gulp.series('pl-copy:css', reloadCSS));
gulp.watch(resolvePath(paths().source.styleguide) + '/**/*.*', { awaitWriteFinish: true }).on('change', gulp.series('pl-copy:styleguide', 'pl-copy:styleguide-css', reloadCSS));
gulp.watch(path.resolve(paths().source.css, '**/*.scss')).on('change', gulp.series('pl-sass'));
var patternWatches = [
resolvePath(paths().source.patterns) + '/**/*.json',
resolvePath(paths().source.patterns) + '/**/*.md',
resolvePath(paths().source.data) + '/*.json',
resolvePath(paths().source.fonts) + '/*',
resolvePath(paths().source.js) + '/*',
resolvePath(paths().source.images) + '/*',
resolvePath(paths().source.meta) + '/*',
resolvePath(paths().source.annotations) + '/*'
].concat(getTemplateWatches());
console.log(patternWatches);
gulp.watch(patternWatches, { awaitWriteFinish: true }).on('change', gulp.series(build, 'pl-copy:js', reload));
}
gulp.task('patternlab:connect', gulp.series(function (done) {
browserSync.init({
server: {
baseDir: resolvePath(paths().public.root)
},
snippetOptions: {
// Ignore all HTML files within the templates folder
blacklist: ['/index.html', '/', '/?*']
},
notify: {
styles: [
'display: none',
'padding: 15px',
'font-family: sans-serif',
'position: fixed',
'font-size: 1em',
'z-index: 9999',
'bottom: 0px',
'right: 0px',
'border-top-left-radius: 5px',
'background-color: #1B2032',
'opacity: 0.4',
'margin: 0',
'color: white',
'text-align: center'
]
}
}, function () {
console.log('PATTERN LAB NODE WATCHING FOR CHANGES');
done();
});
}));
/******************************************************
* COMPOUND TASKS
******************************************************/
gulp.task('default', gulp.series('patternlab:build'));
gulp.task('patternlab:watch', gulp.series('patternlab:build', watch));
gulp.task('patternlab:serve', gulp.series('patternlab:build', 'patternlab:connect', watch));
/******************************************************
* CUSTOM TASKS - tasks unique to this repo
******************************************************/
// Linting
gulp.task('scsslint', function () {
return gulp.src('source/css/**/*.s+(a|c)ss')
.pipe(sassLint())
.pipe(sassLint.format())
.pipe(sassLint.failOnError())
});
// Compound tasks
gulp.task('lint', gulp.parallel('scsslint'));
gulp.task('commitdist', gulp.series('dist', function (done) {
var version = require('./package.json').version;
var tag;
var branch;
git.revParseAsync({ args: '--abbrev-ref HEAD' })
.then(function (parsedBranch) {
if (parsedBranch === 'HEAD') {
// We're on Travis and need to check it.
branch = process.env.PULL_REQUEST !== 'false' ?
process.env.TRAVIS_BRANCH :
process.env.TRAVIS_PULL_REQUEST_BRANCH;
} else {
branch = parsedBranch;
}
if (branch === 'master') {
tag = new Date().toISOString().replace(/:/g, '.') + '-' + version;
} else if (branch === 'release') {
tag = version;
} else {
tag = branch + '-' + new Date().toISOString().replace(/:/g, '.') + '-' + version;
}
}).then(function () {
return git.checkoutAsync(tag, { args: '-b' })
}).then(function () {
gutil.log('Force adding dist to git');
var stream = gulp.src('dist/')
.pipe(git.add({ args: '-f' }))
.pipe(git.commit('Distribution for ' + version));
stream.on('end', function () {
gutil.log('Tagging');
git.tagAsync(tag, '')
.then(function () {
gutil.log('Checking out original branch');
return git.checkoutAsync(branch);
}).then(function () {
gutil.log('Pushing tag to remote');
return git.pushAsync('origin', [], { args: " --tags" });
}).error(function (err) {
throw err;
gutil.log('Added and commited');
}).done(function () {
done()
})
});
}).error(function (err) {
throw err;
});
}));