Module: WebpackerCli

Defined in:
lib/webpacker_cli.rb,
lib/webpacker_cli/version.rb,
lib/webpacker_cli/rails_template.rb

Defined Under Namespace

Modules: RailsTemplate

Constant Summary collapse

FILE_MANIFEST =
%w[
  bin
  bin/rails
  config
  config/application.rb
  config/webpack/development.js
  config/webpack/environment.js
  config/webpack/loaders
  config/webpack/production.js
  config/webpack/test.js
  package.json
  Gemfile
  Rakefile
]
VERSION =
"0.9.3"

Class Method Summary collapse

Class Method Details

.bundler_env(test) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/webpacker_cli.rb', line 65

def bundler_env(test)
  if test
    Bundler.with_clean_env { yield }
  else
    yield
  end
end

.executableObject



73
74
75
76
77
78
79
# File 'lib/webpacker_cli.rb', line 73

def executable
  begin
    Gem.bin_path("webpacker_cli", "webpacker-cli")
  rescue
    File.expand_path('../bin/webpacker-cli', __dir__)
  end
end

.init(**opts) ⇒ Object



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
56
57
58
59
60
61
62
63
# File 'lib/webpacker_cli.rb', line 24

def init(**opts)
  dir = opts.fetch(:dir) { Dir.pwd }
  test = opts.fetch(:test) { !!ENV['WEBPACKER_CLI'].to_s['TEST'] }

  Dir.chdir(dir) do
    File.open('Gemfile', 'w') {|file|
      file.write(WebpackerCli::RailsTemplate.gemfile(test))
    }
    File.open('Rakefile', 'w') {|file| file.write(WebpackerCli::RailsTemplate.rakefile) }

    FileUtils.mkdir_p('bin')
    File.open('bin/rails', 'w') {|file| file.write(WebpackerCli::RailsTemplate.bin_rails) }

    File.open('package.json', 'w') {|file| file.write(WebpackerCli::RailsTemplate.package_json) }

    FileUtils.mkdir_p('config/webpack/loaders')
    File.open('config/application.rb', 'w') {|file| file.write(WebpackerCli::RailsTemplate.config_application) }

    env = {
      BUNDLE_GEMFILE: "#{dir}/Gemfile"
    }

    if test
      env.update( {
        GEM_HOME: ENV["GEM_HOME"],
        GEM_PATH: ENV["GEM_PATH"],
      })
    end

    bundler_env(test) do
      TTY::Command.
        new(color: false, uuid: false).
        run(env, 'bundle install', chdir: dir)

      TTY::Command.
        new(color: false, uuid: false).
        run(env, "bundle exec #{executable} install", chdir: dir)
    end
  end
end