@@ -31,8 +31,6 @@ type ModuleInstaller interface {
3131 InstallToLayer (string , string ) error
3232 RebuildLayer (string , string ) error
3333 CleanAndCopyToDst (string , string ) error
34- // WriteProfileD(string) error
35- // WriteENV(string) error
3634}
3735
3836type Modules struct {
@@ -79,39 +77,43 @@ func (m Modules) Contribute() error {
7977 }
8078
8179 if m .buildContribution {
82- m .cacheLayer .AppendPathEnv ("NODE_PATH" , filepath .Join (m .cacheLayer .Root , "node_modules" ))
8380 if ! m .launchContribution {
81+ m .logger .FirstLine ("%s: %s to cache" , logHeader (), color .YellowString ("Contributing" ))
8482 if err := m .installInCache (); err != nil {
85- return fmt .Errorf ("Failed to install in cache for build : %v" , err )
83+ return fmt .Errorf ("failed to install in cache for build : %v" , err )
8684 }
8785 }
88- }
89-
90- if m .launchContribution {
91- if err := m .writeProfile (); err != nil {
92- return fmt .Errorf ("Failed to write profile.d : %v" , err )
93- }
9486
95- sameSHASums , err := m . packageLockMatchesMetadataSha ( )
96- if err != nil {
87+ m . logger . SubsequentLine ( "Writing NODE_PATH" )
88+ if err := m . cacheLayer . AppendPathEnv ( "NODE_PATH" , filepath . Join ( m . cacheLayer . Root , "node_modules" )); err != nil {
9789 return err
9890 }
91+ }
9992
100- if sameSHASums {
93+ if m .launchContribution {
94+ if sameSHASums , err := m .packageLockMatchesMetadataSha (); err != nil {
95+ return err
96+ } else if sameSHASums {
97+ m .logger .FirstLine ("%s: %s cached launch layer" , logHeader (), color .GreenString ("Reusing" ))
10198 return nil
10299 }
103100
101+ m .logger .FirstLine ("%s: %s to launch" , logHeader (), color .YellowString ("Contributing" ))
102+
104103 if err := m .installInCache (); err != nil {
105- return fmt .Errorf ("Failed to install in cache for launch : %v" , err )
104+ return fmt .Errorf ("failed to install in cache for launch : %v" , err )
106105 }
107106
108107 if err := m .installInLaunch (); err != nil {
109- return fmt .Errorf ("Failed to install in launch : %v" , err )
108+ return fmt .Errorf ("failed to install in launch : %v" , err )
109+ }
110+
111+ if err := m .writeProfile (); err != nil {
112+ return fmt .Errorf ("failed to write profile.d : %v" , err )
110113 }
111114 }
112115
113116 appModulesDir := filepath .Join (m .app .Root , "node_modules" )
114- m .logger .SubsequentLine ("Removing node_modules from app" )
115117 if err := os .RemoveAll (appModulesDir ); err != nil {
116118 return fmt .Errorf ("failed to clean up the node_modules: %v" , err )
117119 }
@@ -127,36 +129,33 @@ func (m Modules) packageLockMatchesMetadataSha() (bool, error) {
127129 return false , fmt .Errorf ("there is no package-lock.json in the app" )
128130 }
129131
130- packageLockSha := sha256 .New ()
131- if buf , err := ioutil .ReadFile (packageLockPath ); err != nil {
132- return false , fmt .Errorf ("failed to read metadata: %v" , err )
133- } else {
134- packageLockSha .Write (buf )
132+ buf , err := ioutil .ReadFile (packageLockPath )
133+ if err != nil {
134+ return false , fmt .Errorf ("failed to read package-lock.json: %v" , err )
135135 }
136136
137137 var metadata Metadata
138- m .launchLayer .ReadMetadata (& metadata )
138+ if err := m .launchLayer .ReadMetadata (& metadata ); err != nil {
139+ return false , err
140+ }
141+
139142 metadataHash , err := hex .DecodeString (metadata .SHA256 )
140143 if err != nil {
141144 return false , err
142145 }
143146
144- return bytes .Equal (metadataHash , packageLockSha .Sum (nil )), nil
147+ hash := sha256 .Sum256 (buf )
148+ return bytes .Equal (metadataHash , hash [:]), nil
145149}
146150
147151func (m Modules ) writeMetadataSha (path string ) error {
148- sha := sha256 . New ( )
149- if buf , err := ioutil . ReadFile ( path ); err != nil {
152+ buf , err := ioutil . ReadFile ( path )
153+ if err != nil {
150154 return fmt .Errorf ("failed to read %s: %v" , path , err )
151- } else {
152- if _ , err := sha .Write (buf ); err != nil {
153- return err
154- }
155155 }
156156
157- return m .launchLayer .WriteMetadata (Metadata {
158- SHA256 : hex .EncodeToString (sha .Sum (nil )),
159- })
157+ hash := sha256 .Sum256 (buf )
158+ return m .launchLayer .WriteMetadata (Metadata {SHA256 : hex .EncodeToString (hash [:])})
160159}
161160
162161func (m * Modules ) copyModulesToLayer (src , dest string ) error {
@@ -167,34 +166,30 @@ func (m *Modules) copyModulesToLayer(src, dest string) error {
167166 return err
168167 }
169168 }
170-
171- if err := utils .CopyDirectory (src , dest ); err != nil {
172- return err
173- }
174-
175- return nil
169+ return utils .CopyDirectory (src , dest )
176170}
177171
178172func (m Modules ) installInCache () error {
179173 appModulesDir := filepath .Join (m .app .Root , "node_modules" )
174+
180175 vendored , err := libjavabuildpack .FileExists (appModulesDir )
181176 if err != nil {
182177 return fmt .Errorf ("could not locate app modules directory : %s" , err )
183178 }
184179
185180 if vendored {
186- m .logger .SubsequentLine ("%s cached node_modules" , color .YellowString ("Rebuilding" ))
181+ m .logger .SubsequentLine ("%s node_modules" , color .YellowString ("Rebuilding" ))
182+
187183 if err := m .npm .RebuildLayer (m .app .Root , m .cacheLayer .Root ); err != nil {
188184 return fmt .Errorf ("failed to rebuild node_modules: %v" , err )
189185 }
190186 } else {
191187 m .logger .SubsequentLine ("%s node_modules" , color .YellowString ("Installing" ))
192188
193189 cacheModulesDir := filepath .Join (m .cacheLayer .Root , "node_modules" )
194-
195- if exist , err := libjavabuildpack .FileExists (cacheModulesDir ); err != nil {
190+ if exists , err := libjavabuildpack .FileExists (cacheModulesDir ); err != nil {
196191 return err
197- } else if ! exist {
192+ } else if ! exists {
198193 if err := os .MkdirAll (cacheModulesDir , 0777 ); err != nil {
199194 return fmt .Errorf ("could not make node modules directory : %s" , err )
200195 }
@@ -203,33 +198,17 @@ func (m Modules) installInCache() error {
203198 if err := os .Symlink (cacheModulesDir , appModulesDir ); err != nil {
204199 return fmt .Errorf ("could not symlink node modules directory : %s" , err )
205200 }
206- defer func () error {
207- if err := os .Remove (appModulesDir ); err != nil {
208- return err
209- }
210- return nil
211- }()
201+ defer os .Remove (appModulesDir )
212202
213203 if err := m .npm .InstallToLayer (m .app .Root , m .cacheLayer .Root ); err != nil {
214204 return fmt .Errorf ("failed to install and copy node_modules: %v" , err )
215205 }
216-
217206 }
218207
219208 return nil
220209}
221210
222211func (m Modules ) installInLaunch () error {
223- sameSHASums , err := m .packageLockMatchesMetadataSha ()
224- if err != nil {
225- return err
226- }
227-
228- if sameSHASums {
229- m .logger .SubsequentLine ("app and launch layers match." )
230- return nil
231- }
232-
233212 cacheModulesDir := filepath .Join (m .cacheLayer .Root , "node_modules" )
234213 launchModulesDir := filepath .Join (m .launchLayer .Root , "node_modules" )
235214
@@ -245,12 +224,15 @@ func (m Modules) installInLaunch() error {
245224}
246225
247226func (m Modules ) writeProfile () error {
248- launchModulesDir := filepath .Join (m .launchLayer .Root , "node_modules" )
249-
250227 m .logger .SubsequentLine ("Writing profile.d/NODE_PATH" )
228+
229+ launchModulesDir := filepath .Join (m .launchLayer .Root , "node_modules" )
251230 if err := m .launchLayer .WriteProfile ("NODE_PATH" , fmt .Sprintf ("export NODE_PATH=%s" , launchModulesDir )); err != nil {
252231 return fmt .Errorf ("failed to write NODE_PATH in the launch layer: %v" , err )
253232 }
254-
255233 return nil
256234}
235+
236+ func logHeader () string {
237+ return color .New (color .FgBlue , color .Bold ).Sprint ("Node Modules" )
238+ }
0 commit comments