forked from angular-fullstack/generator-angular-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (168 loc) · 6.21 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict';
var util = require('util');
var yeoman = require('yeoman-generator');
var exec = require('child_process').exec;
var chalk = require('chalk');
var path = require('path');
var Generator = module.exports = function Generator() {
yeoman.generators.Base.apply(this, arguments);
this.sourceRoot(path.join(__dirname, './templates'));
try {
this.appname = require(path.join(process.cwd(), 'bower.json')).name;
} catch (e) {
this.appname = path.basename(process.cwd());
}
this.appname = this._.slugify(this.appname);
this.filters = this.config.get('filters') || {};
};
util.inherits(Generator, yeoman.generators.NamedBase);
Generator.prototype.askForName = function askForName() {
var done = this.async();
var prompts = [{
name: 'deployedName',
message: 'Name to deploy as (Leave blank for a random name):'
}];
this.prompt(prompts, function (props) {
this.deployedName = this._.slugify(props.deployedName);
done();
}.bind(this));
};
Generator.prototype.askForRegion = function askForRegion() {
var done = this.async();
var prompts = [{
type: "list",
name: 'region',
message: 'On which region do you want to deploy ?',
choices: [ "US", "EU"],
default: 0
}];
this.prompt(prompts, function (props) {
this.region = props.region.toLowerCase();
done();
}.bind(this));
};
Generator.prototype.checkInstallation = function checkInstallation() {
if(this.abort) return;
var done = this.async();
exec('heroku --version', function (err) {
if (err) {
this.log.error('You don\'t have the Heroku Toolbelt installed. ' +
'Grab it from https://toolbelt.heroku.com/');
this.abort = true;
}
done();
}.bind(this));
};
Generator.prototype.gitInit = function gitInit() {
if(this.abort) return;
var done = this.async();
this.log(chalk.bold('\nInitializing deployment repo'));
this.mkdir('dist');
var child = exec('git init', { cwd: 'dist' }, function (err, stdout, stderr) {
done();
}.bind(this));
child.stdout.on('data', function(data) {
console.log(data.toString());
});
};
Generator.prototype.herokuCreate = function herokuCreate() {
if(this.abort) return;
var done = this.async();
var regionParams = (this.region !== 'us') ? ' --region ' + this.region : '';
this.log(chalk.bold('Creating heroku app and setting node environment'));
var child = exec('heroku apps:create ' + this.deployedName + regionParams + ' && heroku config:set NODE_ENV=production', { cwd: 'dist' }, function (err, stdout, stderr) {
if (err) {
this.abort = true;
this.log.error(err);
} else {
this.log('stdout: ' + stdout);
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
var output = data.toString();
this.log(output);
}.bind(this));
};
Generator.prototype.copyProcfile = function copyProcfile() {
if(this.abort) return;
var done = this.async();
this.log(chalk.bold('Creating Procfile'));
this.copy('Procfile', 'dist/Procfile');
this.conflicter.resolve(function (err) {
done();
});
};
Generator.prototype.gruntBuild = function gruntBuild() {
if(this.abort) return;
var done = this.async();
this.log(chalk.bold('\nBuilding dist folder, please wait...'));
var child = exec('grunt build', function (err, stdout) {
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
Generator.prototype.gitCommit = function gitInit() {
if(this.abort) return;
var done = this.async();
this.log(chalk.bold('Adding files for initial commit'));
var child = exec('git add -A && git commit -m "Initial commit"', { cwd: 'dist' }, function (err, stdout, stderr) {
if (stdout.search('nothing to commit') >= 0) {
this.log('Re-pushing the existing "dist" build...');
} else if (err) {
this.log.error(err);
} else {
this.log(chalk.green('Done, without errors.'));
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
Generator.prototype.gitForcePush = function gitForcePush() {
if(this.abort) return;
var done = this.async();
this.log(chalk.bold("\nUploading your initial application code.\n This may take "+chalk.cyan('several minutes')+" depending on your connection speed..."));
var child = exec('git push -f heroku master', { cwd: 'dist' }, function (err, stdout, stderr) {
if (err) {
this.log.error(err);
} else {
var hasWarning = false;
if(this.filters.mongoose) {
this.log(chalk.yellow('\nBecause you\'re using mongoose, you must add mongoDB to your heroku app.\n\t' + 'from `/dist`: ' + chalk.bold('heroku addons:create mongolab') + '\n'));
hasWarning = true;
}
if(this.filters.facebookAuth) {
this.log(chalk.yellow('You will need to set environment variables for facebook auth. From `/dist`:\n\t' +
chalk.bold('heroku config:set FACEBOOK_ID=appId\n\t') +
chalk.bold('heroku config:set FACEBOOK_SECRET=secret\n')));
hasWarning = true;
}
if(this.filters.googleAuth) {
this.log(chalk.yellow('You will need to set environment variables for google auth. From `/dist`:\n\t' +
chalk.bold('heroku config:set GOOGLE_ID=appId\n\t') +
chalk.bold('heroku config:set GOOGLE_SECRET=secret\n')));
hasWarning = true;
}
if(this.filters.twitterAuth) {
this.log(chalk.yellow('You will need to set environment variables for twitter auth. From `/dist`:\n\t' +
chalk.bold('heroku config:set TWITTER_ID=appId\n\t') +
chalk.bold('heroku config:set TWITTER_SECRET=secret\n')));
hasWarning = true;
}
this.log(chalk.green('\nYour app should now be live. To view it run\n\t' + chalk.bold('cd dist && heroku open')));
if(hasWarning) {
this.log(chalk.green('\nYou may need to address the issues mentioned above and restart the server for the app to work correctly.'));
}
this.log(chalk.yellow('After app modification run\n\t' + chalk.bold('grunt build') +
'\nThen deploy with\n\t' + chalk.bold('grunt buildcontrol:heroku')));
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};