如何列出 package.json 中所有可用的命令?

huangapple go评论70阅读模式
英文:

How to list all the commands available in package.json?

问题

A package.json 可以有很多命令,常见的包括 npm startnpm test,但通常还有更多命令。

有没有一种列出所有命令的方法?

我目前使用 less package.json,但它有太多噪音要显示。

英文:

A package.json can have lot of commands and common ones are npm start, npm test but there are generally more commands.

Is there a way to list all the commands?

Currently I use less package.json but it has too much noise to show.

答案1

得分: 91

你可以使用:

npm run

来列出所有命令。 (Yarn 也在 yarn run 上具有类似功能。)

这个行为在 npm help run 的帮助页面中明确说明了:

> 这会运行一个包的 "scripts" 对象中的任意命令。 如果没有提供 "command",它将列出可用的脚本。


要快速查看是否安装了 jq,您可以执行:

jq .scripts package.json

如果需要的话,您仍然可以将这个子集传递给 less

jq .scripts package.json | less

此外,您可以通过 npm-completion 使用制表符补全,然后在按制表符键时看到可能的命令列表,并且如果只剩下一个选项,则会自动完成命令。

您可以通过以下方式临时设置它:

source <(npm completion)

根据您的终端,您可以将其设置为持久行为,将以下内容添加到相关的配置文件中:

npm completion >> ~/.bashrc
npm completion >> ~/.zshrc
英文:

You can use:

npm run

to list all commands. (Yarn also features similar functionality on yarn run.)

This behavior is made explicit in the help page of npm help run:

> This runs an arbitrary command from a package's "scripts" object. If no "command" is provided, it will list the available scripts.


To get a quick overview if you have jq installed:

jq .scripts package.json

You can still pipe that subset to less if you need to

jq .scripts package.json | less

Furthermore, you can use tab completion via npm-completion and then you should see a list of possible commands when hitting tab and completes the commands if there is only one option left.

You can set it up temporarily via

source <(npm completion)

Depending on your terminal, you can make it persistent behavior by adding to your relevant config file:

npm completion >> ~/.bashrc
npm completion >> ~/.zshrc

答案2

得分: 5

我很惊讶没有人建议使用jq中的to_entries()函数,也就是:

jq -r '.scripts|to_entries[]|((.key))' package.json
# 输出
lint
build:css
build:dev
build:node
build:release

这基本上意味着:

  • 选择package.json文件中的.scripts部分
  • 使用原始输出(-r)
  • 将这个对象转换为.key .value对
  • 仅显示键
英文:

I am surprised nobody suggested the to_entries() func in jq , aka:

jq -r '.scripts|to_entries[]|((.key))' package.json
# output
lint
build:css
build:dev
build:node
build:release

Which basically means :

  • pick the .scripts section of the package.json file
  • use row output - -r
  • convert the object of this to .key .value pair
  • show the keys only

答案3

得分: 3

以下是翻译好的部分:

这是一个只使用Node的极简方法!

仅列出可用命令。

node -e "console.log(Object.keys(require('.' + require('path').sep + 'package.json').scripts || {}))"

列出所有可用命令及其目标。

node -e "console.log(require('.' + require('path').sep + 'package.json').scripts || {})"

更进一步,您可以将其转化为运行命令!在package.json中将其添加到_scripts_。

示例:

"scripts": {
   "ls": "node -e \"console.log(require('.' + require('path').sep + 'package.json').scripts || {})\""
}

现在您可以运行npm run ls!🚀

英文:

Here's a minimalist approach using only node!

List only the available commands.

node -e "console.log(Object.keys(require('.' + require('path').sep + 'package.json').scripts || {}))"

List all the available commands and their target.

node -e "console.log(require('.' + require('path').sep + 'package.json').scripts || {})"

Taking it one step further, you can turn this into a run command!
In package.json add it to the scripts.

Example:

"scripts": {
   "ls": "node -e \"console.log(require('.' + require('path').sep + 'package.json').scripts || {})\""
}

Now you can npm run ls 🔥!

huangapple
  • 本文由 发表于 2020年1月6日 22:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定