【发布时间】:2019-03-04 13:07:25
【问题描述】:
我需要向我的客户提供我们项目的所有依赖项(库名称 + github / npm url)。我可以在每个项目中使用npm ll 轻松做到这一点。但这给了我一个树形结构的输出。有没有办法将此输出更改为表格?
我已经阅读了 npm ll 命令的文档,但没有找到任何东西。
【问题讨论】:
我需要向我的客户提供我们项目的所有依赖项(库名称 + github / npm url)。我可以在每个项目中使用npm ll 轻松做到这一点。但这给了我一个树形结构的输出。有没有办法将此输出更改为表格?
我已经阅读了 npm ll 命令的文档,但没有找到任何东西。
【问题讨论】:
如果一切都失败了,您可以使用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"
【讨论】:
"get-licenses": "npm ll --json 2>/dev/null| jq -r 'recurse(.dependencies[]) | [.name, .version, .repository.url?, .license] | @csv' | sort | uniq > *${npm_package_name}.csv*"
不确定它是否完全回答了问题,但您始终可以将输出改写为更易于管理的内容 - 例如以下脚本:
#!/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
【讨论】:
┬├│─└。我所做的就是尝试npm ls,然后直接从 shell 输出中复制粘贴它们。或者您可以尝试使用排除模式:s/^[^a-zA-Z0-9@_\.-]*//g(尽管这更不可预测,因为我不确定 NPM 模块名称中允许或不允许作为起始字符的字符)。