|
| 1 | +'use strict'; |
| 2 | +var util = require('util'); |
| 3 | +var path = require('path'); |
| 4 | +var yeoman = require('yeoman-generator'); |
| 5 | +var yosay = require('yosay'); |
| 6 | + |
| 7 | +var YargsGenerator = yeoman.generators.Base.extend({ |
| 8 | + |
| 9 | + constructor: function() { |
| 10 | + yeoman.generators.Base.apply(this, arguments); |
| 11 | + this.option("coffee", { |
| 12 | + desc: "Generate a project for CoffeeScript", |
| 13 | + type: "boolean", |
| 14 | + default: false |
| 15 | + }); |
| 16 | + }, |
| 17 | + |
| 18 | + initializing: function () { |
| 19 | + this.pkg = require('../package.json'); |
| 20 | + |
| 21 | + }, |
| 22 | + |
| 23 | + prompting: function () { |
| 24 | + var done = this.async(); |
| 25 | + |
| 26 | + this.useCoffee = !!this.options['coffee']; |
| 27 | + |
| 28 | + // Have Yeoman greet the user. |
| 29 | + this.log(yosay( |
| 30 | + 'Welcome to the great Yargs command generator!' |
| 31 | + )); |
| 32 | + |
| 33 | + var prompts = [ |
| 34 | + { |
| 35 | + type: 'input', |
| 36 | + name: 'author', |
| 37 | + message: 'Who is the author?', |
| 38 | + default: "Linus Torvalds" |
| 39 | + }, |
| 40 | + { |
| 41 | + type: 'input', |
| 42 | + name: 'commandName', |
| 43 | + message: 'What should your command be called?', |
| 44 | + default: "rm-rf-slash" |
| 45 | + }, |
| 46 | + { |
| 47 | + type: 'input', |
| 48 | + name: 'description', |
| 49 | + message: 'Describe what your command does.', |
| 50 | + default: "Formats your hard drive." |
| 51 | + } |
| 52 | + ]; |
| 53 | + |
| 54 | + if(!this.useCoffee) |
| 55 | + prompts.push({ |
| 56 | + type: 'confirm', |
| 57 | + name: 'useCoffee', |
| 58 | + message: "Would you like to use CoffeeScript?", |
| 59 | + default: false |
| 60 | + }) |
| 61 | + |
| 62 | + this.prompt(prompts, function (props) { |
| 63 | + for(var key in props){ |
| 64 | + this[key] = props[key]; |
| 65 | + } |
| 66 | + done(); |
| 67 | + }.bind(this)); |
| 68 | + }, |
| 69 | + |
| 70 | + writing: { |
| 71 | + app: function () { |
| 72 | + this.template('_package.json', 'package.json'); |
| 73 | + this.template('.gitignore', '.gitignore'); |
| 74 | + this.template('.npmignore', '.npmignore'); |
| 75 | + this.dest.mkdir('src'); |
| 76 | + |
| 77 | + if(this.useCoffee) |
| 78 | + { |
| 79 | + this.src.copy('Gruntfile.coffee', 'Gruntfile.coffee') |
| 80 | + this.src.copy('src/index.coffee', 'src/index.coffee'); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + this.src.copy('src/index.js', 'src/index.js'); |
| 85 | + } |
| 86 | + }, |
| 87 | + |
| 88 | + }, |
| 89 | + |
| 90 | + end: function () { |
| 91 | + this.installDependencies(); |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +module.exports = YargsGenerator; |
0 commit comments