文章目录
- 前言
- 一、linux
- 二、windows
前言
最近公司在重构项目,使用的monorepo,这就导致多个项目有多个node_modules。所以在主项目的package.json中写一个清除所有项目的node_modules。第一次研究命令行的代码,记录一下。但我感觉我写的不太好,执行效率低,有熟悉命令行的大佬可以帮我改一下,先行道谢啦
一、linux
// 就删除子项目下的node_modules以及主项目下的node_modules
"clearLin": "rm -rf **/node_modules && rm -rf node_modules"
二、windows
// windows 不识别rm; rd 不识别*。在诸多限制之下,就写了一个for循环。本来主项目的node_modules也是写在for循环里面的,但是感觉有点慢,不知道是命令写的不对还是for循环遍历的慢,写在外面就快了些,就这样写把
"clearWin": "rd /s /q node_modules && for /d %i in (*) do @if exist %i\\node_modules rd /s /q %i\\node_modules""clearWin1": "for /d %i in (*) do ((@if %i == node_modules rd /s /q %i) && (@if exist %i\\node_modules rd /s /q %i\\node_modules))"// /d 是在本文件夹中遍历所有子文件夹,%i是指子子文件夹。do代表着找到子文件夹之后做的事情。第一个if是子文件夹的名称叫xxx就删了它,第二个if是子文件夹下面的文件夹有叫xxx的就删了它// /r是深度遍历,本来想用/r加*来写的,但node_modules里面的项目也有node_modules,就遍历太深啦,只想遍历两层