Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
10 KiB

  1. .TH "NPM\-SCRIPTS" "7" "August 2018" "" ""
  2. .SH "NAME"
  3. \fBnpm-scripts\fR \- How npm handles the "scripts" field
  4. .SH DESCRIPTION
  5. .P
  6. npm supports the "scripts" property of the package\.json file, for the
  7. following scripts:
  8. .RS 0
  9. .IP \(bu 2
  10. prepublish:
  11. Run BEFORE the package is packed and published, as well as on local \fBnpm
  12. install\fP without any arguments\. (See below)
  13. .IP \(bu 2
  14. prepare:
  15. Run both BEFORE the package is packed and published, and on local \fBnpm
  16. install\fP without any arguments (See below)\. This is run
  17. AFTER \fBprepublish\fP, but BEFORE \fBprepublishOnly\fP\|\.
  18. .IP \(bu 2
  19. prepublishOnly:
  20. Run BEFORE the package is prepared and packed, ONLY on \fBnpm publish\fP\|\. (See
  21. below\.)
  22. .IP \(bu 2
  23. prepack:
  24. run BEFORE a tarball is packed (on \fBnpm pack\fP, \fBnpm publish\fP, and when
  25. installing git dependencies)
  26. .IP \(bu 2
  27. postpack:
  28. Run AFTER the tarball has been generated and moved to its final destination\.
  29. .IP \(bu 2
  30. publish, postpublish:
  31. Run AFTER the package is published\.
  32. .IP \(bu 2
  33. preinstall:
  34. Run BEFORE the package is installed
  35. .IP \(bu 2
  36. install, postinstall:
  37. Run AFTER the package is installed\.
  38. .IP \(bu 2
  39. preuninstall, uninstall:
  40. Run BEFORE the package is uninstalled\.
  41. .IP \(bu 2
  42. postuninstall:
  43. Run AFTER the package is uninstalled\.
  44. .IP \(bu 2
  45. preversion:
  46. Run BEFORE bumping the package version\.
  47. .IP \(bu 2
  48. version:
  49. Run AFTER bumping the package version, but BEFORE commit\.
  50. .IP \(bu 2
  51. postversion:
  52. Run AFTER bumping the package version, and AFTER commit\.
  53. .IP \(bu 2
  54. pretest, test, posttest:
  55. Run by the \fBnpm test\fP command\.
  56. .IP \(bu 2
  57. prestop, stop, poststop:
  58. Run by the \fBnpm stop\fP command\.
  59. .IP \(bu 2
  60. prestart, start, poststart:
  61. Run by the \fBnpm start\fP command\.
  62. .IP \(bu 2
  63. prerestart, restart, postrestart:
  64. Run by the \fBnpm restart\fP command\. Note: \fBnpm restart\fP will run the
  65. stop and start scripts if no \fBrestart\fP script is provided\.
  66. .IP \(bu 2
  67. preshrinkwrap, shrinkwrap, postshrinkwrap:
  68. Run by the \fBnpm shrinkwrap\fP command\.
  69. .RE
  70. .P
  71. Additionally, arbitrary scripts can be executed by running \fBnpm
  72. run\-script <stage>\fP\|\. \fIPre\fR and \fIpost\fR commands with matching
  73. names will be run for those as well (e\.g\. \fBpremyscript\fP, \fBmyscript\fP,
  74. \fBpostmyscript\fP)\. Scripts from dependencies can be run with `npm explore
  75. .P
  76. <pkg> \-\- npm run <stage>`\.
  77. .SH PREPUBLISH AND PREPARE
  78. .SS DEPRECATION NOTE
  79. .P
  80. Since \fB, the npm CLI has run the\fPprepublish\fBscript for both\fPnpm
  81. publish\fBand\fPnpm install\fB, because it's a convenient way to prepare a package
  82. for use (some common use cases are described in the section below)\. It has
  83. also turned out to be, in practice, [very
  84. confusing](https://github\.com/npm/npm/issues/10074)\. As of\fP\fB, a new
  85. event has been introduced,\fPprepare\fB, that preserves this existing behavior\. A
  86. _new_ event,\fPprepublishOnly\fBhas been added as a transitional strategy to
  87. allow users to avoid the confusing behavior of existing npm versions and only
  88. run on\fPnpm publish` (for instance, running the tests one last time to ensure
  89. they're in good shape)\.
  90. .P
  91. See https://github\.com/npm/npm/issues/10074 for a much lengthier
  92. justification, with further reading, for this change\.
  93. .SS USE CASES
  94. .P
  95. If you need to perform operations on your package before it is used, in a way
  96. that is not dependent on the operating system or architecture of the
  97. target system, use a \fBprepublish\fP script\. This includes
  98. tasks such as:
  99. .RS 0
  100. .IP \(bu 2
  101. Compiling CoffeeScript source code into JavaScript\.
  102. .IP \(bu 2
  103. Creating minified versions of JavaScript source code\.
  104. .IP \(bu 2
  105. Fetching remote resources that your package will use\.
  106. .RE
  107. .P
  108. The advantage of doing these things at \fBprepublish\fP time is that they can be done once, in a
  109. single place, thus reducing complexity and variability\.
  110. Additionally, this means that:
  111. .RS 0
  112. .IP \(bu 2
  113. You can depend on \fBcoffee\-script\fP as a \fBdevDependency\fP, and thus
  114. your users don't need to have it installed\.
  115. .IP \(bu 2
  116. You don't need to include minifiers in your package, reducing
  117. the size for your users\.
  118. .IP \(bu 2
  119. You don't need to rely on your users having \fBcurl\fP or \fBwget\fP or
  120. other system tools on the target machines\.
  121. .RE
  122. .SH DEFAULT VALUES
  123. .P
  124. npm will default some script values based on package contents\.
  125. .RS 0
  126. .IP \(bu 2
  127. \fB"start": "node server\.js"\fP:
  128. If there is a \fBserver\.js\fP file in the root of your package, then npm
  129. will default the \fBstart\fP command to \fBnode server\.js\fP\|\.
  130. .IP \(bu 2
  131. \fB"install": "node\-gyp rebuild"\fP:
  132. If there is a \fBbinding\.gyp\fP file in the root of your package and you
  133. haven't defined your own \fBinstall\fP or \fBpreinstall\fP scripts, npm will
  134. default the \fBinstall\fP command to compile using node\-gyp\.
  135. .RE
  136. .SH USER
  137. .P
  138. If npm was invoked with root privileges, then it will change the uid
  139. to the user account or uid specified by the \fBuser\fP config, which
  140. defaults to \fBnobody\fP\|\. Set the \fBunsafe\-perm\fP flag to run scripts with
  141. root privileges\.
  142. .SH ENVIRONMENT
  143. .P
  144. Package scripts run in an environment where many pieces of information
  145. are made available regarding the setup of npm and the current state of
  146. the process\.
  147. .SS path
  148. .P
  149. If you depend on modules that define executable scripts, like test
  150. suites, then those executables will be added to the \fBPATH\fP for
  151. executing the scripts\. So, if your package\.json has this:
  152. .P
  153. .RS 2
  154. .nf
  155. { "name" : "foo"
  156. , "dependencies" : { "bar" : "0\.1\.x" }
  157. , "scripts": { "start" : "bar \./test" } }
  158. .fi
  159. .RE
  160. .P
  161. then you could run \fBnpm start\fP to execute the \fBbar\fP script, which is
  162. exported into the \fBnode_modules/\.bin\fP directory on \fBnpm install\fP\|\.
  163. .SS package\.json vars
  164. .P
  165. The package\.json fields are tacked onto the \fBnpm_package_\fP prefix\. So,
  166. for instance, if you had \fB{"name":"foo", "version":"1\.2\.5"}\fP in your
  167. package\.json file, then your package scripts would have the
  168. \fBnpm_package_name\fP environment variable set to "foo", and the
  169. \fBnpm_package_version\fP set to "1\.2\.5"\. You can access these variables
  170. in your code with \fBprocess\.env\.npm_package_name\fP and
  171. \fBprocess\.env\.npm_package_version\fP, and so on for other fields\.
  172. .SS configuration
  173. .P
  174. Configuration parameters are put in the environment with the
  175. \fBnpm_config_\fP prefix\. For instance, you can view the effective \fBroot\fP
  176. config by checking the \fBnpm_config_root\fP environment variable\.
  177. .SS Special: package\.json "config" object
  178. .P
  179. The package\.json "config" keys are overwritten in the environment if
  180. there is a config param of \fB<name>[@<version>]:<key>\fP\|\. For example,
  181. if the package\.json has this:
  182. .P
  183. .RS 2
  184. .nf
  185. { "name" : "foo"
  186. , "config" : { "port" : "8080" }
  187. , "scripts" : { "start" : "node server\.js" } }
  188. .fi
  189. .RE
  190. .P
  191. and the server\.js is this:
  192. .P
  193. .RS 2
  194. .nf
  195. http\.createServer(\.\.\.)\.listen(process\.env\.npm_package_config_port)
  196. .fi
  197. .RE
  198. .P
  199. then the user could change the behavior by doing:
  200. .P
  201. .RS 2
  202. .nf
  203. npm config set foo:port 80
  204. .fi
  205. .RE
  206. .SS current lifecycle event
  207. .P
  208. Lastly, the \fBnpm_lifecycle_event\fP environment variable is set to
  209. whichever stage of the cycle is being executed\. So, you could have a
  210. single script used for different parts of the process which switches
  211. based on what's currently happening\.
  212. .P
  213. Objects are flattened following this format, so if you had
  214. \fB{"scripts":{"install":"foo\.js"}}\fP in your package\.json, then you'd
  215. see this in the script:
  216. .P
  217. .RS 2
  218. .nf
  219. process\.env\.npm_package_scripts_install === "foo\.js"
  220. .fi
  221. .RE
  222. .SH EXAMPLES
  223. .P
  224. For example, if your package\.json contains this:
  225. .P
  226. .RS 2
  227. .nf
  228. { "scripts" :
  229. { "install" : "scripts/install\.js"
  230. , "postinstall" : "scripts/install\.js"
  231. , "uninstall" : "scripts/uninstall\.js"
  232. }
  233. }
  234. .fi
  235. .RE
  236. .P
  237. then \fBscripts/install\.js\fP will be called for the install
  238. and post\-install stages of the lifecycle, and \fBscripts/uninstall\.js\fP
  239. will be called when the package is uninstalled\. Since
  240. \fBscripts/install\.js\fP is running for two different phases, it would
  241. be wise in this case to look at the \fBnpm_lifecycle_event\fP environment
  242. variable\.
  243. .P
  244. If you want to run a make command, you can do so\. This works just
  245. fine:
  246. .P
  247. .RS 2
  248. .nf
  249. { "scripts" :
  250. { "preinstall" : "\./configure"
  251. , "install" : "make && make install"
  252. , "test" : "make test"
  253. }
  254. }
  255. .fi
  256. .RE
  257. .SH EXITING
  258. .P
  259. Scripts are run by passing the line as a script argument to \fBsh\fP\|\.
  260. .P
  261. If the script exits with a code other than 0, then this will abort the
  262. process\.
  263. .P
  264. Note that these script files don't have to be nodejs or even
  265. javascript programs\. They just have to be some kind of executable
  266. file\.
  267. .SH HOOK SCRIPTS
  268. .P
  269. If you want to run a specific script at a specific lifecycle event for
  270. ALL packages, then you can use a hook script\.
  271. .P
  272. Place an executable file at \fBnode_modules/\.hooks/{eventname}\fP, and
  273. it'll get run for all packages when they are going through that point
  274. in the package lifecycle for any packages installed in that root\.
  275. .P
  276. Hook scripts are run exactly the same way as package\.json scripts\.
  277. That is, they are in a separate child process, with the env described
  278. above\.
  279. .SH BEST PRACTICES
  280. .RS 0
  281. .IP \(bu 2
  282. Don't exit with a non\-zero error code unless you \fIreally\fR mean it\.
  283. Except for uninstall scripts, this will cause the npm action to
  284. fail, and potentially be rolled back\. If the failure is minor or
  285. only will prevent some optional features, then it's better to just
  286. print a warning and exit successfully\.
  287. .IP \(bu 2
  288. Try not to use scripts to do what npm can do for you\. Read through
  289. npm help 5 \fBpackage\.json\fP to see all the things that you can specify and enable
  290. by simply describing your package appropriately\. In general, this
  291. will lead to a more robust and consistent state\.
  292. .IP \(bu 2
  293. Inspect the env to determine where to put things\. For instance, if
  294. the \fBnpm_config_binroot\fP environment variable is set to \fB/home/user/bin\fP, then
  295. don't try to install executables into \fB/usr/local/bin\fP\|\. The user
  296. probably set it up that way for a reason\.
  297. .IP \(bu 2
  298. Don't prefix your script commands with "sudo"\. If root permissions
  299. are required for some reason, then it'll fail with that error, and
  300. the user will sudo the npm command in question\.
  301. .IP \(bu 2
  302. Don't use \fBinstall\fP\|\. Use a \fB\|\.gyp\fP file for compilation, and \fBprepublish\fP
  303. for anything else\. You should almost never have to explicitly set a
  304. preinstall or install script\. If you are doing this, please consider if
  305. there is another option\. The only valid use of \fBinstall\fP or \fBpreinstall\fP
  306. scripts is for compilation which must be done on the target architecture\.
  307. .RE
  308. .SH SEE ALSO
  309. .RS 0
  310. .IP \(bu 2
  311. npm help run\-script
  312. .IP \(bu 2
  313. npm help 5 package\.json
  314. .IP \(bu 2
  315. npm help 7 developers
  316. .IP \(bu 2
  317. npm help install
  318. .RE