将自定义 grunt 任务添加到 sails.js

adding a custom grunt task to sails.js

我是 sails.js 的新手,但过去几天我一直在阅读大量文档,所以我觉得我对该平台有了基本的了解。但我似乎无法添加和注册自定义 g运行t 任务。我尝试了几种方法,其中 none 似乎有效。所以我想我会保持简单,只用一个现有的寄存器文件注册一个任务,但我仍然不打算让这个方法起作用。

所以我将以下 G运行tfile 添加到 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');
};

然后我将以下行添加到 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'
  ]);
};

然而每当我 运行 sails lift 我得到以下错误

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

我已经为此苦苦思索了几个小时,我不明白为什么 sails lift 不会 运行 我的任务。我觉得我在阅读 sails 文档和其他 Whosebug 文章之间很好地遵循了这些说明。有人可以帮助我了解我在这里缺少什么吗?

谢谢

您正在尝试 运行 您定义的 comp 目标,但是 g运行t 没有找到关联的 comp taskname --> g运行t plugin mapping to execute it.

您似乎在尝试使用 grunt-ts

grunt.loadNpmTasks('grunt-ts');

添加 ts 任务。在目标定义中将 'comp' 更改为 ts

grunt.config.set('ts', {

并更新 compileAssets.js 文件

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

这应该足以修复。如果仍有问题,请确保已安装 grunt-ts

npm install grunt-ts

如果你真的想将一个任务名称静态映射到一个插件,你可以使用类似 jit-g运行t 的东西来为你做这个:

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