1
1
module ReactOnRails
2
2
class AssetsPrecompile
3
+ class SymlinkTargetDoesNotExistException < StandardError ; end
4
+
3
5
# Used by the rake task
4
6
def default_asset_path
5
7
dir = File . join ( Rails . configuration . paths [ "public" ] . first ,
@@ -20,30 +22,35 @@ def initialize(assets_path: nil,
20
22
def symlink_file ( target , symlink )
21
23
target_path = @assets_path . join ( target )
22
24
symlink_path = @assets_path . join ( symlink )
25
+
23
26
target_exists = File . exist? ( target_path )
27
+ raise SymlinkTargetDoesNotExistException , "Target Path was: #{ target_path } " unless target_exists
24
28
25
29
# File.exist?(symlink_path) will check the file the sym is pointing to is existing
26
30
# File.lstat(symlink_path).symlink? confirms that this is a symlink
27
- symlink_already_there_and_valid = File . exist? ( symlink_path ) &&
28
- File . lstat ( symlink_path ) . symlink?
29
- if symlink_already_there_and_valid
30
- puts "React On Rails: Digested #{ symlink } already exists indicating #{ target } did not change."
31
- elsif target_exists
32
- if File . exist? ( symlink_path ) && File . lstat ( symlink_path ) . symlink?
33
- puts "React On Rails: Removing invalid symlink #{ symlink_path } "
34
- `cd #{ @assets_path } && rm #{ symlink } `
35
- end
36
- # Might be like:
37
- # "images/5cf5db49df178f9357603f945752a1ef.png":
38
- # "images/5cf5db49df178f9357603f945752a1ef-033650e1d6193b70d59bb60e773f47b6d9aefdd56abc7cc.png"
39
- # need to cd to directory and then symlink
40
- target_sub_path , _divider , target_filename = target . rpartition ( "/" )
41
- _symlink_sub_path , _divider , symlink_filename = symlink . rpartition ( "/" )
42
- puts "React On Rails: Symlinking \" #{ target } \" to \" #{ symlink } \" "
43
- dest_path = File . join ( @assets_path , target_sub_path )
44
- FileUtils . chdir ( dest_path ) do
45
- File . symlink ( target_filename , symlink_filename )
46
- end
31
+ valid_symlink_already_exists = File . exist? ( symlink_path ) && File . lstat ( symlink_path ) . symlink?
32
+
33
+ if valid_symlink_already_exists
34
+ puts "React On Rails: Digested version of #{ symlink } already exists indicating #{ target } did not change."
35
+ return
36
+ end
37
+
38
+ if file_or_symlink_exists_at_path ( symlink_path )
39
+ puts "React On Rails: Removing existing invalid symlink or file #{ symlink_path } "
40
+ `rm -f "#{ symlink_path } "`
41
+ end
42
+
43
+ # Might be like:
44
+ # "images/5cf5db49df178f9357603f945752a1ef.png":
45
+ # "images/5cf5db49df178f9357603f945752a1ef-033650e1d6193b70d59bb60e773f47b6d9aefdd56abc7cc.png"
46
+ # need to cd to directory and then symlink
47
+ target_sub_path , _divider , target_filename = target . rpartition ( "/" )
48
+ _symlink_sub_path , _divider , symlink_filename = symlink . rpartition ( "/" )
49
+ dest_path = File . join ( @assets_path , target_sub_path )
50
+
51
+ puts "React On Rails: Symlinking \" #{ target } \" to \" #{ symlink } \" "
52
+ FileUtils . chdir ( dest_path ) do
53
+ File . symlink ( target_filename , symlink_filename )
47
54
end
48
55
end
49
56
@@ -74,8 +81,10 @@ def symlink_non_digested_assets
74
81
# already been symlinked by Webpack
75
82
symlink_file ( rails_digested_filename , original_filename )
76
83
77
- # We want the gz ones as well
78
- symlink_file ( "#{ rails_digested_filename } .gz" , "#{ original_filename } .gz" )
84
+ # We want the gz ones as well if they exist
85
+ if File . exist? ( @assets_path . join ( "#{ rails_digested_filename } .gz" ) )
86
+ symlink_file ( "#{ rails_digested_filename } .gz" , "#{ original_filename } .gz" )
87
+ end
79
88
end
80
89
end
81
90
end
@@ -108,5 +117,14 @@ def clobber
108
117
puts "Could not find generated_assets_dir #{ dir } defined in react_on_rails initializer: "
109
118
end
110
119
end
120
+
121
+ private
122
+
123
+ def file_or_symlink_exists_at_path ( path )
124
+ File . lstat ( path )
125
+ true
126
+ rescue
127
+ false
128
+ end
111
129
end
112
130
end
0 commit comments