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

Considering adding a "dry run" option #110

Closed
daaren-urthling opened this issue Jan 6, 2019 · 7 comments · Fixed by yeoman/generator#1141
Closed

Considering adding a "dry run" option #110

daaren-urthling opened this issue Jan 6, 2019 · 7 comments · Fixed by yeoman/generator#1141

Comments

@daaren-urthling
Copy link

It would be useful to have an option that allow to do almost everything but not committing the FS in the end (similar to --dry-run for Angular CLI).
This may allow to review the list of files added, changed and the eventual conflicts.
As far as the MemFS is used, it should be relative simple to skip the last "commit" phase.
Maybe this is already possible by tweaking in some way the generator ...

@SBoudrias
Copy link
Member

Hum, I think you'd need to modify the yeoman-generator package itself to add this option. I'm not sure if yeoman-environment could add support directly (the advantage here is that it'd work for all generators automatically without requiring a version update.)

As you say, because we're using mem-fs, it should be relatively simple to skip the commit phase.

If you're willing to give it a try, I'll be happy to help review the PR and make sure we can move this forward.

@daaren-urthling
Copy link
Author

Ok, I'll give it a try. I am not exactly a guru in node.js development, so I'll take my time ;o)
I am currently working (as a side project) on a generator for a legacy framework of my organization and to fulfill some needs I need to get more and more involved in the yeoman internals, so maybe I will be able to sort it out.
In case of need for some hint, what's the best option? Should I fork the repo and invite you, or add some issue here?

@SBoudrias
Copy link
Member

@daaren-urthling you can ping me on our gitter channel; and maybe some other people can also jump in to give you a hand.

@daaren-urthling
Copy link
Author

Perfect! Let's do this way, thanks a lot.

@anupbaranwal
Copy link

Hey did you try this? I need this feature in my application. I would like to give it a try if I get a bit help. :)

@daaren-urthling
Copy link
Author

No, I didn't progress with this project, I had other priorities

@shousper
Copy link

I was also after this because it's a little faster than writing tests while prototyping generators. I ended up creating a "BaseGenerator" to extend that overrides spawnCommand and fs.

In the constructor I added this:

    // Hi-jack fs module to add dry run and verbose support.
    this.fs = new Proxy(this.fs, {
      get: (target, propKey) => {
        if (typeof target[propKey] !== 'function' || propKey.startsWith('exists') || propKey.startsWith('read')) {
          return target[propKey];
        }
        // eslint-disable-next-line consistent-return
        return (...fnArgs) => {
          if (this._dryRun) {
            this.log(this._verbose
              ? `${propKey} ${JSON.stringify(fnArgs, null, 2)}`
              : `${propKey}(${fnArgs.join('", "')})`);
          } else if (this._verbose) {
            this.log(`${propKey}(${fnArgs.join('", "')})`);
          }
          if (!this._dryRun) {
            return target[propKey](...fnArgs);
          }
        };
      },
    });

And replacing spawnCommand was pretty simple:

  // Override to implement dry-run and verbose modes.
  spawnCommand(command, args) {
    if (this._dryRun) {
      this.log(this._verbose
        ? `${command} ${JSON.stringify(args, null, 2)}`
        : `${command}(${args.join('", "')})`);
    } else if (this._verbose) {
      this.log(`${command}(${args.join('", "')})`);
    }
    if (!this._dryRun) {
      const { error } = super.spawnCommandSync(command, args);
      if (error) throw error;
    }
  }

Please forgive the dirty logging, etc.

Hope this helps someone 🙂

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

Successfully merging a pull request may close this issue.

4 participants