Search code examples
node.jssassgruntjsglob

gruntfile files named patterns, possible?


I'm playing around with Ghost, and I'd like to make the gruntfile compile the sass files from my theme.

So I started by modifying the sass task:

...
sass: {
    admin: {
        files: {
            '<%= paths.adminAssets %>/css/screen.css': '<%= paths.adminAssets %>/sass/screen.scss'
        }
    },
    themes: {
        files:{
            'content/themes/**/css/ie.css': 'content/themes/**/src/sass/ie.sass',
            'content/themes/**/css/print.css': 'content/themes/**/src/sass/print.sass',
            'content/themes/**/css/screen.css': 'content/themes/**/src/sass/screen.sass'
        }
    }
}
...

I realised that I could simplfy this to :

...
sass: {
    admin: {
        files: {
            '<%= paths.adminAssets %>/css/screen.css': '<%= paths.adminAssets %>/sass/screen.scss'
        }
    },
    themes: {
        files:{
            'content/themes/**/css/*.css': 'content/themes/**/src/sass/*.sass',
        }
    }
}
...

But then I was thinking, why isn't it replacing the stars in the destination with what it matches from the source?

Ends up it was just creating the following:

$ ls -al ./content/themes/
total 0
drwxrwxr-x 1 zenobius zenobius  50 Nov 18 02:10 .
drwxrwxr-x 1 zenobius zenobius  46 Nov 15 11:02 ..
drwxrwxr-x 1 zenobius zenobius   6 Nov 18 02:10 **         <----- sigh
drwxrwxr-x 1 zenobius zenobius 128 Nov 15 11:02 casper
drwxrwxr-x 1 zenobius zenobius 250 Nov 18 00:08 crycilium

I guess my question is really:

  • can i use some kind of regex named patterns
  • could I use a function in the files option to process the output name as the destination?

Solution

  • So the solution was to make use of grunt.file.expandMapping, (thanks to : https://stackoverflow.com/a/16672303/454615):

                ...
                themes: {
                    files: grunt.file.expandMapping([
                        "content/themes/**/src/**/*.sass",
                        "!content/themes/**/src/**/_*.sass",
                      ], '', {
                      expand: true,
                      ext: '.css',
                      rename: function(base, src) {
                        grunt.log.write(base + " " + src);
                        return src.replace('/src/', '/../'); // or some variation
                      }
                    })
                }
                ...