This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
lifecycle.js
163 lines (149 loc) · 4.59 KB
/
lifecycle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
exports = module.exports = lifecycle
exports.cmd = cmd
var log = require("./log")
, exec = require("./exec")
, npm = require("../../npm")
, path = require("path")
, readJson = require("./read-json")
, fs = require("./graceful-fs")
, chain = require("./chain")
function lifecycle (pkg, stage, wd, cb) {
while (pkg && pkg._data) pkg = pkg._data
if (!pkg) return cb(new Error("Invalid package data"))
if (typeof wd === "function") cb = wd , wd = null
log(pkg._id, stage)
// set the env variables, then run scripts as a child process.
var env = makeEnv(pkg)
env.npm_lifecycle_event = stage
var pkgDir = path.join(npm.dir, pkg.name, pkg.version)
env.PATH = path.join(pkgDir, "node_modules", ".bin")
+ ":" + path.join(pkgDir, "dep-bin")
+ (env.PATH ? ":" + env.PATH : "")
var packageLifecycle = pkg.scripts && (stage in pkg.scripts)
if (packageLifecycle) {
// define this here so it's available to all scripts.
env.npm_lifecycle_script = pkg.scripts[stage]
}
chain
( packageLifecycle && [runPackageLifecycle, pkg, env, wd]
, [runHookLifecycle, pkg, env, wd]
, cb
)
}
function validWd (d) {
var cwd = process.cwd()
while (d) {
try {
process.chdir(d)
break
} catch (ex) {
d = path.dirname(d)
}
}
process.chdir(cwd)
return d
}
function runPackageLifecycle (pkg, env, wd, cb) {
// run package lifecycle scripts in the package root, or the nearest parent.
var stage = env.npm_lifecycle_event
var d = wd || path.join(npm.dir, pkg.name, pkg.version, "package")
d = validWd(d)
exec("sh", ["-c", env.npm_lifecycle_script], env, true, d,
function (er, code, stdout, stderr) {
if (er && !npm.ROLLBACK) {
log("Failed to exec "+stage+" script", pkg._id)
er.message = pkg._id + " "
+ stage + ": `" + env.npm_lifecycle_script+"`\n"
+ er.message
er.errno = npm.ELIFECYCLE
er.pkgid = pkg._id
er.stage = stage
er.script = env.npm_lifecycle_script
er.pkgname = pkg.name
return cb(er)
} else if (er) {
log.error(er, pkg._id+"."+stage)
log.error("failed, but continuing anyway", pkg._id+"."+stage)
return cb()
}
cb(er)
})
}
function runHookLifecycle (pkg, env, wd, cb) {
// check for a hook script, run if present.
var stage = env.npm_lifecycle_event
var hook = path.join(npm.dir, ".hooks", stage)
var d = wd || path.join(npm.dir, pkg.name, pkg.version, "package")
d = validWd(d)
fs.stat(hook, function (er) {
if (er) return cb()
exec(hook, [], env, true, d, function (er) {
if (er) {
er.message += "\nFailed to exec "+stage+" hook script"
log(er, pkg._id)
}
if (npm.ROLLBACK) return cb()
cb(er)
})
})
}
function makeEnv (data, prefix, env) {
prefix = prefix || "npm_package_"
if (!env) {
env = {}
for (var i in process.env) env[i] = process.env[i]
} else if (!data.hasOwnProperty("_lifecycleEnv")) {
Object.defineProperty(data, "_lifecycleEnv",
{ value : env
, enumerable : false
})
}
for (var i in data) if (i.charAt(0) !== "_") {
var envKey = (prefix+i).replace(/[^a-zA-Z0-9_]/g, '_')
if (data[i] && typeof(data[i]) === "object") {
env[envKey] = JSON.stringify(data[i])
makeEnv(data[i], envKey+"_", env)
} else {
env[envKey] = String(data[i])
}
}
if (prefix !== "npm_package_") return env
prefix = "npm_config_"
var conf = npm.config.get()
for (var i in conf) if (i.charAt(0) !== "_") {
var envKey = (prefix+i).replace(/[^a-zA-Z0-9_]/g, '_')
env[envKey] = String(conf[i])
}
return env
}
function cmd (stage, req) { return function (args, cb_) {
var d = args.length
function cb (er) {
if (er) return cb_(er)
if (-- d === 0) return cb_()
}
args.forEach(function (arg) {
var pkg = arg.split("@")
, name = pkg.shift()
, ver = pkg.join("@") || "active"
, wd = path.join(npm.dir, name, ver, "package")
, json = path.join(wd, "package.json")
readJson(json, function (er, data) {
if (er) return log.er(cb, "Couldn't find "+name + "@" + ver)(er)
if (ver && ver !== data.version) {
data.version = ver
data._id = data.name+"@"+ver
}
if ( !data.scripts
|| !(stage in data.scripts)
&& !("pre"+stage in data.scripts)) {
log("Nothing to do", stage)
return cb(req ? new Error("Nothing to do") : null)
}
lifecycle(data, "pre"+stage, wd, function (er) {
if (er) return log.er(cb, "Failed pre"+stage)(er)
lifecycle(data, stage, wd, cb)
})
})
})
}}