【问题标题】:How to detect same file names in different folders while using Grunt?使用 Grunt 时如何检测不同文件夹中的相同文件名?
【发布时间】:2013-11-20 04:00:48
【问题描述】:

我需要使用 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 目录中?

【问题讨论】:

标签: javascript node.js gruntjs grunt-contrib-concat


【解决方案1】:

我遇到过类似的问题:如果在给定的路径模式中检测到具有相同文件名的文件,我希望我的构建失败。我已经通过编写自定义任务来解决它。你可以使用 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}' ]

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 2014-04-06
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 2020-07-17
    • 2018-12-28
    相关资源
    最近更新 更多