I have a directory structure like the following:
dist/
static/
static/keep1
static/keep2
static/versions/1
static/versions/2
static/versions/3
I want to copy everything from static into dist, with the exception of the versions directory. I want to sort the directory and take the most recent version (e.g. version 3).
If I simply do {expand: true, cwd:'static', src: '**', dest: 'dist/', dot: 'true'}, I'll get old, unnecessary versions that bloat my dist/ directory
Is there a way to programmatically select the most recent version so I don't have to manually update my gruntfile config every time I update static/versions/?
I'm thinking I could potentially node-glob or minimatch will work for me, or maybe I could use grunt-execute or grunt-run (both of which might get ugly). I was hoping there would just be a way to do it with the copy.
This can be achieved without additional grunt plug-ins. However, it is necessary to programmatically find the most recent version stored in the
./versions/directory, and this must be computed in advance of running thecopyTask. grunt-contrib-copy has no built-in functionality to determine that.Once the most recent version directory has been determined simply utilize a couple of Targets in your
copyTask.The following gist demonstrates how this can be achieved:
Note: This solution assumes the most recent version is the highest numbered directory, and is not determined via created or modified dates in any way.
Gruntfile,js
Results
Running
$ gruntusing theGruntfile.jsabove, (with your example directory structure), will result in adistdirectory structured as follows: