Search code examples
gruntjsgrunt-contrib-watchtslintgrunt-ts

grunt-tslint is not working at the watch task


I'm working on an angular2 project and have included grunt-tslint to improve my code. The grunt task "tslint" and default grunt task is workink fine but when I include it to the watch task it's not linting. I tried to delete every other watch task but also not working.

This are my versions:
grunt: v1.0.1
grunt-contrib-watch: 1.0.0
grunt-ts: v6.0.0-beta.3
grunt-tslint: 4.0.0

And the grunt tasks:

module.exports = function (grunt) {

grunt.initConfig({

    pkg: grunt.file.readJSON('package.json'),
    tslint: {
        options: {
            configuration: "tslint.json",
            force: false,
            fix: false
        },
        files: {
            src: ['app/**/*.ts']
        }
    },
    ts: {
        default : {
            src: ['app/**/*.ts'],
            outDir: 'outDir/app/',
            tsconfig: './tsconfig.json'
        }
    },

watch: {
        tslint: {
            files: ['app/**/*.ts'],
            task: ['tslint']
        },
        ts: {
            files: ['app/**/*.ts'],
            tasks: ['ts']
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-ts');
grunt.loadNpmTasks('grunt-tslint');

grunt.registerTask('default', [
    'ts', 'tslint'
]);

grunt.registerTask('validate:ts', ['tslint']);

};


Solution


  • UPDATE:

    Try replacing your watch task with the following instead:

    watch: {
        ts: {
            files: ['app/**/*.ts'],
            tasks: ['tslint', 'ts']
        }
    }
    

    Note:

    1. The target named tslint has been deleted from your watch task.
    2. The call to run the tslint task has now been added to the tasks array of the ts target.

    Now when you run $ grunt watch the files will be:

    1. Linted using tslint according to the rules defined in tslint.json.
    2. If they PASS that check they will be transpiled with grunt-ts.
    3. If they FAIL that check no files will be transpiled, and any errors will be reported to the CLI.

    Hope this helps!


    PREVIOUS ANSWER:

    One potential solution is to try reverting to the last stable version of the grunt-ts package.

    To do this:

    1. cd to your project directory.

    2. Uninstall the current version of grunt-ts:

    $ npm uninstall grunt-ts --save-dev

    1. Then install the last stable release of grunt-ts (which appears to be 5.5.1)

    $ npm install [email protected] --save-dev

    NOTE: I recommend trying the above with a copy of your project directory to see whether its successful !