Class: Jets::Commands::New

Inherits:
Sequence
  • Object
show all
Defined in:
lib/jets/commands/new.rb

Constant Summary collapse

VALID_MODES =
%w[html api job]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sequence

source_root

Class Method Details

.cli_optionsObject

Ugly, but when the class_option is only defined in the Thor::Group class it doesnt show up with jets new help :( If anyone knows how to fix this let me know.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jets/commands/new.rb', line 9

def self.cli_options
  [
    [:bootstrap, type: :boolean, default: true, desc: "Install bootstrap css"], # same option in WebpackerTemplate
    [:database, type: :string, default: 'mysql', desc: "Preconfigure database (options: mysql/postgresql)"],
    [:force, type: :boolean, desc: "Bypass overwrite are you sure prompt for existing files."],
    [:git, type: :boolean, default: true, desc: "Git initialize the project"],
    [:mode, default: 'html', desc: "mode: #{VALID_MODES.join(',')}"],
    [:repo, desc: "GitHub repo to use. Format: user/repo"],
    [:webpacker, type: :boolean, default: true, desc: "Install webpacker"],
  ]
end

Instance Method Details

#bootstrap_installObject

bootstrap is dependent on webpacker, options is used in webpacker_install.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jets/commands/new.rb', line 71

def bootstrap_install
  return unless @bootstrap

  # Add jquery and popper plugin to handle Delete of CRUD
  jquery =<<-JS
const webpack = require('webpack')
environment.plugins.prepend('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  Popper: ['popper.js', 'default']
}))
JS
  after = "const { environment } = require('@rails/webpacker')\n"
  insert_into_file("config/webpack/environment.js", jquery, after: after)

  run("yarn add [email protected] jquery popper.js")
end

#bundle_installObject



55
56
57
58
59
# File 'lib/jets/commands/new.rb', line 55

def bundle_install
  Bundler.with_clean_env do
    system("BUNDLE_IGNORE_CONFIG=1 bundle install")
  end
end

#create_projectObject



43
44
45
46
47
48
49
# File 'lib/jets/commands/new.rb', line 43

def create_project
  options[:repo] ? clone_project : copy_project

  destination_root = "#{Dir.pwd}/#{project_folder}"
  self.destination_root = destination_root
  FileUtils.cd("#{Dir.pwd}/#{project_folder}")
end

#git_initObject



89
90
91
92
93
94
95
96
97
# File 'lib/jets/commands/new.rb', line 89

def git_init
  return if !options[:git]
  return unless git_installed?
  return if File.exist?(".git") # this is a clone repo

  run("git init")
  run("git add .")
  run("git commit -m 'first commit'")
end

#make_bin_executableObject



51
52
53
# File 'lib/jets/commands/new.rb', line 51

def make_bin_executable
  chmod "bin", 0755 & ~File.umask, verbose: false
end

#set_initial_variablesObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jets/commands/new.rb', line 25

def set_initial_variables
  @project_name = project_folder == '.' ? File.basename(Dir.pwd) : project_folder

  # options is a frozen hash by Thor so cannot modify it.
  # Also had trouble unfreezing it with .dup. So using instance variables instead
  case options[:mode]
  when 'html'
    @webpacker = options[:webpacker]
    @bootstrap = options[:bootstrap]
  when 'api', 'job'
    @webpacker = false
    @bootstrap = false
  else
    puts "Invalid mode provided: #{@options[:mode].colorize(:red)}. Please pass in an valid mode: #{VALID_MODES.join(',').colorize(:green)}."
    exit 1
  end
end

#user_messageObject



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
# File 'lib/jets/commands/new.rb', line 99

def user_message
  more_info = if options[:mode] == 'job'
    <<~EOL
      Learn more about jobs here: http://rubyonjets.com/docs/jobs/

      To deploy to AWS Lambda:
        jets deploy
    EOL
  else
    <<~EOL
      To start a server and test locally:
        jets server # localhost:8888 should have the Jets welcome page

      Scaffold example:
        jets generate scaffold Post title:string body:text published:boolean

      To deploy to AWS Lambda, edit your .env.development.remote and add a DATABASE_URL endpoint.
      Then run:

        jets deploy
    EOL
  end

  puts <<~EOL
    #{"="*64}
    Congrats 🎉 You have successfully created a Jets project.

    Cd into the project directory:
      cd #{project_folder}

    #{more_info}
  EOL
end

#webpacker_installObject



61
62
63
64
65
66
67
# File 'lib/jets/commands/new.rb', line 61

def webpacker_install
  return unless @webpacker

  command = "jets webpacker:install"
  command += " FORCE=1" if options[:force]
  run(command)
end