依赖选择器

依赖选择器语法和查询

描述

npm query 命令公开了一种新的依赖选择器语法(由 & 尊重 CSS 选择器 4 规范 的许多方面提供信息),它:

  • 使用健壮的对象模型、元数据和选择器语法标准化依赖图的形状和查询
  • 利用 CSS 中现有的已知语言语法和运算符来广泛访问不同的包信息
  • 解锁回答有关依赖关系、它们的关系和关联元数据的复杂、多方面问题的能力
  • 整合 npm 中类似查询命令的冗余逻辑(例如npm fund, npm ls, npm outdated, npm audit ...)

依赖选择器语法 v1.0.0

概述

  • 没有 "type" 或 "tag" 选择器(例如。div, h1, a) 作为依赖项/目标是唯一可以查询的 Node 类型
  • "dependencies" 一词是指在 Arborist 返回的 tree 中找到的任何 Node

组合器

  • > 直系后代/子代
  • 任何后代/孩子
  • ~ 兄弟姐妹

选择器

    +VuGqYuBUebMojrAscTHDTxxB6eLC5l60QR40q4iDDwJ3l3syPvCov7Nz+J0jfAZw9nHMXOoEinam8313LqUBgsDMgIi5rJIP0yZeBktrEr1ZaTZERj7Yqh7YseDRg0Ypuow7VHYS54ergUn1IA06Lvryph4RSbzq1NaObOewXcw8/MC0aRzwibvN6zhd1fjd70edxk725iBF4Qh6H+PcLBhf2J54PmILB01bZOzTLvkT+I+YYKbg/pkRAdQ4izCrI92CO0hvMAAWYtDqbqObgsFP9O5Oy8wKfMdMguOf0OTeuCiyy/LTlJnIh03s10FKyHfjhCm0O0afV2+2H78w88t7dYRWKi8brUpoBfZUZi+YIKMYJFchwFpcjYE0xyVigAzwc8pVBL1+jzPG8kW/Ub1+v34YHayXtdqbZHnICL0mC5zwB7q/pYFvRJ7lxCcHkPNjoI3cbdsQco0M1ej4Y8chnSRvyWU0bfCS9BzxP3Xj1fjs6NWGOaZu86SmUHsDjIqGyJk90Z4XX6iITO9Cg==

依赖类型选择器

    qHpUw5HlIqM+6IjlALzmvAsHcXKgjc06ifee0jPkMCGKMqZVM6S4tEA/jaQndwu+Vy5B0GJXGit52nsNKHiJhlw2uqWNjpE1+jLm4ZGbhGn0kArrr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WlRKqC8lJQfpK1THwvzE6gHGWj9GOpPSCz0aLvy9+S3UkKaRfZQt+SqZ1FRY1Hq37YIAfgNBll87oTKVboAA

Array & Object 属性选择器

QZ+wbbSmUvdUzVcllwIYsAcPmkRgvaihe7kYKg0PAmTYu5SLYelM7i81x5xd6kydVzEU2O/eS7otpOhpM+yqt34xSwUOYbPoyyt/kUd9b5i9DwwFCd9lV1jSkXfWTNOKQcMPyXYa2MzO4Lg1YLiP82tNoez959UDcliOHNxKaoHGmIJAAenmGgwtppssXHYtqt75gtlzj1DGaMcRTk+f0FMngk1wAQk/YmY0OghwGEIjtEzE6iqu5oH2lX3Em3zf8BRq9PSKAi8S6WvZC2r/zy/xNDjOeLUyb2Rr6jyJeL9bUYGNDk2ubL+hCiFV/ZFWBYfbKZp6qkkNFPXcD7+WIlNsFCSEqVCksioX5wvlwPBgsbvb4KRxKsFTppvy7sVMY6KTOmWHefOfm87nJECsvo+BQN0aqw57bGpoj+ftlLRaYKKu15yLCUf8oA4BcwGXblnJ2koHjL7on2XuNeFWWtdjDaCLC27o90qFbWwN2sNmSgCf/4JuLn58tCIAUlUP+QpRu3uxVexQ9tH4bezAskT8vWKypkJoy9OOKpRVc14nCQCPkffdpxuYxrc5hKVM9QSuGb2bsD8rUR6vOqKj6PCrP5lOoO7BnW9jxbuqm5Wlollm4DOZbHiYhuXHpDo/mnN8nR+fyFPjoWEBpjlz7mh59Wxl9omJtvIY68htYRQ=

Objects

/* return dependencies that have a `scripts.test` containing `"tap"` */
*:attr(scripts, [test~=tap])

嵌套 Objects

MGLE8UuHRM0tVq3O21hBz8Qs8pznG3Dbffc/AKEWKO4tNrAdG0bMOZ9LYKS0eaBi+QHWZU2rP5F7ZXckVbzePQ==

/* return dependencies that have a testling config for opera browsers */
*:attr(testling, browsers, [~=opera])

Arrays

KCsaiB4XK6/sndsShSeUTHlMR5Gr7lIaR7tNjlzif/2hHIkGFl3Pb0jeUNSGKxzCi3iwDT4An9DVH2hDY7WwW/B6ROCBX0axz6DZCP8J25Ji4aV47CPSTj7J0+GeTMI/eLVWh5ziOLuU/3+ZpXylOfGxn0A/fEueWzcICb0uf45QiiGK4MEkgmXO9rOohexNd1DZ8/ciFl3WPgiOM1O4RixEY7kEt1v037efvC2RGewG/SatmPG5FG5XX36Q9er06x9qWJPTMbzRdm0LWrm5y+4MWtnB/uKHHS5Jpkn/upE=

Array 属性选择示例:

/* removes the distinction between properties & arrays */
/* ie. we'd have to check the property & iterate to match selection */
*:attr([keywords^=react])
*:attr(contributors, :attr([name~=Jordan]))

直接匹配值的 Array 示例:

/* return dependencies that have the exact keyword "react" */
/* this is equivalent to `*:keywords([value="react"])` */
*:attr([keywords=react])

Object 中的 Array 示例:

/* returns */
*:attr(contributors, [email=ruyadorno@github.com])

wvYEEpDk+6X8iQLh57z+2aiW6j5/49g/UUP1yv07KMs5/WA719S8iP7NqBp8vOzWxgoDFdXN5kBBfjM7cv4pxqdsla9+8PFbyYV8sEm58kAys0PFZyjm+s1Cm9RcA9/DNUQC4txWRGgyGHDVS9UtCgid+K8IxPfGk2NBz8dgj3REOUSa/N9OIak3mCrysKpD2T/7K+zW/T6AfYnoaS06MR4FTT5CLLVGB7AODk5gAsDnU900+Ynm+j6WI9rbx4OIuf+kq5ZFgE0SVoSUPxUh4dfxtTZb4qUkqe21SDUao3+lWdktYPsX3BUGROe0MrIvMhCOg50Xx30Qov5IfNFm2Clv1ANtz4PrhL5meIZ4Jz5+xR8rJLr0fQGuli8f55apTUjteEzlOYY7o1bLSx9VTAELD7wkHdMNkmNbF2L+UJMi6g3u3beRNQWAAKEi7XEXD8khoYRlb6lcDJlIG3/X+HcY0bj9X/5iqR0IbvXc1hFAUu6h5sd4YoiXWzDuulruZxdACqyeIoX1OndqaVtrGDL1tL1695Vp+/e9BSbh6+KaywMJkfA/I35VpCCsIhRYMW9KbXMmIcUMCJaUVTKID+qqvEkjIV47Pu6OhCWRmcpKUnJ/XrV/MMOfan96inElf0Mu+SapEZygy04N/lgVtzCAl1WuIi40WiXeJF+8ofQ=

    ruC5+jDDxHGlWFgApPWdepyvU9xKI8rMvoyq0uJJg5OucBEMdOeK7nhwhBuemNj9cWrJuoowAHM50vaUNjkn9ZfJg26sL0WihuD2seSuuHzgeo30HO0sAigiujcNyWF1d3vhVrTkfQ7SFAxpDjqEOh78jEf1TBu6Wnk9kaV7dnHGKcio2OMmjr9mbff1aq27og1j3fsPczdin4dFcuTTvch1gwh7PHkwr4B1cvjcZyJO44HZxZhFI/bTlN/ntSbz

DxfSpTIlb3Ig6rfiPuWAgLLvkP7wBbZ4dLRbWxREZ8k1kG/KhqRe+Wb0ZlycWO1NxgdApwLD3x5Bs1ZU0xQhasPbfn7hLyOeeopEeq90fz7/KThVLAf2TbpMIJbUfw3fMTNM2epRq/I1jc5FAH0/pa07GnrVz3hdKD22e3Qnw1Ntci1HwMp3g+n1d1wAfyaLEsJ1fn2M9ahpY8qS3LUiKbdh5bXlFvOidAkgVYDWw28=

程序化用法

    qt5feJov4nueOsFev170kRCOdFfkKFfSh9ZwyA7fDZB/ga6H/vJVjWS6e4jdVKTsbobfYC5Qhm1ugcB3oi2URQrew8k8QOUJHhJmfpOdoQyCYUcQ5l3U+cJGh/pa2f+OsQ7PfcNMdZTxkTzZEM67iEFKqpb8m4jM0gX5bKOZTyRW1HcR5vuK0PhNOXxrRDR1FKtwwRrbVOUh/oEhpbNQNO81l0v+xwVIlu4vhQ5m1VF6m4NhmsUtBonejd9d0zCrAcTYqMdVKuxItgSICQYramCb9jQ0DDsRVu5qVvahoKAi3K9evDZD8mcUBgXu5sMP
const Arborist = require('@npmcli/arborist')
const arb = new Arborist({})
// root-level
arb.loadActual().then(async (tree) => {
  // query all production dependencies
  const results = await tree.querySelectorAll('.prod')
  console.log(results)
})
// iterative
arb.loadActual().then(async (tree) => {
  // query for the deduped version of react
  const results = await tree.querySelectorAll('#react:not(:deduped)')
  // query the deduped react for git deps
  const deps = await results[0].querySelectorAll(':type(git)')
  console.log(deps)
})