mlegenhausen/grunt-ng-constant

DOCS: The example doesn't work well for generated constant value files

snekse opened this issue · 3 comments

The docs illustrate using grunt.file.readJSON, but that will get called when running clean

Example: gruntfile.js

//...
grunt.initConfig ({
  "merge-json": {
    default: {
      src: ['client/src/**/*.validation.json'],
      dest: 'client/dist/assets/generated/app.validation.json'
    }
  },
  ngconstant: {
    options: {
      name: 'validation.constraints', //angular module name to be created
      dest: 'client/dist/assets/generated/validation.constraints.js',
      constants: {
        VALIDATION_CONSTRAINTS_CONFIG: grunt.file.readJSON('client/dist/assets/generated/app.validation.json')
      },
    },
    build: {}
  }
});

//....
//Multiple calls to clean to illustrate the 'Unable to read' ENOENT error
grunt.registerTask('default', ['clean','merge-json', 'ngconstant:build', 'clean', 'clean']);

I'm new to grunt and I'm unsure how to get around this.

You can use the lazy evaluation which will first evaluate the grunt.file.readJSON when the plugin runs, which is handy for generated configuration files. Which is already described in the documentation, but maybe I can improve this.

grunt.initConfig ({
  "merge-json": {
    default: {
      src: ['client/src/**/*.validation.json'],
      dest: 'client/dist/assets/generated/app.validation.json'
    }
  },
  ngconstant: {
    options: {
      name: 'validation.constraints', //angular module name to be created
      dest: 'client/dist/assets/generated/validation.constraints.js',
      constants: function () {
        return {
          VALIDATION_CONSTRAINTS_CONFIG: grunt.file.readJSON('client/dist/assets/generated/app.validation.json')
        };
      },
    },
    build: {}
  }
});

//....
//Multiple calls to clean to illustrate the 'Unable to read' ENOENT error
grunt.registerTask('default', ['clean','merge-json', 'ngconstant:build', 'clean', 'clean']);

Yeah, I didn't quite get that from the current documentation. Your example is better than the work around I came up with. Thanks for the response.

    grunt.registerTask('default', ['clean','merge-json', 'genValidationJs', 'clean', 'clean']);
    grunt.registerTask('genValidationJs', 'Make module', function() {
        //Required to be declared like this so grunt.file.readJSON is evaluated only when task is run
        grunt.config('constantVal', grunt.file.readJSON('client/dist/assets/generated/app.validation.json'));
        grunt.task.run(['ngconstant:build']);
    });