Sha256: c2c7808c5f2c31bb3fe3bf9323dfa81fae9ca88981946fd5518e3734bb8d5b54

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true
require 'open3'

module Opal
  module Util
    extend self

    # Used for uglifying source to minify.
    #
    #     Opal::Util.uglify("javascript contents")
    #
    # @param str [String] string to minify
    # @return [String]
    def uglify(str)
      uglifyjs = DigestSourceCommand.new(:uglifyjs, '-c', ' (install with: "npm install -g uglify-js")')
      uglifyjs.digest(str)
    end

    # Gzip code to check file size.
    def gzip(str)
      gzip = DigestSourceCommand.new(:gzip, '-f', ', it is required to produce the .gz version')
      gzip.digest(str)
    end

    class Command
      def initialize(command, options, message = nil)
        @command, @options, @message = command, options, message
        return unless command_installed? command, message
      end
      attr_reader :command, :options, :message

      private

      def hide_stderr
        if (/mswin|mingw/ =~ RUBY_PLATFORM).nil?
          '2> /dev/null'
        else
          '2> nul'
        end
      end

      # Code from http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
      def which(cmd)
        exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
        ENV['PATH'].split(File::PATH_SEPARATOR).find do |path|
          exts.find { |ext|
            exe = File.join(path, "#{cmd}#{ext}")
            exe if File.executable? exe
          }
        end
      end

      INSTALLED = {}
      def command_installed?(cmd, install_comment)
        command_installed = Command::INSTALLED[cmd.to_s] ||= which(cmd)
        $stderr.puts %Q("#{cmd}" command not found#{install_comment}) unless command_installed
        command_installed
      end
    end

    class DigestSourceCommand < Command
      def digest(source)
        out, _, _ = Open3.capture3("#{command} #{options} #{hide_stderr}", stdin_data: source)
        out
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
opal-0.11.4 lib/opal/util.rb
opal-0.11.3 lib/opal/util.rb
opal-0.11.2 lib/opal/util.rb
opal-0.11.1 lib/opal/util.rb
opal-0.11.1.pre lib/opal/util.rb
opal-0.11.0 lib/opal/util.rb