如何使用Grunt在不同的文件夹中检测相同的文件名?

我需要用grunt编写一个concat脚本。 这是我的样板:

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

我怎样才能知道concat任务,在事件和处理程序文件夹中连接具有相同名称的文件,并将每个单独的连接对放在dist / vents目录中?

我遇到了类似的问题:如果在给定path模式下检测到具有相同文件名的文件,我希望构build失败。 我已经通过编写自定义任务解决了它。 你可以使用grunt.file.expand或grunt.file.recurse GruntAPI

也许这会帮助你(这是coffeescript而不是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 

然后你定义你的任务:

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