@angeloashmore/prismic-cli-poc 0.0.0-canary.1143872
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +98 -0
- package/dist/index.mjs +2508 -0
- package/package.json +53 -0
- package/src/codegen-types.ts +82 -0
- package/src/codegen.ts +45 -0
- package/src/custom-type-add-field-boolean.ts +222 -0
- package/src/custom-type-add-field-color.ts +205 -0
- package/src/custom-type-add-field-date.ts +208 -0
- package/src/custom-type-add-field-embed.ts +205 -0
- package/src/custom-type-add-field-geo-point.ts +202 -0
- package/src/custom-type-add-field-group.ts +179 -0
- package/src/custom-type-add-field-image.ts +205 -0
- package/src/custom-type-add-field-key-text.ts +205 -0
- package/src/custom-type-add-field-link.ts +228 -0
- package/src/custom-type-add-field-number.ts +237 -0
- package/src/custom-type-add-field-rich-text.ts +229 -0
- package/src/custom-type-add-field-select.ts +211 -0
- package/src/custom-type-add-field-timestamp.ts +208 -0
- package/src/custom-type-add-field-uid.ts +188 -0
- package/src/custom-type-add-field.ts +116 -0
- package/src/custom-type-connect-slice.ts +214 -0
- package/src/custom-type-create.ts +112 -0
- package/src/custom-type-disconnect-slice.ts +171 -0
- package/src/custom-type-list.ts +110 -0
- package/src/custom-type-remove-field.ts +171 -0
- package/src/custom-type-remove.ts +138 -0
- package/src/custom-type-set-name.ts +138 -0
- package/src/custom-type-view.ts +118 -0
- package/src/custom-type.ts +85 -0
- package/src/docs-fetch.ts +146 -0
- package/src/docs-list.ts +131 -0
- package/src/docs.ts +54 -0
- package/src/index.ts +132 -0
- package/src/init.ts +64 -0
- package/src/lib/auth.ts +83 -0
- package/src/lib/config.ts +111 -0
- package/src/lib/custom-types-api.ts +438 -0
- package/src/lib/field-path.ts +81 -0
- package/src/lib/file.ts +49 -0
- package/src/lib/framework.ts +143 -0
- package/src/lib/json.ts +3 -0
- package/src/lib/request.ts +116 -0
- package/src/lib/slice.ts +115 -0
- package/src/lib/string.ts +6 -0
- package/src/lib/url.ts +25 -0
- package/src/locale-add.ts +116 -0
- package/src/locale-list.ts +107 -0
- package/src/locale-remove.ts +88 -0
- package/src/locale-set-default.ts +131 -0
- package/src/locale.ts +60 -0
- package/src/login.ts +152 -0
- package/src/logout.ts +36 -0
- package/src/page-type-add-field-boolean.ts +238 -0
- package/src/page-type-add-field-color.ts +224 -0
- package/src/page-type-add-field-date.ts +227 -0
- package/src/page-type-add-field-embed.ts +224 -0
- package/src/page-type-add-field-geo-point.ts +221 -0
- package/src/page-type-add-field-group.ts +198 -0
- package/src/page-type-add-field-image.ts +224 -0
- package/src/page-type-add-field-key-text.ts +224 -0
- package/src/page-type-add-field-link.ts +247 -0
- package/src/page-type-add-field-number.ts +256 -0
- package/src/page-type-add-field-rich-text.ts +248 -0
- package/src/page-type-add-field-select.ts +230 -0
- package/src/page-type-add-field-timestamp.ts +227 -0
- package/src/page-type-add-field-uid.ts +207 -0
- package/src/page-type-add-field.ts +116 -0
- package/src/page-type-connect-slice.ts +214 -0
- package/src/page-type-create.ts +161 -0
- package/src/page-type-disconnect-slice.ts +171 -0
- package/src/page-type-list.ts +109 -0
- package/src/page-type-remove-field.ts +171 -0
- package/src/page-type-remove.ts +138 -0
- package/src/page-type-set-name.ts +138 -0
- package/src/page-type-set-repeatable.ts +147 -0
- package/src/page-type-view.ts +118 -0
- package/src/page-type.ts +90 -0
- package/src/preview-add.ts +126 -0
- package/src/preview-get-simulator.ts +104 -0
- package/src/preview-list.ts +106 -0
- package/src/preview-remove-simulator.ts +80 -0
- package/src/preview-remove.ts +109 -0
- package/src/preview-set-name.ts +137 -0
- package/src/preview-set-simulator.ts +116 -0
- package/src/preview.ts +75 -0
- package/src/pull.ts +242 -0
- package/src/push.ts +405 -0
- package/src/repo-create.ts +195 -0
- package/src/repo-get-access.ts +86 -0
- package/src/repo-list.ts +100 -0
- package/src/repo-set-access.ts +100 -0
- package/src/repo-set-name.ts +102 -0
- package/src/repo-view.ts +113 -0
- package/src/repo.ts +70 -0
- package/src/slice-add-field-boolean.ts +240 -0
- package/src/slice-add-field-color.ts +226 -0
- package/src/slice-add-field-date.ts +226 -0
- package/src/slice-add-field-embed.ts +226 -0
- package/src/slice-add-field-geo-point.ts +223 -0
- package/src/slice-add-field-group.ts +191 -0
- package/src/slice-add-field-image.ts +223 -0
- package/src/slice-add-field-key-text.ts +226 -0
- package/src/slice-add-field-link.ts +245 -0
- package/src/slice-add-field-number.ts +226 -0
- package/src/slice-add-field-rich-text.ts +250 -0
- package/src/slice-add-field-select.ts +232 -0
- package/src/slice-add-field-timestamp.ts +226 -0
- package/src/slice-add-field.ts +111 -0
- package/src/slice-add-variation.ts +139 -0
- package/src/slice-create.ts +203 -0
- package/src/slice-list-variations.ts +67 -0
- package/src/slice-list.ts +88 -0
- package/src/slice-remove-field.ts +122 -0
- package/src/slice-remove-variation.ts +112 -0
- package/src/slice-remove.ts +91 -0
- package/src/slice-rename.ts +122 -0
- package/src/slice-set-screenshot.ts +235 -0
- package/src/slice-view.ts +80 -0
- package/src/slice.ts +95 -0
- package/src/status.ts +873 -0
- package/src/token-create.ts +203 -0
- package/src/token-delete.ts +182 -0
- package/src/token-list.ts +223 -0
- package/src/token-set-name.ts +193 -0
- package/src/token.ts +60 -0
- package/src/webhook-add-header.ts +118 -0
- package/src/webhook-create.ts +152 -0
- package/src/webhook-disable.ts +109 -0
- package/src/webhook-enable.ts +132 -0
- package/src/webhook-list.ts +93 -0
- package/src/webhook-remove-header.ts +117 -0
- package/src/webhook-remove.ts +106 -0
- package/src/webhook-set-triggers.ts +148 -0
- package/src/webhook-status.ts +90 -0
- package/src/webhook-test.ts +106 -0
- package/src/webhook-view.ts +147 -0
- package/src/webhook.ts +95 -0
- package/src/whoami.ts +62 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
|
|
3
|
+
import { isAuthenticated } from "./lib/auth";
|
|
4
|
+
import { safeGetRepositoryFromConfig } from "./lib/config";
|
|
5
|
+
import { stringify } from "./lib/json";
|
|
6
|
+
import { ForbiddenRequestError, request } from "./lib/request";
|
|
7
|
+
import { getInternalApiUrl } from "./lib/url";
|
|
8
|
+
|
|
9
|
+
const HELP = `
|
|
10
|
+
Remove a locale from a Prismic repository.
|
|
11
|
+
|
|
12
|
+
By default, this command reads the repository from prismic.config.json at the
|
|
13
|
+
project root.
|
|
14
|
+
|
|
15
|
+
USAGE
|
|
16
|
+
prismic locale remove <code> [flags]
|
|
17
|
+
|
|
18
|
+
ARGUMENTS
|
|
19
|
+
<code> Locale code (e.g. en-us, fr-fr)
|
|
20
|
+
|
|
21
|
+
FLAGS
|
|
22
|
+
-r, --repo string Repository domain
|
|
23
|
+
-h, --help Show help for command
|
|
24
|
+
|
|
25
|
+
LEARN MORE
|
|
26
|
+
Use \`prismic <command> <subcommand> --help\` for more information about a command.
|
|
27
|
+
`.trim();
|
|
28
|
+
|
|
29
|
+
export async function localeRemove(): Promise<void> {
|
|
30
|
+
const {
|
|
31
|
+
values: { repo = await safeGetRepositoryFromConfig(), help },
|
|
32
|
+
positionals: [code],
|
|
33
|
+
} = parseArgs({
|
|
34
|
+
args: process.argv.slice(4), // skip: node, script, "locale", "remove"
|
|
35
|
+
options: {
|
|
36
|
+
repo: { type: "string", short: "r" },
|
|
37
|
+
help: { type: "boolean", short: "h" },
|
|
38
|
+
},
|
|
39
|
+
allowPositionals: true,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if (help) {
|
|
43
|
+
console.info(HELP);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!code) {
|
|
48
|
+
console.error("Missing required argument: <code>");
|
|
49
|
+
process.exitCode = 1;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!repo) {
|
|
54
|
+
console.error("Missing prismic.config.json or --repo option");
|
|
55
|
+
process.exitCode = 1;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const authenticated = await isAuthenticated();
|
|
60
|
+
if (!authenticated) {
|
|
61
|
+
handleUnauthenticated();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const response = await removeLocale(repo, code);
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
if (response.error instanceof ForbiddenRequestError) {
|
|
68
|
+
handleUnauthenticated();
|
|
69
|
+
} else {
|
|
70
|
+
console.error(`Failed to remove locale: ${stringify(response.value)}`);
|
|
71
|
+
process.exitCode = 1;
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
console.info(`Removed locale: ${code}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function removeLocale(repository: string, code: string) {
|
|
80
|
+
const url = new URL(`/locale/repository/locales/${code}`, await getInternalApiUrl());
|
|
81
|
+
url.searchParams.set("repository", repository);
|
|
82
|
+
return await request(url, { method: "DELETE" });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function handleUnauthenticated() {
|
|
86
|
+
console.error("Not logged in. Run `prismic login` first.");
|
|
87
|
+
process.exitCode = 1;
|
|
88
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import * as v from "valibot";
|
|
3
|
+
|
|
4
|
+
import { isAuthenticated } from "./lib/auth";
|
|
5
|
+
import { safeGetRepositoryFromConfig } from "./lib/config";
|
|
6
|
+
import { stringify } from "./lib/json";
|
|
7
|
+
import { ForbiddenRequestError, request } from "./lib/request";
|
|
8
|
+
import { getInternalApiUrl } from "./lib/url";
|
|
9
|
+
import { type Locale, getLocales } from "./locale-list";
|
|
10
|
+
|
|
11
|
+
const HELP = `
|
|
12
|
+
Set the default locale for a Prismic repository.
|
|
13
|
+
|
|
14
|
+
By default, this command reads the repository from prismic.config.json at the
|
|
15
|
+
project root.
|
|
16
|
+
|
|
17
|
+
USAGE
|
|
18
|
+
prismic locale set-default <code> [flags]
|
|
19
|
+
|
|
20
|
+
ARGUMENTS
|
|
21
|
+
<code> Locale code (e.g. en-us, fr-fr)
|
|
22
|
+
|
|
23
|
+
FLAGS
|
|
24
|
+
-r, --repo string Repository domain
|
|
25
|
+
-h, --help Show help for command
|
|
26
|
+
|
|
27
|
+
LEARN MORE
|
|
28
|
+
Use \`prismic <command> <subcommand> --help\` for more information about a command.
|
|
29
|
+
`.trim();
|
|
30
|
+
|
|
31
|
+
export async function localeSetDefault(): Promise<void> {
|
|
32
|
+
const {
|
|
33
|
+
values: { help, repo = await safeGetRepositoryFromConfig() },
|
|
34
|
+
positionals: [code],
|
|
35
|
+
} = parseArgs({
|
|
36
|
+
args: process.argv.slice(4), // skip: node, script, "locale", "set-default"
|
|
37
|
+
options: {
|
|
38
|
+
repo: { type: "string", short: "r" },
|
|
39
|
+
help: { type: "boolean", short: "h" },
|
|
40
|
+
},
|
|
41
|
+
allowPositionals: true,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (help) {
|
|
45
|
+
console.info(HELP);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!code) {
|
|
50
|
+
console.error("Missing required argument: <code>");
|
|
51
|
+
process.exitCode = 1;
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!repo) {
|
|
56
|
+
console.error("Missing prismic.config.json or --repo option");
|
|
57
|
+
process.exitCode = 1;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const authenticated = await isAuthenticated();
|
|
62
|
+
if (!authenticated) {
|
|
63
|
+
handleUnauthenticated();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const localesResponse = await getLocales(repo);
|
|
68
|
+
if (!localesResponse.ok) {
|
|
69
|
+
if (localesResponse.error instanceof ForbiddenRequestError) {
|
|
70
|
+
handleUnauthenticated();
|
|
71
|
+
} else if (v.isValiError(localesResponse.error)) {
|
|
72
|
+
console.error(
|
|
73
|
+
`Failed to set default locale: Invalid response: ${stringify(localesResponse.error.issues)}`,
|
|
74
|
+
);
|
|
75
|
+
process.exitCode = 1;
|
|
76
|
+
} else {
|
|
77
|
+
console.error(`Failed to set default locale: ${stringify(localesResponse.value)}`);
|
|
78
|
+
process.exitCode = 1;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const locales = localesResponse.value.results;
|
|
85
|
+
const locale = locales.find((l) => l.id === code);
|
|
86
|
+
if (!locale) {
|
|
87
|
+
console.error(
|
|
88
|
+
`Locale "${code}" not found in repository. Available locales: ${locales.map((l) => l.id).join(", ")}`,
|
|
89
|
+
);
|
|
90
|
+
process.exitCode = 1;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (locale.isMaster) {
|
|
95
|
+
console.error(`Locale "${code}" is already the default.`);
|
|
96
|
+
process.exitCode = 1;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const response = await setDefaultLocale(repo, locale);
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
if (response.error instanceof ForbiddenRequestError) {
|
|
103
|
+
handleUnauthenticated();
|
|
104
|
+
} else {
|
|
105
|
+
console.error(`Failed to set default locale: ${stringify(response.value)}`);
|
|
106
|
+
process.exitCode = 1;
|
|
107
|
+
}
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
console.info(`Default locale set: ${code}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function setDefaultLocale(repo: string, locale: Locale) {
|
|
115
|
+
const url = new URL("/locale/repository/locales", await getInternalApiUrl());
|
|
116
|
+
url.searchParams.set("repository", repo);
|
|
117
|
+
return await request(url, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
body: {
|
|
120
|
+
id: locale.id,
|
|
121
|
+
label: locale.label,
|
|
122
|
+
customName: locale.customName,
|
|
123
|
+
isMaster: true,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function handleUnauthenticated() {
|
|
129
|
+
console.error("Not logged in. Run `prismic login` first.");
|
|
130
|
+
process.exitCode = 1;
|
|
131
|
+
}
|
package/src/locale.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
|
|
3
|
+
import { localeAdd } from "./locale-add";
|
|
4
|
+
import { localeList } from "./locale-list";
|
|
5
|
+
import { localeRemove } from "./locale-remove";
|
|
6
|
+
import { localeSetDefault } from "./locale-set-default";
|
|
7
|
+
|
|
8
|
+
const HELP = `
|
|
9
|
+
Manage locales in a Prismic repository.
|
|
10
|
+
|
|
11
|
+
USAGE
|
|
12
|
+
prismic locale <command> [flags]
|
|
13
|
+
|
|
14
|
+
COMMANDS
|
|
15
|
+
add Add a locale to a repository
|
|
16
|
+
list List locales in a repository
|
|
17
|
+
remove Remove a locale from a repository
|
|
18
|
+
set-default Set the default locale for a repository
|
|
19
|
+
|
|
20
|
+
FLAGS
|
|
21
|
+
-h, --help Show help for command
|
|
22
|
+
|
|
23
|
+
LEARN MORE
|
|
24
|
+
Use \`prismic locale <command> --help\` for more information about a command.
|
|
25
|
+
`.trim();
|
|
26
|
+
|
|
27
|
+
export async function locale(): Promise<void> {
|
|
28
|
+
const {
|
|
29
|
+
positionals: [subcommand],
|
|
30
|
+
} = parseArgs({
|
|
31
|
+
args: process.argv.slice(3), // skip: node, script, "locale"
|
|
32
|
+
options: {
|
|
33
|
+
help: { type: "boolean", short: "h" },
|
|
34
|
+
},
|
|
35
|
+
allowPositionals: true,
|
|
36
|
+
strict: false,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
switch (subcommand) {
|
|
40
|
+
case "add":
|
|
41
|
+
await localeAdd();
|
|
42
|
+
break;
|
|
43
|
+
case "list":
|
|
44
|
+
await localeList();
|
|
45
|
+
break;
|
|
46
|
+
case "remove":
|
|
47
|
+
await localeRemove();
|
|
48
|
+
break;
|
|
49
|
+
case "set-default":
|
|
50
|
+
await localeSetDefault();
|
|
51
|
+
break;
|
|
52
|
+
default: {
|
|
53
|
+
if (subcommand) {
|
|
54
|
+
console.error(`Unknown locale subcommand: ${subcommand}\n`);
|
|
55
|
+
process.exitCode = 1;
|
|
56
|
+
}
|
|
57
|
+
console.info(HELP);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/login.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { exec } from "node:child_process";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
|
|
5
|
+
import { saveToken } from "./lib/auth";
|
|
6
|
+
|
|
7
|
+
const LOGIN_TIMEOUT_MS = 3 * 60 * 1000; // 3 minutes
|
|
8
|
+
const PREFERRED_PORT = 5555;
|
|
9
|
+
const CORS_ALLOWED_ORIGIN = "https://prismic.io";
|
|
10
|
+
|
|
11
|
+
const HELP = `
|
|
12
|
+
Log in to Prismic via browser.
|
|
13
|
+
|
|
14
|
+
USAGE
|
|
15
|
+
prismic login [flags]
|
|
16
|
+
|
|
17
|
+
FLAGS
|
|
18
|
+
-h, --help Show help for command
|
|
19
|
+
|
|
20
|
+
LEARN MORE
|
|
21
|
+
Use \`prismic <command> --help\` for more information about a command.
|
|
22
|
+
`.trim();
|
|
23
|
+
|
|
24
|
+
export async function login(): Promise<void> {
|
|
25
|
+
const { values } = parseArgs({
|
|
26
|
+
args: process.argv.slice(3),
|
|
27
|
+
options: { help: { type: "boolean", short: "h" } },
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (values.help) {
|
|
31
|
+
console.info(HELP);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
const server = createServer((req, res) => {
|
|
37
|
+
// Handle CORS preflight
|
|
38
|
+
if (req.method === "OPTIONS") {
|
|
39
|
+
res.writeHead(204, {
|
|
40
|
+
"Access-Control-Allow-Origin": CORS_ALLOWED_ORIGIN,
|
|
41
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
42
|
+
"Access-Control-Allow-Headers": "Content-Type",
|
|
43
|
+
});
|
|
44
|
+
res.end();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Handle POST with credentials
|
|
49
|
+
if (req.method === "POST") {
|
|
50
|
+
let body = "";
|
|
51
|
+
|
|
52
|
+
req.on("data", (chunk) => {
|
|
53
|
+
body += chunk.toString();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
req.on("end", async () => {
|
|
57
|
+
try {
|
|
58
|
+
const { cookies, email } = JSON.parse(body);
|
|
59
|
+
|
|
60
|
+
const cookie: string | undefined = cookies.find((c: string) =>
|
|
61
|
+
c.startsWith("prismic-auth="),
|
|
62
|
+
);
|
|
63
|
+
const token = cookie?.split(";")[0]?.replace(/^prismic-auth=/, "");
|
|
64
|
+
|
|
65
|
+
if (!token) {
|
|
66
|
+
res.writeHead(400, {
|
|
67
|
+
"Access-Control-Allow-Origin": CORS_ALLOWED_ORIGIN,
|
|
68
|
+
"Content-Type": "application/json",
|
|
69
|
+
});
|
|
70
|
+
res.end(JSON.stringify({ error: "Invalid request" }));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
await saveToken(token);
|
|
75
|
+
|
|
76
|
+
console.info(`Logged in to Prismic as ${email}`);
|
|
77
|
+
|
|
78
|
+
res.writeHead(200, {
|
|
79
|
+
"Access-Control-Allow-Origin": CORS_ALLOWED_ORIGIN,
|
|
80
|
+
"Content-Type": "application/json",
|
|
81
|
+
});
|
|
82
|
+
res.end(JSON.stringify({ success: true }));
|
|
83
|
+
|
|
84
|
+
clearTimeout(timeoutId);
|
|
85
|
+
server.close();
|
|
86
|
+
resolve();
|
|
87
|
+
} catch {
|
|
88
|
+
res.writeHead(400, {
|
|
89
|
+
"Access-Control-Allow-Origin": CORS_ALLOWED_ORIGIN,
|
|
90
|
+
"Content-Type": "application/json",
|
|
91
|
+
});
|
|
92
|
+
res.end(JSON.stringify({ error: "Invalid request" }));
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Handle other requests
|
|
100
|
+
res.writeHead(404);
|
|
101
|
+
res.end();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const timeoutId = setTimeout(() => {
|
|
105
|
+
server.close();
|
|
106
|
+
reject(new Error("Login timed out. Please try again."));
|
|
107
|
+
}, LOGIN_TIMEOUT_MS);
|
|
108
|
+
|
|
109
|
+
const onListening = (): void => {
|
|
110
|
+
const address = server.address();
|
|
111
|
+
if (!address || typeof address === "string") {
|
|
112
|
+
clearTimeout(timeoutId);
|
|
113
|
+
server.close();
|
|
114
|
+
reject(new Error("Failed to start login server"));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const port = address.port;
|
|
119
|
+
const loginUrl = buildLoginUrl(port);
|
|
120
|
+
|
|
121
|
+
console.info(`Opening browser to complete login...`);
|
|
122
|
+
console.info(`If the browser doesn't open, visit: ${loginUrl}`);
|
|
123
|
+
|
|
124
|
+
openBrowser(loginUrl);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
server.on("error", (error: NodeJS.ErrnoException) => {
|
|
128
|
+
if (error.code === "EADDRINUSE" && server.listening === false) {
|
|
129
|
+
// Preferred port is in use, fall back to a random port.
|
|
130
|
+
server.listen(0, "0.0.0.0", onListening);
|
|
131
|
+
} else {
|
|
132
|
+
clearTimeout(timeoutId);
|
|
133
|
+
reject(error);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
server.listen(PREFERRED_PORT, "0.0.0.0", onListening);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function buildLoginUrl(port: number): URL {
|
|
142
|
+
const url = new URL(`https://prismic.io/dashboard/cli/login`);
|
|
143
|
+
url.searchParams.set("source", "slice-machine");
|
|
144
|
+
url.searchParams.set("port", port.toString());
|
|
145
|
+
return url;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function openBrowser(url: URL): void {
|
|
149
|
+
const cmd =
|
|
150
|
+
process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
|
151
|
+
exec(`${cmd} "${url.toString()}"`);
|
|
152
|
+
}
|
package/src/logout.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
|
|
3
|
+
import { removeToken } from "./lib/auth";
|
|
4
|
+
|
|
5
|
+
const HELP = `
|
|
6
|
+
Log out of Prismic.
|
|
7
|
+
|
|
8
|
+
USAGE
|
|
9
|
+
prismic logout [flags]
|
|
10
|
+
|
|
11
|
+
FLAGS
|
|
12
|
+
-h, --help Show help for command
|
|
13
|
+
|
|
14
|
+
LEARN MORE
|
|
15
|
+
Use \`prismic <command> --help\` for more information about a command.
|
|
16
|
+
`.trim();
|
|
17
|
+
|
|
18
|
+
export async function logout(): Promise<void> {
|
|
19
|
+
const { values } = parseArgs({
|
|
20
|
+
args: process.argv.slice(3),
|
|
21
|
+
options: { help: { type: "boolean", short: "h" } },
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (values.help) {
|
|
25
|
+
console.info(HELP);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const ok = await removeToken();
|
|
30
|
+
if (ok) {
|
|
31
|
+
console.info("Logged out of Prismic");
|
|
32
|
+
} else {
|
|
33
|
+
console.error("Logout failed. You can log out manually by deleting the file.");
|
|
34
|
+
process.exitCode = 1;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import type { BooleanField, CustomType } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import * as v from "valibot";
|
|
6
|
+
|
|
7
|
+
import { buildTypes } from "./codegen-types";
|
|
8
|
+
import { findUpward } from "./lib/file";
|
|
9
|
+
import { findGroupInTab, isGroupField, parseFieldPath, validateNestedFieldPath } from "./lib/field-path";
|
|
10
|
+
import { type Framework, detectFrameworkInfo } from "./lib/framework";
|
|
11
|
+
import { stringify } from "./lib/json";
|
|
12
|
+
import { humanReadable } from "./lib/string";
|
|
13
|
+
|
|
14
|
+
const HELP = `
|
|
15
|
+
Add a boolean (toggle) field to an existing page type.
|
|
16
|
+
|
|
17
|
+
USAGE
|
|
18
|
+
prismic page-type add-field boolean <type-id> <field-id> [flags]
|
|
19
|
+
|
|
20
|
+
ARGUMENTS
|
|
21
|
+
type-id Page type identifier (required)
|
|
22
|
+
field-id Field identifier (required)
|
|
23
|
+
|
|
24
|
+
FLAGS
|
|
25
|
+
-t, --tab string Target tab (default: first existing tab, or "Main")
|
|
26
|
+
-l, --label string Display label for the field (inferred from field-id if omitted)
|
|
27
|
+
--default Set default value to true
|
|
28
|
+
--true-label string Label shown when toggle is on
|
|
29
|
+
--false-label string Label shown when toggle is off
|
|
30
|
+
--types string Output file for generated types (default: "prismicio-types.d.ts")
|
|
31
|
+
-h, --help Show help for command
|
|
32
|
+
|
|
33
|
+
EXAMPLES
|
|
34
|
+
prismic page-type add-field boolean homepage featured
|
|
35
|
+
prismic page-type add-field boolean article published --default
|
|
36
|
+
prismic page-type add-field boolean product available --true-label "In Stock" --false-label "Out of Stock"
|
|
37
|
+
`.trim();
|
|
38
|
+
|
|
39
|
+
const CustomTypeSchema = v.object({
|
|
40
|
+
id: v.string(),
|
|
41
|
+
label: v.string(),
|
|
42
|
+
repeatable: v.boolean(),
|
|
43
|
+
status: v.boolean(),
|
|
44
|
+
format: v.string(),
|
|
45
|
+
json: v.record(v.string(), v.record(v.string(), v.unknown())),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
function getDocsPath(framework: Framework): string {
|
|
49
|
+
switch (framework) {
|
|
50
|
+
case "next":
|
|
51
|
+
return "nextjs/with-cli";
|
|
52
|
+
case "nuxt":
|
|
53
|
+
return "nuxt/with-cli";
|
|
54
|
+
case "sveltekit":
|
|
55
|
+
return "sveltekit/with-cli";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function pageTypeAddFieldBoolean(): Promise<void> {
|
|
60
|
+
const {
|
|
61
|
+
values: {
|
|
62
|
+
help,
|
|
63
|
+
tab,
|
|
64
|
+
label,
|
|
65
|
+
default: defaultValue,
|
|
66
|
+
"true-label": trueLabel,
|
|
67
|
+
"false-label": falseLabel,
|
|
68
|
+
types,
|
|
69
|
+
},
|
|
70
|
+
positionals: [typeId, fieldId],
|
|
71
|
+
} = parseArgs({
|
|
72
|
+
args: process.argv.slice(5), // skip: node, script, "page-type", "add-field", "boolean"
|
|
73
|
+
options: {
|
|
74
|
+
tab: { type: "string", short: "t" },
|
|
75
|
+
label: { type: "string", short: "l" },
|
|
76
|
+
default: { type: "boolean" },
|
|
77
|
+
"true-label": { type: "string" },
|
|
78
|
+
"false-label": { type: "string" },
|
|
79
|
+
types: { type: "string" },
|
|
80
|
+
help: { type: "boolean", short: "h" },
|
|
81
|
+
},
|
|
82
|
+
allowPositionals: true,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
if (help) {
|
|
86
|
+
console.info(HELP);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!typeId) {
|
|
91
|
+
console.error("Missing required argument: type-id\n");
|
|
92
|
+
console.error("Usage: prismic page-type add-field boolean <type-id> <field-id>");
|
|
93
|
+
process.exitCode = 1;
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!fieldId) {
|
|
98
|
+
console.error("Missing required argument: field-id\n");
|
|
99
|
+
console.error("Usage: prismic page-type add-field boolean <type-id> <field-id>");
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Parse and validate field path
|
|
105
|
+
const fieldPath = parseFieldPath(fieldId);
|
|
106
|
+
const pathValidation = validateNestedFieldPath(fieldPath);
|
|
107
|
+
if (!pathValidation.ok) {
|
|
108
|
+
console.error(pathValidation.error);
|
|
109
|
+
process.exitCode = 1;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Find the page type file
|
|
114
|
+
const projectRoot = await findUpward("package.json");
|
|
115
|
+
if (!projectRoot) {
|
|
116
|
+
console.error("Could not find project root (no package.json found)");
|
|
117
|
+
process.exitCode = 1;
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const modelPath = new URL(`customtypes/${typeId}/index.json`, projectRoot);
|
|
122
|
+
|
|
123
|
+
// Read and parse the model
|
|
124
|
+
let model: CustomType;
|
|
125
|
+
try {
|
|
126
|
+
const contents = await readFile(modelPath, "utf8");
|
|
127
|
+
const result = v.safeParse(CustomTypeSchema, JSON.parse(contents));
|
|
128
|
+
if (!result.success) {
|
|
129
|
+
console.error(`Invalid page type model: ${modelPath.href}`);
|
|
130
|
+
process.exitCode = 1;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
model = result.output as CustomType;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
136
|
+
console.error(`Page type not found: ${typeId}\n`);
|
|
137
|
+
console.error(`Create it first with: prismic page-type create ${typeId}`);
|
|
138
|
+
process.exitCode = 1;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (error instanceof Error) {
|
|
142
|
+
console.error(`Failed to read page type: ${error.message}`);
|
|
143
|
+
} else {
|
|
144
|
+
console.error("Failed to read page type");
|
|
145
|
+
}
|
|
146
|
+
process.exitCode = 1;
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Determine target tab
|
|
151
|
+
const existingTabs = Object.keys(model.json);
|
|
152
|
+
const targetTab = tab ?? existingTabs[0] ?? "Main";
|
|
153
|
+
|
|
154
|
+
// Initialize tab if it doesn't exist
|
|
155
|
+
if (!model.json[targetTab]) {
|
|
156
|
+
model.json[targetTab] = {};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Build field definition
|
|
160
|
+
const fieldDefinition: BooleanField = {
|
|
161
|
+
type: "Boolean",
|
|
162
|
+
config: {
|
|
163
|
+
label: label ?? humanReadable(fieldPath.type === "nested" ? fieldPath.nestedFieldId : fieldId),
|
|
164
|
+
...(defaultValue && { default_value: true }),
|
|
165
|
+
...(trueLabel && { placeholder_true: trueLabel }),
|
|
166
|
+
...(falseLabel && { placeholder_false: falseLabel }),
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// Add field to model
|
|
171
|
+
if (fieldPath.type === "nested") {
|
|
172
|
+
const groupResult = findGroupInTab(model.json[targetTab], fieldPath.groupId, targetTab);
|
|
173
|
+
if (!groupResult.ok) {
|
|
174
|
+
console.error(groupResult.error);
|
|
175
|
+
process.exitCode = 1;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (groupResult.group.config.fields[fieldPath.nestedFieldId]) {
|
|
179
|
+
console.error(`Field "${fieldPath.nestedFieldId}" already exists in group "${fieldPath.groupId}"`);
|
|
180
|
+
process.exitCode = 1;
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
groupResult.group.config.fields[fieldPath.nestedFieldId] = fieldDefinition;
|
|
184
|
+
} else {
|
|
185
|
+
for (const [tabName, tabFields] of Object.entries(model.json)) {
|
|
186
|
+
if (tabFields[fieldId]) {
|
|
187
|
+
console.error(`Field "${fieldId}" already exists in tab "${tabName}"`);
|
|
188
|
+
process.exitCode = 1;
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
for (const [groupFieldId, groupField] of Object.entries(tabFields)) {
|
|
192
|
+
if (isGroupField(groupField) && groupField.config.fields[fieldId]) {
|
|
193
|
+
console.error(`Field "${fieldId}" already exists in group "${groupFieldId}" in tab "${tabName}"`);
|
|
194
|
+
process.exitCode = 1;
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
model.json[targetTab][fieldId] = fieldDefinition;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Write updated model
|
|
203
|
+
try {
|
|
204
|
+
await writeFile(modelPath, stringify(model));
|
|
205
|
+
} catch (error) {
|
|
206
|
+
if (error instanceof Error) {
|
|
207
|
+
console.error(`Failed to update page type: ${error.message}`);
|
|
208
|
+
} else {
|
|
209
|
+
console.error("Failed to update page type");
|
|
210
|
+
}
|
|
211
|
+
process.exitCode = 1;
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (fieldPath.type === "nested") {
|
|
216
|
+
console.info(`Added field "${fieldPath.nestedFieldId}" (Boolean) to group "${fieldPath.groupId}" in ${typeId}`);
|
|
217
|
+
} else {
|
|
218
|
+
console.info(`Added field "${fieldId}" (Boolean) to "${targetTab}" tab in ${typeId}`);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
await buildTypes({ output: types });
|
|
223
|
+
console.info(`Updated types in ${types ?? "prismicio-types.d.ts"}`);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
console.warn(`Could not generate types: ${error instanceof Error ? error.message : error}`);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
console.info();
|
|
229
|
+
console.info("Next: Add more fields with `prismic page-type add-field`");
|
|
230
|
+
|
|
231
|
+
const frameworkInfo = await detectFrameworkInfo();
|
|
232
|
+
if (frameworkInfo?.framework) {
|
|
233
|
+
const docsPath = getDocsPath(frameworkInfo.framework);
|
|
234
|
+
console.info(
|
|
235
|
+
` Run \`prismic docs fetch ${docsPath}#write-page-components\` to learn how to implement a page file`,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|