Search code examples
typescriptgruntjssails.jsgrunt-ts

adding a custom grunt task to sails.js


I'm new to sails.js but I've been reading a bunch of the documentation for the last couple of days so I feel like I have a basic grasp on the platform. But I can't seem to get custom grunt tasks added and registered. I've tried a few ways and none of them seem to work. So I figured I'd keep it simple and just register a task with one of the existing register files, but I'm still not about to get this method to work.

So I've added the following Gruntfile to tasks/config/comp.js

module.exports = function(grunt) {
    grunt.config.set('comp', {
        dev: {
            options: {
                module: 'system',
                moduleResolution: 'node',
                target: 'es3',
                experimentalDecorators: true,
                emitDecoratorMetadata: true,
                noImplicitAny: false
            },
            files: [{
                expand: true,
                cwd: 'assets/js/',
                src: [ '**/*.ts' ],
                dest: '.tmp/public/js',
                ext: '.js'
            }]
        }
    });

    grunt.loadNpmTasks('grunt-ts');
};

And I added the following line to tasks/register/compileAssets.js

/**
 * `compileAssets`
 *
 * ---------------------------------------------------------------
 *
 * This Grunt tasklist is not designed to be used directly-- rather
 * it is a helper called by the `default`, `prod`, `build`, and
 * `buildProd` tasklists.
 *
 * For more information see:
 *   http://sailsjs.org/documentation/anatomy/my-app/tasks/register/compile-assets-js
 *
 */
module.exports = function(grunt) {
  grunt.registerTask('compileAssets', [
    'clean:dev',
    'jst:dev',
    'less:dev',
    'copy:dev',
    'coffee:dev',
    'comp:dev'
  ]);
};

However whenever I run sails lift I get the following error

info: Starting app...

info: info: .-..-. info: info: Sails <| .-..-. info: v0.12.13 |\ info: /|.\ info: / || \ info: ,' |' \ info: .-'.-==|/_--' info: --'-------' info: __---___--___---___--___---___--___ info: ____---___--___---___--___---___--___-__ info: info: Server lifted inC:\Users\josh\Documents\PGW` info: To see your app, visit http://localhost:1337 info: To shut down Sails, press + C at any time.

debug: ------------------------------------------------------- debug: :: Thu Jun 08 2017 16:32:01 GMT-0600 (Mountain Daylight Time)

debug: Environment : development debug: Port : 1337 debug: ------------------------------------------------------- error: ** Grunt :: An error occurred. **

error:

Aborted due to warnings.

Warning: Task "comp:dev" not found.

error: Looks like a Grunt error occurred-- error: Please fix it, then restart Sails to continue running tasks (e.g. watching for changes in assets) error: Or if you're stuck, check out the troubleshooting tips below.

error: Troubleshooting tips: error: error: *-> Are "grunt" and related grunt task modules installed locally? Run npm install if you're not sure. error: error: *-> You might have a malformed LESS, SASS, CoffeeScript file, etc. error: error: *-> Or maybe you don't have permissions to access the .tmp directory? error: e.g., C:\Users\josh\Documents\PGW\.tmp ? error: error: If you think this might be the case, try running: error: sudo chown -R YOUR_COMPUTER_USER_NAME C:\Users\josh\Documents\PGW.tmp

I've been banging my head against this for hours and I don't understand why sails lift won't run my task. I feel like I've followed the directions well between reading the sails docs and other stackoverflow articles. Can someone help me understand what I'm missing here?

Thanks


Solution

  • You are trying to run the comp target you have defined but grunt is finding no associated comp taskname --> grunt plugin mapping to execute it.

    Looks like you are attempting to use grunt-ts

    grunt.loadNpmTasks('grunt-ts');
    

    Which adds the ts task. Change 'comp' to ts in your target defintion:

    grunt.config.set('ts', {
    

    And update the compileAssets.js file

     grunt.registerTask('compileAssets', [
            'clean:dev',
            'jst:dev',
            'less:dev',
            'copy:dev',
            'coffee:dev',
            'ts:dev' //execute grunt-ts
          ]);
    

    This should be enough to fix. If you still have issues make sure you have grunt-ts installed:

    npm install grunt-ts
    

    If you really want to statically map a taskname to a plugin you could use something like jit-grunt to do this for you:

    require('jit-grunt')(grunt, {
          comp: 'grunt-ts', //comp now maps to grunt-ts plugins
    });