简体   繁体   English

如何让tslint监视特定文件夹中的更改?

[英]How do I get tslint to watch for changes in a specific folder?

I am using webpack 2, and it will tell me if there are compile issues with my typescript code. 我正在使用webpack 2,它会告诉我我的打字稿代码是否存在编译问题。 However, I have not figured out a way to run tslint through it and have it run with every change detected by webpack when its running in dev-server mode. 但是,我还没有找到一种方法来运行tslint并让它在webpack运行在dev-server模式时检测到的每个更改都运行。

I have tried getting tslint-loader working, but for each file in my project it simply tells me: 我试过让tslint-loader工作,但对于我项目中的每个文件,它只是告诉我:

/src/main.tsNo valid rules have been specified /src/main.tsNo指定了有效规则

I am using it as such: 我正在使用它:

rules: [
  {
    test: /\.ts$/,
    enforce: 'pre',
    loader: 'tslint-loader',
    options: {
      configuration: {
        configFile: true // I have also tried setting this to "tslint.json"
      }
    }
  },
  ... more loaders...

Still no joy. 仍然没有快乐。

It there a way to either: 有一种方法可以:

  1. Have the tslint-loader I'm using inform me of lint errors in webpack-dev-server mode each time I make a change? 我每次进行更改时,我正在使用的tslint-loader告诉我webpack-dev-server模式中的lint错误吗?
  2. Simply run tslint from the command line and have it continually "watch" the files in my project? 只需从命令行运行tslint并让它不断“监视”我项目中的文件? I'm looking for something like tslint ./src/**/*.ts -t --force , but with an additional --watch flag that doesn't exist according to the tslint docs . 我正在寻找像tslint ./src/**/*.ts -t --force这样的东西,但是根据tslint文档还有一个额外的--watch标志。

I would prefer not to use my editor (such as VS Code), as not everyone on my team uses it. 我不想使用我的编辑器 (例如VS Code),因为我团队中的每个人都不会使用它。 I would prefer that the solution is contained either in the webpack config or the package.json scripts. 我希望解决方案包含在webpack配置或package.json脚本中。

Thank you! 谢谢!

As far as a script you can run from the command line is concerned, you can try using npm-watch: https://www.npmjs.com/package/npm-watch . 就您可以从命令行运行的脚本而言,您可以尝试使用npm-watch: https ://www.npmjs.com/package/npm-watch。

I've used it to successfully do what you're talking about. 我用它来成功地做你正在谈论的事情。 Here's what I did: 这是我做的:

Installed npm-watch to my project: 安装npm-watch到我的项目:

$ npm install npm-watch --save-dev

Added the following to my package.json file: 在我的package.json文件中添加了以下内容:

"watch": {
    "lint": "src/main.ts"
},
"scripts": {
    "lint": "tslint src/**/*.ts -t verbose",
    "watch": "npm-watch"
},

I figure npm-watch is a good tool for giving watch functionality to tools that don't have it, like tslint. 我认为npm-watch是一个很好的工具,可以为没有它的工具提供手表功能,比如tslint。

Update: 更新:

Also, if you don't want to add a "watch" section to your package.json file, I've actually just discovered a new tool I like even better called chokidar. 另外,如果您不想在package.json文件中添加“监视”部分,我实际上刚刚发现了一个我喜欢的新工具,甚至更好地称为chokidar。 It allows you to specify the file selectors and command you want to run all on the same line. 它允许您指定要在同一行上运行的文件选择器和命令。

Here's my updated package.json: 这是我更新的package.json:

"scripts": {
    "lint:watch": "chokidar webpack.config.* src/**/*.ts buildScripts/**/*.ts -c \"npm run lint\" --initial --verbose"
},

You basically give it one or more file selectors, and then use the '-c' parameter to specify the command you want run when any of those files are changed. 您基本上为它提供了一个或多个文件选择器,然后使用'-c'参数指定在更改任何文件时要运行的命令。

So now you can just run the command: 所以现在你可以运行命令:

$ npm run lint:watch

I like to run it with the --initial flag set, so it doesn't wait for any files to change before executing the command. 我喜欢使用--initial标志设置来运行它,因此它不会在执行命令之前等待任何文件更改。

If you are using TypeScript, then you can probably use tsc -w to watch the changes. 如果您使用的是TypeScript,那么您可以使用tsc -w来观察更改。

 "scripts": {
    "start": "tsc -w",
  }

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

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