-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
151 lines (125 loc) · 5.02 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
const fs = require('fs-extra');
const path = require('path');
const { spawn } = require('child_process');
const gulp = require('gulp');
const del = require('del');
const portscanner = require('portscanner');
const log = require('fancy-log');
const jsonConcat = require('gulp-json-concat');
const yaml = require('gulp-yaml');
const processHelpPages = require('./gulp-help-pages');
// /////////////////////////////////////////////////////////////////////////////
// --------------------------- Variables -------------------------------------//
// ---------------------------------------------------------------------------//
// Environment
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const readPackage = () => JSON.parse(fs.readFileSync('package.json'));
// Set the version in an env variable.
process.env.APP_VERSION = readPackage().version;
process.env.APP_BUILD_TIME = Date.now();
const parcelCli = path.join(__dirname, './node_modules/parcel/lib/cli.js');
const parcelConfig = path.join(__dirname, '.parcelrc');
// /////////////////////////////////////////////////////////////////////////////
// ----------------------- Watcher and custom tasks --------------------------//
// ---------------------------------------------------------------------------//
function ymlStrings() {
return gulp
.src('content/strings/*.yml')
.pipe(yaml({ safe: true }))
.pipe(
jsonConcat('strings.json', (data) => Buffer.from(JSON.stringify(data)))
)
.pipe(gulp.dest('app/assets/scripts/'));
}
function helpPages() {
return gulp
.src('content/help-documentation/*.md')
.pipe(processHelpPages('docs'))
.pipe(gulp.dest('dist/docs'));
}
// Function to register file watching
function watcher() {
// Add custom watcher processes, below this line
gulp.watch(['content/strings/*'], ymlStrings);
gulp.watch(['content/help-documentation/*'], gulp.series(helpPages));
// ---
// Watch static files. DO NOT REMOVE.
gulp.watch(['static/**/*'], copyFiles);
}
// /////////////////////////////////////////////////////////////////////////////
// ---------------------------- Base tasks -----------------------------------//
// ---------------------------------------------------------------------------//
function clean() {
// Remove build cache and dist.
return del(['dist', '.parcel-cache']);
}
// Simple task to copy the static files to the dist directory. The static
// directory will be watched so that files are copied when anything changes.
function copyFiles() {
// When running directly from this repo both sources will be the same, but
// when this runs from the config repo, the CWD will be set to the config
// repo, effectively copying static files from there. We need to also use an
// absolute url to ensure any default files are copied as well.
return gulp
.src([path.join(__dirname, 'static/**/*'), 'static/**/*'])
.pipe(gulp.dest('dist'));
}
// Below are the parcel related tasks. One for the build process and other to
// start the development server.
// Parcel will look up the directory tree an search for the highest
// package.json to use. When running this directly from the UI repo (for
// development) we need to actively point parcel to the correct target
// otherwise the site doesn't get built.
const parcelTarget = process.cwd() === __dirname ? ['app/index.html'] : [];
function parcelServe(cb) {
portscanner.findAPortNotInUse(9000, 9999, function (error, port) {
if (port !== 9000) {
log.warn(` Port 9000 is busy.`);
}
// const args = ['--config', parcelConfig, '--port', port, '--open'];
const args = ['--config', parcelConfig, '--port', port];
// Run parcel in serve mode using the same stdio that started gulp. This is
// needed to ensure the output is colored and behaves correctly.
spawn('node', [parcelCli, 'serve', ...args, ...parcelTarget], {
stdio: 'inherit'
});
cb(error);
});
}
function parcelBuild(cb) {
// Build the app using parcel. Since the build task finishes, we have to
// listen for it to mark the gulp task as finished.
const args = [
'--config',
parcelConfig,
'--public-url',
process.env.PUBLIC_URL || '/'
];
const pr = spawn('node', [parcelCli, 'build', ...args, ...parcelTarget], {
stdio: 'inherit'
});
pr.on('close', () => cb());
}
// //////////////////////////////////////////////////////////////////////////////
// ---------------------------- Task export -----------------------------------//
// ----------------------------------------------------------------------------//
module.exports.clean = clean;
// Task orchestration used during the development process.
module.exports.serve = gulp.series(
gulp.parallel(ymlStrings, helpPages),
gulp.parallel(
// Task to copy the files. DO NOT REMOVE
copyFiles
),
gulp.parallel(watcher, parcelServe)
);
// Task orchestration used during the production process.
module.exports.default = gulp.series(
clean,
gulp.parallel(ymlStrings, helpPages),
gulp.parallel(
// Task to copy the files. DO NOT REMOVE
copyFiles
),
parcelBuild
);