Skip to content

Commit 3ef3ff0

Browse files
committed
refactor the scripts section out
1 parent 841f3b5 commit 3ef3ff0

File tree

2 files changed

+81
-85
lines changed

2 files changed

+81
-85
lines changed

README.md

-85
Original file line numberDiff line numberDiff line change
@@ -97,91 +97,6 @@ more easily.
9797
If you have strong feelings about package managers, I'd love to hear your
9898
opinions.
9999

100-
101-
## What works now:
102-
103-
These are the commands that actually do things, as of today. If they don't do
104-
what they say they do, then please [post an issue](http://github.com/isaacs/npm/issues)
105-
about it.
106-
107-
## Package Lifecycle Scripts
108-
109-
npm supports the "scripts" member of the package.json script, for the
110-
following scripts:
111-
112-
`preinstall` - Run BEFORE the package is installed
113-
114-
`install` - Run AFTER the package is installed.
115-
116-
`preactivate` - Run BEFORE the package is activated.
117-
118-
`activate` - Run AFTER the package has been activated.
119-
120-
`deactivate` - Run BEFORE the package is deactivated.
121-
122-
`postdeactivate` - Run AFTER the package is deactivated.
123-
124-
`uninstall` - Run BEFORE the package is uninstalled.
125-
126-
`postuninstall` - Run AFTER the package is uninstalled.
127-
128-
### Package Lifecycle Env Vars
129-
130-
Package scripts are run in an environment where the package.json fields have
131-
been tacked onto the `npm_package_` prefix. So, for instance, if you had
132-
`{"name":"foo", "version":"1.2.5"}` in your package.json file, then in your
133-
various lifecycle scripts, this would be true:
134-
135-
process.env.npm_package_name === "foo"
136-
process.env.npm_package_version === "1.2.5"
137-
138-
Objects are flattened following this format, so if you had
139-
`{"scripts":{"install":"foo.js"}}` in your package.json, then you'd see this
140-
in the script:
141-
142-
process.env.npm_package_scripts_install = "foo.js"
143-
144-
Last but not least, the `npm_lifecycle_event` environment variable is set to
145-
whichever stage of the cycle is being executed. So, you could have a single
146-
script used for different parts of the process which switches based on what's
147-
currently happening.
148-
149-
If the script exits with a code other than 0, then this will abort the
150-
process.
151-
152-
Note that these script files don't have to be nodejs or even javascript
153-
programs. They just have to be some kind of executable file.
154-
155-
For example, if your package.json contains this:
156-
157-
{ "scripts" :
158-
{ "install" : "scripts/install.js"
159-
, "postinstall" : "scripts/install.js"
160-
, "activate" : "scripts/install.js"
161-
, "uninstall" : "scripts/uninstall.js"
162-
}
163-
}
164-
165-
then the `scripts/install.js` will be called for the install, post-install,
166-
and activate stages of the lifecycle, and the `scripts/uninstall.js` would be
167-
called when the package is uninstalled. Since `scripts/install.js` is running
168-
for three different phases, it would be wise in this case to look at the
169-
`npm_lifecycle_event` environment variable.
170-
171-
If you want to run a make command, you can do so. This works just fine:
172-
173-
{ "scripts" :
174-
{ "preinstall" : "./configure"
175-
, "install" : "make"
176-
, "test" : "make test"
177-
}
178-
}
179-
180-
However, the script line is not simply a command line, so `make && make install`
181-
would try to execute the `make` command with the arguments `&&`, `make`, and
182-
`install`. If you have a lot of stuff to run in a command, put it in a script
183-
file.
184-
185100
## Deviations from and Extensions to the Packages/1.0 Spec
186101

187102
npm aims to implement the commonjs

doc/scripts.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
npm-scripts(1) -- How npm handles the "scripts" field
2+
=====================================================
3+
4+
## DESCRIPTION
5+
6+
npm supports the "scripts" member of the package.json script, for the
7+
following scripts:
8+
9+
* preinstall:
10+
Run BEFORE the package is installed
11+
* install:
12+
Run AFTER the package is installed.
13+
* preactivate:
14+
Run BEFORE the package is activated.
15+
* activate:
16+
Run AFTER the package has been activated.
17+
* deactivate:
18+
Run BEFORE the package is deactivated.
19+
* postdeactivate:
20+
Run AFTER the package is deactivated.
21+
* uninstall:
22+
Run BEFORE the package is uninstalled.
23+
* postuninstall:
24+
Run AFTER the package is uninstalled.
25+
26+
## Package Lifecycle Env Vars
27+
28+
Package scripts are run in an environment where the package.json fields have
29+
been tacked onto the `npm_package_` prefix. So, for instance, if you had
30+
`{"name":"foo", "version":"1.2.5"}` in your package.json file, then in your
31+
various lifecycle scripts, this would be true:
32+
33+
process.env.npm_package_name === "foo"
34+
process.env.npm_package_version === "1.2.5"
35+
36+
Objects are flattened following this format, so if you had
37+
`{"scripts":{"install":"foo.js"}}` in your package.json, then you'd see this
38+
in the script:
39+
40+
process.env.npm_package_scripts_install = "foo.js"
41+
42+
Last but not least, the `npm_lifecycle_event` environment variable is set to
43+
whichever stage of the cycle is being executed. So, you could have a single
44+
script used for different parts of the process which switches based on what's
45+
currently happening.
46+
47+
If the script exits with a code other than 0, then this will abort the
48+
process.
49+
50+
Note that these script files don't have to be nodejs or even javascript
51+
programs. They just have to be some kind of executable file.
52+
53+
For example, if your package.json contains this:
54+
55+
{ "scripts" :
56+
{ "install" : "scripts/install.js"
57+
, "postinstall" : "scripts/install.js"
58+
, "activate" : "scripts/install.js"
59+
, "uninstall" : "scripts/uninstall.js"
60+
}
61+
}
62+
63+
then the `scripts/install.js` will be called for the install, post-install,
64+
and activate stages of the lifecycle, and the `scripts/uninstall.js` would be
65+
called when the package is uninstalled. Since `scripts/install.js` is running
66+
for three different phases, it would be wise in this case to look at the
67+
`npm_lifecycle_event` environment variable.
68+
69+
If you want to run a make command, you can do so. This works just fine:
70+
71+
{ "scripts" :
72+
{ "preinstall" : "./configure"
73+
, "install" : "make"
74+
, "test" : "make test"
75+
}
76+
}
77+
78+
However, the script line is not simply a command line, so `make && make install`
79+
would try to execute the `make` command with the arguments `&&`, `make`, and
80+
`install`. If you have a lot of stuff to run in a command, put it in a script
81+
file.

0 commit comments

Comments
 (0)