<commit_before>#!/bin/sh
set -ex

# travis_retry isn't available to sub-scripts
retry () { for i in 1 2 3; do "$@" && return || sleep 10; done; exit 1; }

# Prepare for deploy
commit=$ECRU_COMMIT # TODO fall back to parsing from local git repo
SHA=$commit npm run predeploy
echo "module.exports = '$commit';" > ./version.js

STAGING_RANCH_FILE='.ranch.staging.yaml'
PRODUCTION_RANCH_FILE=`[ -f '.ranch.production.yaml' ] && echo '.ranch.production.yaml' || echo '.ranch.yaml'`

# Deploy staging
ranch deploy -f $STAGING_RANCH_FILE
ranch run -f $STAGING_RANCH_FILE -- npm run postdeploy
smoke_test () { SMOKE_TEST_ENV=staging npm run test:smoke; }
retry smoke_test

if [ "$DEPLOY_PRODUCTION" -eq 1 ]; then
  # Deploy production
  ranch deploy -f $PRODUCTION_RANCH_FILE
  ranch run -f $PRODUCTION_RANCH_FILE -- npm run postdeploy
else
  echo "DEPLOY_PRODUCTION env var is not set to 1. Not deploying to production."
fi

# Apply changes to Statsfile, if any.
if [ -f ./Statsfile.coffee ]; then
  is_babel=
  statsfile_hash=$(coffee -e 'console.log(JSON.stringify(require("./Statsfile.coffee")))' | tail -1 | md5sum | cut -d ' ' -f 1)
elif [ -f ./Statsfile.js ]; then
  is_babel=1
  # NOTE: importing Statsfile may import some app modules and log things, so we hash only on the last logged line
  # For example, apps are known to log "STATUS_API_TOKEN required, but not set. Configuring goodeggs-status in simulate mode"
  # within a full JSON log line with a timestamp - which makes this hashing nondeterministic. :(
  statsfile_hash=$(babel-node -e 'console.log(JSON.stringify(require("./Statsfile.js")))' | tail -1 | md5sum | cut -d ' ' -f 1)
fi
apply_statsfile() {
  if [ ! -z $is_babel ]; then args='--require=babel-register'; fi
  goodeggs-stats $args
  if [ ! -d ./.goodeggs-stats-state ]; then mkdir ./.goodeggs-stats-state; fi
  echo $statsfile_hash > ./.goodeggs-stats-state/md5_hash
}
if [ ! -f ./.goodeggs-stats-state/md5_hash ]; then
  echo 'No cached Statsfile hash; applying it'
  apply_statsfile
else<commit_msg>goodeggs-deploy supports custom goodeggs-stats args.<commit_after>#!/bin/sh
set -ex

# travis_retry isn't available to sub-scripts
retry () { for i in 1 2 3; do "$@" && return || sleep 10; done; exit 1; }

# Prepare for deploy
commit=$ECRU_COMMIT # TODO fall back to parsing from local git repo
SHA=$commit npm run predeploy
echo "module.exports = '$commit';" > ./version.js

STAGING_RANCH_FILE='.ranch.staging.yaml'
PRODUCTION_RANCH_FILE=`[ -f '.ranch.production.yaml' ] && echo '.ranch.production.yaml' || echo '.ranch.yaml'`

# Deploy staging
ranch deploy -f $STAGING_RANCH_FILE
ranch run -f $STAGING_RANCH_FILE -- npm run postdeploy
smoke_test () { SMOKE_TEST_ENV=staging npm run test:smoke; }
retry smoke_test

if [ "$DEPLOY_PRODUCTION" -eq 1 ]; then
  # Deploy production
  ranch deploy -f $PRODUCTION_RANCH_FILE
  ranch run -f $PRODUCTION_RANCH_FILE -- npm run postdeploy
else
  echo "DEPLOY_PRODUCTION env var is not set to 1. Not deploying to production."
fi

# Apply changes to Statsfile, if any.
if [ -f ./Statsfile.js ]; then
  # NOTE: importing Statsfile may import some app modules and log things, so we hash only on the last logged line
  # For example, apps are known to log "STATUS_API_TOKEN required, but not set. Configuring goodeggs-status in simulate mode"
  # within a full JSON log line with a timestamp - which makes this hashing nondeterministic. :(
  statsfile_hash=$(babel-node -e 'console.log(JSON.stringify(require("./Statsfile.js")))' | tail -1 | md5sum | cut -d ' ' -f 1)
fi
apply_statsfile() {
  # Assume babel 6 if not overriden. Someday we should remove this.
  DEFAULT_GOODEGGS_STATS_ARGS='--require=babel-register'
  args=${GOODEGGS_STATS_ARGS-$DEFAULT_GOODEGGS_STATS_ARGS}
  goodeggs-stats $args
  if [ ! -d ./.goodeggs-stats-state ]; then mkdir ./.goodeggs-stats-state; fi
  echo $statsfile_hash > ./.goodeggs-stats-state/md5_hash
}
if [ ! -f ./.goodeggs-stats-state/md5_hash ]; then
  echo 'No cached Statsfile hash; applying it'
  apply_statsfile
else
{'fraction_non_alphanumeric': 0.11955168119551682, 'fraction_numerical': 0.007721046077210461, 'mean_word_length': 4.0388958594730235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3260828', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1400, 'n_words': 442}