-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
132 lines (118 loc) · 3.41 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
const autoprefixer = require('autoprefixer');
const browsersync = require('browser-sync').create();
const child = require('child_process');
const gulp = require('gulp');
const jsonImporter = require('node-sass-json-importer');
const log = require('fancy-log');
const postcss = require('gulp-postcss');
const sass = require('gulp-sass')(require('sass'));
const stylelint = require('stylelint');
const tildeImporter = require('node-sass-tilde-importer');
const packageJson = require('./package.json');
const harbourStylelintJson = '.stylelintrc.json';
const scssIndex = 'scss/index.scss';
const scssGlobs = 'scss/**/*.scss';
const stylelintGlobs = [
'scss/components/*.scss',
'scss/controls/*.scss',
'scss/layouts/*.scss',
'scss/reset/*.scss',
'scss/themes/*.scss',
'scss/vendor-overrides/*.scss',
'!scss/**/index.scss'
];
const siteGlobs = [
"./site/**/*",
];
const jekyllSiteRoot = '_gh_pages';
const cssFolder = '_gh_pages/css/';
let isBuild = false; // Used to keep process running when encountering errors while developing
function setBuild(done) {
isBuild = true;
done();
}
function browserSync(done) {
browsersync.init({
server: {
baseDir: jekyllSiteRoot,
},
port: 4000
});
done();
}
function browserSyncReload(done) {
browsersync.reload();
done();
}
function lintScss() {
return stylelint
.lint({
files: stylelintGlobs,
configFile: harbourStylelintJson,
fix: !isBuild
})
.then(function(report) {
if (report.errored) {
const erroredResults = report.results.filter(result => result.errored);
erroredResults.map((erroredResult) => {
log(`⚠️ Errors found in ${erroredResult.source}`);
erroredResult.warnings.map((warning) => {
log(`❌ Error on line ${warning.line}:${warning.column} -> ${warning.rule}`);
});
});
// Provide exit code for pipeline/CLI integration, when in build mode
if (isBuild) {
process.exit(1);
}
}
if (!report.errored) {
let suffix = isBuild ? 'stylint build task succesful' : 'or we\'ve automatically fixed them';
log(`🔥 💯 🔥 No style errors found ${suffix}`);
}
});
}
// Build Scss
function buildScss() {
const outputStyle = isBuild ? 'compressed' : 'expanded';
let hasCompilerError = false;
return gulp.src(scssIndex)
.pipe(
sass({
outputStyle: outputStyle,
importer: [tildeImporter, jsonImporter()]
})
.on('error', function(error) {
hasCompilerError = true;
sass.logError.bind(this)(error);
})
)
.pipe(postcss([
autoprefixer()
]))
.pipe(gulp.dest(cssFolder))
.pipe(browsersync.stream())
.on('finish', function() {
if (hasCompilerError && isBuild) {
process.exit(1);
}
if (!hasCompilerError) {
log('🎨 Compiled SCSS to CSS');
}
});
}
function jekyll() {
const env = Object.create( process.env );
env.packageVersion = packageJson.version;
return child.spawn("bundle", ["exec", "jekyll", "build"], { stdio: 'inherit', env });
}
function watchFiles() {
// Watch files that need linting and trigger linting process
gulp.watch(stylelintGlobs, lintScss);
// Watch all scss files (including ones that don't need linting) and trigger compilation
gulp.watch(scssGlobs, buildScss);
// Watch site source folder and trigger jekyll
gulp.watch(siteGlobs, gulp.series(jekyll, browserSyncReload));
}
// Exported tasks
exports.build = gulp.series(setBuild, lintScss, gulp.parallel(buildScss, jekyll));
exports.start = gulp.parallel(watchFiles, browserSync);