叉烧店并不卖叉烧

npm中package.json的scripts域

npm TTT 164℃ 0评论

参考 参考二

怎么使用

有如下package.json文件:

{
  "name": "basic",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {
    "webpack": "^4.17.1",
    "webpack-cli": "^3.1.0"
  }
}

如果要运行scripts中的test,只需要在package.json文件所在目录的命令行中输入npm test就可以运行了。

其中,scripts域中的值都是当前命令行处理器的脚本语言。是可以直接运行的

有哪些值

npm supports the "scripts" property of the package.json file, for the following scripts:

  • prepublish: Run BEFORE the package is packed and published, as well as on localnpm install without any arguments. (See below)
  • prepare: Run both BEFORE the package is packed and published, and on localnpm install without any arguments (See below). This is run AFTERprepublish, but BEFOREprepublishOnly.
  • prepublishOnly: Run BEFORE the package is prepared and packed, ONLY onnpm publish. (See below.)
  • prepack: run BEFORE a tarball is packed (onnpm pack,npm publish, and when installing git dependencies)
  • postpack: Run AFTER the tarball has been generated and moved to its final destination.
  • publish, postpublish: Run AFTER the package is published.
  • preinstall: Run BEFORE the package is installed
  • install, postinstall: Run AFTER the package is installed.
  • preuninstall, uninstall: Run BEFORE the package is uninstalled.
  • postuninstall: Run AFTER the package is uninstalled.
  • preversion: Run BEFORE bumping the package version.
  • version: Run AFTER bumping the package version, but BEFORE commit.
  • postversion: Run AFTER bumping the package version, and AFTER commit.
  • pretest, test, posttest: Run by thenpm test command.
  • prestop, stop, poststop: Run by thenpm stop command.
  • prestart, start, poststart: Run by thenpm start command.
  • prerestart, restart, postrestart: Run by thenpm restart command. Note:npm restart will run the stop and start scripts if norestart script is provided.
  • preshrinkwrap, shrinkwrap, postshrinkwrap: Run by thenpm shrinkwrap command.

以上是package.json文件中scripts域中的不同特性,其中每个特性名称后面都给出了对应特性的运行条件。比如果我们要运行scripts域的test特性,它后面给出直接运行以下命令即可npm test

运行任意的scripts

另外,可以执行 npm run-script <stage>来执行任意的scripts。

npm run test
npm run-script test

pre和post前缀可以匹配对应名字的脚本,pre前缀在脚本运行之前运行,post前缀在脚本运行之后运行(比如,npm run myscript脚本的运行顺序如:1.premyscript,2.myscript,3.postmyscript)。

依赖中的脚本能通过npm explore <pkg> -- npm run <stage>来运行。

原理

npm脚本的原理非常简单。每当执行npm run,就会自动新建一个Shell,在这个Shell里面执行指定的脚本命令。因此,只要是Shell(一般是Bash)可以运行的命令,就可以写在npm脚本里面

比较特别的是,npm run新建的这个Shell,会将当前目录的npm_modules/.bin子目录加入PATH变量,执行结束后,再将PATH变量恢复原样

这意味着,当前目录的node_modules/.bin子目录里面的所有脚本,都可以直接用脚本名调用,而不必加上路径。比如,当前项目的依赖里面有Mocha,只要直接写mocha test就可以了

"test": "mocha test"

而不用写成下面这样

"test": "./node_modules/.bin/mocha test test"

由于npm脚本的唯一要求就是可以在Shell执行,因此它不一定是Node脚本,任何可执行文件都可以写在里面

npm脚本的退出码,也遵循Shell脚本规则。如果退出码不是0,npm就认为这个脚本执行失败

变量

npm脚本有一个非常强大的功能,就是可以使用npm的内部变量

首先,通过npm_package_前缀,npm脚本可以拿到package.json里面的字段。比如,下面是一个package.json

{
  "name": "com.chashaotao",
  "version": "1.0.0",
  "description": "exercise webpack",
  "main": "index.js",
  "scripts": {
    "test": "echo myTest",
    "pretest": "echo 'pre myTest'",
    "posttest": "echo 'post myTest'",
    "test2": "echo myTest2",
    "test3": "node app/foo.js",
    "start": "webpack --config webpack.config.js"
  },
console.log(process.env.npm_package_name);

如果foo.js文件有如上内容,当我们通过npm 运行test3脚本的时候,就可以读取到package.json的内容

C:\webpack\exercise\basic>npm run test3

> com.chashaotao@1.0.0 test3 C:\webpack\exercise\basic
> node app/foo.js

com.chashaotao

转载请注明:叉烧店 » npm中package.json的scripts域

喜欢 (0)
发表我的评论
取消评论

CAPTCHA Image
Reload Image
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址