一起使用 grunt-bump 和 grunt-ng-constant 的正确方法

Proper way of using grunt-bump and grunt-ng-constant together

虽然任务似乎以正确的顺序执行(先碰撞,然后 ngconstant 根据 package.json-s 版本 属性 创建配置文件),但我认为它们实际上是并行执行的,而 ngconstant 读取package.json 之前 bump 已经写好了。

Running "bump" task
md
>> Version bumped to 2.0.6 (in package.json)

Running "ngconstant:production" (ngconstant) task
Creating module config at app/scripts/config.js...OK

结果 package.json 的版本是 2.0.6,而 config.js 的版本是 2.0.5。

我的 ngconstant 配置只使用

grunt.file.readJSON('package.json')

阅读 json。

所以,基本上问题是,在使用 ngconstant 读取 json 之前,我如何确保 bump 的写入完成,以及究竟是什么导致了上述情况?

编辑:原始 Gruntfile:https://github.com/dekztah/sc2/blob/18acaff22ab027000026311ac8215a51846786b8/Gruntfile.js

编辑:解决问题的更新 Gruntfile:https://github.com/dekztah/sc2/blob/e7985db6b95846c025ba0b615bf239c4f9c11e8f/Gruntfile.js

可能您的 package.json 文件存储在内存中,并且在您的 运行 下一个任务之前没有更新。

workaround 将在您的文件中创建一个脚本 package.json 作为:

  "scripts": {
    "bumb-and-ngconstant": "grunt:bump && grunt:build"
  }

根据 grunt-ng-constant 文档:

Or if you want to calculate the constants value at runtime you can create a lazy evaluated method which should be used if you generate your json file during the build process.

grunt.initConfig({
  ngconstant: {
    options: {
      dest: 'dist/module.js',
      name: 'someModule'
    },
    dist: {
      constants: function () {
        return {
          lazyConfig: grunt.file.readJSON('build/lazy-config.json')
        };
      }
    }
  },
})

这会强制 json 在任务运行时读取,而不是在 grunt 启动 ngconstant 任务时读取。