Skip to content

Commit 53908b1

Browse files
committed
fix(build): add a Makefile that can be included by implementing projects
1 parent f1e461b commit 53908b1

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Makefile

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# This Makefile is intended to be called by higher level/project Makefiles
2+
# It bakes in a number of standard practices include the use of the src
3+
# directory (choose your build dir as appropriate).
4+
#
5+
# It also includes database scripts, which technically aren't the concern of this
6+
# module, but very often services do have a database component but don't need
7+
# any sort of service-specific database modules (just plain pg). So this Makefile
8+
# has targets for setting up a database for the service, and generating types
9+
# using kysely, and using db-migrate for schema managment.
10+
#
11+
# With all that, you can have a Makefile that does this:
12+
#
13+
# .PHONY: all service dbi ts
14+
#
15+
# export DB_NAME ?= cool_db
16+
# export SERVICE_NAME ?= my-cool-serv
17+
#
18+
# include node_modules/@openapi-typescript-infra/service/Makefile
19+
#
20+
# all: service dbi ts
21+
#
22+
23+
build_dir := $(node -e "console.log(require('./package.json').main.replace(/^.\//, '').split('/')[0])")
24+
src_files := $(shell find src -name '*.ts')
25+
build_files := $(patsubst src/%.ts,$(build_dir)/%.js,$(src_files))
26+
27+
# General utilities
28+
clean:
29+
yarn dlx rimraf ./$(build_dir) src/generated
30+
31+
# Typescript items
32+
ts: $(word 1, $(build_files))
33+
34+
$(word 1, $(build_files)): $(src_files)
35+
./node_modules/.bin/tsc -p tsconfig.build.json
36+
37+
service: src/generated/service/index.ts
38+
39+
src/generated/service/index.ts: api/${SERVICE_NAME}.yaml
40+
echo "Building service interface"
41+
yarn dlx openapi-typescript-express ./api/${SERVICE_NAME}.yaml \
42+
--output ./src/generated/service/index.ts
43+
./node_modules/.bin/prettier ./src/generated/service/index.ts --write
44+
45+
# Postgres database things
46+
export PGUSER ?= postgres
47+
export PGPASSWORD ?= postgres
48+
export PGHOST ?= localhost
49+
50+
db-ci:
51+
yarn run-pg-sql -q postgres ./migrations/setup/ci_setup.sql
52+
yarn run-pg-sql -q postgres ./migrations/setup/db_setup.sql
53+
yarn migration:apply
54+
yarn run-pg-sql $(DB_NAME) ./migrations/setup/dev_setup.sql
55+
56+
db-drop:
57+
yarn run-pg-sql -q postgres -e "DROP DATABASE IF EXISTS $(DB_NAME);"
58+
59+
db+:
60+
yarn migration:apply
61+
62+
db-:
63+
yarn migration:undo
64+
65+
db-clean: db-drop db-ci
66+
67+
dbi:
68+
echo "Generating database types"
69+
DATABASE_URL=postgres://$(PGUSER):$(PGPASSWORD)@$(PGHOST)/$(DB_NAME) yarn kysely-codegen \
70+
--dialect postgres --schema public \
71+
--out-file src/generated/database.ts

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ This module has the following core functionality:
3838
7. Setup infrastructure for interservice calls with tracing.
3939
8. Provide a central service runner that handles loading your service and getting to a running state in both development and production environments.
4040

41+
Please see the plop-based [project builder](../create) for an easy way to build an example project.

0 commit comments

Comments
 (0)