class JsSourcemap::Api

Constants

EXTENSIONS

Public Instance Methods

clean_unused_files() click to toggle source
# File lib/js-sourcemap/api.rb, line 148
def clean_unused_files
        files = Dir[File.join(env.mapping_dir,"**","*.map")]
        files.each_with_index do |file,i|
                if env.manifest[get_relative_path(file)].nil?
                        remove_files(file)
                end
        end
end
complete_build() click to toggle source
# File lib/js-sourcemap/api.rb, line 168
def complete_build
        generate_mapping
        clean_unused_files
        sync_to_s3
end
compress?(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 80
def compress?(file)
  if EXTENSIONS.include?(File.extname(file))
    !File.exists?(gzname(file)) || File.mtime(gzname(file)) < File.mtime(file)
  end
end
config() click to toggle source
# File lib/js-sourcemap/api.rb, line 13
def config
        @config ||= Config.new(env)
end
config_hash() click to toggle source
# File lib/js-sourcemap/api.rb, line 17
def config_hash
        config.to_h
end
copy_source(smo) click to toggle source
# File lib/js-sourcemap/api.rb, line 90
def copy_source(smo)
        puts "=> Copying #{smo["minified_file_path"]} to #{smo["original_file_path"]}"
        FileUtils.cp(smo["minified_file_path"], smo["original_file_path"])
end
correct_file?(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 57
def correct_file?(file)
        return (File.extname(file) == ".js")
end
create_js_gz() click to toggle source
# File lib/js-sourcemap/api.rb, line 74
def create_js_gz
        puts "=> gziping js files"
        %x(for f in `find "#{env.sources_dir}" -follow -name '*.js'`; do gzip -c -9 "$f" > "$f".gz; done)
        # %x(gzip -c -9 "#{gz_file}" > "#{gzname(gz_file)}") if compress?(gz_file)
end
create_min_map(smo, min_content,sourcem_content) click to toggle source
# File lib/js-sourcemap/api.rb, line 61
def create_min_map(smo, min_content,sourcem_content)
        puts "=> writing minified file : #{smo["minified_file_path"]} ..."
        f = File.open(smo["minified_file_path"], "w")
        f.write(min_content)
        f.close

        puts "=> writing js-sourcemap file : #{smo["source_map_path"]} ..."
        f = File.open(smo["source_map_path"], "w")
        f.write(sourcem_content)
        f.close

end
env() click to toggle source
# File lib/js-sourcemap/api.rb, line 9
def env
        @env ||= ::JsSourcemap::Env.new
end
generate_mapping() click to toggle source
# File lib/js-sourcemap/api.rb, line 30
def generate_mapping
        # empty_dirs
        beginning_time = Time.now
        if !File.exists?(env.sources_dir)
                puts ">>> Directory #{env.sources_dir} doesn't exist"
                return
        end
        files = Dir[File.join(env.sources_dir,"**","*.js")]
        count = files.count
        files.each_with_index do |file, i|
                puts "#{i+1} of #{count} - #{file} ..........."
                if File.file?(file) and correct_file?(file)
                        smo = source_map_options file
                        if mapping_creation_required?(file,smo)
                                copy_source(smo)
                                #:source_url => "SourceUrl in minified", :source_map_url => "SourceMappingUrl in minified", :source_filename => "original_file_name_in_map", :source_root=> "lol4", :minified_file_path => "lol5", :input_source_map => "lol6"
                                uglified, source_map = Uglifier.new(:source_map_url => smo["source_map_file_absolute_path"], :source_filename => smo["original_file_absolute_path"], :output => {:comments => :copyright}).compile_with_map(File.read(file))
                                create_min_map(smo,uglified,source_map)
                        end
                end
        end
        create_js_gz
        end_time = Time.now
        puts ".... Completed ......"
        puts "Time elapsed #{((end_time - beginning_time)/60.0).round(3)} minutes"
end
get_mapping_dir(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 111
def get_mapping_dir(file)
        dirpath = File.dirname(file)
        dirpath.gsub(/.*#{env.sources_dir}/,"#{env.mapping_dir}")  # new mapping dir
end
get_relative_path(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 164
def get_relative_path(file)
        file.gsub(/.*(#{env.mapping_dir}\/)/,'').gsub(/\.map/,'')
end
gzname(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 86
def gzname(file)
  "#{file}.gz"
end
mapping_creation_required?(file,smo) click to toggle source
# File lib/js-sourcemap/api.rb, line 126
def mapping_creation_required?(file,smo)
        return !(File.exists?(smo["original_file_path"]) and File.exists?(smo["source_map_path"]))
end
mapping_file_path(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 121
def mapping_file_path(file)
        dirpath = get_mapping_dir file
        File.join(dirpath, File.basename(file)) + ".map"
end
original_file_path(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 116
def original_file_path(file)
        dirpath = get_mapping_dir file
        File.join(dirpath, File.basename(file,'.js')) + "-original.js"
end
reload!() click to toggle source
# File lib/js-sourcemap/api.rb, line 21
def reload!
        @env = nil
        @config = nil
end
remove_files(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 157
def remove_files(file)
        original_file = file.gsub(/\.js\.map/,'-original.js')
        puts "removing #{file} && #{original_file}"
        File.unlink(file) if File.exists?(file)
        File.unlink(original_file) if File.exists?(original_file)
end
source_map_options(file) click to toggle source
# File lib/js-sourcemap/api.rb, line 95
def source_map_options(file)
        a = Hash.new

        mapping_dirpath = get_mapping_dir file

        FileUtils.mkpath(mapping_dirpath) unless File.exists?(mapping_dirpath) # creating new mapping dir

        a["minified_file_path"] = file # something-digest.js
        a["original_file_path"] = original_file_path file  # something-digest-original.js
        a["source_map_path"] = mapping_file_path file # something-digest.js.map
        a["source_map_file_absolute_path"] = File.join(env.build_absolute_path mapping_file_path file)  # map absolute url
        a["original_file_absolute_path"] = File.join(env.build_absolute_path original_file_path file) # original file absolute url

        a
end
sync_to_s3() click to toggle source
# File lib/js-sourcemap/api.rb, line 130
def sync_to_s3
        if sync_to_s3?
          if asset_prefix = Rails.application.config.assets.prefix
            puts "starting sync to s3 bucket"
            puts "s3cmd sync -r --skip-existing #{env.mapping_dir}/ s3://#{env.sourcemap_config.fetch("privateassets_bucket_name")}#{asset_prefix}/ --acl-private --no-check-md5"
            if system("s3cmd sync -r --skip-existing #{env.mapping_dir}/ s3://#{env.sourcemap_config.fetch("privateassets_bucket_name")}#{asset_prefix}/ --acl-private --no-check-md5")
              puts "successfully synced assets to s3"
            else
              puts "Failed to sync asets to s3"
            end
          else
            puts "assets host not specified in application configuration"
          end
        else
          puts "Syncing to s3 disabled as per sourcemap configuration"
        end
end
sync_to_s3?() click to toggle source
# File lib/js-sourcemap/api.rb, line 26
def sync_to_s3?
        env.sourcemap_config.fetch("sync_to_s3") == "yes" || false
end