|
2 | 2 | var yeoman = require('yeoman-generator'); |
3 | 3 | var chalk = require('chalk'); |
4 | 4 | var yosay = require('yosay'); |
5 | | - |
| 5 | +var _ = require('lodash'); |
| 6 | +var path = require('path'); |
| 7 | +var appFolder = 'app'; |
6 | 8 | module.exports = yeoman.generators.Base.extend({ |
| 9 | + initializing: function () { |
| 10 | + this.argument('name', { type: String, required: false }); |
| 11 | + this.appName = this.name || path.basename(process.cwd()) || 'webpack-redux-react-starter'; |
| 12 | + this.appPath = this.env.options.appPath; |
| 13 | + this.version = "0.0.1"; |
| 14 | + }, |
7 | 15 | prompting: function () { |
8 | 16 | var done = this.async(); |
9 | 17 |
|
10 | 18 | // Have Yeoman greet the user. |
11 | 19 | this.log(yosay( |
12 | | - 'Welcome to the exceptional ' + chalk.red('GeneratorReactRedux') + ' generator!' |
| 20 | + 'Welcome to the exceptional ' + chalk.red('Webpack Redux React') + ' generator!' |
13 | 21 | )); |
14 | 22 |
|
15 | 23 | var prompts = [{ |
16 | 24 | type: 'confirm', |
17 | 25 | name: 'someOption', |
18 | | - message: 'Would you like to enable this option?', |
19 | | - default: true |
| 26 | + message: 'Would you like to include Matter.js for authentication?', |
| 27 | + default: false |
20 | 28 | }]; |
21 | 29 |
|
22 | 30 | this.prompt(prompts, function (props) { |
23 | 31 | this.props = props; |
24 | 32 | // To access props later use this.props.someOption; |
25 | | - |
26 | 33 | done(); |
27 | 34 | }.bind(this)); |
28 | 35 | }, |
29 | | - |
30 | 36 | writing: { |
31 | 37 | app: function () { |
32 | | - this.fs.copy( |
33 | | - this.templatePath('_package.json'), |
34 | | - this.destinationPath('package.json') |
35 | | - ); |
36 | | - this.fs.copy( |
37 | | - this.templatePath('_bower.json'), |
38 | | - this.destinationPath('bower.json') |
39 | | - ); |
| 38 | + var filesArray = [ |
| 39 | + {src:'_index.html', dest:'index.html'}, |
| 40 | + {src:'app/**', dest: 'app'}, |
| 41 | + {src:'assets/**', dest: 'assets'}, |
| 42 | + {src:'bin/**', dest: 'bin'}, |
| 43 | + {src:'lib/**', dest: 'lib'}, |
| 44 | + ]; |
| 45 | + this.copyFiles(filesArray); |
40 | 46 | }, |
41 | | - |
42 | 47 | projectfiles: function () { |
43 | | - this.fs.copy( |
44 | | - this.templatePath('editorconfig'), |
45 | | - this.destinationPath('.editorconfig') |
46 | | - ); |
47 | | - this.fs.copy( |
48 | | - this.templatePath('jshintrc'), |
49 | | - this.destinationPath('.jshintrc') |
50 | | - ); |
| 48 | + var filesArray = [ |
| 49 | + {src:'_package.json', dest: 'package.json'}, |
| 50 | + {src:'webpack-dev.config.js'}, |
| 51 | + {src:'webpack.config.js'}, |
| 52 | + {src:'.gitignore'} |
| 53 | + ] |
| 54 | + this.copyFiles(filesArray); |
51 | 55 | } |
52 | 56 | }, |
53 | | - |
54 | 57 | install: function () { |
55 | | - this.installDependencies(); |
56 | | - } |
| 58 | + this.npmInstall(); |
| 59 | + }, |
| 60 | + /** |
| 61 | + * @param {Array|Object} filesArray |
| 62 | + */ |
| 63 | + copyFiles: function(filesArray){ |
| 64 | + var array = []; |
| 65 | + if(_.isArray(filesArray)){ |
| 66 | + array = filesArray; |
| 67 | + } else { //Handle array of arguments if first argument is not array |
| 68 | + array = arguments; |
| 69 | + } |
| 70 | + for(var i=0; i < array.length; i++){ |
| 71 | + var src = ''; |
| 72 | + var destination = ''; |
| 73 | + if (!_.has(array[i],'src')) { |
| 74 | + if (_.isString(array[i])) { |
| 75 | + src = array[i]; |
| 76 | + } else { |
| 77 | + console.error('Invalid source for file copying.'); |
| 78 | + throw new Error('Invalid source for file copy.'); |
| 79 | + } |
| 80 | + } |
| 81 | + if(_.isObject(array[i])){ |
| 82 | + src = array[i].src; |
| 83 | + destination = array[i].dest || array[i].src; //Make destination source if not provided |
| 84 | + } |
| 85 | + if(src.charAt(0) === "_"){ //template if filename starts with _ |
| 86 | + //Copy with templating |
| 87 | + this.template(src, destination, this.templateContext); |
| 88 | + } else if(src.indexOf('*') !== -1 || src.indexOf('/**') !== -1) { |
| 89 | + //TODO: make this work better (work with nested folders and use src correctly) |
| 90 | + src.replace("**", ""); //Remove /** |
| 91 | + src.replace("/", ""); //Remove / |
| 92 | + this.directory(destination, destination); |
| 93 | + } else { |
| 94 | + //Normal copy |
| 95 | + this.fs.copy( |
| 96 | + this.templatePath(src), |
| 97 | + this.destinationPath(destination) |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
57 | 102 | }); |
0 commit comments