From 833129dba7ad5c923187f6254f497869d0dce43d Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Thu, 16 Jun 2016 10:54:20 -0400 Subject: [PATCH] refactor(gen:heroku): refactor to match the main generators --- src/generators/heroku/index.js | 392 +++++++++--------- .../templates => templates/heroku}/Procfile | 0 2 files changed, 197 insertions(+), 195 deletions(-) rename {src/generators/heroku/templates => templates/heroku}/Procfile (100%) diff --git a/src/generators/heroku/index.js b/src/generators/heroku/index.js index 18c5352b2..e728d2b66 100644 --- a/src/generators/heroku/index.js +++ b/src/generators/heroku/index.js @@ -1,204 +1,206 @@ '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 s = require('underscore.string'); - -var Generator = module.exports = function Generator() { - yeoman.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()); +import util from 'util'; +import yeoman from 'yeoman-generator'; +import {Base} from 'yeoman-generator'; +import {exec} from 'child_process'; +import chalk from 'chalk'; +import path from 'path'; +import s from 'underscore.string'; +import {genNamedBase} from '../generator-base'; + +export default class Generator extends Base { + constructor(...args) { + super(...args); + + this.sourceRoot(path.join(__dirname, '../../templates/heroku')); } - this.appname = s.slugify(this.appname); - this.filters = this.config.get('filters') || {}; -}; - -util.inherits(Generator, yeoman.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 = s.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.build = function build() { - if(this.abort) return; - var done = this.async(); - var buildCommand = this.filters.grunt ? 'grunt build' : 'gulp build'; - - this.log(chalk.bold('\nBuilding dist folder, please wait...')); - var child = exec(buildCommand, 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; + initializing() { + return genNamedBase(this); + } + + askForName() { + var done = this.async(); + + var prompts = [{ + name: 'deployedName', + message: 'Name to deploy as (Leave blank for a random name):' + }]; + + return this.prompt(prompts).then(props => { + this.deployedName = s.slugify(props.deployedName); + }); + } + + 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 + }]; + + return this.prompt(prompts).then(props => { + this.region = props.region.toLowerCase(); + }); + } + + checkInstallation() { + if(this.abort) return; + var done = this.async(); + + exec('heroku --version', err => { + if(err) { + this.log.error('You don\'t have the Heroku Toolbelt installed. Grab it from https://toolbelt.heroku.com/'); + this.abort = 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; + done(); + }); + } + + 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' }, (err, stdout, stderr) => { + done(); + }); + + child.stdout.on('data', data => { + this.log(data.toString()); + }); + } + + 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' }, (err, stdout, stderr) => { + if(err) { + this.abort = true; + this.log.error(err); + } else { + this.log('stdout: ' + stdout); } - 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; + done(); + }); + + child.stdout.on('data', data => { + this.log(data.toString()); + }); + } + + copyProcfile() { + if(this.abort) return; + var done = this.async(); + this.log(chalk.bold('Creating Procfile')); + this.copy('Procfile', 'dist/Procfile'); + this.conflicter.resolve(err => { + done(); + }); + } + + build() { + if(this.abort) return; + var done = this.async(); + var buildCommand = this.filters.grunt ? 'grunt build' : 'gulp build'; + + this.log(chalk.bold('\nBuilding dist folder, please wait...')); + + var child = exec(buildCommand, (err, stdout) => { + done(); + }); + + child.stdout.on('data', data => { + this.log(data.toString()); + }); + } + + 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)); - 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.')); + child.stdout.on('data', data => { + this.log(data.toString()); + }); + } + + 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' }, (err, stdout, stderr) => { + if(err) { + this.log.error(err); + } else { + var hasWarning = false; + + if(this.filters.mongoose) { + this.log(chalk.yellow(` +Because you're using mongoose, you must add mongoDB to your heroku app. +\tfrom \`/dist\`: ${chalk.bold('heroku addons:create mongolab')} +`)); + hasWarning = true; + } + + let oauthMessage = strategy => chalk.yellow(` +You will need to set environment variables for ${strategy} auth. From \`/dist\`: +\t${chalk.bold(`heroku config:set ${strategy.toUpperCase()}_ID=appId`)} +\t${chalk.bold(`heroku config:set ${strategy.toUpperCase()}_SECRET=secret`)} +`); + if(this.filters.facebookAuth) { + this.log(oauthMessage('facebook')); + hasWarning = true; + } + if(this.filters.googleAuth) { + this.log(oauthMessage('google')); + hasWarning = true; + } + if(this.filters.twitterAuth) { + this.log(oauthMessage('twitter')); + hasWarning = true; + } + + this.log(chalk.green(` +Your app should now be live. To view it run +\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 +\t${chalk.bold(this.filters.grunt ? 'grunt build' : 'gulp build')} +Then deploy with +\t${chalk.bold(this.filters.grunt ? 'grunt buildcontrol:heroku' : 'gulp buildcontrol:heroku')}`)); } + done(); + }); - this.log(chalk.yellow( - 'After app modification run\n\t' + - chalk.bold(this.filters.grunt ? 'grunt build' : 'gulp build') + - '\nThen deploy with\n\t' + - chalk.bold(this.filters.grunt ? 'grunt buildcontrol:heroku' : 'gulp buildcontrol:heroku') - )); - } - done(); - }.bind(this)); - - child.stdout.on('data', function(data) { - this.log(data.toString()); - }.bind(this)); -}; + child.stdout.on('data', data => { + this.log(data.toString()); + }); + } +} diff --git a/src/generators/heroku/templates/Procfile b/templates/heroku/Procfile similarity index 100% rename from src/generators/heroku/templates/Procfile rename to templates/heroku/Procfile