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

Overhaul CLI #97

Merged
merged 1 commit into from
Jun 1, 2016
Merged

Overhaul CLI #97

merged 1 commit into from
Jun 1, 2016

Conversation

jchansen
Copy link
Contributor

@jchansen jchansen commented Jun 1, 2016

This PR overhauls the CLI and solves #69 and #76.

Commander replaced with Yargs

Replaced commander with yargs as commander offered no support for nested commands. Yargs is also much more actively maintained and improved.

Nested commands nested help

User help and guidance has been greatly improved. Running lore will now generate help automatically that looks like this. Note that all generators are grouped into generate, and we can easily add more categories (like initializers, tutorial, whatever) over time.

Usage: $ <command>

Commands:
  new       Create a new application
  generate  Generate common project files

Options:
  --version  Show version number                             [boolean]
  --help     Show help                                                 [boolean]

If you then run lore generate you will get a list of all generators:

Usage: $ generate <command>

Commands:
  collection       Generate a new collection
  component        Generate a new component
  generator        Generate a new generator
  model            Generate a new model
  reducer          Generate a new reducer
  surge            Generate a gulp file for publishing your project to surge.sh
  tutorial         Generate files for the tutorial
  tutorial:server  Generates the API server for the Lore tutorial

Options:
  --help  Show help                                                    [boolean]

And if you run lore generate component you'll get help specific to that command that shows the options:

Usage: $ generate component [options] <componentName>

Options:
  --componentName  Name of the component                                [string]
  --es6, -6        Generate an ES6 version of the component
  --connect, -c    Wrap the component in the lore.connect decorator
  --router, -r     Configure the component to use the router
  --force          Overwrite existing files                            [boolean]
  --help           Show help                                           [boolean]

Well-behaved CLI Arguments

CLI arguments are now respected like you would expect from a real CLI. This solves #69. The following commands are all valid:

lore generate component MyComponent --es6 --router --connect
lore generate component ---connect MyComponent
lore generate component MyComponent -c6r

Colors!

Color has been added to the output for improved readability. This was done through debug and debug-logger, though there's an issue (called out below).

index_js_-lore-___lore_lore

Improved Extensibility

As a big step towards making the CLI extensible, all generators now define their own interface, that looks like this:

var Generator = require('./generator');

module.exports = {

  command: "model",

  describe: "Generate a new model",

  options: {
    params: '<modelName>',

    options: {
      modelName: {
        description: 'Name of the model',
        type: 'string'
      }
    },

    handler: function(argv) {
      var generator = new Generator();
      generator.generate(argv);
    }
  }

};

This is the interface for lore-generate-model. All generators also inherit from Generator in lore-generate using a template pattern. For example, this is the interface for lore-generate-model:

var path = require('path');
var camelCase = require('camel-case');
var Generator = require('lore-generate').Generator;

module.exports = Generator.extend({

  moduleRoot: path.resolve(__dirname),

  templatesDirectory: path.resolve(__dirname, './templates'),

  after: function(options, targets) {
    var modelName = options.modelName;
    var dest = targets[0].destination.relativePath;
    this.logger.info('Created a new model `' + modelName + '` at `' + dest + '`');
  },

  targets: function(options) {
    var result = {};
    result['./src/models/' + camelCase(options.modelName) + '.js'] = { copy: './model.js'};
    return result;
  }

});

All generators have access to a logger that supports trace, debug, log, info, warn, error.

Steps needed to complete extensibility

Two things need to be done to support an extendible CLI:

  1. lore-cli needs to inspect the .lorerc file and look for other generators, then dynamically load them.
  2. To support allowing for nested categories, all generators should include a category field.

Issue w/ Debug

The CLI uses debug as the underlying logger, but it has a hard-coded feature that means ALL logs have a timestamp attached to them. While not too obnoxious for simple statements, it severely impacts the readability of instructions like this:

lore-generate_index_js_-_lore_-____lore_lore_

There is a PR to make it optional but it hasn't been accepted for merge. I'll either fork debug and use that version (to disable timestamps) or find a different logging solution. Undecided.

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 this pull request may close these issues.

None yet

1 participant