gpt4 book ai didi

node.js - 如何快速检查是否运行 npm install Node

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:52 27 4
gpt4 key购买 nike

下一个脚本会考虑所有文件夹并安装依赖项

var fs            = require( "fs" ),
path = require( "path" ),
child_process = require( "child_process" );

var rootPath = "./";

var dirs = fs.readdirSync( rootPath )
.filter( function( dir ) {
return fs.statSync( path.join( rootPath, dir )).isDirectory();
});

var install = function()
{
if ( dirs.length === 0 )
return;

var dir = dirs.shift();
console.log( "installing dependencies for : '" + dir + "'" );

child_process.exec( "npm prune --production | npm install", {
cwd: rootPath + dir
}, install );
};
install();

仅当文件夹中存在 package.json 时,如何运行 npm install 命令?

最佳答案

尝试这个命令:

ls | grep package.json && (npm prune --production | npm install)

我假设您正在 Linux 中运行它。

理论上,如果我没记错的话,命令ls的输出将通过管道传输到grep命令,并且只有当grep命令找到结果时,才会执行命令(npm prune --product | npm install)

在撰写本文时,我还没有对此进行测试,因为我现在没有 Linux 机器来测试它,但我希望它能起作用。

更新:根据丹的评论,有效的命令是

test -f package.json && (npm prune --production | npm install)

关于node.js - 如何快速检查是否运行 npm install Node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32505491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com