Sha256: 7e2de13190615b0f72134e0f3c112606a64637adbd00c4cea638c9d6366f4947

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Commands
    class Webpack < Thor::Group
      include Thor::Actions
      extend Summarizable

      Registrations.register do
        register(Webpack, "webpack", "webpack ACTION", Webpack.summary)
      end

      def self.banner
        "bridgetown webpack ACTION"
      end
      summary "Perform actions on the bridgetown webpack configuration"

      def self.exit_on_failure?
        true
      end

      def webpack
        @logger = Bridgetown.logger
        return show_actions if args.empty?

        action = args.first
        if supported_actions.include?(action)
          perform action
        else
          @logger.error "Error:".red, "🚨 Please enter a valid action."
          say "\n"
          show_actions
        end
      end

      def self.source_root
        File.expand_path("./webpack", __dir__)
      end

      def self.destination_root
        config.root_dir
      end

      def config
        @config ||= Bridgetown.configuration({ root_dir: Dir.pwd })
      end

      protected

      def perform(action)
        automation = find_in_source_paths("#{action}.rb")
        inside(New.created_site_dir || Dir.pwd) do
          apply automation, verbose: false
        end
      end

      def show_actions
        say "Available actions:\n".bold

        longest_action = supported_actions.keys.max_by(&:size).size
        supported_actions.each do |action, description|
          say action.ljust(longest_action).to_s.bold.blue + "\t" + "# #{description}"
        end
      end

      def supported_actions
        {
          setup: "Sets up a webpack integration with Bridgetown in your project",
          update: "Updates the Bridgetown webpack defaults to the latest available version",
          "enable-postcss": "Configures PostCSS in your project",
        }.with_indifferent_access
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bridgetown-core-0.21.0 lib/bridgetown-core/commands/webpack.rb
bridgetown-core-0.21.0.beta4 lib/bridgetown-core/commands/webpack.rb