bkbarton
bkbarton

Reputation: 349

Building a package.json and Gruntfile using Yeoman

New to Yeoman as of today. I am building a generator that will create a new package.json with data that will help build the applications Gruntfile. For (probably) unnecessary reasons, I separated out the Yeoman pieces into 2 files. Below is an excerpt from index.js...

 // index.js 
myGenerator.prototype.packagejson = function packagejson() {
    var projectName = this.projectName;
    var pkg = {
      "name": projectName,
      "version": "0.0.0",
      "dependencies": {},
      "srcDir": process.cwd(),
    };
    this.write('package.json',JSON.stringify(pkg));
};

myGenerator.prototype.gruntfile = function gruntfile(){
    this.template('Gruntfile.js','Gruntfile.js');
}
  1. This creates a package.json file and writes the JSON string to the file. projectName is a prompt asked when the template is loaded in the command line. Process.cwd() refers to the current file directory.

  2. Then it creates a Gruntfile from a template. Except below:

    //Grunt - Yeoman template
      module.exports = function(grunt){
       grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
         sync:{
           dev:{
            files:[ 
             {src:['./index.html','./app.css','./js/*.js'],dest:'m:/dev/project/'},
             {cwd:"<% pkg.srcDir %>", src:['**/*.js','**/*.css','**/*.html'], dest:'m:/dev/project/'}
            ]
           }
         }
       })
     };
    

This, in my mind should...

  1. Create a path that was generated by index.js (via process.cwd()) that was printed to the package.json file. This is read into the gruntfile via grunt.file.readJSON.
  2. Then the path reference (string) should be accessible through the object property: pkg.srcDir.

However, I only get back an empty string.

    // Gruntfile.js for new application
    cwd:"", src:['**/*.js','**/*.css','**/*.html'], dest:'m:/dev/project/'

Any obvious reason why I am not able to read in the package.json info and populate my gruntfile?

Thanks

Upvotes: 1

Views: 1979

Answers (1)

Hariadi
Hariadi

Reputation: 606

Add double percent in the opening template tag

<%% pkg.srcDir %>

Upvotes: 3

Related Questions