-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-external-api-types.sh
executable file
·40 lines (28 loc) · 1.72 KB
/
update-external-api-types.sh
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
#!/bin/bash
set -euf -o pipefail
# To execute and copy to stackbit-api: npm run update-external-api-types:copy
# This horrific and hopefully temporary script creates a TypeScript definition file for stackbit-app
# that includes types from stackbit-api that are used in requests/responses of the HTTP API. You can
# find a list of these types in src/external-api-types.ts .
#
# The process isn't elegant, but here are the steps:
# 1. Generate an initial dist/export.d.ts file using tsc.
# 2. Prefix all stackbit-api modules with `stackbit-api/`, except for external-api-types.
# 3. Prefix imports with `stackbit-api/`, unless they are external type dependencies.
# 4. Add an explanation line to the file.
#
# Finally, note that stackbit-app must have installed as dev dependencies, all the stackbit-api type
# dependencies. For packages that have their types provided from @types/, it isn't so bad. For those
# that have embedded types, it means potentially many sub-dependencies. This is required, for now.
# Step 1
npx tsc -p tsconfig-export.json
# Step 2
sed --in-place --regexp-extended 's/^(declare module ")/\1stackbit-api\//g' dist/export.d.ts
sed --in-place --regexp-extended 's/^(declare module ")stackbit-api\/(external-api-types)/\1\2/g' dist/export.d.ts
# Step 3
# This relies on an interesting feature in .d.ts where `from "name"` imports from a local module and
# `from 'name'` imports from an external module. If this ever breaks, use grep to identify external
# modules references (`/// <reference types />`) and exclude them.
sed --in-place --regexp-extended 's/( from ")/\1stackbit-api\//g' dist/export.d.ts
# Step 4
sed --in-place '1s;^;\/\/ This file is auto-generated by stackbit-api/update-external-api-types.sh\n;' dist/export.d.ts