js-sourcemap 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/js-sourcemap/api.rb +64 -7
- data/lib/js-sourcemap/env.rb +47 -5
- data/lib/js-sourcemap/version.rb +1 -1
- data/lib/tasks/js-sourcemap.rake +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a43dc6a5ef34e165818f5960dd876fbb90c6c1c5
|
4
|
+
data.tar.gz: 0fde72ff8d54252c52557706e86c65e81e11ea64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99fda8658ab5a90d55bb7ade2cac234023bb43431f33ca65b2e9eec47e9288efe868f0265f4f19a1dbf39d3b352f9ba6cd328515c984d2eb0a31ac95e87808b3
|
7
|
+
data.tar.gz: ddbcfbec285a7ad150245210a83ecf0534aed094ecfb7646ff23ed88715fab47cb7c91368783933b2e242fa8684d52987a43d82761f92c0f03fdf3e5e4b43d97
|
data/lib/js-sourcemap/api.rb
CHANGED
@@ -23,6 +23,10 @@ module JsSourcemap
|
|
23
23
|
@config = nil
|
24
24
|
end
|
25
25
|
|
26
|
+
def sync_to_s3?
|
27
|
+
env.sourcemap_config.fetch("sync_to_s3") == "yes" || false
|
28
|
+
end
|
29
|
+
|
26
30
|
def generate_mapping
|
27
31
|
# empty_dirs
|
28
32
|
beginning_time = Time.now
|
@@ -30,13 +34,12 @@ module JsSourcemap
|
|
30
34
|
puts ">>> Directory #{env.sources_dir} doesn't exist"
|
31
35
|
return
|
32
36
|
end
|
33
|
-
Find.find(env.sources_dir)
|
37
|
+
count = Find.find(env.sources_dir).count
|
38
|
+
Find.find(env.sources_dir).each_with_index do |file, i|
|
39
|
+
puts "#{i+1} of #{count} - #{file} ..........."
|
34
40
|
if File.file?(file) and correct_file?(file)
|
35
41
|
smo = source_map_options file
|
36
42
|
if mapping_creation_required?(file,smo)
|
37
|
-
# puts ">>>>>>> minified_file_path : #{smo["minified_file_path"]}, original_file_path : #{smo["original_file_path"]} , source_map_path : #{smo["source_map_path"]}, source_map_file_absolute_path : #{smo["source_map_file_absolute_path"]}, original_file_absolute_path : #{smo["original_file_absolute_path"]}"
|
38
|
-
puts ">>>>>>> .................... >>>>>>>>>"
|
39
|
-
|
40
43
|
copy_source(smo)
|
41
44
|
#: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"
|
42
45
|
uglified, source_map = Uglifier.new(:source_map_url => smo["source_map_file_absolute_path"], :source_filename => smo["original_file_absolute_path"]).compile_with_map(File.read(file))
|
@@ -45,7 +48,7 @@ module JsSourcemap
|
|
45
48
|
end
|
46
49
|
end
|
47
50
|
end_time = Time.now
|
48
|
-
puts "
|
51
|
+
puts ".... Completed ......"
|
49
52
|
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
|
50
53
|
end
|
51
54
|
|
@@ -87,6 +90,12 @@ module JsSourcemap
|
|
87
90
|
def source_map_options(file)
|
88
91
|
a = Hash.new
|
89
92
|
|
93
|
+
mapping_dirpath = get_mapping_dir file
|
94
|
+
original_dirpath = get_original_dir file
|
95
|
+
|
96
|
+
FileUtils.mkpath(mapping_dirpath) unless File.exists?(mapping_dirpath) # creating new mapping dir
|
97
|
+
FileUtils.mkpath(original_dirpath) unless File.exists?(original_dirpath) # creating new original dir
|
98
|
+
|
90
99
|
a["minified_file_path"] = file # something-digest.js
|
91
100
|
a["original_file_path"] = original_file_path file # something-digest-original.js
|
92
101
|
a["source_map_path"] = mapping_file_path file # something-digest.js.map
|
@@ -96,13 +105,23 @@ module JsSourcemap
|
|
96
105
|
a
|
97
106
|
end
|
98
107
|
|
99
|
-
def
|
108
|
+
def get_mapping_dir(file)
|
100
109
|
dirpath = File.dirname(file)
|
110
|
+
dirpath.gsub(/.*#{env.sources_dir}/,"#{env.mapping_dir}") # new mapping dir
|
111
|
+
end
|
112
|
+
|
113
|
+
def get_original_dir(file)
|
114
|
+
dirpath = File.dirname(file)
|
115
|
+
dirpath.gsub(/.*#{env.sources_dir}/,"#{env.original_dir}") # new mapping dir
|
116
|
+
end
|
117
|
+
|
118
|
+
def original_file_path(file)
|
119
|
+
dirpath = get_original_dir file
|
101
120
|
File.join(dirpath, File.basename(file,'.js')) + "-original.js"
|
102
121
|
end
|
103
122
|
|
104
123
|
def mapping_file_path(file)
|
105
|
-
dirpath =
|
124
|
+
dirpath = get_mapping_dir file
|
106
125
|
File.join(dirpath, File.basename(file)) + ".map"
|
107
126
|
end
|
108
127
|
|
@@ -110,5 +129,43 @@ module JsSourcemap
|
|
110
129
|
return !(File.exists?(smo["original_file_path"]) and File.exists?(smo["source_map_path"]))
|
111
130
|
end
|
112
131
|
|
132
|
+
def sync_to_s3
|
133
|
+
if sync_to_s3?
|
134
|
+
if asset_prefix = Rails.application.config.assets.prefix
|
135
|
+
puts "starting sync to s3 bucket"
|
136
|
+
puts "s3cmd sync -r --delete-removed --skip-existing private#{asset_prefix}/ s3://#{env.sourcemap_config.fetch("privateassets_bucket_name")}#{asset_prefix}/ --acl-private --no-check-md5"
|
137
|
+
if system("s3cmd sync -r --delete-removed --skip-existing private#{asset_prefix}/ s3://#{env.sourcemap_config.fetch("privateassets_bucket_name")}#{asset_prefix}/ --acl-private --no-check-md5")
|
138
|
+
puts "successfully synced assets to s3"
|
139
|
+
else
|
140
|
+
puts "Failed to sync asets to s3"
|
141
|
+
end
|
142
|
+
else
|
143
|
+
puts "assets host not specified in application configuration"
|
144
|
+
end
|
145
|
+
else
|
146
|
+
puts "Syncing to s3 disabled as per sourcemap configuration"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def clean_unused_files
|
151
|
+
files = Dir[File.join(env.mapping_dir,"**","*.map")]
|
152
|
+
files.each_with_index do |file,i|
|
153
|
+
if env.manifest[get_relative_path(file)].nil?
|
154
|
+
remove_files(file)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def remove_files(file)
|
160
|
+
original_file = file.gsub(/\.js\.map/,'-original.js')
|
161
|
+
puts "removing #{file} && #{original_file}"
|
162
|
+
File.unlink(file) if File.exists?(file)
|
163
|
+
File.unlink(original_file) if File.exists?(original_file)
|
164
|
+
end
|
165
|
+
|
166
|
+
def get_relative_path(file)
|
167
|
+
file.gsub(/.*(#{env.mapping_dir}\/|#{env.original_dir}\/)/,'').gsub(/\.map/,'')
|
168
|
+
end
|
169
|
+
|
113
170
|
end
|
114
171
|
end
|
data/lib/js-sourcemap/env.rb
CHANGED
@@ -3,12 +3,37 @@ require "json"
|
|
3
3
|
module JsSourcemap
|
4
4
|
class Env
|
5
5
|
|
6
|
-
attr_accessor :sources_dir, :domain
|
6
|
+
attr_accessor :sources_dir, :domain, :mapping_dir, :original_dir, :is_build_dir_private, :sourcemap_yml, :manifest
|
7
7
|
|
8
8
|
|
9
9
|
def initialize
|
10
|
-
self.sources_dir = rails_path "public
|
11
|
-
self.
|
10
|
+
self.sources_dir = rails_path "public#{Rails.application.config.assets.prefix}"
|
11
|
+
self.mapping_dir = rails_path "private#{Rails.application.config.assets.prefix}"
|
12
|
+
self.original_dir = rails_path "private#{Rails.application.config.assets.prefix}"
|
13
|
+
# keep both dir mapping_dir and original_dir either public or private
|
14
|
+
self.is_build_dir_private = true # false when build_dir is public/assets or app/assets or vendor/assets or lib/assets
|
15
|
+
self.sourcemap_yml = rails_path "config/sourcemap.yml"
|
16
|
+
self.domain = sourcemap_config.fetch "domain"
|
17
|
+
self.manifest = get_manifest_file
|
18
|
+
end
|
19
|
+
|
20
|
+
def sourcemap_config
|
21
|
+
@sourcemap_config ||= load_config
|
22
|
+
end
|
23
|
+
|
24
|
+
def config_path
|
25
|
+
@config_path ||= Pathname.new(self.sourcemap_yml)
|
26
|
+
end
|
27
|
+
|
28
|
+
def readable?
|
29
|
+
config_path.readable?
|
30
|
+
end
|
31
|
+
|
32
|
+
def load_config
|
33
|
+
return {} if not readable?
|
34
|
+
File.open config_path do |f|
|
35
|
+
YAML.load f.read
|
36
|
+
end
|
12
37
|
end
|
13
38
|
|
14
39
|
def relative_path(path)
|
@@ -20,9 +45,26 @@ module JsSourcemap
|
|
20
45
|
"#{Rails.root}/#{path}"
|
21
46
|
end
|
22
47
|
|
48
|
+
def get_manifest_file
|
49
|
+
return @manifest_hash if @manifest_hash
|
50
|
+
files = Dir[File.join(self.sources_dir,"manifest*")]
|
51
|
+
if files and files.length == 1
|
52
|
+
file = File.read(files.first)
|
53
|
+
@manifest_hash = JSON.parse(file)["files"]
|
54
|
+
else
|
55
|
+
puts "Either none or more than one minifest file exists"
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
23
60
|
def build_absolute_path(path)
|
24
|
-
|
25
|
-
|
61
|
+
if self.is_build_dir_private
|
62
|
+
path = path.gsub(/.*(#{Rails.root}\/)/,'')
|
63
|
+
"#{self.domain}#{path}"
|
64
|
+
else
|
65
|
+
path = path.gsub(/.*(#{self.sources_dir}\/|#{Rails.root}\/|#{self.mapping_dir}\/|#{self.original_dir}\/)/,'')
|
66
|
+
"#{self.domain}assets/#{path}"
|
67
|
+
end
|
26
68
|
end
|
27
69
|
|
28
70
|
end
|
data/lib/js-sourcemap/version.rb
CHANGED
data/lib/tasks/js-sourcemap.rake
CHANGED
@@ -5,4 +5,14 @@ namespace :smap do
|
|
5
5
|
task "generate_mapping" => "assets:environment" do
|
6
6
|
sm.generate_mapping
|
7
7
|
end
|
8
|
+
|
9
|
+
desc "sync JS original code and mappings to s3"
|
10
|
+
task :sync_to_s3 => :environment do
|
11
|
+
sm.sync_to_s3
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "clean Unused/Old JS files - Original & Map"
|
15
|
+
task :clean => :environment do
|
16
|
+
sm.clean_unused_files
|
17
|
+
end
|
8
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-sourcemap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jayprakash
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uglifier
|