.PHONY: *

BRANCH_NAME = ${DRONE_COMMIT_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}
CURRENT_DIR = $(shell pwd)
PORT ?= 8018
SKIP_SHARED_BUILD ?= false
GREEN=\033[0;32m
NC=\033[0m # No Color

ifeq (${SHOW_PROGRESS}, true)
	# If SHOW_PROGRESS is explicitly true, then reset to default of empty:
	SHOW_PROGRESS=
endif

audit:
	npm audit fix

install:
	@echo "Installing dependencies for next"
	npm ci

webpack:
	npx --no -- webpack $(if ${SHOW_PROGRESS},,--progress)

webpack-watch:
	npx --no -- webpack -w $(if ${SHOW_PROGRESS},,--progress)

webpack-debug:
	node --inspect-brk ./node_modules/webpack/bin/webpack.js

build:
	@echo "Building next in development mode"
	make clean
	SKUID_CORE_ENV=development make dist-theme
	npx --no -- grunt dist --no-color --env=development
	make generate-pegjs-parsers
	SKUID_CORE_ENV=development make webpack

build-and-watch:
	@echo "Building next in development mode, and watching for changes"
	make clean
	SKUID_CORE_ENV=development make dist-theme
	npx --no -- grunt dist --no-color --env=development
	make generate-pegjs-parsers
	SKUID_CORE_ENV=development make watch-themes &
	SKUID_CORE_ENV=development make webpack-watch

build-es5:
	@echo "Building v1 in ES5 mode"
	make clean
	SKUID_CORE_ENV=development make dist-theme
	npx --no -- grunt dist --no-color --env=development
	make generate-pegjs-parsers
	SKUID_CORE_ENV=development TRANSPILE_ES5=true make webpack

build-and-watch-es5:
	@echo "Building v1 in ES5 mode, and watching for JS changes"
	make clean
	SKUID_CORE_ENV=development make dist-theme
	npx --no -- grunt dist --no-color --env=development
	make generate-pegjs-parsers
	SKUID_CORE_ENV=development TRANSPILE_ES5=true make webpack-watch

build-prod:
	@echo "Building next in Production mode"
	make clean
	SKUID_CORE_ENV=production make dist-theme
	npx --no -- grunt dist --no-color --env=production
	make generate-pegjs-parsers
	SKUID_CORE_ENV=production make webpack

build-and-watch-prod:
	@echo "Building next in Production mode, and watching for JS changes"
	make clean
	SKUID_CORE_ENV=production make dist-theme
	npx --no -- grunt dist --no-color --env=production
	make generate-pegjs-parsers
	SKUID_CORE_ENV=production make webpack-watch

bump-build:
	@echo "Bumping next build release number"
	bash ./bash/bump-build-number.sh
	bash ./bash/release.sh

bump-patch:
	@echo "Bumping next patch release number"
	bash ./bash/bump-patch-number.sh
	bash ./bash/release.sh

watch:
	@echo "Building next JS only in development mode, and watching for JS changes"
	SKUID_CORE_ENV=development make webpack-watch

watch-themes:
	@echo "Watching next theme files for changes"
	SKUID_CORE_ENV=development node ./buildScripts/watchThemes.js

watch-all:
	@echo "Watching next JS and theme files for changes"
	SKUID_CORE_ENV=development make watch-themes &
	SKUID_CORE_ENV=development make webpack-watch

watch-es5:
	@echo "Building v1 JS only in ES5 mode, and watching for JS changes"
	SKUID_CORE_ENV=development TRANSPILE_ES5=true make webpack-watch

watch-live:
	@echo "Building next JS only in development mode, and watching for JS changes, with live reload"
	ENABLE_LIVE_RELOAD=true SKUID_CORE_ENV=development make webpack-watch

watch-prod:
	@echo "Building next JS only in Production mode, and watching for JS changes"
	SKUID_CORE_ENV=production make webpack-watch

local-shared:
	@$(MAKE) -C .. local-shared SKIP_SHARED_BUILD=$(SKIP_SHARED_BUILD)

local:
	@$(MAKE) -C .. local PORT=$(PORT) SKIP_SHARED_BUILD=$(SKIP_SHARED_BUILD)

lint-js:
	@echo "Linting next"
	npx --no -- eslint . --ext .json,.js,.ts,.jsx,.tsx

lint-ts:
	@echo "TypeScript type checking"
	npx --no -- tsc --noEmit --project tsconfig.json

lint:
	@make lint-js
	@make lint-ts

lint-fix:
	@echo "Lint fixing next"
	npx --no -- eslint . --ext .json,.js,.ts,.jsx,.tsx --fix

test:
	@echo "Testing next"
	TZ=1 npx --no -- grunt test

test-module:
	@echo "Testing next (module-filtered)"
	@if [ -z "${MODULES}" ]; then \
		echo "Usage: make test-module MODULES=<module-name[,module-name]>"; \
		exit 1; \
	fi
	TZ=1 npx --no -- grunt test --modules="${MODULES}"

test-filter:
	@echo "Testing next (name-filtered)"
	@if [ -z "${FILTER}" ]; then \
		echo "Usage: make test-filter FILTER=<test-name-fragment>"; \
		exit 1; \
	fi
	TZ=1 npx --no -- grunt test --filter="${FILTER}"

test-spec:
	@echo "Testing next (spec-filtered)"
	@if [ -z "${SPEC}" ]; then \
		echo "Usage: make test-spec SPEC=<spec-path-fragment[,fragment]>"; \
		exit 1; \
	fi
	TZ=1 npx --no -- grunt test --spec="${SPEC}"

test-projector:
	@echo "Testing next (projector override)"
	@if [ -z "${PROJECTOR}" ]; then \
		echo "Usage: make test-projector PROJECTOR=<maquette|react> [MODULES=...] [FILTER=...] [SPEC=...]"; \
		exit 1; \
	fi
	TZ=1 npx --no -- grunt test --projector="${PROJECTOR}" $(if ${MODULES},--modules="${MODULES}",) $(if ${FILTER},--filter="${FILTER}",) $(if ${SPEC},--spec="${SPEC}",)

browser-test:
	@echo "Testing next"
	TZ=1 npx --no -- grunt testInBrowser

test-with-coverage:
	@echo "Testing next and generating coverage report"
	TZ=1 npx --no -- nyc --reporter=html grunt test

open-coverage:
	@echo "Opening coverage report"
	open -a "Google Chrome" ./coverage/index.html

dist-theme:
	node ./buildScripts/compileScriptsForThemeCompilation.js
	npx --no -- grunt themeCompile --stack

dist-js:
	npx --no -- grunt dist-js

clean:
	rm -rf dist/* build/ buildScripts/dist/
	mkdir -p dist build/js

d.ts:
	npx --no -- -p typescript tsc --declaration --allowJs --emitDeclarationOnly --outDir types

generate-pegjs-parsers:
	npm run generate-formula-debugger
