Nyavro
Nyavro

Reputation: 8866

How to run tslint only on modified files

I want to configure tslint to run only on git modified files. In packages.json I have the 'lint' task defined as:

{
  "scripts": {
     "lint": "tslint \"./src/**/*.{ts,tsx}\"",
     ...
  },
  "dependencies": {
     "tslint": "4.3.1",
     ... 
  },
  ...
}

So lint task works fine but it processes all files in project by hardcoded mask. Suppose I have a javascript listModified.js which produces list of git modified files. How do I pass the string produced by js as argument to tslint?

I can't use piping here since tslint accepts argument not a pipe. I failed to use eval to form argument.

How to do it?

Upvotes: 2

Views: 1440

Answers (2)

mixel
mixel

Reputation: 25866

I suggest you to use some build system because it should have plugins that allow to check if file was modified by computing hash or by checking last modified time.

For example, using gulp with gulp-tslint and gulp-changed-in-place plugins, and jsonfile package for reading and writing JSON files:

var changedInPlace = require('gulp-changed-in-place'),
    gulpTsLint = require('gulp-tslint'),
    jsonfile = require('jsonfile');

var tsHashFile = 'ts.hash.json';
gulp.task('tslint', function (cb) {
    jsonfile.readFile(tsHashFile, function (err, obj) {
        var firstPass = false;
        if (err) {
            firstPass = true;
            obj = {};
        }
        return gulp.src(tsGlob)
            .pipe(changedInPlace({
                cache: obj,
                firstPass: firstPass
            }))
            .pipe(gulpTsLint())
            .pipe(gulpTsLint.report())
            .on('end', function (event) {
                jsonfile.writeFile(tsHashFile, obj, function () {
                    cb(event);
                });
            });
    });
});

tsHashFile is used to persist computed hashes and when you will run gulp tslint again tslint will not be executed for files which hash is not changed.

Upvotes: 1

wookieb
wookieb

Reputation: 4489

try xargs

echo "file1.ts\nfile2.ts" | xargs tslint

Obviously you can also use git status or git log to produce list of files to check

If xargs is not available in your system then try to use regular javascript API https://www.npmjs.com/package/tslint#library-1

Upvotes: 2

Related Questions