-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
96 lines (81 loc) · 2.26 KB
/
util.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
const { src, dest, series } = require('gulp');
const rename = require('gulp-rename');
const del = require('del');
const { config } = require('../package.json');
/*
* Typical PreProcessor workflow
* Reads files from /src/ directory, runs processes, and outputs to /dist/ directory
*
* Pass in a `rename` option to rename the processed files
* Pass in a `cleanup` parameter to remove unnecessary files
*/
function makeTask(opts) {
const { key, pipe = [] } = opts;
function runTask() {
// Gather the files from the entry directory (dir)
src(
opts.src.map((file) => config.srcDir + file),
{
sourcemaps: config.sourcemaps
}
).pipe(dest(config.distDir)); // Copy to the Dist dir
const filesToModify = src(
opts.src.map((file) => config.distDir + file),
{
sourcemaps: config.sourcemaps
}
); // Make sure we include any additional files from the dist dir
const pipes = pipe.map((processor) => {
const fn = require(processor.require);
return fn.apply(null, processor.args);
});
if (opts.rename) {
pipes.push(makeRename(opts.rename));
}
// Pipe the files through the all of the task's functions
return (
pipes
.reduce((stream, processor) => {
return stream.pipe(processor);
}, filesToModify)
// Pipe the output to the destination
.pipe(
dest(config.distDir, {
sourcemaps: '.'
})
)
);
}
runTask.displayName = key;
if (opts.cleanup) {
return series(runTask, makeCleanup(opts));
}
return runTask;
}
/*
* Rename files in the pipeline.
*/
function makeRename(opts) {
// Find & replace of the basename if `find` option passed in
if (opts.find) {
return rename(function (path) {
path.basename = path.basename.replace(opts.find, opts.replace);
});
}
return rename(opts);
}
/*
* Delete unnecessary files in the /dist/ directory
*/
function makeCleanup(opts) {
function runCleanup(done) {
const filesToDelete = Array.isArray(opts.cleanup) ? opts.cleanup : opts.src;
del.sync(filesToDelete.map((file) => config.distDir + file));
done();
}
runCleanup.displayName = 'cleanup:' + opts.key;
return runCleanup;
}
module.exports = {
makeTask
};