How can I access the Grunt config property site
to read the project.json
file at the path specified by the config property value?
grunt.registerTask('build', function(target) {
grunt.config('site', target);
grunt.task.run('foo:dist', 'bar:dist');
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
site: grunt.file.readJSON('./sites/' + grunt.config('site') + '/project.json')
});
grunt-cli:
grunt build:sitename
>> Error: Unable to read "./sites/undefined/project.json"
Using an example from the docs, I also tried this:
grunt.registerTask('global', 'site', function(prop, value) {
global[prop] = val;
});
grunt.registerTask('build', ['foo:dist', 'bar:dist']);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
site: grunt.file.readJSON('./sites/' + global.site + '/project.json')
});
grunt-cli:
grunt global:site:sitename
>> Error: Unable to read "./sites/undefined/project.json"
Update:
Using the @FelixKling answer as a guide, I've made some progress:
grunt.registerTask('build', function(target) {
grunt.config.set('target', target);
grunt.config.set('site', grunt.file.readJSON('./sites/' + grunt.config.get('target') + '/project.json'));
grunt.task.run('watch');
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
site: grunt.config.get('site'),
watch: {
sass: {
files: ['<%= site.dev %>/scss/*.scss'],
tasks: ['sass:dist']
}
},
sass: {
dist: {
files: {
'<%= site.dist %>/style.css': '<%= site.dev %>/scss/style.scss'
}
}
},
});
Now I'm able to read in the project.json
file successfully, and (somehow) it's even able to recognize when edits are made to watched files. But for some reason when it runs the sass:dist
task, I get this error: Warning: An error occurred while processing a template (Cannot read property 'dev' of undefined).
I'm not clear on how the watch
task is able to get the correct value for site
, but more importantly, I need to figure out a way to get that same value to the sass
task.