I have a grunt build that should do the following:
- Copy items from a location
- Load a package.json file from those copied files and extract a version number
- Copy items from a folder dependent on that version number
I have a task that reads the version number:
grunt.task.registerTask(
"readVersionNumber",
"Reads a version number",
function(){
var versionNumber = grunt.file.readJSON("pathToJson");
grunt.config.set("versionNumber",versionNumber);
}
);
(typed out rather than copy/paste so possibly some minor typos)
and I have a copy task with a cwd:
"cwd": "//networkPath/<%= versionNumber %>/docs"
But this is not working...
I have a very similar setup for replacing text in html / js files and looking at the grunt-text-replace.js file it uses grunt.template.process to replace the template in the string with the actual value. I suspect that for the copy task this is done before the copy task starts (before I have loaded and set the version number).
I can't find much info on how the cwd property is evaluated except that it seems to happen in the guts of grunt itself. It seems odd that core grunt functionality would not properly process templates but from what I can see it's not happening.
BTW - all this works fine if I use a template pointing to a value in my package.json file:
"cwd": "//networkPath/<%= pkg.path.to.version %>/docs"
Any help much appreciated
You could consider:
package.jsonwhilst copying the items from your location, using the options.process function.copytask target.The following gist seems to work:
Gruntfile.js
Note: This will obviously need adapting to suit your requirements. Particularly the path references!
Directory setup
The example gist in the
Gruntfile.jsassumes a fictitious project directory set up as follows:...Whereby the
versionproperty ofpackage.json, (the one residing in thesrcfolder), is set to:...and the folder named networkPath is what I'm assuming to be where your docs are stored.
Running the task
Execute the following command via the CLI:
$ grunt copyFilesResultant output
Running the task creates a new folder in the root
projectdirectory as follows:Points to note:
All the items from the
srcfolder (the one shown in the Directory setup section above) are copied to the newly created folder named copiedItems.The
docsfolder and its contents from the folder named 1.1.1 (the one shown in the Directory setup section above) are copied to the newly created folder named copiedItems. This particular folder is being copied because its parent directory name matches the version property of package.json being copied.I hope this helps!
Update:
When adding some binary images to the
srcfolder, I've since discovered that the resultant copied images were getting corrupted. See this issue logged in thegrunt-contrib-copyrepo for further info.The
Gruntfile.jsabove now utilizes the options.noprocess as a global in thecopytask as a safeguard from this.Image files are now copied successfully :)