title: Ionic 5: best practice to get the built platform
body: <p>I'm writing an app using Ionic 5, Angular and Cordova. I would like to get the &quot;platform&quot; variable used to build my application (i.e. the argument used in a command like &quot;ionic cordova build android).</p>
  <p>Unfortunately, all I found on the web was the Platform service (<a href="https://ionicframework.com/docs/angular/platform" rel="nofollow noreferrer">https://ionicframework.com/docs/angular/platform</a>), useful but not quite what I needed (e.g. platform.is('android') can return true if the application runs on a web app on an android phone, on a browser on an android phone or if I click F12 on chrome...).
  What I need is to find the platform the application was built for.</p>
  <p>The best way I found to achieve the result, was to add &quot;build:before&quot; hook in my ionic.config.json file</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  "hooks": {
      "build:before": "scripts/build-before.js",
      "build:after": "scripts/build-after.js"
     }</code></pre>
  </div>
  </div>
  </p>
  <p>and change the constant in a service before building</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>#!/usr/bin/env node
  var process = require('process');
  var fs = require('fs');
  
  let input=process.argv;
  
  let android="android";
  let browser="browser";
  let ios="ios";
  let platformFile="MYFILE"
  
  
  module.exports = function() {
      let nbArgs=input.length;
      let platform;
      for(let i=0;i&lt;nbArgs;i++){
          if(input[i]==android){
              platform=android;
          }
          if(input[i]==browser){
              platform=browser;
          }
          if(input[i]==ios){
              platform=ios;
          }
      }
  
      let file = fs.readFileSync(platformFile, 'utf8');
      let replacement="MYSTART '"+platform+"';";
      let result = file.replace(/MYREGEX/g, replacement);
      fs.writeFileSync(platformFile, result);
  
      console.log("Service updated with platform: "+platform);
  }</code></pre>
  </div>
  </div>
  </p>
  <p>Then in the build:after I restore the file as it was (to avoid to commit the file on GIT after each build...).</p>
  <p>I added the code in case someone has the same issue and looks for a quick workaround.</p>
  <p>My question is: does anyone know of an official/smarter way to achieve the result?</p>
  <p>Thanks,</p>