0

I would like to create/update file(s) after the dependencies have been installed, when the generator(generator-custom) has been executed.

$ yo custom

Any pointer would be very helpful.


For example, I am trying to update the devDependencies section of the package.json with respect to the dependent packages devDependencies after the dependencies have been installed. However I am unable to achieve it. Please find the below code snippet.

install: function () {
  this.installDependencies({
    skipInstall: this.options['skip-install'],
    callback: function () {
      var pkgPath = process.cwd() + '/package.json';
      var pkg = require(pkgPath);
      pkg.devDependencies = {
        "grunt": "~0.4.2"
      };
      this.write(pkgPath, JSON.stringify(pkg));
    }.bind(this)
  });
}

The idea behind updating the package.json, was to rerun the installDependencies function. So that when ever the core package updates its dependency, the generator does not require to update its template.

As suggested by @SimonBoudrias, the above is not efficient way to perform the operation. The dependent packages can be installed by using peerDependencies of the main dependent package.


Sarbbottam
  • 5,410
  • 4
  • 30
  • 41
  • Why do you want to add devDependencies after the installation ran? This'll just confuse the hell out of your users. – Simon Boudrias Apr 26 '15 at 19:27
  • Actually, I want to copy the devDependencies from the package.json of one of the dependent package and rerun the install. – Sarbbottam Apr 26 '15 at 19:29
  • The reason behind, is that I can avoid updating the generator when the main dependency's dependency changes. Please suggest if there are any better way to do so. – Sarbbottam Apr 26 '15 at 19:32
  • Use composability then. No need to wait for a composed generator installation to run, and you'll be able to write the same files without actually hitting the hard drive. http://yeoman.io/authoring/composability.html and http://yeoman.io/authoring/file-system.html – Simon Boudrias Apr 26 '15 at 19:34
  • Thanks @SimonBoudrias. I guess I can achieve the same with peerDependencies of the main dependent package. – Sarbbottam Apr 26 '15 at 19:43
  • @SimonBoudrias could you suggest me how to perform write/update file operation after dependency installation? – Sarbbottam Apr 26 '15 at 20:28
  • My point is, Yeoman is not expecting you to write after the installation phase. So most way you'll try won't work. There's no reason you should need the installation to run before hand. By fetching the `package.json`, you'll have all the information you need to write any files. – Simon Boudrias Apr 26 '15 at 21:08

1 Answers1

0

I would like to create/update file(s) after the dependencies have been installed, when the generator(generator-custom) has been executed.

The following snippet serves the desired use case.

this.npmInstall(['npm-module'], {}, function() {
  this.write('path/to/file', 'file-content');
}.bind(this));
Sarbbottam
  • 5,410
  • 4
  • 30
  • 41