2

I need to write a concat script using grunt. here is my boilerplate:

___js
|____dist
| |____vents
| | |____carousel.js
| | |____compare.js
| | |____style.js
|____src
| |____events
| | |____carousel.js
| | |____compare.js
| | |____styles.js
| |____handlers
| | |____carousel.js
| | |____compare.js
| | |____style.js

How can I tell concat task, to concat the files that have the same name in events and handlers folder and put each individual concatenated pair in the dist/vents directory?

Iman Mohamadi
  • 6,552
  • 3
  • 34
  • 33
  • 1
    possible duplicate of [how to write advanced concat grunt script that find matches in seperate folders?](http://stackoverflow.com/questions/20082257/how-to-write-advanced-concat-grunt-script-that-find-matches-in-seperate-folders) – steveax Nov 20 '13 at 05:43
  • I need this too, some news? – borkie Oct 16 '14 at 21:47

1 Answers1

0

I've had similar problem: I wanted my build to fail if there are files with the same file names detected in given path pattern. I have solved it by writing custom task. You could use grunt.file.expand or grunt.file.recurse GruntAPI

Maybe this will help you (this is coffeescript rather than js).

  grunt.registerMultiTask "noduplicates", "Detects duplicated filenames", () ->
    path = require('path')

    dupFilenamesCounted = {}
    haveDuplicates = false

    options =
      cwd: this.data.cwd

    grunt.file.expand(options, this.data.src).forEach (filepath) ->
      filepathParts = filepath.split(path.sep)
      filename = filepathParts.slice(-1).join(path.sep)

      unless dupFilenamesCounted[filename] is undefined
        dupFilenamesCounted[filename].counter++
        dupFilenamesCounted[filename].filepaths.push(filepath)
      else
        dupFilenamesCounted[filename] = { counter: 0, filepaths: [ filepath ] }

    for filename of dupFilenamesCounted
      if dupFilenamesCounted[filename].counter > 0
        grunt.log.error "Filename: " + filename + ' has ' + dupFilenamesCounted[filename].counter + ' duplicates: ' + dupFilenamesCounted[filename].filepaths
        haveDuplicates = true

    # Fail by returning false if this task had errors
    return false if haveDuplicates

Then you define your task:

noduplicates:
  images:
    cwd: '<%= pkg.src %>'
    src: [ 'static/**/*.{gif,png,jpg,jpeg}' ]
b1r3k
  • 802
  • 8
  • 15