Skip to content

Commit 753bb2c

Browse files
committed
✨ add get-custom-types script
1 parent a40995a commit 753bb2c

File tree

6 files changed

+167
-23
lines changed

6 files changed

+167
-23
lines changed

custom_types/index.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
"repeatable": true,
77
"value": "page.json"
88
}
9-
]
9+
]

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
"name": "@example/minimal",
33
"version": "0.0.1",
44
"private": true,
5+
"type": "module",
56
"scripts": {
67
"dev": "astro dev",
78
"start": "astro dev",
89
"build": "astro build",
910
"preview": "astro preview",
10-
"format": "prettier --ignore-path .gitignore . -wu"
11+
"format": "prettier --ignore-path .gitignore . -wu",
12+
"get-custom-types": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"esnext\", \"target\": \"es2017\"}' node --no-warnings --loader ts-node/esm --experimental-specifier-resolution=node scripts/get-custom-types.ts"
1113
},
1214
"dependencies": {
1315
"@prismicio/client": "^6.4.2",
@@ -20,14 +22,19 @@
2022
},
2123
"devDependencies": {
2224
"@prismicio/types": "^0.1.27",
25+
"@tsconfig/node14": "^1.0.1",
2326
"astro": "^1.0.0-beta.37",
2427
"dotenv": "^16.0.1",
2528
"eslint": "^8.12.0",
2629
"eslint-plugin-node": "^11.1.0",
2730
"eslint-plugin-prettier-doc": "^1.1.0",
31+
"node-fetch": "^3.2.5",
32+
"ora": "^6.1.0",
2833
"prettier": "^2.6.1",
2934
"prettier-plugin-astro": "^0.0.12",
3035
"prismic-ts-codegen": "^0.0.3",
36+
"prompts": "^2.4.2",
37+
"ts-node": "^10.8.0",
3138
"typescript": "^4.6.3"
3239
}
3340
}

prismicCodegen.config.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ const config: Config = {
55
repositoryName: process.env.PRISMIC_REPO,
66
accessToken: process.env.PRISMIC_TOKEN,
77
customTypesAPIToken: process.env.PRISMIC_TYPES_TOKEN,
8-
locales: {
9-
ids: ['en-us'],
10-
fetchFromRepository: true,
11-
},
12-
models: {
13-
files: ['./custom_types/**/index.json'],
14-
fetchFromRepository: true,
15-
},
8+
locales: { fetchFromRepository: true },
9+
models: { fetchFromRepository: true },
1610
output: './src/prismic.generated.ts',
1711
}
1812

scripts/get-custom-types.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import 'dotenv/config'
2+
import fetch from 'node-fetch'
3+
import { writeFile } from 'fs/promises'
4+
import { join } from 'path'
5+
import { oraPromise as loader } from 'ora'
6+
7+
const { PRISMIC_REPO, PRISMIC_TYPES_TOKEN } = process.env
8+
9+
type CustomType = {
10+
id: string
11+
label: string
12+
repeatable: boolean
13+
json: Record<string, unknown>
14+
status: boolean
15+
}
16+
17+
const s = (object: unknown) => JSON.stringify(object, null, 2)
18+
const jsonPath = (fileName: string) => join('custom_types', `${fileName}.json`)
19+
20+
if (!PRISMIC_REPO) {
21+
throw new Error('no PRISMIC_REPO on .env')
22+
}
23+
if (!PRISMIC_TYPES_TOKEN) {
24+
throw new Error('no PRISMIC_TYPES_TOKEN on .env')
25+
}
26+
27+
const headers = {
28+
repository: PRISMIC_REPO,
29+
Authorization: `Bearer ${PRISMIC_TYPES_TOKEN}`,
30+
}
31+
32+
export default (async () => {
33+
const x = await loader(fetch('https://customtypes.prismic.io/customtypes', { headers }), {
34+
text: 'pulling custom types',
35+
})
36+
const allCustomTypes = (await loader(x.json(), { text: 'decoding' })) as CustomType[]
37+
38+
const indexFileContent = allCustomTypes
39+
.filter(({ status }) => status)
40+
.map(({ id, label, repeatable }) => ({
41+
id,
42+
name: id,
43+
label,
44+
repeatable,
45+
value: `${id}.json`,
46+
}))
47+
48+
await loader(writeFile(jsonPath('index'), s(indexFileContent)), {
49+
text: 'writing index file',
50+
})
51+
52+
allCustomTypes.forEach(async ({ id, json }) => {
53+
console.log(`writing ${id}.json`)
54+
await loader(writeFile(jsonPath(id), s(json)), { text: `writing ${id}.json` })
55+
console.log(`wrote ${id}.json`)
56+
})
57+
})()

src/prismic.generated.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,3 @@ type PageDocumentDataBodySlice = PageDocumentDataBodyHorizontalSlice | PageDocum
150150
* @typeParam Lang - Language API ID of the document.
151151
*/
152152
export type PageDocument<Lang extends string = "en-us"> = prismicT.PrismicDocumentWithUID<Simplify<PageDocumentData>, "page", Lang>;
153-
/** Content for x documents */
154-
type XDocumentData = Record<string, never>;
155-
/**
156-
* x document from Prismic
157-
*
158-
* - **API ID**: `x`
159-
* - **Repeatable**: `true`
160-
* - **Documentation**: https://prismic.io/docs/core-concepts/custom-types
161-
*
162-
* @typeParam Lang - Language API ID of the document.
163-
*/
164-
export type XDocument<Lang extends string = "en-us"> = prismicT.PrismicDocumentWithoutUID<Simplify<XDocumentData>, "x", Lang>;

yarn.lock

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@
298298
"@babel/helper-validator-identifier" "^7.16.7"
299299
to-fast-properties "^2.0.0"
300300

301+
"@cspotcode/source-map-support@^0.8.0":
302+
version "0.8.1"
303+
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz#00629c35a688e05a88b1cda684fb9d5e73f000a1"
304+
integrity sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==
305+
dependencies:
306+
"@jridgewell/trace-mapping" "0.3.9"
307+
301308
"@emmetio/abbreviation@^2.2.3":
302309
version "2.2.3"
303310
resolved "https://registry.yarnpkg.com/@emmetio/abbreviation/-/abbreviation-2.2.3.tgz#2b3c0383c1a4652f677d5b56fb3f1616fe16ef10"
@@ -382,6 +389,14 @@
382389
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.11.tgz#771a1d8d744eeb71b6adb35808e1a6c7b9b8c8ec"
383390
integrity sha512-Fg32GrJo61m+VqYSdRSjRXMjQ06j8YIYfcTqndLYVAaHmroZHLJZCydsWBOTDqXS2v+mjxohBWEMfg97GXmYQg==
384391

392+
"@jridgewell/[email protected]":
393+
version "0.3.9"
394+
resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz#6534fd5933a53ba7cbf3a17615e273a0d1273ff9"
395+
integrity sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==
396+
dependencies:
397+
"@jridgewell/resolve-uri" "^3.0.3"
398+
"@jridgewell/sourcemap-codec" "^1.4.10"
399+
385400
"@jridgewell/trace-mapping@^0.3.0":
386401
version "0.3.4"
387402
resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.4.tgz#f6a0832dffd5b8a6aaa633b7d9f8e8e94c83a0c3"
@@ -524,6 +539,26 @@
524539
mkdirp "^1.0.4"
525540
path-browserify "^1.0.1"
526541

542+
"@tsconfig/node10@^1.0.7":
543+
version "1.0.8"
544+
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.8.tgz#c1e4e80d6f964fbecb3359c43bd48b40f7cadad9"
545+
integrity sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==
546+
547+
"@tsconfig/node12@^1.0.7":
548+
version "1.0.9"
549+
resolved "https://registry.yarnpkg.com/@tsconfig/node12/-/node12-1.0.9.tgz#62c1f6dee2ebd9aead80dc3afa56810e58e1a04c"
550+
integrity sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==
551+
552+
"@tsconfig/node14@^1.0.0", "@tsconfig/node14@^1.0.1":
553+
version "1.0.1"
554+
resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.1.tgz#95f2d167ffb9b8d2068b0b235302fafd4df711f2"
555+
integrity sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==
556+
557+
"@tsconfig/node16@^1.0.2":
558+
version "1.0.2"
559+
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
560+
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==
561+
527562
"@types/acorn@^4.0.0":
528563
version "4.0.6"
529564
resolved "https://registry.yarnpkg.com/@types/acorn/-/acorn-4.0.6.tgz#d61ca5480300ac41a7d973dd5b84d0a591154a22"
@@ -656,7 +691,12 @@ acorn-jsx@^5.0.0, acorn-jsx@^5.3.1:
656691
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937"
657692
integrity sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==
658693

659-
acorn@^8.0.0:
694+
acorn-walk@^8.1.1:
695+
version "8.2.0"
696+
resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.2.0.tgz#741210f2e2426454508853a2f44d0ab83b7f69c1"
697+
integrity sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==
698+
699+
acorn@^8.0.0, acorn@^8.4.1:
660700
version "8.7.1"
661701
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.7.1.tgz#0197122c843d1bf6d0a5e83220a788f278f63c30"
662702
integrity sha512-Xx54uLJQZ19lKygFXOWsscKUbsBZW0CPykPhVQdhIeIwrbPmJzqeASDInc8nKBnp/JT6igTs82qPXz069H8I/A==
@@ -720,6 +760,11 @@ ansi-styles@^6.1.0:
720760
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.1.0.tgz#87313c102b8118abd57371afab34618bf7350ed3"
721761
integrity sha512-VbqNsoz55SYGczauuup0MFUyXNQviSpFTj1RQtFzmQLk18qbVSpTFFGMT293rmDaQuKCT6InmbuEyUne4mTuxQ==
722762

763+
arg@^4.1.0:
764+
version "4.1.3"
765+
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
766+
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
767+
723768
argparse@^1.0.7:
724769
version "1.0.10"
725770
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
@@ -1112,6 +1157,11 @@ convert-source-map@^1.7.0:
11121157
dependencies:
11131158
safe-buffer "~5.1.1"
11141159

1160+
create-require@^1.1.0:
1161+
version "1.1.1"
1162+
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
1163+
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
1164+
11151165
cross-fetch@^3.1.5:
11161166
version "3.1.5"
11171167
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.5.tgz#e1389f44d9e7ba767907f7af8454787952ab534f"
@@ -1226,6 +1276,11 @@ dequal@^2.0.0:
12261276
resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.2.tgz#85ca22025e3a87e65ef75a7a437b35284a7e319d"
12271277
integrity sha512-q9K8BlJVxK7hQYqa6XISGmBZbtQQWVXSrRrWreHC94rMt1QL/Impruc+7p2CYSYuVIUr+YCt6hjrs1kkdJRTug==
12281278

1279+
diff@^4.0.1:
1280+
version "4.0.2"
1281+
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
1282+
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
1283+
12291284
diff@^5.0.0:
12301285
version "5.0.0"
12311286
resolved "https://registry.yarnpkg.com/diff/-/diff-5.0.0.tgz#7ed6ad76d859d030787ec35855f5b1daf31d852b"
@@ -2908,6 +2963,11 @@ magic-string@^0.25.9:
29082963
dependencies:
29092964
sourcemap-codec "^1.4.8"
29102965

2966+
make-error@^1.1.1:
2967+
version "1.3.6"
2968+
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
2969+
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
2970+
29112971
map-obj@^1.0.0:
29122972
version "1.0.1"
29132973
resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-1.0.1.tgz#d933ceb9205d82bdcf4886f6742bdc2b4dea146d"
@@ -3628,6 +3688,15 @@ node-fetch@^3.2.3, node-fetch@^3.2.4:
36283688
fetch-blob "^3.1.4"
36293689
formdata-polyfill "^4.0.10"
36303690

3691+
node-fetch@^3.2.5:
3692+
version "3.2.5"
3693+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.2.5.tgz#7d31da657804db5185540ddac7ddd516a9a2bd26"
3694+
integrity sha512-u7zCHdJp8JXBwF09mMfo2CL6kp37TslDl1KP3hRGTlCInBtag+UO3LGVy+NF0VzvnL3PVMpA2hXh1EtECFnyhQ==
3695+
dependencies:
3696+
data-uri-to-buffer "^4.0.0"
3697+
fetch-blob "^3.1.4"
3698+
formdata-polyfill "^4.0.10"
3699+
36313700
node-releases@^2.0.3:
36323701
version "2.0.5"
36333702
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.5.tgz#280ed5bc3eba0d96ce44897d8aee478bfb3d9666"
@@ -4638,6 +4707,25 @@ ts-morph@^14.0.0:
46384707
"@ts-morph/common" "~0.13.0"
46394708
code-block-writer "^11.0.0"
46404709

4710+
ts-node@^10.8.0:
4711+
version "10.8.0"
4712+
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.8.0.tgz#3ceb5ac3e67ae8025c1950626aafbdecb55d82ce"
4713+
integrity sha512-/fNd5Qh+zTt8Vt1KbYZjRHCE9sI5i7nqfD/dzBBRDeVXZXS6kToW6R7tTU6Nd4XavFs0mAVCg29Q//ML7WsZYA==
4714+
dependencies:
4715+
"@cspotcode/source-map-support" "^0.8.0"
4716+
"@tsconfig/node10" "^1.0.7"
4717+
"@tsconfig/node12" "^1.0.7"
4718+
"@tsconfig/node14" "^1.0.0"
4719+
"@tsconfig/node16" "^1.0.2"
4720+
acorn "^8.4.1"
4721+
acorn-walk "^8.1.1"
4722+
arg "^4.1.0"
4723+
create-require "^1.1.0"
4724+
diff "^4.0.1"
4725+
make-error "^1.1.1"
4726+
v8-compile-cache-lib "^3.0.1"
4727+
yn "3.1.1"
4728+
46414729
tsconfig-resolver@^3.0.1:
46424730
version "3.0.1"
46434731
resolved "https://registry.yarnpkg.com/tsconfig-resolver/-/tsconfig-resolver-3.0.1.tgz#c9e62e328ecfbeaae4a4f1131a92cdbed12350c4"
@@ -4865,6 +4953,11 @@ uvu@^0.5.0, uvu@^0.5.3:
48654953
kleur "^4.0.3"
48664954
sade "^1.7.3"
48674955

4956+
v8-compile-cache-lib@^3.0.1:
4957+
version "3.0.1"
4958+
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
4959+
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==
4960+
48684961
v8-compile-cache@^2.0.3:
48694962
version "2.3.0"
48704963
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee"
@@ -5118,6 +5211,11 @@ yargs-parser@^21.0.1:
51185211
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.0.1.tgz#0267f286c877a4f0f728fceb6f8a3e4cb95c6e35"
51195212
integrity sha512-9BK1jFpLzJROCI5TzwZL/TU4gqjK5xiHV/RfWLOahrjAko/e4DJkRDZQXfvqAsiZzzYhgAzbgz6lg48jcm4GLg==
51205213

5214+
5215+
version "3.1.1"
5216+
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
5217+
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==
5218+
51215219
yocto-queue@^0.1.0:
51225220
version "0.1.0"
51235221
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"

0 commit comments

Comments
 (0)