简体   繁体   English

在Windows上的npm脚本中使用通配符

[英]Using wildcards in npm scripts on windows

I am trying to lint all my javascript files using jshint with an npm script command. 我试图使用jshint和npm脚本命令来lint我所有的javascript文件。

I am running on windows and no matter what wildcard I specify I can't seem to lint more than one file. 我在Windows上运行,无论我指定哪个通配符,我似乎都不能lint多个文件。

Referencing a specific file works: 引用特定文件有效:

"scripts": {
    "lint": "jshint app/main.js"
}

But all of the following results in errors: 但以下所有结果都会导致错误:

"scripts": {
    // results in Can't open app/**/*.js'
    "lint1": "jshint app/**/*.js",

    // results in Can't open app/*.js'
    "lint2": "jshint app/*.js",

    // results in Can't open app/**.js'
    "lint3": "jshint app/**.js",
}

Although you can't use the wildcards when running jshint as a script task in npm on windows, you can work around it. 虽然在Windows上的npm中将jshint作为脚本任务运行时无法使用通配符,但您可以解决它。 By default, if jshint is passed a directory, it will search that directory recursively. 默认情况下,如果jshint传递给目录,它将以递归方式搜索该目录。 So in your case you could simply do: 所以在你的情况下,你可以简单地做:

"script": {
  "lint": "jshint app"
}

or even 甚至

"script": {
  "lint": "jshint ."
}

This will result in all files - including those in node_modules being linted - which is probably not what you want. 这将导致所有文件 - 包括node_modules中的文件被删除 - 这可能不是您想要的。 The easiest way to get round that is to have a file called .jshintignore in the root of your project containing the folders and scripts you don't want linted: 最简单的方法是在项目的根目录中包含一个名为.jshintignore的文件,其中包含您不想要的文件夹和脚本:

node_modules/
build/
dir/another_unlinted_script.js

This is then a cross-platform solution for jshint as a script task in npm. 这是jshint作为npm脚本任务的跨平台解决方案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM