-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathindex.js
169 lines (137 loc) · 4.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const Generators = require('yeoman-generator');
const utils = require('../../utils/all');
const prompts = require('./prompts');
const path = require('path');
const fs = require('fs');
const packageInfo = require('../../package.json');
// Set the base root directory for our files. Make sure to always use the node_modules
// base path instead of the require call only. This is needed because require.resolve
// also includes the path set in package.json main keys!
const baseRootPath = path.join(path.dirname(require.resolve('react-webpack-template')), '..');
/**
* Base generator. Will copy all required files from react-webpack-template
*/
class AppGenerator extends Generators.Base {
constructor(args, options) {
super(args, options);
// Make options available
this.option('skip-welcome-message', {
desc: 'Skip the welcome message',
type: Boolean,
defaults: false
});
this.option('skip-install');
// Use our plain template as source
this.sourceRoot(baseRootPath);
this.config.save();
}
initializing() {
if(!this.options['skip-welcome-message']) {
this.log(require('yeoman-welcome'));
this.log('Out of the box I include Webpack and some default React components.\n');
}
}
prompting() {
return this.prompt(prompts).then((answers) => {
// Make sure to get the correct app name if it is not the default
if(answers.appName !== utils.yeoman.getAppName()) {
answers.appName = utils.yeoman.getAppName(answers.appName);
}
// Set needed global vars for yo
this.appName = answers.appName;
this.style = answers.style;
this.cssmodules = answers.cssmodules;
this.postcss = answers.postcss;
this.generatedWithVersion = parseInt(packageInfo.version.split('.').shift(), 10);
// Set needed keys into config
this.config.set('appName', this.appName);
this.config.set('appPath', this.appPath);
this.config.set('style', this.style);
this.config.set('cssmodules', this.cssmodules);
this.config.set('postcss', this.postcss);
this.config.set('generatedWithVersion', this.generatedWithVersion);
});
}
configuring() {
// Generate our package.json. Make sure to also include the required dependencies for styles
let defaultSettings = this.fs.readJSON(`${baseRootPath}/package.json`);
let packageSettings = {
name: this.appName,
private: true,
version: '0.0.1',
description: `${this.appName} - Generated by generator-react-webpack`,
main: 'src/index.js',
scripts: defaultSettings.scripts,
repository: '',
keywords: [],
author: 'Your name here',
devDependencies: defaultSettings.devDependencies,
dependencies: defaultSettings.dependencies
};
// Add needed loaders if we have special styles
let styleConfig = utils.config.getChoiceByKey('style', this.style);
if(styleConfig && styleConfig.packages) {
for(let dependency of styleConfig.packages) {
packageSettings.devDependencies[dependency.name] = dependency.version;
}
}
// Add postcss module if enabled
let postcssConfig = utils.config.getChoiceByKey('postcss', 'postcss');
if(this.postcss && postcssConfig && postcssConfig.packages) {
for(let dependency of postcssConfig.packages) {
packageSettings.devDependencies[dependency.name] = dependency.version;
}
}
// Add cssmodules if enabled
const cssmoduleConfig = utils.config.getChoiceByKey('cssmodules', 'cssmodules');
if(this.cssmodules && cssmoduleConfig && cssmoduleConfig.packages) {
for(let dependency of cssmoduleConfig.packages) {
packageSettings.dependencies[dependency.name] = dependency.version;
}
}
this.fs.writeJSON(this.destinationPath('package.json'), packageSettings);
}
writing() {
const excludeList = [
'LICENSE',
'README.md',
'CHANGELOG.md',
'node_modules',
'package.json',
'.istanbul.yml',
'.travis.yml'
];
// Get all files in our repo and copy the ones we should
fs.readdir(this.sourceRoot(), (err, items) => {
for(let item of items) {
// Skip the item if it is in our exclude list
if(excludeList.indexOf(item) !== -1) {
continue;
}
// Copy all items to our root
let fullPath = path.join(baseRootPath, item);
if(fs.lstatSync(fullPath).isDirectory()) {
this.bulkDirectory(item, item);
} else {
if (item === '.npmignore') {
this.copy(item, '.gitignore');
} else {
this.copy(item, item);
}
}
}
});
}
install() {
// Currently buggy!
if(this.postcss) {
const postcss = require('./postcss');
postcss.write(path.join(this.destinationRoot(), 'conf/webpack/Base.js'));
}
if(!this.options['skip-install']) {
this.installDependencies({ bower: false });
}
}
}
module.exports = AppGenerator;