[英]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 脚本相同的命令时,它不会运行。
"scripts": {
// ...
"clean:public": "rm -rf public/!(*.md)",
//...
}
> rm -rf public/!(*.md)
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `rm -rf public/**/!(*.md)'
rm -rf 'public/!(*.md)'
rm -rf 'public/!(*.md)'
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.