-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgulpfile.js
More file actions
293 lines (262 loc) · 7.9 KB
/
gulpfile.js
File metadata and controls
293 lines (262 loc) · 7.9 KB
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
const gulp = require('gulp');
const prefix = require('gulp-autoprefixer');
const del = require('del');
const sass = require('gulp-sass')(require('sass'));
const yaml = require('gulp-yaml');
const webp = require('gulp-webp');
const git = require('gulp-git');
const tagVersion = require('gulp-tag-version');
const jsYaml = require('js-yaml');
const replace = require('gulp-replace');
// Dependencies for compendium tasks.
const fs = require('fs');
const shell = require('gulp-shell')
const mergeStream = require("merge-stream");
const path = require("path");
const clean = require("gulp-clean");
// Constants.
const PACK_SRC = "src/packs";
const PACK_DEST = "dist/packs";
/* ----------------------------------------- */
/* Compile Compendia
/* ----------------------------------------- */
/**
* Gulp Task: Compile packs from the yaml source content to .db files.
*/
function compilePacks() {
// Every folder in the src dir will become a compendium.
const folders = fs.readdirSync(PACK_SRC).filter((file) => {
return fs.statSync(path.join(PACK_SRC, file)).isDirectory();
});
const packs = folders.map((folder) => {
return gulp.src(path.join(PACK_SRC, folder))
.pipe(shell([
`fvtt package --id dungeonworld --type System pack <%= file.stem %> -c --yaml --in "<%= file.path %>" --out ${PACK_DEST}`
]))
})
return mergeStream.call(null, packs);
}
/**
* Cleanup the packs directory.
*
* This task will delete the existing compendiums so that the compile task can
* write fresh copies in their place.
*/
function cleanPacks() {
return gulp.src(`${PACK_DEST}`, { allowEmpty: true }, {read: false}).pipe(clean());
}
/* ----------------------------------------- */
/* Export Compendia
/* ----------------------------------------- */
/**
* Gulp Task: Export Packs
*
* This gulp task will load all compendium .db files from the dest directory,
* load them into memory, and then export them to a human-readable YAML format.
*/
function extractPacks() {
// Start a stream for all db files in the packs dir.
const packs = gulp.src(`${PACK_DEST}/*`)
.pipe(shell([
'fvtt package --id dungeonworld --type System unpack <%= file.stem %> -c --yaml --in dist/packs --out src/packs/<%= file.stem %>'
]));
// Call the streams.
return mergeStream.call(null, packs);
}
/* ----------------------------------------- */
/* Compile Sass
/* ----------------------------------------- */
// Small error handler helper function.
function handleError(err) {
console.log(err.toString());
this.emit('end');
}
const SYSTEM_SCSS = ["src/styles/src/**/*.scss"];
function compileScss() {
// Configure options for sass output. For example, 'expanded' or 'nested'
let options = {
outputStyle: 'compressed'
};
return gulp.src(SYSTEM_SCSS)
.pipe(
sass(options)
.on('error', handleError)
)
.pipe(prefix({
cascade: false
}))
.pipe(gulp.dest("./dist/styles/dist"))
}
const cssTask = gulp.series(compileScss);
/* ----------------------------------------- */
/* Compile YAML
/* ----------------------------------------- */
const SYSTEM_YAML = ['src/yaml/**/*.yml', 'src/yaml/**/*.yaml'];
function compileYaml() {
return gulp.src(SYSTEM_YAML)
.pipe(yaml({ space: 2 }))
.pipe(gulp.dest('./dist'))
}
const yamlTask = gulp.series(compileYaml);
/* ----------------------------------------- */
/* Delete files
/* ----------------------------------------- */
const SYSTEM_DELETE = ['dist'];
function deleteFiles() {
return del([
'dist/**',
'!dist/packs'
], {force: true});
}
const deleteTask = gulp.series(deleteFiles);
/* ----------------------------------------- */
/* Copy files
/* ----------------------------------------- */
const SYSTEM_COPY = [
'src/assets/**/*',
'!src/assets/**/*.{png,jpeg,jpg}',
'src/module/**/*',
'src/packs/**/*.db',
'src/scripts/**/*',
'src/styles/lib/**/*',
'src/templates/**/*',
'src/tokens/**/*',
'!src/tokens/**/*.{png,jpeg,jpg}'
];
function copyFiles() {
return gulp.src(SYSTEM_COPY, {base: 'src'})
.pipe(gulp.dest('./dist'))
}
const copyTask = gulp.series(copyFiles);
/* ----------------------------------------- */
/* Convert images
/* ----------------------------------------- */
const SYSTEM_IMAGES = [
'src/assets/**/*.{png,jpeg,jpg}',
'src/tokens/**/*.{png,jpeg,jpg}'
];
function compileImages() {
return gulp.src(SYSTEM_IMAGES, {base: 'src'})
.pipe(webp())
.pipe(gulp.dest('./dist'));
};
const imageTask = gulp.series(compileImages);
/* ----------------------------------------- */
/* Retrieve current version from system.yml
/* ----------------------------------------- */
function getTagVersion() {
try {
// Load the manifest and determine the version.
const doc = jsYaml.safeLoad(fs.readFileSync('./src/yaml/system.yml', 'utf8'));
return doc.version;
} catch (e) {
console.log(e);
// Output the original file as a fail safe.
return false;
}
}
/* ----------------------------------------- */
/* Increment version in system.yml
/* ----------------------------------------- */
function inc(importance) {
let version = getTagVersion();
if (version) {
let oldVersion = version;
let newVersionSplit = version.split('.');
switch (importance) {
case 'patch':
newVersionSplit[2]++;
break;
case 'minor':
newVersionSplit[1]++;
break;
case 'major':
newVersionSplit[0]++;
break;
default:
break;
}
let newVersion = newVersionSplit.join('.');
// Output the version to the file.
return gulp.src(['./src/yaml/system.yml'])
// String replacements.
.pipe(replace(oldVersion, newVersion))
.pipe(replace('jobs/artifacts/master', `jobs/artifacts/${newVersion}`))
// Overwrite system.yml.
.pipe(gulp.dest('./src/yaml'))
} else {
return gulp.src(['./src/yaml/system.yml']);
}
}
/* ----------------------------------------- */
/* Commit changes and make a new git tag.
/* ----------------------------------------- */
function commitTag() {
let version = getTagVersion();
if (version) {
return gulp.src(['./system.json'])
.pipe(git.commit(`Release ${version}`))
.pipe(tagVersion({ prefix: '' }));
}
else {
return gulp.src(['./system.json']);
}
}
/* ----------------------------------------- */
/* Watch Updates
/* ----------------------------------------- */
function watchUpdates() {
gulp.watch(SYSTEM_YAML, yamlTask);
gulp.watch(SYSTEM_IMAGES, compileImages);
gulp.watch(SYSTEM_SCSS, gulp.series(compileScss, copyFiles));
gulp.watch(SYSTEM_COPY, copyTask);
}
/* ----------------------------------------- */
/* Export Tasks
/* ----------------------------------------- */
const defaultTask = gulp.series(
deleteFiles,
compileYaml,
compileImages,
compileScss,
copyFiles,
watchUpdates
);
const buildTask = gulp.series(
cleanPacks,
deleteFiles,
gulp.parallel(
compileYaml,
compileScss,
copyFiles,
compilePacks
)
);
exports.default = defaultTask;
exports.build = buildTask;
exports.copy = copyTask;
exports.images = imageTask;
exports.css = cssTask;
exports.yaml = yamlTask;
exports.cleanPacks = gulp.series(cleanPacks);
exports.compilePacks = gulp.series(cleanPacks, compilePacks);
exports.extractPacks = gulp.series(extractPacks);
/**
* Bumping version number and tagging the repository with it.
* Please read http://semver.org/
*
* You can use the commands
*
* gulp patch # makes v0.1.0 → v0.1.1
* gulp feature # makes v0.1.1 → v0.2.0
* gulp release # makes v0.2.1 → v1.0.0
*
* To bump the version numbers accordingly after you did a patch,
* introduced a feature or made a backwards-incompatible release.
*/
patch = function() { return inc('patch') };
major = function() { return inc('major') };
minor = function() { return inc('minor') };
exports.patch = gulp.series(patch, buildTask, commitTag);
exports.minor = gulp.series(minor, buildTask, commitTag);
exports.major = gulp.series(major, buildTask, commitTag);