【问题标题】:How to change output format of npm ll?如何更改 npm ll 的输出格式?
【发布时间】:2019-03-04 13:07:25
【问题描述】:

我需要向我的客户提供我们项目的所有依赖项(库名称 + github / npm url)。我可以在每个项目中使用npm ll 轻松做到这一点。但这给了我一个树形结构的输出。有没有办法将此输出更改为表格?

我已经阅读了 npm ll 命令的文档,但没有找到任何东西。

【问题讨论】:

    标签: node.js shell npm


    【解决方案1】:

    如果一切都失败了,您可以使用jq 提取您需要的内容:

    npm ll --json | jq -r 'recurse(.dependencies[]) | [.name, .version, .repository.url?] | @csv'

    示例输出:

    "express","4.16.4","git+https://github.com/expressjs/express.git"
    "accepts","1.3.5","git+https://github.com/jshttp/accepts.git"
    "mime-types","2.1.22","git+https://github.com/jshttp/mime-types.git"
    "mime-db","1.38.0","git+https://github.com/jshttp/mime-db.git"
    "negotiator","0.6.1","git+https://github.com/jshttp/negotiator.git"
    

    【讨论】:

    • 感谢这真的帮助了我。只是为了完成更多你的答案,我添加了这个以在我的 package.json 中创建一个自定义脚本并删除重复的行。 "get-licenses": "npm ll --json 2>/dev/null| jq -r 'recurse(.dependencies[]) | [.name, .version, .repository.url?, .license] | @csv' | sort | uniq > *${npm_package_name}.csv*"
    【解决方案2】:

    不确定它是否完全回答了问题,但您始终可以将输出改写为更易于管理的内容 - 例如以下脚本:

    #!/bin/bash
    # list packages and remove tree structure prefix + "deduped" suffix (keep only module@version)
    data=($(npm ls | sed 's/^[┬├│─└ ]*//g' | sed 's/deduped$//g'))
    for module in ${data[*]}
    do
      # split on @ only when it separates module and version
      # (not the @ prefix on @username/module@version)
      split=($(echo $module | sed -r 's/^(.+)@/\1 /'))
      # an example display
      printf "%-30s%10s https://www.npmjs.org/packages/%s\n" ${split[0]} ${split[1]} ${split[0]}
    done
    

    【讨论】:

    • 嗨,这可能有效,但我得到了这个:./test.sh: 进入循环之前替换错误。
    • 呃,很奇怪。我从一个工作脚本中粘贴了这个,但也许你/我的浏览器和/或你/我的编辑器之间的字符编码搞砸了?我的意思是这些:┬├│─└。我所做的就是尝试npm ls,然后直接从 shell 输出中复制粘贴它们。或者您可以尝试使用排除模式:s/^[^a-zA-Z0-9@_\.-]*//g(尽管这更不可预测,因为我不确定 NPM 模块名称中允许或不允许作为起始字符的字符)。
    猜你喜欢
    • 2022-01-21
    • 2019-08-19
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多