yeoman / yo

CLI tool for running Yeoman generators

Home Page:http://yeoman.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Write synchronous .yo-rc.json

f-liva opened this issue · comments

My install method contains many spawn commands that may break during their execution.
Interruption of even one of them, due to an error or a major cause, will result in having to restart the generator by re-executing all previously executed spawn commands.
To avoid this, I thought of using this.config.set and this.config.get to save the state of correct execution of each spawn command and to recover this state in a subsequent execution of the generator, which at that point will know which spawn command to relaunch and which not because they have already been executed.

The problem, is that Yeoman generates the .yo-rc.json file only at the end of everything, making the approach I had thought useless. Running this.config.set or this.config.save over and over again does not result in a write (at that precise moment) to the configuration file, and therefore if the generator stops due to a spawn command error, nothing will be written to the .yo-rc.json file.

How can I solve this issue and force Yeoman to update the .yo-rc.json file when I say so and when I need it, without waiting for the completion of the generator execution?

See code example here. See comments on spawn commands

module.exports = class extends Generator {

    async prompting () {
        this.answers = await this.prompt([
            ...
        ])
    }

    writing () {
        ...
    }

    install () {
        if (!this.config.get('foo-executed')) {
            this.spawnCommandSync('foo-command')

            this.config.set('foo-executed', true) // This will not be saved to .yo-rc.json because...
        }

        // I need at this point the .yo-rc.json file has already been saved and contains foo-executed: true

        if (!this.config.get('bar-executed')) {
            this.spawnCommandSync('bar-command') // ... this will cause an error and stop the execution of the generator!

            this.config.set('bar-executed', true)
        }
    }

    end () {
        this.log('Have a nice day')
    }
}