Skip to content

Commit a71a72b

Browse files
committed
feat(dry): use shared Makefile, remove db drop script and update types
1 parent 8d51618 commit a71a72b

File tree

4 files changed

+21
-52
lines changed

4 files changed

+21
-52
lines changed

src/plopfile.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ export default function (plop: NodePlopAPI) {
9090
// By default is false, but if "true" will log the output of commands
9191
verbose: true,
9292
},
93-
].filter(a => typeof a === 'object');
93+
`
94+
Your project is ready to roll! We ran yarn for you, so your next steps are:
95+
* Go into the api directory and edit your API specification.
96+
You could use https://editor.swagger.io/ and then paste the yaml back in.
97+
${data?.features.includes('db') ? `* Go into migrations/sql and add your initial database up/down migration to the existing files
98+
* Run "make db-ci" to create the database and run the migrations.` : ''}
99+
* Run "yarn test" to just make sure the environment is all sorted out.
100+
* Run "yarn build" to build the database interface, service types and TS code.
101+
* Start implementing your API by adding appropriate handler methods.
102+
103+
Happy hacking!`,
104+
].filter(a => typeof a === 'object' || typeof a === 'string');
94105
},
95106
});
96107
}

templates/all/Makefile

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,11 @@
1-
#!make
2-
.PHONY: all build clean
3-
4-
src_files := $(shell find src -name '*.ts')
5-
build_files := $(patsubst src/%.ts,build/%.js,$(src_files))
1+
# Makefiles are useful for slightly more complex scenarios than npm scripts can easily handle.
2+
# You can use gulp or whatever other thing you like, but we prefer the simplicity of Make.
63

7-
export PGUSER ?= postgres
8-
export PGPASSWORD ?= postgres
9-
export PGHOST ?= localhost
4+
.PHONY: all build clean
105

116
all: service {{#inArray features 'db'}}dbi {{/inArray}}$(word 1, $(build_files))
127

13-
ts: $(word 1, $(build_files))
14-
15-
$(word 1, $(build_files)): $(src_files)
16-
./node_modules/.bin/tsc -p tsconfig.build.json
17-
18-
clean:
19-
yarn dlx rimraf ./build src/generated
20-
21-
service: src/generated/service/index.ts
22-
23-
{{#inArray features 'db'}}
24-
db-ci:
25-
yarn run-pg-sql -q postgres ./migrations/setup/ci_setup.sql
26-
yarn run-pg-sql -q postgres ./migrations/setup/db_setup.sql
27-
yarn migration:apply
28-
yarn run-pg-sql {{dbName}} ./migrations/setup/dev_setup.sql
29-
30-
db-drop:
31-
yarn run-pg-sql -q {{dbName}} ./migrations/setup/db_clean.sql || true
32-
33-
db+:
34-
yarn migration:apply
35-
36-
db-:
37-
yarn migration:undo
38-
39-
db-clean: db-drop db-ci
40-
41-
dbi:
42-
echo "Generating database types"
43-
DATABASE_URL=postgres://$(PGUSER):$(PGPASSWORD)@$(PGHOST)/{{dbName}} yarn kysely-codegen \
44-
--dialect postgres --schema public \
45-
--out-file src/generated/database.ts
46-
{{/inArray}}
8+
{{#inArray features 'db'}}export DB_NAME ?= {{dbName}}
9+
{{/inArray}}export SERVICE_NAME ?= {{dashCase name}}
4710

48-
src/generated/service/index.ts: api/{{dashCase name}}.yaml
49-
echo "Building service interface"
50-
yarn dlx openapi-typescript-express ./api/{{dashCase name}}.yaml \
51-
--output ./src/generated/service/index.ts
52-
./node_modules/.bin/prettier ./src/generated/service/index.ts --write
11+
include node_modules/@openapi-typescript-infra/service/Makefile

templates/all/src/index.ts.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { Kysely, PostgresDialect } from 'kysely';
77
import type { DB } from './generated/database';
88
{{else}}
99
{{/inArray}}
10-
import type { {{properCase name}}Service, {{properCase name}}ConfigSchema } from './types';
10+
import type { {{properCase name}}Locals, {{properCase name}}Service, {{properCase name}}ConfigSchema } from './types';
1111

1212
type Config = {{properCase name}}ConfigSchema;
1313

1414
export function service(): {{properCase name}}Service {
15-
const base = useService();
15+
const base = useService<{{properCase name}}Locals>();
1616
return {
1717
...base,
1818
async start(app) {
@@ -38,7 +38,7 @@ export function service(): {{properCase name}}Service {
3838
});
3939
},
4040
async stop(app) {
41-
await base.stop(app);
41+
await base.stop?.(app);
4242
const { locals } = app;
4343
// Shutdown any service wide infra here
4444
{{#inArray features 'db'}}

templates/db/migrations/setup/db_clean.sql.hbs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)