

# ./node_modules/.bin on the PATH
export PATH := $(PATH):./node_modules/.bin

# Use bash not sh
SHELL := /bin/bash

ifeq ("$(wildcard node_modules/@financial-times/rel-engage/index.mk)", "")
PATH_TO_RELENGAGE :=
else
PATH_TO_RELENGAGE := node_modules/@financial-times/rel-engage/
endif

# Colour utils
OK_COLOUR    := \033[0;32m
ERROR_COLOUR := \033[0;31m
COMMAND_COLOUR := \033[0;36m
NO_COLOUR    := \033[m

# Some handy utilities
# Force NPM to use colours even when there is no tty
NPM_INSTALL = OLD_COLOUR=$$(npm config get color) trap 'npm config set color $$OLD_COLOUR' EXIT; npm config set color always && npm install $1
DONE = printf "$(OK_COLOUR)✓$(NO_COLOUR) $(NO_COLOUR)$@ $(OK_COLOUR)done\n$(NO_COLOUR)"
#
# META TASKS
#

# Export environment variables if a .env file is present.
ifeq ($(ENV_EXPORTED),) # ENV vars not yet exported
ifneq ("$(wildcard .env)","")
sinclude .env
export $(shell [ -f .env ] && sed 's/=.*//' .env)
export ENV_EXPORTED=true
$(info Note — An .env file exists. Its contents have been exported as environment variables.)
endif
endif

ifneq ($(IS_PROD_ENV),)
ifeq ($(CI),)
ifeq ($(USE_PROD_ENV),)
$(error 'prod env variables in use locally. Either run `make env` to install test variables, or set `USE_PROD_ENV=true` if this is deliberate')
endif
endif
endif

# Export all declared ENV vars to child commands
.EXPORT_ALL_VARIABLES:

# Note: A 'node_modules' directory is created when rel-engage self-installs.
# However: Subsequent calls to `node_modules`, made in order to execute $NPM_INSTALL,
# are *not* processed because the node_modules directory exists.
# Therefore use .PHONY to tell Make to not look for a node_modules file/folder,
# but to run the node_modules task instead.
.PHONY: test node_modules

# Silent makefile output unless VERBOSE=1 is passed
ifndef VERBOSE
.SILENT:
endif

#
# COMMON TASKS
#

env: ## env: Populate a .env file with variables from Doppler. Usage: `make env` or `make env ENV=dev/ci/staging/prod`
	doppler secrets download -p $(PROJECT_NAME) -c "$(if $(ENV),$(ENV),dev)" --format env-no-quotes --no-file > .env

clea%: ## clean: Clean this git repository.
# HACK: Can't use -e option here because it's not supported by Jenkins
	@git clean -fxdi
	@$(DONE)

ini%: ## init: Clean this repository and start from a fresh build.
ini%:
	$(MAKE) clean
	$(MAKE) install
	@$(DONE)

instal%: ## install: Setup this repository.
instal%: node_modules .editorconfig .eslintrc.js .eslintignore .prettierrc.js .prettierignore .huskyrc.js .lintstagedrc.cjs
	@$(DONE)

verif%: ## verify: Verify this repository.
verif%: _verify_prettier _verify_eslint _verify_stylelint
	@$(DONE)


NPM_PUBLISH_TAG := $(shell [[ "$(CIRCLE_TAG)" =~ -[a-z-]+ ]] && echo "pre-release" || echo "latest")
npm-publish:
	npm version --no-git-tag-version $(CIRCLE_TAG)
	npm publish --access public --tag $(NPM_PUBLISH_TAG)

#
# SUB-TASKS
#

# INSTALL SUB-TASKS

# These tasks have been intentionally left blank
package.json:

# Regular npm install
node_modules: package.json .huskyrc.js
	@if [ -e package.json ]; then printf "$(COMMAND_COLOUR)Running npm install$(NO_COLOUR)\n" && $(NPM_INSTALL) && $(DONE); fi

# Install various dot/config files
.editorconfig .eslintrc.js .eslintignore .prettierrc.js .prettierignore .huskyrc.js .lintstagedrc.cjs:
	@cp './$(PATH_TO_RELENGAGE)templates/dotfiles/tmpl$@' $@ && $(DONE)

# VERIFY SUB-TASKS

_verify_eslint:
	@if [ -e .eslintrc.js ]; then \
		[ ! -z "$(CI)" ] && FLAGS="" || FLAGS="--cache --fix" && \
		COMMAND="./node_modules/.bin/eslint $$FLAGS --color --ext js,jsx ." && \
		printf "\nRunning $(COMMAND_COLOUR)$$COMMAND$(NO_COLOUR)\n" && eval $$COMMAND && $(DONE) \
	; fi

_verify_stylelint:
	# We're not bundling any stylelint or any defaults with rel-engage, it's up the consuming
	# project to install and configure it. We may revisit this decision again in future.
	@if [ -e .stylelintrc.js ] || [ -e .stylelintrc ]; then \
		[ ! -z "$(CI)" ] && FLAGS="" || FLAGS="--fix --cache" && \
		COMMAND="./node_modules/.bin/stylelint $$FLAGS '*/**/*.{css,scss}'" && \
		printf "\nRunning $(COMMAND_COLOUR)$$COMMAND$(NO_COLOUR)\n" && eval $$COMMAND && $(DONE) \
	; fi

_verify_prettier:
	@if [ -e .prettierrc.js ]; then \
		[ ! -z "$(CI)" ] && FLAGS="--check" || FLAGS="--write" && \
		COMMAND="./node_modules/.bin/prettier $$FLAGS \"**/*.{js,jsx,json,md,yml,yaml,gql,graphql,css,scss}\"" && \
		printf "\nRunning $(COMMAND_COLOUR)$$COMMAND$(NO_COLOUR)\n" && eval $$COMMAND && $(DONE) \
	; fi

# DEPLOY SUB-TASKS

# BUILD SUB-TASKS

hel%: ## help: Show this help message.
	@printf "usage: make [target] ..."
	@printf ""
	@printf "targets:"
	@printf "\n"
	@# replace the first : with £ to avoid splitting columns on URLs
	@grep -Eh '^.+:\ ##\ .+' ${MAKEFILE_LIST} | cut -d ' ' -f '3-' | sed 's/:/£/' | column -t -c 2 -s '£'
