forked from freeCodeCamp/testable-projects-fcc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
147 lines (129 loc) · 3.43 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
const path = require('path');
const { pipeline } = require('stream');
const gulp = require('gulp');
const through2 = require('through2');
const Vinyl = require('vinyl');
const concat = require('gulp-concat');
const watch = require('gulp-watch');
const babel = require('gulp-babel');
const sass = require('gulp-sass');
const pug = require('pug');
sass.compiler = require('node-sass');
const compileHtml = pug.compileFile('./src/projects/index.pug');
const babelOptions = {
babelrc: false,
presets: ['@babel/preset-env', '@babel/preset-react']
};
const noop = () => {};
const bundle =
process.env.BUNDLE_URL || '../../testable-projects-fcc/v1/bundle.js';
const projectsPath = process.env.PROJECTS_PATH || './src/projects';
const buildPath = process.env.BUILD_PATH || './build/pages';
function js(projectPath) {
return pipeline(
gulp.src(projectPath + '/**/*.{js,jsx}'),
babel(babelOptions).on('error', (err) => {
const message = err.toString();
process.stderr.write(`${message}\n`);
this.emit('end');
}),
concat('index.js'),
noop
);
}
function css(projectPath) {
return pipeline(
gulp.src(projectPath + '/**/*.{css,scss,sass}'),
sass().on('error', sass.logError),
concat('index.css'),
noop
);
}
function html(projectPath) {
return pipeline(
gulp.src(projectPath + '/**/*.html'),
concat('index.html'),
noop
);
}
function getData(stream) {
return new Promise((resolve, reject) => {
let result;
stream
.on('data', (data) => {
result = data;
})
.on('error', (err) => reject(err))
.on('end', () => resolve(result));
}).then((data) => (data ? data.contents.toString() : ''));
}
async function buildProjectFromDirectory(projectPath, cb) {
try {
const projectName = projectPath.split(path.sep).reverse()[0];
const title = `FCC: ${projectName
.split('-')
.map((word) => word[0].toUpperCase() + word.slice(1))
.join(' ')}`;
const _js = getData(js(projectPath));
const _css = getData(css(projectPath));
const _html = getData(html(projectPath));
const [script, style, contents] = await Promise.all([_js, _css, _html]);
if (!script && !style && !contents) {
cb();
} else {
const vinyl = new Vinyl({
cwd: '/',
path: '/index.html',
contents: Buffer.from(
compileHtml({
contents,
style,
script,
bundle,
title
})
),
projectName
});
cb(null, vinyl);
}
} catch (err) {
cb(err);
}
}
function buildProject() {
const _buildFromDirectory = (vinyl, _, cb) => {
if (vinyl.isDirectory()) {
buildProjectFromDirectory(vinyl.path, cb);
} else {
cb();
}
};
return through2.obj(_buildFromDirectory);
}
function build() {
return pipeline(
gulp.src(`${projectsPath}/*`),
buildProject(),
gulp.dest((file) => `${buildPath}/${file.projectName}`),
noop
);
}
function watchProjects() {
return pipeline(
watch(
[
`${projectsPath}/**/*.{js,jsx}`,
`${projectsPath}/**/*.{css,scss,sass}`,
`${projectsPath}/**/*.html`
],
{ read: false }
),
through2.obj((file, _, cb) => buildProjectFromDirectory(file.dirname, cb)),
gulp.dest((file) => `${buildPath}/${file.projectName}`),
noop
);
}
exports.build = build;
exports.watch = watchProjects;
exports.default = gulp.series(build, watchProjects);