From cda86c5af5a3a0d5244df9c80877b03a289b274d Mon Sep 17 00:00:00 2001 From: joaoGabriel55 Date: Sat, 17 Aug 2024 16:57:17 -0300 Subject: [PATCH] lint: update files generate by cli --- lib/sinatra_scaffold/cli.rb | 143 +++++++++++++++++++++++------------- 1 file changed, 92 insertions(+), 51 deletions(-) diff --git a/lib/sinatra_scaffold/cli.rb b/lib/sinatra_scaffold/cli.rb index c908345..bb1ada2 100644 --- a/lib/sinatra_scaffold/cli.rb +++ b/lib/sinatra_scaffold/cli.rb @@ -27,33 +27,33 @@ def create_directory_structure(app_name) def create_main_files(app_name) File.write("#{app_name}/config.ru", <<~RUBY) - require './app' - + require "./app" + run Sinatra::Application RUBY File.write("#{app_name}/app/routes/users_routes.rb", <<~RUBY) - require 'sinatra/base' - - get '/users' do - ['User 1', 'User 2'].to_json + require "sinatra/base" + + get "/users" do + ["User 1", "User 2"].to_json end RUBY File.write("#{app_name}/server.rb", <<~RUBY) - require 'sinatra' - require 'sinatra/base' - require 'dotenv' - + require "sinatra" + require "sinatra/base" + require "dotenv" + Dotenv.load - - set :bind, '0.0.0.0' + + set :bind, "0.0.0.0" set :default_content_type, :json - - require './app/routes/users_routes' - - get '/health-check' do - { status: 'ok' }.to_json + + require "./app/routes/users_routes" + + get "/health-check" do + {status: "ok"}.to_json end RUBY @@ -63,22 +63,23 @@ def create_main_files(app_name) File.write("#{app_name}/spec/spec_helper.rb", <<~RUBY) # frozen_string_literal: true - ENV['RACK_ENV'] = 'test' - - require 'rubygems' - require 'bundler' - - require_relative '../server' - + + ENV["RACK_ENV"] = "test" + + require "rubygems" + require "bundler" + + require_relative "../server" + Bundler.require(:default) # load all the default gems Bundler.require(Sinatra::Base.environment) # load all the environment specific gems - + Dotenv.load - + def app Sinatra::Application end - + RSpec.configure do |config| config.include Rack::Test::Methods config.expect_with :rspec do |expectations| @@ -91,7 +92,7 @@ def app config.run_all_when_everything_filtered = true config.disable_monkey_patching! config.warnings = true - config.default_formatter = 'doc' if config.files_to_run.one? + config.default_formatter = "doc" if config.files_to_run.one? config.profile_examples = 10 config.order = :random Kernel.srand config.seed @@ -99,46 +100,86 @@ def app RUBY File.write("#{app_name}/spec/requests/users_spec.rb", <<~RUBY) - require 'spec_helper' - - RSpec.describe 'GET /users' do - it 'returns a list of users' do - get '/users' - + require "spec_helper" + + RSpec.describe "GET /users" do + it "returns a list of users" do + get "/users" + expect(last_response).to be_ok - expect(last_response.body).to eq(['User 1', 'User 2'].to_json) + expect(last_response.body).to eq(["User 1", "User 2"].to_json) end end RUBY File.write("#{app_name}/spec/requests/health_check_spec.rb", <<~RUBY) - require 'spec_helper' - - RSpec.describe 'GET /health-check' do - it 'returns ok' do - get '/health-check' - + require "spec_helper" + + RSpec.describe "GET /health-check" do + it "returns ok" do + get "/health-check" + expect(last_response).to be_ok - expect(last_response.body).to eq({ status: 'ok' }.to_json) + expect(last_response.body).to eq({status: "ok"}.to_json) end end RUBY File.write("#{app_name}/Gemfile", <<~RUBY) - source 'https://rubygems.org' - - gem 'sinatra' - gem 'dotenv' - + source "https://rubygems.org" + + gem "sinatra" + gem "dotenv" + gem "rake" + group :test do - gem 'rack-test' - gem 'rspec' + gem "rack-test" + gem "rspec" end - + group :development, :test do - gem 'pry' + gem "pry" + gem "standard" end RUBY + + File.write("#{app_name}/Rakefile", <<~RUBY) + require "rspec/core/rake_task" + + RSpec::Core::RakeTask.new(:spec) + + require "standard/rake" + + task default: %i[spec standard] + RUBY + + File.write("#{app_name}/README.md", <<~RUBY) + # #{app_name} + + My Sinatra application + + ## Running + Run: + ```sh + ruby server.rb + ``` + + ## Testing + Run: + ```sh + bundle exec rspec + bundle exec rspec [file-or-directory-path] + ``` + + ## Lint + ```sh + bundle exec rake standard + ``` + Lint and fix with: + ```sh + bundle exec rake standard:fix + ``` + RUBY end end end