generated from samialdury/nodejs-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
223 lines (180 loc) · 6.55 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
.DEFAULT_GOAL ?= help
COMMIT_SHA ?= $(shell git rev-parse --short HEAD)
PROJECT_NAME ?= nodejs-api
RED ?= $(shell tput setaf 1)
GREEN ?= $(shell tput setaf 2)
YELLOW ?= $(shell tput setaf 3)
CYAN ?= $(shell tput setaf 6)
NC ?= $(shell tput sgr0)
BIN := node_modules/.bin
LOCAL_DIR ?= local
DEV_DIR ?= $(LOCAL_DIR)/dev
TEST_DIR ?= $(LOCAL_DIR)/test
SCRIPTS_DIR ?= $(LOCAL_DIR)/scripts
SRC_DIR ?= src
BUILD_DIR ?= build
CACHE_DIR ?= .cache
TEST_FILES ?= {src,test}/**/*.test.ts
MYSQL_SEEDS_DIR ?= seeds/mysql
PROXY_COMPOSE_FILE ?= $(LOCAL_DIR)/proxy.docker-compose.yml
DEV_COMPOSE_FILE ?= $(DEV_DIR)/dev.docker-compose.yml
TEST_COMPOSE_FILE ?= $(TEST_DIR)/test.docker-compose.yml
COMPOSE_PROXY ?= docker compose -f $(PROXY_COMPOSE_FILE)
COMPOSE_DEV ?= docker compose -f $(DEV_COMPOSE_FILE)
COMPOSE_TEST ?= docker compose -f $(TEST_COMPOSE_FILE)
RUN_IN_DOCKER ?= $(SCRIPTS_DIR)/run-cmd.sh
WAIT_UNTIL ?= $(SCRIPTS_DIR)/wait-until.sh
##@ Misc
.PHONY: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "Usage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-16s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
# You can remove this target once you've ran it.
.PHONY: prepare
prepare: ## Prepare template (name=<string>)
@node $(SCRIPTS_DIR)/prepare-template.js $(name)
@rm -f $(SCRIPTS_DIR)/prepare-template.js
##@ Development
.PHONY: install
install: ## install all dependencies (skip-postinstall=<boolean>?)
@pnpm install
ifeq ($(skip-postinstall),true)
@echo "Skipping postinstall"
else
@$(BIN)/husky install
endif
.PHONY: dev-support
dev-support: ## start the support services
@$(COMPOSE_PROXY) up --detach --wait
@$(COMPOSE_DEV) --profile support up --detach --wait
.PHONY: dev-prepare
dev-prepare: ## prepare the dev environment
@echo "=== $(CYAN)preparing dev environment$(NC) ==="
@echo
@echo "=== $(CYAN)pulling & building docker images$(NC) ==="
@$(COMPOSE_DEV) build app
@$(COMPOSE_DEV) pull mysql
@echo "=== $(GREEN)docker images ready$(NC) ==="
@echo "=== $(CYAN)preparing docker network$(NC) ==="
@docker network create $(PROJECT_NAME) || true
@echo "=== $(GREEN)docker network ready$(NC) ==="
@echo "=== $(CYAN)preparing database$(NC) ==="
@$(COMPOSE_DEV) --profile support up --detach --wait
@echo "=== $(GREEN)database ready$(NC) ==="
@echo "=== $(CYAN)syncing DB schema$(NC) ==="
@make db-push
@echo "=== $(GREEN)DB schema synced$(NC) ==="
@echo
@echo "=== $(GREEN)dev environment ready$(NC) ==="
.PHONY: dev-local
dev-local: ## run TS and watch for changes
@node --env-file $(DEV_DIR)/.dev.env --no-warnings --import tsx --watch --watch-preserve-output $(SRC_DIR)/main.ts
.PHONY: dev
dev: ## run TS and watch for changes (Docker)
@$(COMPOSE_DEV) --profile app up
.PHONY: run-local
run-local: ## run JS
@node --env-file .env $(BUILD_DIR)/$(SRC_DIR)/main.js
.PHONY: run
run: ## run JS (Docker)
@$(RUN_IN_DOCKER) $(DEV_COMPOSE_FILE) 'make run-local'
##@ Database
.PHONY: db-push
db-push: ## sync database with the schema
@$(RUN_IN_DOCKER) $(or $(compose), $(DEV_COMPOSE_FILE)) \
'$(BIN)/drizzle-kit push:mysql'
.PHONY: db-seed
db-seed: ## run seed (name=<string>)
ifeq ($(name),)
@echo "$(RED)name is required$(NC)"
@exit 1
else
@$(RUN_IN_DOCKER) $(or $(compose), $(DEV_COMPOSE_FILE)) \
'node --env-file $(DEV_DIR)/.dev.env --no-warnings --import tsx $(MYSQL_SEEDS_DIR)/$(name).ts'
endif
##@ Build
.PHONY: build
build: ## build the project
@echo "=== $(YELLOW)cleaning build directory$(NC) ==="
@rm -rf $(BUILD_DIR)
@echo "=== $(CYAN)building project$(NC) (TS $$($(BIN)/tsc --version)) ==="
@$(BIN)/tsc
@echo "=== $(GREEN)build successful$(NC) ==="
.PHONY: build-image
build-image: ## build Docker image (args=<build args>?, tag=<string>?)
@docker build $(args) --build-arg COMMIT_SHA='dev,$(COMMIT_SHA)' -t $(or $(tag), $(PROJECT_NAME)) . -f ./Dockerfile
##@ Test
.PHONY: test-prepare
test-prepare: ## prepare the test environment
@echo "=== $(CYAN)preparing test environment$(NC) ==="
@echo
@echo "=== $(CYAN)pulling & building docker images$(NC) ==="
@$(COMPOSE_TEST) build app
@$(COMPOSE_TEST) pull mysql
@echo "=== $(GREEN)docker images ready$(NC) ==="
@echo "=== $(CYAN)preparing docker network$(NC) ==="
@docker network create $(PROJECT_NAME) || true
@echo "=== $(GREEN)docker network ready$(NC) ==="
@echo "=== $(CYAN)preparing database$(NC) ==="
@$(COMPOSE_TEST) --profile support up --detach --wait
@echo "=== $(GREEN)database ready$(NC) ==="
@echo "=== $(CYAN)syncing DB schema$(NC) ==="
@make db-push compose=$(TEST_COMPOSE_FILE)
@echo "=== $(GREEN)DB schema synced$(NC) ==="
@echo
@echo "=== $(GREEN)test environment ready$(NC) ==="
.PHONY: test-local
test-local: ## run tests
@$(BIN)/glob -c 'node --env-file $(TEST_DIR)/.test.env --no-warnings --import tsx --test $(args)' '$(TEST_FILES)'
.PHONY: test-watch-local
test-watch-local: ## run tests and watch for changes
@make test-local args='--watch --watch-preserve-output'
.PHONY: test
test: ## run tests (Docker)
@$(RUN_IN_DOCKER) $(TEST_COMPOSE_FILE) 'make test-local'
.PHONY: test-watch
test-watch: ## run tests and watch for changes (Docker)
@$(RUN_IN_DOCKER) $(TEST_COMPOSE_FILE) 'make test-watch-local'
##@ Code quality
.PHONY: format
format: ## format the code
@echo "=== $(CYAN)running Prettier$(NC) ==="
@$(BIN)/prettier --cache --cache-location=$(CACHE_DIR)/prettier --write --log-level warn .
@echo "=== $(GREEN)format successful$(NC) ==="
.PHONY: lint
lint: ## lint the code
@echo "=== $(CYAN)running ESLint$(NC) ==="
@$(BIN)/eslint --max-warnings 0 --cache --cache-location $(CACHE_DIR)/eslint --fix .
@echo "=== $(GREEN)lint successful$(NC) ==="
##@ CI
.PHONY: install-ci
install-ci: ## install all dependencies (CI)
@pnpm install --frozen-lockfile
.PHONY: build-ci
build-ci: build ## build the project (CI)
.PHONY: test-ci
test-ci: ## run tests (CI)
@echo "=== $(CYAN)preparing CI environment$(NC) ==="
@node $(SCRIPTS_DIR)/prepare-ci-env.js
@echo "=== $(GREEN)CI environment ready$(NC) ==="
@echo
@make test-prepare
@echo
@echo "=== $(CYAN)running tests$(NC) ==="
@echo
@make test
@echo
@echo "=== $(GREEN)tests ran successfully$(NC) ==="
.PHONY: format-ci
format-ci: ## format the code (CI)
@echo "=== $(CYAN)running Prettier$(NC) ==="
@$(BIN)/prettier --check --log-level warn .
@echo "=== $(GREEN)format successful$(NC) ==="
.PHONY: lint-ci
lint-ci: ## lint the code (CI)
@echo "=== $(CYAN)running ESLint$(NC) ==="
@$(BIN)/eslint --max-warnings 0 .
@echo "=== $(GREEN)lint successful$(NC) ==="
##@ Release
.PHONY: release
release: ## create a new release
@$(BIN)/semantic-release