@@ -278,7 +278,6 @@ func (p *NodePackager) npmInstall(packageName string) error {
278278
279279 npmArgs := []string {
280280 "install" ,
281- fmt .Sprintf ("--prefix=%s" , p .tempDir ),
282281 fmt .Sprintf ("--registry=%s" , p .Registry ),
283282 "--no-fund" ,
284283 "--omit=dev" ,
@@ -296,7 +295,9 @@ func (p *NodePackager) npmInstall(packageName string) error {
296295 npmArgs = append (npmArgs , packageName )
297296 }
298297
299- return p .execute (exec .Command ("npm" , npmArgs ... )) //nolint:gosec
298+ cmd := exec .Command ("npm" , npmArgs ... )
299+ cmd .Dir = p .tempDir
300+ return p .execute (cmd )
300301}
301302
302303// npmBundleDeps installs module 'https://www.npmjs.com/package/bundle-deps' and runs 'bundle-deps' for current library to bundle the dependencies.
@@ -308,7 +309,6 @@ func (p *NodePackager) npmBundleDeps() error {
308309
309310 npmArgs := []string {
310311 "install" ,
311- fmt .Sprintf ("--prefix=%s" , p .tempDir ),
312312 fmt .Sprintf ("--registry=%s" , p .Registry ),
313313 "bundle-deps" ,
314314 "--no-save" ,
@@ -323,7 +323,9 @@ func (p *NodePackager) npmBundleDeps() error {
323323 }
324324
325325 // Install bundle-deps
326- err := p .execute (exec .Command ("npm" , npmArgs ... )) //nolint:gosec
326+ cmd := exec .Command ("npm" , npmArgs ... )
327+ cmd .Dir = p .tempDir
328+ err := p .execute (cmd )
327329 if err != nil {
328330 return err
329331 }
@@ -334,29 +336,9 @@ func (p *NodePackager) npmBundleDeps() error {
334336 nodeArgs := []string {
335337 path .Join (p .tempDir , "node_modules" , "bundle-deps" , "bundle-deps.js" )}
336338
337- // Reset working dir
338- defer func () {
339- if p .Verbose {
340- utils .Printfln ("reset working dir to %s ..." , p .workingDir )
341- }
342-
343- if err := os .Chdir (p .workingDir ); err != nil {
344- p .spinner .Describe (fmt .Sprintf ("failed to reset working dir: %v" , err ))
345- utils .Warnfln ("failed to reset working dir: %v" , err )
346- }
347- }()
348-
349- if p .Verbose {
350- utils .Printfln ("changing working dir to %s ..." , p .tempDir )
351- }
352-
353- // We need to change current working dir to temp dir to make bundle-deps work correctly,
354- // which needs to be run at package.json level.
355- if err := os .Chdir (p .tempDir ); err != nil {
356- return err
357- }
358-
359- return p .execute (exec .Command ("node" , nodeArgs ... )) //nolint:gosec
339+ cmd = exec .Command ("node" , nodeArgs ... )
340+ cmd .Dir = p .tempDir
341+ return p .execute (cmd )
360342}
361343
362344// npmAuditFix runs 'npm audit fix' in production mode.
@@ -372,7 +354,6 @@ func (p *NodePackager) npmAuditFix() error {
372354 npmArgs := []string {
373355 "audit" ,
374356 "fix" ,
375- fmt .Sprintf ("--prefix=%s" , p .tempDir ),
376357 fmt .Sprintf ("--registry=%s" , defaultRegistry ),
377358 fmt .Sprintf ("--audit-level=%s" , p .AuditLevel ),
378359 "--only=prod" ,
@@ -387,7 +368,9 @@ func (p *NodePackager) npmAuditFix() error {
387368 npmArgs = append (npmArgs , "--verbose" )
388369 }
389370
390- if err := p .execute (exec .Command ("npm" , npmArgs ... )); err != nil { //nolint:gosec
371+ cmd := exec .Command ("npm" , npmArgs ... )
372+ cmd .Dir = p .tempDir
373+ if err := p .execute (cmd ); err != nil {
391374 return fmt .Errorf ("vulnerability fix (level: %v) failed: try again with a decreased 'audit-level'" , p .AuditLevel )
392375 }
393376
@@ -406,7 +389,6 @@ func (p *NodePackager) npmAudit() error {
406389
407390 npmArgs := []string {
408391 "audit" ,
409- fmt .Sprintf ("--prefix=%s" , p .tempDir ),
410392 fmt .Sprintf ("--registry=%s" , defaultRegistry ),
411393 fmt .Sprintf ("--audit-level=%s" , p .AuditLevel ),
412394 "--only=prod" ,
@@ -421,11 +403,13 @@ func (p *NodePackager) npmAudit() error {
421403 npmArgs = append (npmArgs , "--verbose" )
422404 }
423405
424- if err := p .execute (exec .Command ("npm" , npmArgs ... )); err != nil { //nolint:gosec
406+ cmd := exec .Command ("npm" , npmArgs ... )
407+ cmd .Dir = p .tempDir
408+ if err := p .execute (cmd ); err != nil {
425409 return fmt .Errorf ("vulnerability audit (level: %v) failed: try again with '--no-audit' or increased 'audit-level'" , p .AuditLevel )
426410 }
427411
428- return p . execute ( exec . Command ( "npm" , npmArgs ... )) //nolint:gosec
412+ return nil
429413}
430414
431415// npmPack packs the package to a tarball (*.tgz).
@@ -434,7 +418,6 @@ func (p *NodePackager) npmPack() error {
434418
435419 npmArgs := []string {
436420 "pack" ,
437- fmt .Sprintf ("--prefix=%s" , p .tempDir ),
438421 fmt .Sprintf ("--registry=%s" , p .Registry ),
439422 }
440423
@@ -446,31 +429,13 @@ func (p *NodePackager) npmPack() error {
446429 npmArgs = append (npmArgs , "--verbose" )
447430 }
448431
449- // Reset working dir
450- defer func () {
451- if p .Verbose {
452- utils .Printfln ("reset working dir to %s ..." , p .workingDir )
453- }
454-
455- if err := os .Chdir (p .workingDir ); err != nil {
456- utils .Warnfln ("failed to reset working dir: %v" , err )
457- }
458- }()
459-
460- if p .Verbose {
461- utils .Printfln ("changing working dir to %s ..." , p .tempDir )
462- }
463-
464- // We need to change current working dir to temp dir to make bundle-deps work correctly,
465- // which needs to be run at package.json level.
466- if err := os .Chdir (p .tempDir ); err != nil {
467- return err
468- }
469-
470- tarballName , err := p .executeStdout (exec .Command ("npm" , npmArgs ... )) //nolint:gosec
432+ cmd := exec .Command ("npm" , npmArgs ... )
433+ cmd .Dir = p .tempDir
434+ tarballName , err := p .executeStdout (cmd )
471435 if err != nil {
472436 return err
473437 }
438+
474439 p .tarballName = strings .TrimSpace (tarballName )
475440 p .tarballPath = path .Join (p .tempDir , p .tarballName )
476441 return nil
@@ -758,7 +723,6 @@ func (p *NodePackager) getWorkingDir() error {
758723
759724// createTempDir creates and returns the temp dir.
760725func (p * NodePackager ) createTempDir () error {
761-
762726 // Create temp dir
763727 tempDir , err := os .MkdirTemp (p .workingDir , p .libraryNameToFolderName (p .libraryNameWithoutVersion )+ "-" )
764728 if err != nil {
@@ -816,12 +780,19 @@ func (p *NodePackager) executeStdout(cmd *exec.Cmd) (string, error) {
816780 return output .String (), nil
817781}
818782
819- // libraryNameToFolderName returns the library name as a folder name by replacing all file path chars ('/', ':') and '@' with underscores '_'.
783+ // libraryNameToFolderName converts the library name to a valid folder name, by replacing special path chars ('\', ' /', ':', '.', '@') by '_'.
820784func (p * NodePackager ) libraryNameToFolderName (name string ) string {
821785 folder := filepath .ToSlash (name )
822- folder = strings .ReplaceAll (folder , "@" , "_" )
823- folder = strings .ReplaceAll (folder , string (os .PathSeparator ), "_" )
824- folder = strings .ReplaceAll (folder , string (os .PathListSeparator ), "_" )
786+
787+ charsToReplace := []string {
788+ "." ,
789+ "@" ,
790+ "/" ,
791+ string (os .PathListSeparator )}
792+
793+ for _ , c := range charsToReplace {
794+ folder = strings .ReplaceAll (folder , c , "_" )
795+ }
825796 return folder
826797}
827798
0 commit comments