#!/usr/bin/env bash

# Generates all the pre-translated code in lib/translated/{locale}.

set -e
rm -rf lib

# build the commonjs version
NODE_ENV=transpile babel --out-dir lib src &

# build an es modules version
babel --out-dir lib/modules src &

CPUS=$(node --eval "console.log(Math.max(1, require('os').cpus().length - 1))")
echo "building pretranslated output in lib/translated $CPUS at a time ..."

ls locales/*.json | \
  xargs basename -s .json | \
  xargs -t -n 1 -P $CPUS -I % env \
    BUILD_LOCALE=% ./node_modules/.bin/babel --out-dir lib/translated/%/modules --ignore locales* src

wait # wait for the default untranslated builds that we backgrounded above with an "&"
