NODE_ENV ?= development
VERSION ?= `node -p "require('./package').version"`
FILES_LINT_COMMONJS = webpack*.js index.js express configs tools .hermione.conf.js src/**/*.hermione.js `find src -name 'ru.js' -o -name 'en.js' | xargs`
FILES_LINT_ES = src
CACHE_MODE ?= read

export NODE_ENV
export TZ=Etc/GMT-3
export COMPOSE_HTTP_TIMEOUT=180

node_modules: package.json .npmrc
	npm install
	@touch node_modules

.PHONY: start
start:
	node index.js

test:: test-express
test:: test-react
test:: test-auto

.PHONY: test-express
test-express: node_modules
	npx jest -c jest-express.config.js

.PHONY: test-react
test-react: node_modules
	npx jest

.PHONY: test-react-update-snapshot
test-react-update-snapshot: node_modules
	npx jest --updateSnapshot

test-ci:: test-express-ci
test-ci:: test-react-ci
# https://st.yandex-team.ru/CERTOR-1861
# test-ci:: test-auto-ci

.PHONY: test-express-ci
test-express-ci: reports-dir-ci node_modules
	npx jest -c jest-express.config.js >> reports/test.express.txt 2>&1

.PHONY: test-react-ci
test-react-ci: reports-dir-ci node_modules
	npx jest >> reports/test.react.txt 2>&1

build:: build-ru
build:: build-en

.PHONY: build-ru
build-ru: node_modules
	BEM_LANG=ru npx webpack --config webpack.prod.js

.PHONY: build-en
build-en: node_modules
	BEM_LANG=en npx webpack --config webpack.prod.js

build-dev:: build-dev-ru
build-dev:: build-dev-en

.PHONY: build-dev-ru
build-dev-ru: node_modules
	BEM_LANG=ru npx webpack --config webpack.dev.js

.PHONY: build-dev-en
build-dev-en: node_modules
	BEM_LANG=en npx webpack --config webpack.dev.js

lint:: lint-common-js-modules
lint:: lint-es-modules
lint:: lint-stylus
lint:: lint-test-yaml

.PHONY: lint-common-js-modules
lint-common-js-modules: node_modules
	npx eslint --no-eslintrc --no-ignore --config .eslintrc-common.json $(FILES_LINT_COMMONJS)

.PHONY: lint-es-modules
lint-es-modules: node_modules
	npx eslint $(FILES_LINT_ES)

.PHONY: lint-stylus
lint-stylus: node_modules
	npx stylint --config ./node_modules/tools-access-configs/dotfiles/stylintrc.json src

.PHONY: lint-test-yaml
lint-test-yaml: node_modules
	npx palmsync validate

lint-ci:: lint-common-js-modules-ci
lint-ci:: lint-es-modules-ci
lint-ci:: lint-stylus-ci
lint-ci:: lint-test-yaml-ci

.PHONY: lint-common-js-modules-ci
lint-common-js-modules-ci: node_modules
	npx eslint --no-eslintrc --no-ignore --config .eslintrc-common.json $(FILES_LINT_COMMONJS) -f node_modules/eslint-html-reporter/reporter.js -o reports/lint.common.html

.PHONY: lint-es-modules-ci
lint-es-modules-ci: node_modules
	npx eslint $(FILES_LINT_ES) -f node_modules/eslint-html-reporter/reporter.js -o reports/lint.es.html

.PHONY: reports-dir-ci
reports-dir-ci:
	mkdir -p reports

.PHONY: lint-stylus-ci
lint-stylus-ci: reports-dir-ci node_modules
	npx stylint --config ./node_modules/tools-access-configs/dotfiles/stylintrc.json src >> reports/lint.stylus.txt 2>&1

.PHONY: lint-test-yaml-ci
lint-test-yaml-ci: reports-dir-ci node_modules
	$(MAKE) lint-test-yaml >> reports/lint.test-yml.txt 2>&1

.PHONY: releaser-build
releaser-build:
	releaser build -v $(VERSION)

.PHONY: releaser-push
releaser-push: releaser-build
	releaser push -v $(VERSION)

.PHONY: releaser-deploy
releaser-deploy: releaser-push
	releaser deploy -v $(VERSION)

.PHONY: releaser-deploy-ci
releaser-deploy-ci: reports-dir-ci
	$(MAKE) releaser-deploy >> reports/release.txt 2>&1

.PHONY: version-patch
version-patch:
	npm version patch --no-git-tag-version
	$(MAKE) changelog

.PHONY: version-minor
version-minor:
	npm version minor --no-git-tag-version
	$(MAKE) changelog

.PHONY: version-major
version-major:
	npm version major --no-git-tag-version
	$(MAKE) -s changelog

changelog: package.json
	releaser changelog -v $(VERSION)
	git add package.json
	git add package-lock.json
	git add changelog.md
	git commit -m "v$(VERSION)"
	git tag $(VERSION)

.PHONY: test-auto-compose-up
test-auto-compose-up: test-auto-compose-down
	docker network create bridge6
	docker-compose --file .hermione.docker-compose.yml up --detach --build

.PHONY: test-auto-compose-down
test-auto-compose-down:
	docker-compose --file .hermione.docker-compose.yml down
	docker network rm bridge6 || true

.PHONY: test-auto-hermione
test-auto-hermione: node_modules
	npx hermione $(HERMIONE_FLAGS) $(HERMIONE_TEST_PATH)

.PHONY: test-auto-screenshots-write
test-auto-screenshots-write:
	HERMIONE_FLAGS="--update-refs" $(MAKE) test-auto

.PHONY: test-auto-clement-create
test-auto-clement-create:
	CACHE_MODE=create $(MAKE) test-auto

.PHONY: test-auto-clement-create-screenshots-write
test-auto-clement-create-screenshots-write:
	CACHE_MODE=create HERMIONE_FLAGS="--update-refs" $(MAKE) test-auto

.PHONY: test-auto
test-auto: test-auto-compose-up
	$(MAKE) test-auto-hermione; \
		CODE=$$? && \
		$(MAKE) test-auto-compose-down; \
		if [ $$CODE -ne 0 ]; then exit $$CODE; fi

.PHONY: sync-testpalm
sync-testpalm: node_modules
	npx palmsync synchronize

.PHONY: sync-testpalm-ci
sync-testpalm-ci: reports-dir-ci node_modules
	NODE_TLS_REJECT_UNAUTHORIZED=0 $(MAKE) sync-testpalm >> reports/sync-testpalm.txt 2>&1

.PHONY: test-auto-ci
test-auto-ci: reports-dir-ci node_modules
	docker-compose --file .hermione.docker-compose.yml up --detach selenoid nginx >> reports/test.auto.txt 2>&1
	npx hermione >> reports/test.auto.txt 2>&1

ci-pr-checks:: lint-ci
ci-pr-checks:: test-ci
ci-pr-checks:: sync-testpalm-ci
