Sha256: 5bdb92f4027e71c3b57c4930ac9114741a0fbe25a43131f87267c04c9f0b676f

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true
require 'sewing_kit/webpack/webpack'

module SewingKit
  module Webpack
    class Compiler
      class NodeNotInstalled < StandardError
        def initialize
          super(
            "nodejs is not installed.  " \
            "Try creating a `.buildpacks` file in the root of the project with following content:

            https://github.com/heroku/heroku-buildpack-nodejs.git
            https://github.com/heroku/heroku-buildpack-ruby.git
            "
          )
        end
      end

      def compile
        raise NodeNotInstalled unless node_installed?
        raise NodeSewingKitNotRunnable, node_env unless File.exist?('node_modules/.bin/sewing-kit')

        result = Kernel.system(
          environment,
          *command,
          chdir: Rails.root.to_s,
          out: $stdout,
          err: $stderr
        )

        unless result
          puts "sewing-kit compile failed with error code #{$CHILD_STATUS}"
          exit(1)
        end
        result
      end

      private

      def command
        [
          'node_modules/.bin/sewing-kit',
          'build',
          '--mode',
          node_env,
        ].concat(options).reject(&:empty?)
      end

      def environment
        environment = {
          'NODE_ENV' => node_env,
        }

        if defined?(ShopifyCloud)
          environment['SK_ASSET_DIR'] = ShopifyCloud::AssetUploader.asset_directory
        end

        environment
      end

      def node_env
        ENV['NODE_ENV'] || Rails.env.to_s
      end

      def options
        build_options = SewingKit.configuration.build_options
        return [] unless build_options

        build_options.map { |key, value| ["--#{key.to_s.tr('_', '-')}", value.to_s] }.flatten
      end

      def node_installed?
        Kernel.system('node --version')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sewing_kit-0.130.0 lib/sewing_kit/webpack/compiler.rb
sewing_kit-0.129.0 lib/sewing_kit/webpack/compiler.rb