Class: Brut::CLI::Apps::BuildAssets::JS

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/build_assets.rb

Instance Method Summary collapse

Methods inherited from Command

#after_bootstrap, args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #handle_bootstrap_exception, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#executeObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/brut/cli/apps/build_assets.rb', line 85

def execute
  js_bundle           = Brut.container.js_bundle_output_dir / options.output_file(default: "app.js")
  js_bundle_source    = Brut.container.front_end_src_dir /  "js" / options.source_file(default: "index.js")
  esbuild_metafile    = Brut.container.tmp_dir / "build-js-meta.json"
   = Brut.container.

  name_with_hash_regexp = /app\/public\/(?<path>.+)\/(?<name>.+)\-(?<hash>.+)\.js/
  if global_options.clean?(default: global_options.env != "production")
    out.puts "Cleaning old JS files from #{Brut.container.js_bundle_output_dir}"
    Dir[Brut.container.js_bundle_output_dir / "*.*"].each do |file|
      if File.file?(file)
        out.puts "Deleting #{file}"
        FileUtils.rm(file)
      end
    end
  end

  command = "npx esbuild --metafile=#{esbuild_metafile} --entry-names=[name]-[hash] --sourcemap --bundle #{js_bundle_source} --outfile=#{js_bundle}"
  env_for_command = {
    "NODE_PATH" => (Brut.container.project_root / "lib").to_s, # Not needed once Brut is properly bundled
  }
  out.puts "Building JS bundle '#{js_bundle}' with '#{command}'"
  system!(env_for_command,command)

  if !File.exist?(esbuild_metafile)
    err.puts "'#{esbuild_metafile}' was not generated - cannot continue"
    exit 1
  end

   = Brut::FrontEnd::AssetMetadata.new(asset_metadata_file:,out:)
  .merge!(extension: ".js", esbuild_metafile:)
  .save!
end