简体   繁体   English

npm 脚本删除目录中的所有文件,除了具有某些扩展名的文件(mac os)

[英]npm script to remove all files in directory except files with certain extensions (mac os)

I know there are a lot of similar questions on here but I haven't found the answer I'm searching for.我知道这里有很多类似的问题,但我还没有找到我正在寻找的答案。 Here is the command I am trying to use in a npm script.这是我尝试在 npm 脚本中使用的命令。

rm -rf public/!(*.md)

I have enabled extended globing by using the command shopt -s extglob and the command runs fine in the terminal.我通过使用命令shopt -s extglob启用了扩展全局连接,并且该命令在终端中运行良好。 The issue I have is that it doesn't run when I use the same command as an npm script.我遇到的问题是,当我使用与 npm 脚本相同的命令时,它不会运行。

The Script剧本

"scripts": {
   // ...
   "clean:public": "rm -rf public/!(*.md)",
   //...
}

The error I get in my terminal when running the script运行脚本时我在终端中遇到的错误

> rm -rf public/!(*.md)

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `rm -rf public/**/!(*.md)'

Possible solutions that have failed...失败的可能解决方案...

  1. Quoting the glob rm -rf 'public/!(*.md)'引用 glob rm -rf 'public/!(*.md)'
    • This got rid of the error but the script doesn't remove the files.这消除了错误,但脚本不会删除文件。
  2. I also tried quoting the glob with escaped double quotes and had the same result.我还尝试用转义的双引号引用 glob 并得到相同的结果。
  3. I tried escaping the parentheses and got a json parse error.我尝试转义括号并收到 json 解析错误。

What I think the problem is...我觉得问题是...

I just learned that npm run scripts don't use the shell of the user that runs the command.我刚刚了解到 npm run 脚本不使用运行命令的用户的 shell。 Im not sure how to solve this except by maybe installing additional npm packages?我不知道如何解决这个问题,除非安装额外的 npm 包? If that's the case, which packages should I install?如果是这种情况,我应该安装哪些软件包?

Run your script under bash.在 bash 下运行你的脚本。 As you see from the error message, ie sh: , this is not bash.正如您从错误消息中看到的,即sh: ,这不是 bash。 If the script has to be run under sh, sh-wrapper script.如果脚本必须在 sh 下运行,请使用 sh-wrapper 脚本。 For instance, if your current bash script (containing the rm or what ever you need to do) is foo.sh , write the command as:例如,如果您当前的 bash 脚本(包含rm或您需要做的任何事情)是foo.sh ,请将命令写为:

"clean:public": "bash /path/to/your/script/foo.sh"

Ran into the same issue and got it working with a recommendation of the del-cli package.遇到了同样的问题,并在del-cli包的推荐下让它工作。

npm install --save-dev del-cli

And then in package.json然后在 package.json

"clean:public": "del public/** '!public/*.md'"

For those curious, del can be used outside the current directory with the --force flag对于那些好奇的人,可以使用--force标志在当前目录之外使用del

"clean:public": "del ../some-upper-dir/** '!../some-upper-dir/*.md' -f"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Glob 和 Node.js 选择指定目录及以下特定扩展名的所有文件 - Selecting all files of certain extensions in specified directory and below with Glob & Node.js 简单的 nodemon.json 配置以查看除某些文件之外的所有文件 - Simple nodemon.json config to watch all except for certain files 除了单个文件之外,您能否清除目录中的所有文件? - Can you gulp clean all files in a directory except for a single file? 如何将除正在调试的文件之外的所有脚本文件黑箱化? - How to black box all the script files except the file you are debugging? npm使用gulp将静态文件连接到目录中的所有文件 - npm concat a static file to all files in directory using gulp 编写函数以在退出目录后从目录中删除所有文件 - Writing a function to remove all files from a directory upon exiting the directory 删除所有文件和文件夹,除了 - Delete all files and folder except 在 nodejs 中复制除 .map 文件之外的所有文件 - Copy all files except .map files in nodejs Globs和NPM Minimatch:递归匹配所有文件和目录(特定目录除外) - Globs & NPM Minimatch: Match all files and directories recursively except for specific directories 如何使用node js删除目录中的所有旧文件 - How to remove all old files in directory using node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM