How to wait until copy action ends in Yeoman

I am writing a Yeoman generator and I want to copy a directory. You can find the code below. However, it works correctly, however, after the copy is complete, I want to perform additional actions, such as "npm install". Since copying is performed asynchronously, "npm install" is performed before all files have been copied. How can I wait for all copy actions to complete?

this.expandFiles('**', {
    cwd: this.sourceRoot(),
    dot: true
}).forEach(function (el) {

    this.copy(el, el);

}, this);
+4
source share
3 answers

Chain calls this.copywith .onand run your function in an event 'end'.

this.copy(el, el)
  .on('end', function() {
     console.log("Copy is complete");
   });
+2
source

- , , async, , . :

var me = this;
var async = require('async');

var array = this.expandFiles('**', {
    cwd: this.sourceRoot(),
    dot: true
});

async.each(array, function(el, callback) {
    me.copy(...);  // assuming that this copy() is synchronous
    callback(null);
}, function(err) {
    console.log("done with copying....");
});
0

You can use Yeoman this.async ()

First you need to get the function necessary to resume the generator:

var done = this.async();

After copying is complete (possibly with some callback), call done()when you want to resume the generator.

Docs: https://yeoman.imtqy.com/generator/RunContext.html

0
source

Source: https://habr.com/ru/post/1546984/


All Articles