Module: BridgetownPluginNano::Commands::Nano::GeneralHelpers

Included in:
BridgetownPluginNano::Commands::Nano
Defined in:
lib/bridgetown-plugin-nano/command_helpers/general_helpers.rb

Instance Method Summary collapse

Instance Method Details

#configure_new_nano_appObject

rubocop:disable Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bridgetown-plugin-nano/command_helpers/general_helpers.rb', line 7

def configure_new_nano_app # rubocop:disable Metrics/MethodLength
  template "bridgetown_root/config.ru", "config.ru"
  copy_file "bridgetown_root/Rakefile", "Rakefile"

  gsub_file "start.js",
            'sleep 4; yarn serve --port " + port',
            'sleep 4; bundle exec rake nano:start -- --port=" + port'
  gsub_file "webpack.config.js",
            %(const path = require("path");\n),
            %(const path = require("path");\nconst webpack = require("webpack");\n)

  inject_into_file "webpack.config.js", <<-JS, after: "\n  plugins: [\n"
    new webpack.DefinePlugin({
      NANO_API_URL: JSON.stringify(process.env.NANO_API_URL || "/backend")
    }),
  JS

  append_to_file "frontend/javascript/index.js" do
    <<~JS

      async function testNanoAPI() {
        const resp = await fetch(`${NANO_API_URL}/nano`)
        const data = await resp.json()
        document.querySelector("main").innerHTML += `<p><code>${JSON.stringify(data)}</code></p>`
      }
      
      testNanoAPI()            
    JS
  end

  append_to_file "Gemfile" do
    <<~RUBY

      # Gems required by the Nano backend:
      gem "dotenv"
      gem "puma"
      gem "rails"
      gem "rack-cors"
    RUBY
  end
  Bridgetown.with_unbundled_env do
    run "bundle install", abort_on_failure: true
  end

  logger = Bridgetown.logger
  logger.info ""
  logger.info "Success!".green, "🎉 Your Nano backend was" \
              " generated in #{folder_name.cyan}."
end