问题
I am very new to Promises, but would like to use them to sequentially fire off a clean (deleting files) and then a make (copying files from source to build).
gulp.task('make-app', function makeApp(done) {
function make() {
return gulp.src(getPatterns('src-app'))
.pipe(gulp.dest('./build/app/'));
}
return (args.noclean && !args.deploy)
? make()
: del(getPatterns('dest-app')
.then(make);
// .then(done);
});
Some things to note:
getPatterns()
is a function which simply returns a glob pattern from my config.
The src-app
glob is: [ './source/app/**/*' ]
. The (args.noclean && !args.deploy)
evaluate to false
, which starts the del
module (which is v2, so it returns a Promise
). To my limited understanding of Promises, we .then()
it to the make()
function, which I'd love to return a Promise and just chain a .then(done)
, but that's where I'm lost. Currently, I'm just returning a stream
, which is resulting in not all the files copying over.
UPDATE
I've taken about 70 more random stabs (the absolute, most miserable way to gain experience!) and have come up with this:
return del(getPatterns('dest-app'))
.then(function() {
return gulp.src(getPatterns('src-app'))
.pipe(gulp.dest(dest + app));
});
And the build process completes, the files always get deleted first, but Gulp only copies over the first 16 files/folders from the stream.
UPDATE 2 - The below code LOOKS as if it'd work, but Gulp reports that it is not completing (so a Stream, Promise or Callback is not being returned).
var make = new Promise(function(resolve, reject) {});
var clean = del(getPatterns('dest-app'))
.then(function() {
var makeStream = gulp.src(getPatterns('src-app'))
.pipe(gulp.dest(dest + app));
makeStream.on('end', function() {
make.resolve();
});
});
return Promise.all([clean, make]);
回答1:
No, your update 2 doesn't look like it should be working. make
is promise and has no resolve
method that you could call, instead you'd need to call the resolve
function that is given to your constructor callback. In your version, make
(and the returned Promise.all
) never resolve.
This is how it should look like:
function make() {
return new Promise(function(resolve, reject) {
gulp.src(getPatterns('src-app'))
.pipe(gulp.dest('./build/app/'))
.on("end", resolve)
.on("error", reject);
});
}
return (args.noclean && !args.deploy)
? make()
: del(getPatterns('dest-app')).then(make);
来源:https://stackoverflow.com/questions/33181234/using-gulp-4-with-promises-to-clean-copy-files