forked from angular-fullstack/generator-angular-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
299 lines (261 loc) · 10.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict';
var util = require('util');
var yeoman = require('yeoman-generator');
var childProcess = require('child_process');
var chalk = require('chalk');
var path = require('path');
var exec = childProcess.exec;
var spawn = childProcess.spawn;
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).split('-').join('');
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:',
default: this.appname
}];
this.prompt(prompts, function (props) {
this.deployedName = this._.slugify(props.deployedName).split('-').join('');
done();
}.bind(this));
};
Generator.prototype.checkInstallation = function checkInstallation() {
if(this.abort) return;
var done = this.async();
exec('rhc --version', function (err) {
if (err) {
this.log.error('OpenShift\'s rhc command line interface is not available. ' +
'You can install it via RubyGems with: gem install rhc');
this.abort = true;
}
done();
}.bind(this));
};
Generator.prototype.gitInit = function gitInit() {
if(this.abort) return;
var done = this.async();
this.log(chalk.bold('Initializing deployment repo'));
this.mkdir('dist');
exec('git init', { cwd: 'dist' }, function (err, stdout, stderr) {
this.log(stdout);
done();
}.bind(this));
};
Generator.prototype.gitRemoteCheck = function gitRemoteCheck() {
this.openshift_remote_exists = false;
if(this.abort || typeof this.dist_repo_url !== 'undefined') return;
var done = this.async();
this.log(chalk.bold("\nChecking for an existing git remote named '"+'openshift'+"'..."));
exec('git remote -v', { cwd: 'dist' }, function (err, stdout, stderr) {
var lines = stdout.split('\n');
var dist_repo = '';
if (err && stderr.search('DL is deprecated') === -1) {
this.log.error(err);
} else {
var repo_url_finder = new RegExp('openshift'+"[ ]*");
lines.forEach(function(line) {
if(line.search(repo_url_finder) === 0 && dist_repo === '') {
var dist_repo_detailed = line.slice(line.match(repo_url_finder)[0].length);
dist_repo = dist_repo_detailed.slice(0, dist_repo_detailed.length - 7);
}});
if (dist_repo !== ''){
console.log("Found an existing git remote for this app: "+dist_repo);
this.dist_repo_url = dist_repo;
this.openshift_remote_exists = true;
} else {
console.log('No existing remote found.');
}
}
done();
}.bind(this));
};
Generator.prototype.rhcAppShow = function rhcAppShow() {
if(this.abort || typeof this.dist_repo_url !== 'undefined') return;
var done = this.async();
this.log(chalk.bold("\nChecking for an existing OpenShift hosting environment..."));
var child = exec('rhc app show '+this.deployedName+' --noprompt', { cwd: 'dist' }, function (err, stdout, stderr) {
var lines = stdout.split('\n');
var dist_repo = '';
// Unauthenticated
if (stdout.search('Not authenticated') >= 0 || stdout.search('Invalid characters found in login') >= 0) {
this.log.error('Error: Not authenticated. Run "rhc setup" to login to your OpenShift account and try again.');
this.abort = true;
}
// No remote found
else if (stdout.search('not found.') >= 0) {
console.log('No existing app found.');
}
// Error
else if (err && stderr.search('DL is deprecated') === -1) {
this.log.error(err);
}
// Remote found
else {
this.log(stdout);
var repo_url_finder = / *Git URL: */;
lines.forEach(function(line) {
if(line.search(repo_url_finder) === 0) {
dist_repo = line.slice(line.match(repo_url_finder)[0].length);
console.log("Found an existing git remote for this app: "+dist_repo);
}});
if (dist_repo !== '') this.dist_repo_url = dist_repo;
}
done();
}.bind(this));
};
Generator.prototype.rhcAppCreate = function rhcAppCreate() {
if(this.abort || typeof this.dist_repo_url !== 'undefined') return;
var done = this.async();
this.log(chalk.bold("\nCreating your OpenShift hosting environment, this may take a couple minutes..."));
var child = exec('rhc app create '+this.deployedName+' nodejs-0.10 mongodb-2.4 -s --noprompt --no-git NODE_ENV=production', { cwd: 'dist' }, function (err, stdout, stderr) {
var lines = stdout.split('\n');
this.log(stdout);
if (stdout.search('Not authenticated') >= 0 || stdout.search('Invalid characters found in login') >= 0) {
this.log.error('Error: Not authenticated. Run "rhc setup" to login to your OpenShift account and try again.');
this.abort = true;
} else if (err && stderr.search('DL is deprecated') === -1) {
this.log.error(err);
} else {
var dist_repo = '';
var repo_url_finder = / *Git remote: */;
lines.forEach(function(line) {
if(line.search(repo_url_finder) === 0) {
dist_repo = line.slice(line.match(repo_url_finder)[0].length);
}});
if (dist_repo !== '') this.dist_repo_url = dist_repo;
if(this.dist_repo_url !== undefined) {
this.log("New remote git repo at: "+this.dist_repo_url);
}
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
Generator.prototype.gitRemoteAdd = function gitRemoteAdd() {
if(this.abort || typeof this.dist_repo_url === 'undefined' || this.openshift_remote_exists) return;
var done = this.async();
this.log(chalk.bold("\nAdding remote repo url: "+this.dist_repo_url));
var child = exec('git remote add '+'openshift'+' '+this.dist_repo_url, { cwd: 'dist' }, function (err, stdout, stderr) {
if (err) {
this.log.error(err);
} else {
this.openshift_remote_exists = true;
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
Generator.prototype.enableOpenShiftHotDeploy = function enableOpenshiftHotDeploy() {
if(this.abort || !this.openshift_remote_exists ) return;
var done = this.async();
this.log(chalk.bold("\nEnabling HotDeploy for OpenShift"));
this.copy('hot_deploy', 'dist/.openshift/markers/hot_deploy');
this.conflicter.resolve(function (err) {
done();
});
};
Generator.prototype.gruntBuild = function gruntBuild() {
if(this.abort || !this.openshift_remote_exists ) return;
var done = this.async();
this.log(chalk.bold('\nBuilding dist folder, please wait...'));
var child = exec('grunt build', function (err, stdout) {
if (err) {
this.log.error(err);
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(data.toString());
}.bind(this));
};
Generator.prototype.gitCommit = function gitInit() {
if(this.abort || !this.openshift_remote_exists ) return;
var done = this.async();
this.log(chalk.bold('\nAdding 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 || !this.openshift_remote_exists) 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 push = spawn('git', ['push', '-f', 'openshift', 'master'], {cwd: 'dist'});
var error = null;
push.stderr.on('data', function (data) {
var output = data.toString();
this.log.error(output);
}.bind(this));
push.stdout.on('data', function (data) {
var output = data.toString();
this.log.stdin(output);
}.bind(this));
push.on('exit', function (code) {
if (code !== 0) {
this.abort = true;
return done();
}
done();
}.bind(this));
};
Generator.prototype.restartApp = function restartApp() {
if(this.abort || !this.openshift_remote_exists ) return;
this.log(chalk.bold("\nRestarting your openshift app.\n"));
var child = exec('rhc app restart -a ' + this.deployedName, function(err, stdout, stderr) {
var host_url = '';
var hasWarning = false;
var before_hostname = this.dist_repo_url.indexOf('@') + 1;
var after_hostname = this.dist_repo_url.length - ( 'openshift'.length + 12 );
host_url = 'http://' + this.dist_repo_url.slice(before_hostname, after_hostname) + 'com';
if(this.filters.facebookAuth) {
this.log(chalk.yellow('You will need to set environment variables for facebook auth:\n\t' +
chalk.bold('rhc set-env FACEBOOK_ID=id -a ' + this.deployedName + '\n\t') +
chalk.bold('rhc set-env FACEBOOK_SECRET=secret -a ' + this.deployedName + '\n')));
hasWarning = true;
}
if(this.filters.googleAuth) {
this.log(chalk.yellow('You will need to set environment variables for google auth:\n\t' +
chalk.bold('rhc set-env GOOGLE_ID=id -a ' + this.deployedName + '\n\t') +
chalk.bold('rhc set-env GOOGLE_SECRET=secret -a ' + this.deployedName + '\n')));
hasWarning = true;
}
if(this.filters.twitterAuth) {
this.log(chalk.yellow('You will need to set environment variables for twitter auth:\n\t' +
chalk.bold('rhc set-env TWITTER_ID=id -a ' + this.deployedName + '\n\t') +
chalk.bold('rhc set-env TWITTER_SECRET=secret -a ' + this.deployedName + '\n')));
hasWarning = true;
}
this.log(chalk.green('\nYour app should now be live at \n\t' + chalk.bold(host_url)));
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 \n\t' +
'rhc app-restart -a ' + this.deployedName));
}
this.log(chalk.yellow('After app modification run\n\t' + chalk.bold('grunt build') +
'\nThen deploy with\n\t' + chalk.bold('grunt buildcontrol:openshift')));
}.bind(this));
};