#!/usr/bin/env node

const yargs = require('yargs');
const cp = require('child_process');
const fs = require('fs');
const chalk = require('chalk');

const argv = yargs.argv;
const exec = command => cp.execSync(command, {stdio: 'inherit'});

// (option: --bh) Base href
const bh = argv['bh'] || '/';

// (option: --langs) List of languages separated by `,``
// Notice: the first language should be the one used in the app source code
// (which don't have a defined "messages.<lang>.xlf" file)
const langs = argv['langs'] ? argv['langs'].split(',') : [];

// (option: --dl) Default language to use in apache config when redirecting bad url
const defaultLang = langs.length ? (argv['dl'] || langs[0]) : null;

// Execute build command according to "<lang>" and "messages.<lang>.xlf" availibility
const execBuildCmd = (lang, hasXlf) => {
  let cmd;
  if (!lang) {
    cmd = `ng build --output-path=dist/ --aot -prod --bh ${bh}`;
  } else {
    const xlfOptions = hasXlf ? '--i18n-file=src/i18n/messages.$lang.xlf --i18n-format=xlf' : '';
    cmd = `lang=${lang}; ng build --output-path=dist/$lang --aot -prod --bh ${bh}$lang/ --locale=$lang ${xlfOptions}`;
  }
  console.log(chalk.green(`\nBuilding "dist/${lang || ''}"...`));
  console.log(`${cmd}\n`);
  exec(cmd);
};

// Start building...

console.log(chalk.white.bgBlue('Building App for production'));
console.log(`\nUsing baseHref: "${bh}"`);

if (!langs.length) {
  // Build app
  execBuildCmd();
} else {
  // Build app for each language
  langs.forEach((lang, index) => execBuildCmd(lang, index !== 0));

  // Build apache config
  console.log(chalk.green(`\nConfiguring apache (using default language: "${defaultLang}"):`));
  console.log([
    'dist/.htaccess',
    'dist/index.html'
  ].join('\n'));

  let htaccess = fs.readFileSync('src/apache/.htaccess', 'utf8');
  htaccess =
    htaccess
      .replace(/<BASE_HREF>/g, bh)
      .replace(/<DEFAULT_LANG>/g, defaultLang)
      .replace(/<LANGS>/g, `(${langs.join('|')})`);

  let index = fs.readFileSync('src/apache/index.html', 'utf8');
  index =
    index
      .replace(/<BASE_HREF>/g, bh)
      .replace(/<DEFAULT_LANG>/g, defaultLang);

  fs.writeFileSync('dist/.htaccess', htaccess, 'utf8');
  fs.writeFileSync('dist/index.html', index, 'utf8');
}

console.log();

@@ -700,16 +700,156 @@
  null;%0A%0A
+// (option: --htaccess) Add .htaccess in the dist/ folder ?%0Alet useHtaccess = argv%5B'htaccess'%5D === undefined %7C%7C argv%5B'htaccess'%5D === true;%0A%0A
 // Execu
@@ -1898,24 +1898,21 @@
 %0A%0A  let 
-htaccess
+index
  = fs.re
@@ -1934,25 +1934,26 @@
 /apache/
-.htaccess
+index.html
 ', 'utf8
@@ -1962,31 +1962,25 @@
 ;%0A  
-htaccess =%0A    htaccess
+index =%0A    index
 %0A   
@@ -2061,73 +2061,103 @@
 ang)
-%0A      .replace(/%3CLANGS%3E/g, %60($%7Blangs.join('%7C')%7D)%60);%0A%0A  let index
+;%0A%0A  fs.writeFileSync('dist/index.html', index, 'utf8');%0A%0A  if (useHtaccess) %7B%0A    let htaccess
  = f
@@ -2179,34 +2179,33 @@
 'src/apache/
-index.html
+.htaccess
 ', 'utf8');%0A
@@ -2206,34 +2206,46 @@
 f8');%0A  
-index
+  htaccess
  =%0A    
+  
-index%0A
+htaccess%0A  
       .r
@@ -2263,32 +2263,34 @@
 SE_HREF%3E/g, bh)%0A
+  
       .replace(/
@@ -2311,35 +2311,90 @@
 /g, defaultLang)
+%0A        .replace(/%3CLANGS%3E/g, %60($%7Blangs.join('%7C')%7D)%60)
 ;%0A%0A
+  
   fs.writeFileSy
@@ -2439,59 +2439,9 @@
 ;%0A  
-fs.writeFileSync('dist/index.html', index, 'utf8');
+%7D
 %0A%7D%0A%0A

Make optional .htaccess in the `build.prod.js`