简体   繁体   中英

Grunt ngconstant: generation from multiple exchangeable files

In an angular project started with Yeoman , we have json files (dev, test, dist...) with the constants needed for each environment to work so that the grunt serve task is defined like:

grunt.registerTask('serve', function(environment) {
  grunt.task.run([
    ...
    'ngconstant:'+environment,
    ...
  ]);
});

and then in ngconstant we have:

ngconstant: {
  options: {
    name: 'environment',
    dest: ...
  },
  dev: {
    constants: {
      'ENV': grunt.file.readJSON('environments/dev.json')
    }
  },
  test: {
    constants: {
      'ENV': grunt.file.readJSON('environments/test.json')
    }
  },
  ...
  }
}

so that it can be run with grunt serve:dev , grunt serve:test ...

Now we need to also modify certain aspects of the application depending on a second criteria (let's call it user target), in a way that it could be built like grunt serve:dev:target1 , grunt serve:test:target2 ...

Can this be done with ngconstant? the only similar idea I had so far was to define targets like dev-target1 , dev-target2 , test-target1 ... where each set ENV to one file and TARGET to the other, but that's not scalable (we're working with just two targets but we expect more coming soon) and just plain ugly.

Ok, found a way to do this. ngconstant in the Gruntfile:

ngconstant: {
  dev: {
    options: {
      name: 'environment',
      dest: '<%= yeoman.app %>/js/modules/environment/config/environment.js'
    },
    constants: {
      'ENV': grunt.file.readJSON('environments/dev.json')
    }
  },
  test: {
    options: {
      name: 'environment',
      dest: '<%= yeoman.app %>/js/modules/environment/config/environment.js'
    },
    constants: {
      'ENV': grunt.file.readJSON('environments/test.json')
    }
  },
  prod: {
    options: {
      name: 'environment',
      dest: '<%= yeoman.app %>/js/modules/environment/config/environment.js'
    },
    constants: {
      'ENV': grunt.file.readJSON('environments/prod.json')
    }
  },
  local: {
    options: {
      name: 'environment',
      dest: '<%= yeoman.app %>/js/modules/environment/config/environment.js'
    },
    constants: {
      'ENV': grunt.file.readJSON('environments/local.json')
    }
  },
  superior: {
    options: {
      name: 'target',
      dest: '<%= yeoman.app %>/js/modules/target/config/target.js'
    },
    constants: {
      'TGT': grunt.file.readJSON('targets/superior.json')
    }
  },
  profesional: {
    options: {
      name: 'target',
      dest: '<%= yeoman.app %>/js/modules/target/config/target.js'
    },
    constants: {
      'TGT': grunt.file.readJSON('targets/profesional.json')
    }
  },
}

It's messy because everything is bundled together in the same module, but having the serve task defined like this:

  grunt.registerTask('serve', function(environment, target) {
grunt.task.run([
  ...
  'ngconstant:'+environment,
  'ngconstant:'+target,
  ...
]);

});

and calling it like grunt serve:dev:superior will first generate the environment/config/environment.js file with the "dev" ENV constants and then the target/config/target.js file with the "superior" TGT constants as defined in their respective files.

I've tried setting the same destination file for both types but the file is overwritten instead of constants being appended or anything similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM