# 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 { "NODE_ENV" => node_env, "SK_ASSET_DIR" => asset_dir, }.filter { |_, v| v } end def node_env ENV["NODE_ENV"] || Rails.env.to_s end def asset_dir directory = SewingKit.configuration.build_options&.dig(:asset_directory) return nil if directory == false return ShopifyCloud::AssetUploader.asset_directory if directory.nil? && defined?(ShopifyCloud) directory end def options build_options = SewingKit.configuration.build_options&.except(:asset_directory) 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