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.json
whilst copying the items from your location, using the options.process function.copy
task 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.js
assumes a fictitious project directory set up as follows:...Whereby the
version
property ofpackage.json
, (the one residing in thesrc
folder), 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 copyFiles
Resultant output
Running the task creates a new folder in the root
project
directory as follows:Points to note:
All the items from the
src
folder (the one shown in the Directory setup section above) are copied to the newly created folder named copiedItems.The
docs
folder 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
src
folder, I've since discovered that the resultant copied images were getting corrupted. See this issue logged in thegrunt-contrib-copy
repo for further info.The
Gruntfile.js
above now utilizes the options.noprocess as a global in thecopy
task as a safeguard from this.Image files are now copied successfully :)