@@ -13,36 +13,13 @@ function lifecycle (pkg, stage, cb) {
13
13
while ( pkg && pkg . _data ) pkg = pkg . _data
14
14
if ( ! pkg ) return cb ( new Error ( "Invalid package data" ) )
15
15
log . verbose ( pkg . _id , "lifecycle " + stage )
16
- if ( ! pkg . scripts || ! ( stage in pkg . scripts ) ) return cb ( )
17
16
18
- // run package lifecycle scripts in the package root, or the nearest parent.
19
- var d = path . join ( npm . dir , pkg . name , pkg . version , "package" )
20
- while ( d ) {
21
- try {
22
- process . chdir ( d )
23
- break
24
- } catch ( ex ) {
25
- d = path . dirname ( d )
26
- }
27
- }
28
- log ( pkg . _id , stage )
29
-
30
- // set the env variables, then run the script as a child process.
31
- // NOTE: The env vars won't work until node supports env hashes for child procs
17
+ // set the env variables, then run scripts as a child process.
32
18
var env = makeEnv ( pkg )
33
19
env . npm_lifecycle_event = stage
34
- env . npm_lifecycle_script = pkg . scripts [ stage ]
35
20
log . silly ( env , "lifecycle env" )
36
- exec ( "sh" , [ "-c" , env . npm_lifecycle_script ] , env , function ( er ) {
37
- if ( er && ! npm . ROLLBACK ) {
38
- log ( "Failed to exec " + stage + " script" , pkg . _id )
39
- er . message = pkg . _id + " " + stage + ": `" + env . npm_lifecycle_script + "`\n"
40
- + er . message
41
- return cb ( er )
42
- } else if ( er ) {
43
- log . error ( er , pkg . _id + "." + stage + " failed" )
44
- }
45
- // check for a hook script
21
+
22
+ function runHook ( ) { // check for a hook script
46
23
var hook = path . join ( npm . dir , ".hooks" , stage )
47
24
fs . stat ( hook , function ( er ) {
48
25
if ( er ) return cb ( )
@@ -52,7 +29,38 @@ function lifecycle (pkg, stage, cb) {
52
29
cb ( er )
53
30
} )
54
31
} )
55
- } )
32
+ }
33
+
34
+ if ( pkg . scripts && ( stage in pkg . scripts ) ) {
35
+ // define the lifecycle script, since it exists.
36
+ // this will also be available in runHook
37
+ env . npm_lifecycle_script = pkg . scripts [ stage ]
38
+
39
+ // run package lifecycle scripts in the package root, or the nearest parent.
40
+ var d = path . join ( npm . dir , pkg . name , pkg . version , "package" )
41
+ while ( d ) {
42
+ try {
43
+ process . chdir ( d )
44
+ break
45
+ } catch ( ex ) {
46
+ d = path . dirname ( d )
47
+ }
48
+ }
49
+ log ( pkg . _id , stage )
50
+
51
+ exec ( "sh" , [ "-c" , env . npm_lifecycle_script ] , env , function ( er ) {
52
+ if ( er && ! npm . ROLLBACK ) {
53
+ log ( "Failed to exec " + stage + " script" , pkg . _id )
54
+ er . message = pkg . _id + " " + stage + ": `" + env . npm_lifecycle_script + "`\n"
55
+ + er . message
56
+ return cb ( er )
57
+ } else if ( er ) {
58
+ log . error ( er , pkg . _id + "." + stage + " failed" )
59
+ }
60
+ runHook ( )
61
+ } )
62
+ } else runHook ( ) // try to run a hook script even
63
+ // if a pkg.script isn't defined
56
64
}
57
65
58
66
function makeEnv ( data , prefix , env ) {
0 commit comments