-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
209 lines (178 loc) · 6.47 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
'use strict';
var util = require('util');
var genUtils = require('../util.js');
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.askForRoute = function askForRoute() {
var done = this.async();
var prompts = [
{
name: 'routeName',
message: 'Name of route to deploy (Leave blank for a random route name):'
}
];
this.prompt(prompts, function(props) {
this.routeName = this._.slugify(props.routeName);
done();
}.bind(this));
};
Generator.prototype.checkInstallation = function checkInstallation() {
if (this.abort) return;
var done = this.async();
exec('cf --version', function(err) {
if (err) {
this.log.error('You don\'t have the Cloud Foundry CLI installed. ' +
'Grab it from https://github.com/cloudfoundry/cli');
this.abort = true;
}
done();
}.bind(this));
};
Generator.prototype.askForApiEndpoint = function askForApiEndpoint() {
if (this.abort) return;
var done = this.async();
var prompts = [
{
name: 'apiEndpoint',
default: 'api.run.pivotal.io',
message: 'What api endpoint will you be using for Cloud Foundry?:'
}
];
this.prompt(prompts, function(props) {
this.apiEndpoint = props.apiEndpoint;
done();
}.bind(this));
};
Generator.prototype.cfInit = function cfInit() {
if (this.abort) return;
var done = this.async();
this.log(chalk.bold('Setting Cloud Foundry api endpoint'));
this.mkdir('dist');
var child = exec('cf api ' + this.apiEndpoint, { cwd: 'dist' }, function(err, stdout, stderr) {
if (err) {
this.abort = true;
this.log.error(err);
} else {
if (stdout.indexOf('Not logged in.') !== -1) {
this.log.error('You don\'t appear to be logged. Please login and try again.');
this.abort = true;
} else {
this.log('stdout: ' + stdout);
}
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(this._.trim(data.toString(), "\n\r"));
}.bind(this));
}
Generator.prototype.copyProcfile = function copyProcfile() {
if (this.abort) return;
var done = this.async();
this.log(chalk.bold('Creating Procfile and manifest.yml'));
genUtils.processDirectory(this, '.', './dist');
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.cfPush = function cfPush() {
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 randomRoute = this.routeName === '' ? '--random-route' : '';
var child = exec(['cf push', this.appname, randomRoute, ' --no-start'].join(' '), { 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) {
this.log(this._.trim(data.toString(), "\n\r"));
}.bind(this));
};
Generator.prototype.cfSetEnvVars = function cfSetEnvVars() {
if (this.abort) return;
var done = this.async();
var child = exec('cf set-env ' + this.appname + ' NODE_ENV production', { cwd: 'dist' }, function(err, stdout, stderr) {
if (err) {
this.abort = true;
this.log.error(err);
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(this._.trim(data.toString(), "\n\r"));
}.bind(this));
};
Generator.prototype.cfStart = function cfStart() {
if (this.abort) return;
var done = this.async();
var child = exec('cf start ' + this.appname, { cwd: 'dist' }, function(err, stdout, stderr) {
if (err) {
this.abort = true;
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 cloud foundry app.\n\t' + 'from `/dist`: ' + chalk.bold('cf create-service mongolab sandbox my-mongo') + '\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('cf set-env ' + this.appname + ' FACEBOOK_ID appId\n\t') +
chalk.bold('cf set-env ' + this.appname + ' 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('cf set-env ' + this.appname + ' GOOGLE_ID appId\n\t') +
chalk.bold('cf set-env ' + this.appname + ' 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('cf set-env ' + this.appname + ' TWITTER_ID appId\n\t') +
chalk.bold('cf set-env ' + this.appname + ' TWITTER_SECRET secret\n')));
hasWarning = true;
}
this.log(chalk.green('\nYour app should now be live.'));
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 (from dist directory ) with\n\t' + chalk.bold('cf push')));
}
done();
}.bind(this));
child.stdout.on('data', function(data) {
this.log(this._.trim(data.toString(), "\n\r"));
}.bind(this));
};