Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write synchronous .yo-rc.json #740

Open
f-liva opened this issue Mar 24, 2022 · 0 comments
Open

Write synchronous .yo-rc.json #740

f-liva opened this issue Mar 24, 2022 · 0 comments

Comments

@f-liva
Copy link

f-liva commented Mar 24, 2022

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')
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant