Class: JsSourcemap::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/js-sourcemap/api.rb

Constant Summary collapse

EXTENSIONS =
%w(.js)

Instance Method Summary collapse

Instance Method Details

#clean_unused_filesObject



148
149
150
151
152
153
154
155
# 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_buildObject



168
169
170
171
172
# File 'lib/js-sourcemap/api.rb', line 168

def complete_build
	generate_mapping
	clean_unused_files
	sync_to_s3
end

#compress?(file) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# 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

#configObject



13
14
15
# File 'lib/js-sourcemap/api.rb', line 13

def config
	@config ||= Config.new(env)
end

#config_hashObject



17
18
19
# File 'lib/js-sourcemap/api.rb', line 17

def config_hash
	config.to_h
end

#copy_source(smo) ⇒ Object



90
91
92
93
# 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) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/js-sourcemap/api.rb', line 57

def correct_file?(file)
	return (File.extname(file) == ".js")
end

#create_js_gzObject



74
75
76
77
78
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# 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

#envObject



9
10
11
# File 'lib/js-sourcemap/api.rb', line 9

def env
	@env ||= ::JsSourcemap::Env.new
end

#generate_mappingObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



111
112
113
114
# 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) ⇒ Object



164
165
166
# File 'lib/js-sourcemap/api.rb', line 164

def get_relative_path(file)
	file.gsub(/.*(#{env.mapping_dir}\/)/,'').gsub(/\.map/,'')
end

#gzname(file) ⇒ Object



86
87
88
# File 'lib/js-sourcemap/api.rb', line 86

def gzname(file)
  "#{file}.gz"
end

#mapping_creation_required?(file, smo) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# 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) ⇒ Object



121
122
123
124
# 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) ⇒ Object



116
117
118
119
# 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!Object



21
22
23
24
# File 'lib/js-sourcemap/api.rb', line 21

def reload!
	@env = nil
	@config = nil
end

#remove_files(file) ⇒ Object



157
158
159
160
161
162
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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_s3Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/js-sourcemap/api.rb', line 26

def sync_to_s3?
	env.sourcemap_config.fetch("sync_to_s3") == "yes" || false
end