From b73920bf6daf5bcace04d6ef99a16ebd3e8b61bc Mon Sep 17 00:00:00 2001 From: Andrew Worcester Date: Sun, 7 Feb 2016 09:29:20 -0500 Subject: [PATCH] small adjustments to help screen layout and code location --- .jshintrc | 2 +- README.md | 9 ++++++--- mod/console-out.js | 32 +++++++++++++++++++++++++++++++- plop.js | 44 +++++++++----------------------------------- 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/.jshintrc b/.jshintrc index cf831fd3..4bd80902 100644 --- a/.jshintrc +++ b/.jshintrc @@ -51,7 +51,7 @@ "expr" : false, // Tolerate `ExpressionStatement` as Programs. "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`. "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` - "latedef" : true, // Prohibit variable use before definition. + "latedef" : "nofunc", // Prohibit variable use before definition. "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. "loopfunc" : false, // Allow functions to be defined within loops. "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. diff --git a/README.md b/README.md index 34039118..a7fc94e2 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ Plop Micro-generator framework that makes it easy for an entire team to create files with a level or uniformity. +[![npm](https://img.shields.io/npm/dm/plop.svg)]() +[![npm](https://img.shields.io/npm/v/plop.svg)]() + ![plop demo](http://i.imgur.com/penUFkr.gif) Plop is essentially glue code between [inquirer](https://github.com/SBoudrias/Inquirer.js/) prompts and [handlebar](https://github.com/wycats/handlebars.js/) templates. You can also add your own handlebar helper methods and use them in your templates. @@ -127,14 +130,14 @@ The `Add` and `Modify` actions will take care of almost every case that plop is _See the [example plopfile](https://github.com/amwmedia/plop/blob/master/example/plopfile.js) for a sample synchronous custom action._ -### Using an Actions Function -Alternatively, `actions` can be a function that takes responses `data` as a parameter and should return an array of actions. +### Using a Dynamic Action Array +Alternatively, `actions` can itself be a function that takes responses `data` as a parameter and should return the actions array. This allows you to adapt actions to provided answers: ``` javascript module.exports = function (plop) { - plop.setGenerator('test', { + plop.setGenerator('test', { description: 'this is a test', prompts: [{ type: 'input', diff --git a/mod/console-out.js b/mod/console-out.js index 34038f87..c9d1bb05 100644 --- a/mod/console-out.js +++ b/mod/console-out.js @@ -2,9 +2,11 @@ var colors = require('colors'); var inquirer = require('inquirer'); +var fs = require('fs'); var q = require('q'); module.exports = (function () { + function chooseOptionFromList(plopList) { var _d = q.defer(); @@ -27,7 +29,35 @@ module.exports = (function () { return _d.promise; } + function displayHelpScreen() { + console.log( + '\n' + + 'USAGE:\n' + + ' $ plop \t\tRun a generator registered under that name\n' + + + '\n' + + 'OPTIONS:\n' + + ' -h, --help\t\tShow this help display\n' + + ' -i, --init\t\tGenerate initial plopfile.js\n' + + ' -v, --version\t\tPrint current version\n' + ); + } + + function createInitPlopfile(cwd, callback){ + var initString = 'module.exports = function (plop) {\n\n' + + '\tplop.setGenerator(\'basics\', {\n' + + '\t\tdescription: \'this is a skeleton plopfile\',\n' + + '\t\tprompts: [],\n' + + '\t\tactions: []\n' + + '\t});\n\n' + + '};'; + + fs.writeFile( + '/plopfile.js', initString, callback); + } + return { - chooseOptionFromList: chooseOptionFromList + chooseOptionFromList: chooseOptionFromList, + displayHelpScreen: displayHelpScreen, + createInitPlopfile: createInitPlopfile }; })(); diff --git a/plop.js b/plop.js index 209d82fd..9bf12fa8 100755 --- a/plop.js +++ b/plop.js @@ -3,7 +3,6 @@ 'use strict'; var path = require('path'); -var fs = require('fs'); var Liftoff = require('liftoff'); var argv = require('minimist')(process.argv.slice(2)); var v8flags = require('v8flags'); @@ -35,18 +34,17 @@ function run(env) { // handle request for usage and options if (argv.help || argv.h) { - displayHelpScreen(); + out.displayHelpScreen(); process.exit(0); } - + + // handle request for initializing a new plopfile if (argv.init || argv.i) { - return createInitPlopfile(function(err){ + return out.createInitPlopfile(env.cwd, function(err){ if (err){ console.log(err); - process.exit(1); } - process.exit(0); }); } @@ -66,7 +64,7 @@ function run(env) { // abort if there's no plopfile found if (plopfilePath == null) { console.error(colors.red('[PLOP] ') + 'No plopfile found'); - displayHelpScreen(); + out.displayHelpScreen(); process.exit(1); } @@ -78,41 +76,17 @@ function run(env) { generators = plop.getGeneratorList(); if (!generator) { - out.chooseOptionFromList(generators).then(go); + out.chooseOptionFromList(generators).then(doThePlop); }else if (generators.map(function (v) { return v.name; }).indexOf(generator) > -1) { - go(generator); + doThePlop(generator); } else { console.error(colors.red('[PLOP] ') + 'Generator "' + generator + '" not found in plopfile'); process.exit(1); } - function displayHelpScreen(){ - console.log('\n' + - '\tUsage\n' + - '\t\t$ plop \t\tRun a generator registered under that name\n' + - - '\n' + - '\tOptions\n' + - '\t\t-h, --help\t\tShow this help display\n' + - '\t\t-i, --init\t\tGenerate initial plopfile.js\n' + - '\t\t-v, --version\t\tPrint current version\n'); - } - - function createInitPlopfile(callback){ - var initString = 'module.exports = function (plop) {\n\n' + - '\tplop.setGenerator(\'basics\', {\n' + - '\t\tdescription: \'this is a skeleton plopfile\',\n' + - '\t\tprompts: [],\n' + - '\t\tactions: []\n' + - '\t});\n\n' + - '};'; - - fs.writeFile(env.cwd + '/plopfile.js', initString, callback); - } - } -function go(generator) { +function doThePlop(generator) { logic.getPlopData(generator) .then(logic.executePlop) .then(function (result) { @@ -127,4 +101,4 @@ function go(generator) { console.error('[ERROR]'.red, err.message, err.stack); process.exit(1); }); -} \ No newline at end of file +}