Last Updated: February 25, 2016
·
596
· apaganobeleno

Static Sites for Heroku unix function

One of the common tasks we do on frontend is to create a small static site at heroku to show our progress to our clients, by default we follow the instructions from

https://devcenter.heroku.com/articles/static-sites-ruby

But it becomes boring to look fort that link and repeat the same steps every time, so i decided to write a small function on my .zshrc file and i ended with this :

init_static_site(){
  PROJECT_NAME=$1

  if [[ -z "$PROJECT_NAME" ]] then
    PROJECT_NAME="site"
    echo "| You didn't define a name for the site folder, setting it to 'site' by default"
  fi

  mkdir -p $PROJECT_NAME/public/{images,js,css}
  touch $PROJECT_NAME/{config.ru,public/index.html}

  cd $PROJECT_NAME && touch Gemfile && echo "source \"https://rubygems.org\"\ngem 'rack'" >> Gemfile

  echo "use Rack::Static, :urls => [\"/images\", \"/js\", \"/css\"],:root => \"public\"\nrun lambda { |env|\n[200, {'Content-Type'  => 'text/html', 'Cache-Control' => 'public, max-age=86400' },File.open('public/index.html', File::RDONLY)]}" >> config.ru

  bundle install
  cd ..
}

it creates the basic files and directories for the application, receives a name for the folder and runs bundle for the gem dependencies.

Hope you like it and waiting for your comments to improve it.