Skip to content

Override Generators #104

Open
Open

Description

@imaustink

What

A related issue (donejs/generator-donejs#140) outlines the method of using the .donejs folder to customize a generator's templates. It would be nice to have the ability to custom the generator itself.

Why

For a react-view-model project. Since a custom element tag name that the component prompt collects doesn't translate well to a react component so I'd like to change the prompt and it's validation.

You could also do things like making a component generator that prompts you for a type and builds one of several predefined types.

How

My immediate thought would be to modify the add command to do something like:

var path = require('path');
var utils = require('../utils');
var generate = require('./cmd-generate');
var debug = require('debug')('donejs-cli:add');  

module.exports = function(root, name, params) {
  var generators = require(path.join(root, 'node_modules', 'generator-donejs'));
  var customGeneratorPath = path.join(process.cwd(), '.donejs', 'name', 'index.js');

  // Use the custom generator here!
  if (fs.exists(customGeneratorPath)) {
    return utils.generate(root, '.donejs', [name].concat(params))
  }

  if (generators[name]) {
    debug('add called but running generate instead', name, params);
    return generate(root, name, params);
  }

  debug('add', name, params);
  return utils.add(path.join(root, 'node_modules'), name, params);
};

Comments Please 😃

I am happy to implement this in my spare time because I'd like to see it done ASAP. I just would like a little input first so I know if I am heading down the wrong path before I start.

Activity

  1. justinbmeyer commented on Apr 12, 2018

    @justinbmeyer

    There is a generator generator already and a guide for it:https://donejs.com/generator.html

    Does this do what you need?

  2. justinbmeyer commented on Apr 12, 2018

    @justinbmeyer

    To restate, I think you should create an rvm-component generator (instead of mixing this into the current generator). The types of generators is unlimited, making trying to support them all within DoneJS problematic. This is why we made creating new ones easy.

  3. matthewp commented on Apr 12, 2018

    @matthewp
    Contributor

    I agree about this becoming a generator. I think one reason why this type of request comes up often is because it's more natural to do donejs add component MyComponent than it is to do donejs add rvm-component MyComponent. For can-components it's not donejs add can-component my-component.

    I wonder if we should support some method to alias generator names, so that you can use the shorthand. The flow might be like this:

    donejs add rvm-component
    
    donejs alias component rvm-component
    
    donejs add component MyComponent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions