-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
129 lines (119 loc) · 4.5 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
'use strict';
var generators = require('yeoman-generator');
var gen = generators.Base.extend({
initializing: function() {
try {
this.username = process.env.USER || process.env.USERPROFILE.split(require('path').sep)[2];
} catch (e) {
this.username = '';
}
},
prompting: function() {
var done = this.async();
var self = this;
this.prompt([
{
type: 'input',
name: 'name',
message: 'Your project name',
validate: function(name) {
if (!name) {
return 'Project name cannot be empty';
}
if (!/\w+/.test(name)) {
return 'Project name should only consist of 0~9, a~z, A~Z, _, .';
}
var fs = require('fs');
if (!fs.existsSync(self.destinationPath(name))) {
return true;
}
if (require('fs').statSync(self.destinationPath(name)).isDirectory()) {
return 'Project already exist';
}
return true;
}
},
{
type: 'input',
name: 'description',
message: 'Your project description',
default: ''
},
{
type: 'input',
name: 'username',
message: 'Your name',
default: this.username
},
{
type: 'input',
name: 'email',
message: 'Your email',
default: ''
},
{
type: 'confirm',
name: 'pushState',
message: 'Use html5 mode?',
default: true
},
{
type: 'list',
name: 'registry',
message: 'Which registry would you use?',
choices: [
'https://registry.npm.taobao.org',
'https://registry.npmjs.org'
]
}
])
.then(function(answers) {
require('date-util');
self.answers = answers;
self.answers.date = new Date().format('mmm d, yyyy');
self.obj = {answers: self.answers};
done();
});
},
configuring: function(answers) {
var path = require('path');
var fs = require('fs');
var self = this;
var done = this.async();
fs.exists(this.destinationPath(this.answers.name), function(exists) {
if (exists && fs.statSync(self.destinationPath(self.answers.name)).isDirectory()) {
self.log.error('Directory [' + self.answers.name + '] exists');
process.exit(1);
}
self.destinationRoot(path.join(self.destinationRoot(), self.answers.name));
done();
});
},
writing: function() {
var self = this;
var _ = require('lodash');
self.fs.copyTpl(self.templatePath('etc/config.js'), self.destinationPath('etc/config.js'), self.obj);
self.directory(self.templatePath('img'), self.destinationPath('img'));
self.directory(self.templatePath('js'), self.destinationPath('js'), function(body) {
return _.template(body, {
interpolate: /<%=([\s\S]+?)%>/g
})(self.obj);
});
self.directory(self.templatePath('css'), self.destinationPath('css'));
self.fs.copy(self.templatePath('eslintrc'), self.destinationPath('.eslintrc'));
self.fs.copy(self.templatePath('gitignore'), self.destinationPath('.gitignore'));
self.fs.copy(self.templatePath('index.html_vm'), self.destinationPath('index.html_vm'));
self.fs.copyTpl(self.templatePath('package.json_vm'), self.destinationPath('package.json'), self.obj);
self.fs.copyTpl(self.templatePath('webpack.config.dev.js'),
self.destinationPath('webpack.config.dev.js'), self.obj);
self.fs.copyTpl(self.templatePath('webpack.config.prod.js'),
self.destinationPath('webpack.config.prod.js'), self.obj);
},
install: function() {
this.npmInstall(null, {registry: this.answers.registry});
},
end: function() {
this.log.ok('Project ' + this.answers.name + ' generated!!!');
}
});
module.exports = gen;