向项目添加自定义 build/prepare 步骤

Add custom build/prepare step to project

我有一个针对 Android 和 iOS 的离子应用程序。

我想实现的,但我不确定该怎么做,是能够在 building/preparing 两个平台的代码时指定一个命令行标志。 我的第一个目标是用另一个资源替换资源 - 在本例中是图像 - 仅用于测试版本(比如用 beta 图标替换发布图标)。

如何做到这一点? 甚至可以使用 ionic build/prepare?

使用Ionic Hooks(荣誉@sebaferreras)我设法让它工作如下:

  1. 将挂钩添加到 ionic.config.json:

    "hooks": { "build:before": "./scripts/build-before.js", "serve:before": "./scripts/serve-before.js" }

  2. 创建要使用的挂钩脚本和资源。 (例如 ionic build 的简单钩子脚本 - 没有检查,为简单起见:

module.exports = function(ctx)
{
  // Use console.log(ctx); to print the context to the console when running 'ionic build/serve'

  const projectDir = ctx.project.dir;

  if(isDevBuild(ctx))
  {
    useDevelopmentImage(projectDir);
    console.log('Using development logo.');
  }
  else
  {
    useProductionImage(projectDir);
    console.log('Using production logo.');
  }
};

function isDevBuild(context)
{
  if(context.build.prod)
    return false;

  return true;
}

function useDevelopmentImage(projectDir)
{
  const devLogoPath = projectDir + '/images/dev_logo.png';
  // Could also use context.project.src instead of projectDir + '/src...'
  const targetPath  = projectDir + '/src/assets/imgs/logo.png';

  let fs = require('fs');
  fs.copyFileSync(devLogoPath, targetPath); 
}

function useProductionImage(projectDir)
{
  const prodLogoPath = projectDir + '/images/prod_logo.png';
  const targetPath   = projectDir + '/src/assets/imgs/logo.png';

  let fs = require('fs');
  fs.copyFileSync(prodLogoPath, targetPath);
}