【问题标题】:using execution directory as Gruntfile's file base使用执行目录作为 Gruntfile 的文件库
【发布时间】:2016-02-10 01:58:05
【问题描述】:

我正在尝试使用 Grunt 清理一个大型项目。对于这个特定示例,我正在尝试运行单元测试,并且希望对当前grunt 执行目录下的路径(即pwd 的结果)执行此操作。

我想要一个位于项目根目录的 Gruntfile。我知道grunt 会从任何子目录中毫无问题地找到并执行它。如果我将我的测试运行器选项定义为查看"test/",它只会在{project root/}test/ 下运行测试。有没有办法告诉项目级 Gruntfile 使其路径(全部或部分)相对于执行位置?

注意事项:

  • 我不需要被告知“你为什么要这样做?Grunt 应该管理你的整个项目!”这是一次改造,在一切正常之前,我希望/需要它零碎。
  • 重申一下,"**/test/" 不是答案,因为我只想要当前 grunt 执行目录下的测试。
  • --base 也不起作用,因为 Grunt 会在基准位置查找 Node 包。
  • 对于类似的情况,我使用了一个共享配置 JSON 文件,该文件是通过 grunt.config.merge(grunt.file.readJSON("../grunt-shared.json")); 导入的。但是,这需要子文件夹中的 Gruntfiles,以及共享文件的硬编码路径(例如,../),这似乎很脆弱。
  • 我可以编写代码来做一些目录爬取和路径构建,但我想把它作为最后的手段。

【问题讨论】:

    标签: gruntjs minimatch


    【解决方案1】:

    这是我想出的解决方案(H/T 至 @firstdoit,https://stackoverflow.com/a/28763634/356016):

    • 在项目的根目录中创建一个共享的 JavaScript 文件以集中 Grunt 行为。
    • 每个“子项目”目录都有一个最小的样板文件Gruntfile.js
    • 手动调整共享文件中 Grunt 的文件库以从一个 node_modules 源加载。

    Gruntfile.js

    /**
     * This Gruntfile is largely just to establish a file path base for this
     * directory. In all but the rarest cases, it can simply allow Grunt to
     * "pass-through" to the project-level Gruntfile.
     */
    module.exports = function (grunt)
    {
        var PATH_TO_ROOT = "../";
    
        // If customization is needed...
        // grunt.config.init({});
        // grunt.config.merge(require(PATH_TO_ROOT + "grunt-shared.js")(grunt));
    
        // Otherwise, just use the root...
        grunt.config.init(require(PATH_TO_ROOT + "grunt-shared.js")(grunt));
    };
    

    PATH_TO_ROOT 使用var 在很大程度上是不必要的,但它为跨子项目使用此样板文件提供了一个单一的焦点。

    {ROOT}/grunt-shared.js

    module.exports = function (grunt)
    {
        // load needed Node modules
        var path = require("path");
    
        var processBase = process.cwd();
        var rootBase    = path.dirname(module.filename);
    
        /*
         * Normally, load-grunt-config also provides the functionality
         * of load-grunt-tasks. However, because of our "root modules"
         * setup, we need the task configurations to happen at a different
         * file base than task (module) loading. We could pass the base
         * for tasks to each task, but it is better to centralize it here.
         *
         * Set the base to the project root, load the modules/tasks, then
         * reset the base and process the configurations.
         *
         * WARNING: This is only compatible with the default base. An explicit base will be lost.
         */
        grunt.file.setBase(rootBase);
        require("load-grunt-tasks")(grunt);
    
        // Restore file path base.
        grunt.file.setBase(processBase);
    
        // Read every config file in {rootBase}/grunt/ into Grunt's config.
        var configObj = require("load-grunt-config")(grunt, {
            configPath: path.join(rootBase, "grunt"),
            loadGruntTasks: false
        });
    
        return configObj;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多