Skip to content

Commit 9ee9b4b

Browse files
committed
feat(gen:cloudfoundry): add cloudfoundry deploy sub-generator
1 parent f29e907 commit 9ee9b4b

File tree

5 files changed

+191
-1
lines changed

5 files changed

+191
-1
lines changed

app/templates/Gruntfile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ module.exports = function (grunt) {
216216
'<%%= yeoman.dist %>/*',
217217
'!<%%= yeoman.dist %>/.git*',
218218
'!<%%= yeoman.dist %>/.openshift',
219-
'!<%%= yeoman.dist %>/Procfile'
219+
'!<%%= yeoman.dist %>/.cfignore',
220+
'!<%%= yeoman.dist %>/Procfile',
221+
'!<%%= yeoman.dist %>/manifest.yml'
220222
]
221223
}]
222224
},

cloudfoundry/USAGE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Description:
2+
Initalizes a cloud foundr app and generates a `dist` folder which is ready to push to cloud foundry.
3+
4+
Example:
5+
yo angular-fullstack:cloudfoundry
6+
7+
This will create:
8+
a dist folder and initialize a cloud foundry app

cloudfoundry/index.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
'use strict';
2+
var util = require('util');
3+
var genUtils = require('../util.js');
4+
var yeoman = require('yeoman-generator');
5+
var exec = require('child_process').exec;
6+
var chalk = require('chalk');
7+
var path = require('path');
8+
9+
var Generator = module.exports = function Generator() {
10+
yeoman.generators.Base.apply(this, arguments);
11+
this.sourceRoot(path.join(__dirname, './templates'));
12+
13+
try {
14+
this.appname = require(path.join(process.cwd(), 'bower.json')).name;
15+
} catch (e) {
16+
this.appname = path.basename(process.cwd());
17+
}
18+
this.appname = this._.slugify(this.appname);
19+
this.filters = this.config.get('filters') || {};
20+
};
21+
22+
util.inherits(Generator, yeoman.generators.NamedBase);
23+
24+
Generator.prototype.askForRoute = function askForRoute() {
25+
var done = this.async();
26+
27+
var prompts = [
28+
{
29+
name: 'routeName',
30+
message: 'Name of route to deploy (Leave blank for a random route name):'
31+
}
32+
];
33+
34+
this.prompt(prompts, function (props) {
35+
this.routeName = this._.slugify(props.routeName);
36+
done();
37+
}.bind(this));
38+
};
39+
40+
Generator.prototype.checkInstallation = function checkInstallation() {
41+
if (this.abort) return;
42+
var done = this.async();
43+
44+
exec('cf --version', function (err) {
45+
if (err) {
46+
this.log.error('You don\'t have the Cloud Foundry CLI installed. ' +
47+
'Grab it from https://github.com/cloudfoundry/cli');
48+
this.abort = true;
49+
}
50+
done();
51+
}.bind(this));
52+
};
53+
54+
Generator.prototype.askForApiEndpoint = function askForApiEndpoint() {
55+
if (this.abort) return;
56+
var done = this.async();
57+
58+
var prompts = [
59+
{
60+
name: 'apiEndpoint',
61+
default: 'api.run.pivotal.io',
62+
message: 'What api endpoint will you be using for Cloud Foundry?:'
63+
}
64+
];
65+
66+
this.prompt(prompts, function (props) {
67+
this.apiEndpoint = props.apiEndpoint;
68+
done();
69+
}.bind(this));
70+
};
71+
72+
Generator.prototype.cfInit = function cfInit() {
73+
if (this.abort) return;
74+
var done = this.async();
75+
76+
this.log(chalk.bold('Setting Cloud Foundry api endpoint'));
77+
this.mkdir('dist');
78+
var child = exec('cf api ' + this.apiEndpoint, { cwd: 'dist' }, function (err, stdout, stderr) {
79+
if (err) {
80+
this.abort = true;
81+
this.log.error(err);
82+
} else {
83+
if (stdout.indexOf('Not logged in.') !== -1) {
84+
this.log.error('You don\'t appear to be logged. Please login and try again.');
85+
this.abort = true;
86+
} else {
87+
this.log('stdout: ' + stdout);
88+
}
89+
90+
}
91+
done();
92+
}.bind(this));
93+
94+
child.stdout.on('data', function (data) {
95+
this.log(this._.trim(data.toString(), "\n\r"));
96+
}.bind(this));
97+
}
98+
99+
Generator.prototype.copyProcfile = function copyProcfile() {
100+
if (this.abort) return;
101+
var done = this.async();
102+
this.log(chalk.bold('Creating Procfile and manifest.yml'));
103+
genUtils.processDirectory(this, '.', './dist');
104+
this.conflicter.resolve(function (err) {
105+
done();
106+
});
107+
};
108+
109+
Generator.prototype.gruntBuild = function gruntBuild() {
110+
if (this.abort) return;
111+
var done = this.async();
112+
113+
this.log(chalk.bold('\nBuilding dist folder, please wait...'));
114+
var child = exec('grunt build', function (err, stdout) {
115+
done();
116+
}.bind(this));
117+
child.stdout.on('data', function (data) {
118+
this.log(data.toString());
119+
}.bind(this));
120+
};
121+
122+
Generator.prototype.cfPush = function cfPush() {
123+
if (this.abort) return;
124+
var done = this.async();
125+
126+
this.log(chalk.bold("\nUploading your initial application code.\n This may take " + chalk.cyan('several minutes') + " depending on your connection speed..."));
127+
128+
var randomRoute = this.routeName === '' ? '--random-route' : '';
129+
var child = exec(['cf push', this.appname, randomRoute].join(' '), { cwd: 'dist' }, function (err, stdout, stderr) {
130+
if (err) {
131+
this.log.error(err);
132+
} else {
133+
var hasWarning = false;
134+
135+
if (this.filters.mongoose) {
136+
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'));
137+
hasWarning = true;
138+
}
139+
140+
if (this.filters.facebookAuth) {
141+
this.log(chalk.yellow('You will need to set environment variables for facebook auth. From `/dist`:\n\t' +
142+
chalk.bold('cf set-env ' + this.appName + ' FACEBOOK_ID appId\n\t') +
143+
chalk.bold('cf set-env ' + this.appName + ' FACEBOOK_SECRET secret\n')));
144+
hasWarning = true;
145+
}
146+
if (this.filters.googleAuth) {
147+
this.log(chalk.yellow('You will need to set environment variables for google auth. From `/dist`:\n\t' +
148+
chalk.bold('cf set-env ' + this.appName + ' GOOGLE_ID appId\n\t') +
149+
chalk.bold('cf set-env ' + this.appName + ' GOOGLE_SECRET secret\n')));
150+
hasWarning = true;
151+
}
152+
if (this.filters.twitterAuth) {
153+
this.log(chalk.yellow('You will need to set environment variables for twitter auth. From `/dist`:\n\t' +
154+
chalk.bold('cf set-env ' + this.appName + ' TWITTER_ID appId\n\t') +
155+
chalk.bold('cf set-env ' + this.appName + ' TWITTER_SECRET secret\n')));
156+
hasWarning = true;
157+
}
158+
159+
this.log(chalk.green('\nYour app should now be live.'));
160+
if (hasWarning) {
161+
this.log(chalk.green('\nYou may need to address the issues mentioned above and restart the server for the app to work correctly.'));
162+
}
163+
/*
164+
todo: build grunt plugin grunt-cf-deploy and add to this generator
165+
this.log(chalk.yellow('After app modification run\n\t' + chalk.bold('grunt build') +
166+
'\nThen deploy with\n\t' + chalk.bold('grunt cfDeploy')));
167+
*/
168+
}
169+
done();
170+
}.bind(this));
171+
child.stdout.on('data', function (data) {
172+
this.log(this._.trim(data.toString(), "\n\r"));
173+
}.bind(this));
174+
};

cloudfoundry/templates/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: node server/app.js
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
applications:
3+
- name: <%= appname %>
4+
buildpack: https://github.com/cloudfoundry/heroku-buildpack-nodejs.git
5+
command: node server/app.js

0 commit comments

Comments
 (0)