@phtngyn/cms 0.1.0-beta.4 → 0.2.0-beta.1
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/README.md +85 -5
- package/package.json +12 -1
- package/src/ApplicationTypes.d.ts +82 -0
- package/src/ApplicationTypes.js +1 -0
- package/src/{Configuration-CDMYfTre.js → Configuration-CjVUBBwa.js} +3 -3
- package/src/{Configuration-CDMYfTre.js.map → Configuration-CjVUBBwa.js.map} +1 -1
- package/src/Configuration.d.ts +9 -4
- package/src/Configuration.js.map +1 -1
- package/src/Content.d.ts +67 -0
- package/src/Content.js +291 -0
- package/src/Content.js.map +1 -0
- package/src/ContentTransfer-CRQet3QJ.d.ts +48 -0
- package/src/{ContentTransfer-C3Av9sWz.js → ContentTransfer-CTzGGKFV.js} +8 -4
- package/src/ContentTransfer-CTzGGKFV.js.map +1 -0
- package/src/{DocumentEngine-J7g-emdf.js → DocumentEngine-BbhfnTAc.js} +4 -4
- package/src/{DocumentEngine-J7g-emdf.js.map → DocumentEngine-BbhfnTAc.js.map} +1 -1
- package/src/DocumentModel.d.ts +8 -1
- package/src/DocumentModel.js +29 -2
- package/src/DocumentModel.js.map +1 -1
- package/src/{Forms-Dj6NuW66.js → Forms-02-p8qEw.js} +2 -2
- package/src/{Forms-Dj6NuW66.js.map → Forms-02-p8qEw.js.map} +1 -1
- package/src/{Forms-BsJMev5_.d.ts → Forms-BorTcIlp.d.ts} +2 -5
- package/src/Forms.d.ts +25 -14
- package/src/Forms.js.map +1 -1
- package/src/I18n-BXlVunfa.d.ts +27 -0
- package/src/I18n.d.ts +2 -20
- package/src/Module.js +14 -5
- package/src/Module.js.map +1 -1
- package/src/{S3MediaObjects-CpPSzJto.js → S3MediaObjects-DC0c8sYC.js} +54 -35
- package/src/{S3MediaObjects-CpPSzJto.js.map → S3MediaObjects-DC0c8sYC.js.map} +1 -1
- package/src/cli/content.js +63 -221
- package/src/cli/content.js.map +1 -1
- package/src/cli.js +93 -4
- package/src/cli.js.map +1 -1
- package/src/{connection-CBAy2qGy.js → connection-BPf4BQUZ.js} +2 -2
- package/src/{connection-CBAy2qGy.js.map → connection-BPf4BQUZ.js.map} +1 -1
- package/src/dev-lifecycle-vQLxwCFq.js +428 -0
- package/src/dev-lifecycle-vQLxwCFq.js.map +1 -0
- package/src/{installation-CNfIriVm.js → installation-DsGst1BH.js} +578 -457
- package/src/installation-DsGst1BH.js.map +1 -0
- package/src/internal/application-types.js +43 -0
- package/src/internal/application-types.js.map +1 -0
- package/src/internal/authoring-catalog.js +9 -4
- package/src/internal/authoring-catalog.js.map +1 -1
- package/src/internal/authoring-metadata.js +1 -1
- package/src/internal/configuration.js +10 -6
- package/src/internal/configuration.js.map +1 -1
- package/src/internal/dev-lifecycle.js +3 -0
- package/src/internal/document-model.js +2 -2
- package/src/internal/document-model.js.map +1 -1
- package/src/internal/i18n.js +21 -0
- package/src/internal/i18n.js.map +1 -1
- package/src/{location-BTqdfIzX.js → location-BgYhXEZp.js} +2 -2
- package/src/{location-BTqdfIzX.js.map → location-BgYhXEZp.js.map} +1 -1
- package/src/{models-Bah5B3bT.js → models-DgkGTFus.js} +6 -1
- package/src/models-DgkGTFus.js.map +1 -0
- package/src/{models-B4IoTmp7.js → models-k2nXyD6q.js} +2 -2
- package/src/{models-B4IoTmp7.js.map → models-k2nXyD6q.js.map} +1 -1
- package/src/runtime/delivery/context.js +1 -1
- package/src/runtime/delivery/plugin.js +27 -5
- package/src/runtime/delivery/plugin.js.map +1 -1
- package/src/runtime/delivery/preview.d.ts +1 -1
- package/src/runtime/delivery/types.d.ts +2 -85
- package/src/runtime/delivery/useCmsDocumentSeo.d.ts +8 -1
- package/src/runtime/delivery/useCmsDocumentSeo.js +19 -7
- package/src/runtime/delivery/useCmsDocumentSeo.js.map +1 -1
- package/src/runtime/delivery/useDocument.d.ts +2 -2
- package/src/runtime/delivery/useDocument.js.map +1 -1
- package/src/runtime/delivery/useDocumentQuery.d.ts +3 -3
- package/src/runtime/delivery/useDocumentQuery.js.map +1 -1
- package/src/runtime/forms/document.js +1 -1
- package/src/runtime/forms/useCmsForm.js +81 -41
- package/src/runtime/forms/useCmsForm.js.map +1 -1
- package/src/runtime/i18n/useCmsI18n.d.ts +9 -7
- package/src/runtime/i18n/useCmsI18n.js +5 -4
- package/src/runtime/i18n/useCmsI18n.js.map +1 -1
- package/src/runtime/installation.js +2 -2
- package/src/runtime/plugin.js +1 -1
- package/src/runtime/preview/connection.js +1 -1
- package/src/runtime/preview/controller.js +2 -2
- package/src/runtime/preview/plugin.client.js +3 -3
- package/src/runtime/preview/store.js +1 -1
- package/src/runtime/sitemap.js +1 -1
- package/src/scalars-CA6kyipS.d.ts +7 -0
- package/src/{store-b2CSXgMt.js → store-BYgSt7E3.js} +3 -3
- package/src/{store-b2CSXgMt.js.map → store-BYgSt7E3.js.map} +1 -1
- package/src/types-DutFRtaG.d.ts +90 -0
- package/studio/{CreateDocumentDialog-GGey1uBc.js → CreateDocumentDialog-BdiM77bb.js} +1 -1
- package/studio/{documentPlanning-DRErYmBU.js → documentPlanning-B7fyUj9r.js} +7072 -7065
- package/studio/host.js +2 -2
- package/src/ContentTransfer-C3Av9sWz.js.map +0 -1
- package/src/installation-CNfIriVm.js.map +0 -1
- package/src/models-Bah5B3bT.js.map +0 -1
package/src/cli/content.js
CHANGED
|
@@ -1,258 +1,100 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Config, Console, Crypto, Effect, FileSystem, Option, Path, Redacted, Result, Schema } from "effect";
|
|
4
|
-
import { Cookies, HttpClient, HttpClientRequest } from "effect/unstable/http";
|
|
1
|
+
import { CmsContentError, createCmsContentClient, diffCmsContentSources, readCmsContentDirectory } from "../Content.js";
|
|
2
|
+
import { Config, Console, Effect, Option, Redacted } from "effect";
|
|
5
3
|
import { createTwoFilesPatch } from "diff";
|
|
6
4
|
import { CliError, Prompt } from "effect/unstable/cli";
|
|
7
5
|
|
|
8
6
|
//#region src/cli/content.ts
|
|
9
|
-
const IndexEntry = Schema.Struct({
|
|
10
|
-
sourcePath: ContentSource.fields.sourcePath,
|
|
11
|
-
published: Schema.Boolean,
|
|
12
|
-
sha256: Schema.String.check(Schema.isPattern(/^[a-f0-9]{64}$/u))
|
|
13
|
-
});
|
|
14
|
-
const IndexMetadata = Schema.Struct({
|
|
15
|
-
version: Schema.Literal(1),
|
|
16
|
-
sources: Schema.Array(IndexEntry)
|
|
17
|
-
});
|
|
18
|
-
const ContentIndex = Schema.Struct({
|
|
19
|
-
...IndexMetadata.fields,
|
|
20
|
-
exportSha256: IndexEntry.fields.sha256
|
|
21
|
-
});
|
|
22
7
|
function userError(message) {
|
|
23
8
|
return new CliError.UserError({
|
|
24
9
|
cause: new Error(message),
|
|
25
10
|
userMessage: message
|
|
26
11
|
});
|
|
27
12
|
}
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const metadataHash = Effect.fn("CmsCli.content.metadataHash")(function* (metadata) {
|
|
33
|
-
return yield* hash(yield* Schema.encodeEffect(Schema.fromJsonString(IndexMetadata))(metadata));
|
|
34
|
-
});
|
|
35
|
-
const resolveConnection = Effect.fn("CmsCli.resolveConnection")(function* (options) {
|
|
36
|
-
const configuredUrl = options.url || (yield* Config.String("CMS_URL").pipe(Config.withDefault("")));
|
|
37
|
-
const configuredEmail = options.email || (yield* Config.String("CMS_EMAIL").pipe(Config.withDefault("")));
|
|
38
|
-
const rawUrl = configuredUrl || (yield* Prompt.String({ message: "CMS URL" }));
|
|
39
|
-
const email = configuredEmail || (yield* Prompt.String({ message: "Administrator email" }));
|
|
40
|
-
if (!rawUrl || !email) return yield* userError("CMS URL and administrator email are required.");
|
|
41
|
-
const url = yield* Effect.try({
|
|
42
|
-
try: () => new URL(rawUrl),
|
|
43
|
-
catch: () => userError("CMS URL is invalid.")
|
|
44
|
-
});
|
|
45
|
-
if (url.protocol !== "https:" && !(url.protocol === "http:" && [
|
|
46
|
-
"localhost",
|
|
47
|
-
"127.0.0.1",
|
|
48
|
-
"[::1]"
|
|
49
|
-
].includes(url.hostname))) throw userError("Use HTTPS for CMS credentials; HTTP is allowed only on localhost.");
|
|
50
|
-
if (url.username || url.password || url.search || url.hash) throw userError("CMS URL must be an origin without credentials, query, or fragment.");
|
|
51
|
-
return {
|
|
52
|
-
url: new URL(url.origin),
|
|
53
|
-
email
|
|
54
|
-
};
|
|
55
|
-
});
|
|
56
|
-
const SignIn = Schema.Struct({
|
|
57
|
-
email: Schema.String,
|
|
58
|
-
password: Schema.String
|
|
59
|
-
});
|
|
60
|
-
const ApiError = Schema.Struct({ message: Schema.optionalKey(Schema.String) });
|
|
61
|
-
const connect = Effect.fn("CmsCli.connect")(function* (options) {
|
|
62
|
-
const { url, email } = yield* resolveConnection(options);
|
|
13
|
+
const client = Effect.fn("CmsCli.content.client")(function* (options) {
|
|
14
|
+
const baseUrl = options.url || (yield* Config.String("CMS_URL").pipe(Config.withDefault(""))) || (yield* Prompt.String({ message: "CMS URL" }));
|
|
15
|
+
const email = options.email || (yield* Config.String("CMS_EMAIL").pipe(Config.withDefault(""))) || (yield* Prompt.String({ message: "Administrator email" }));
|
|
16
|
+
if (!baseUrl || !email) return yield* userError("CMS URL and administrator email are required.");
|
|
63
17
|
const configured = yield* Config.Redacted("CMS_PASSWORD").pipe(Config.option);
|
|
64
|
-
const password = Option.isSome(configured) ? configured.value : yield* Prompt.Password({ message: `Password for ${email} at ${
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
18
|
+
const password = Option.isSome(configured) ? Redacted.value(configured.value) : Redacted.value(yield* Prompt.Password({ message: `Password for ${email} at ${baseUrl}` }));
|
|
19
|
+
return yield* attempt(() => createCmsContentClient({
|
|
20
|
+
baseUrl,
|
|
21
|
+
credentials: {
|
|
22
|
+
email,
|
|
23
|
+
password
|
|
24
|
+
}
|
|
69
25
|
}));
|
|
70
|
-
const response = yield* client.execute(signInRequest);
|
|
71
|
-
if (response.status < 200 || response.status >= 300) return yield* userError(`CMS sign-in failed (${response.status}).`);
|
|
72
|
-
const cookie = Cookies.toCookieHeader(response.cookies);
|
|
73
|
-
if (!cookie) return yield* userError("CMS sign-in did not return a session cookie.");
|
|
74
|
-
return {
|
|
75
|
-
url,
|
|
76
|
-
cookie
|
|
77
|
-
};
|
|
78
26
|
});
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
const url = new URL(`${STUDIO_API_PATH}/content/${path}`, session.url);
|
|
82
|
-
const base = (body === void 0 ? HttpClientRequest.get(url) : HttpClientRequest.post(url)).pipe(HttpClientRequest.setHeader("accept", "application/json"), HttpClientRequest.setHeader("cookie", session.cookie));
|
|
83
|
-
const prepared = body === void 0 ? base : yield* HttpClientRequest.bodyJson(base, body);
|
|
84
|
-
const response = yield* client.execute(prepared);
|
|
85
|
-
const payload = yield* response.json;
|
|
86
|
-
if (response.status < 200 || response.status >= 300) {
|
|
87
|
-
const decoded = Schema.decodeUnknownResult(ApiError)(payload);
|
|
88
|
-
return yield* userError(`CMS content ${path} failed: ${Result.isSuccess(decoded) ? decoded.success.message ?? `HTTP ${response.status}` : `HTTP ${response.status}`}`);
|
|
89
|
-
}
|
|
90
|
-
return payload;
|
|
91
|
-
});
|
|
92
|
-
function contentPath(directory, sourcePath, path) {
|
|
93
|
-
const resolved = path.resolve(directory, sourcePath);
|
|
94
|
-
if (!resolved.startsWith(`${directory}${path.sep}`)) throw userError(`Unsafe content path: ${sourcePath}`);
|
|
95
|
-
return resolved;
|
|
96
|
-
}
|
|
97
|
-
const readIndex = Effect.fn("CmsCli.readIndex")(function* (directory) {
|
|
98
|
-
const fileSystem = yield* FileSystem.FileSystem;
|
|
99
|
-
const indexPath = (yield* Path.Path).join(directory, ".cms-content.json");
|
|
100
|
-
if (!(yield* fileSystem.exists(indexPath))) return void 0;
|
|
101
|
-
return yield* Schema.decodeEffect(Schema.fromJsonString(ContentIndex))(yield* fileSystem.readFileString(indexPath));
|
|
27
|
+
const readSources = Effect.fn("CmsCli.content.readSources")(function* (directory) {
|
|
28
|
+
return (yield* attempt(() => readCmsContentDirectory(directory))).sources;
|
|
102
29
|
});
|
|
103
|
-
const readSources = Effect.fn("CmsCli.readSources")(function* (directory) {
|
|
104
|
-
const fileSystem = yield* FileSystem.FileSystem;
|
|
105
|
-
const path = yield* Path.Path;
|
|
106
|
-
const root = path.resolve(directory);
|
|
107
|
-
const index = yield* readIndex(root);
|
|
108
|
-
if (!index) {
|
|
109
|
-
const paths = yield* markdownFiles(root);
|
|
110
|
-
if (paths.length === 0) return yield* userError(`No Markdown files found in ${root}.`);
|
|
111
|
-
return yield* Effect.forEach(paths, (file) => Effect.gen(function* () {
|
|
112
|
-
const sourcePath = path.relative(root, file).replaceAll("\\", "/");
|
|
113
|
-
const markdown = yield* fileSystem.readFileString(file);
|
|
114
|
-
return yield* Schema.decodeEffect(ContentSource)({
|
|
115
|
-
sourcePath,
|
|
116
|
-
markdown,
|
|
117
|
-
published: true
|
|
118
|
-
});
|
|
119
|
-
}));
|
|
120
|
-
}
|
|
121
|
-
const seen = /* @__PURE__ */ new Set();
|
|
122
|
-
const sources = [];
|
|
123
|
-
for (const entry of index.sources) {
|
|
124
|
-
if (seen.has(entry.sourcePath)) return yield* userError(`Duplicate source path in index: ${entry.sourcePath}`);
|
|
125
|
-
seen.add(entry.sourcePath);
|
|
126
|
-
const markdown = yield* fileSystem.readFileString(contentPath(root, entry.sourcePath, path));
|
|
127
|
-
sources.push(ContentSource.make({
|
|
128
|
-
sourcePath: entry.sourcePath,
|
|
129
|
-
markdown,
|
|
130
|
-
published: entry.published
|
|
131
|
-
}));
|
|
132
|
-
}
|
|
133
|
-
return sources;
|
|
134
|
-
});
|
|
135
|
-
function markdownFiles(directory) {
|
|
136
|
-
return Effect.gen(function* () {
|
|
137
|
-
const fileSystem = yield* FileSystem.FileSystem;
|
|
138
|
-
const path = yield* Path.Path;
|
|
139
|
-
const names = yield* fileSystem.readDirectory(directory);
|
|
140
|
-
return (yield* Effect.forEach(names, (name) => Effect.gen(function* () {
|
|
141
|
-
const file = path.join(directory, name);
|
|
142
|
-
const info = yield* fileSystem.stat(file);
|
|
143
|
-
if (info.type === "Directory") return yield* markdownFiles(file);
|
|
144
|
-
return info.type === "File" && [".md", ".mdc"].includes(path.extname(file)) ? [file] : [];
|
|
145
|
-
}))).flat().sort((left, right) => left.localeCompare(right));
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
30
|
const exportContent = Effect.fn("CmsCli.content.export")(function* (connection, directory) {
|
|
149
|
-
const
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
const session = yield* connect(connection);
|
|
153
|
-
const snapshot = yield* Schema.decodeUnknownEffect(ContentSnapshot)(yield* request(session, "snapshot"));
|
|
154
|
-
const previous = yield* readIndex(root);
|
|
155
|
-
const oldEntries = new Map(previous?.sources.map((entry) => [entry.sourcePath, entry]));
|
|
156
|
-
if (previous && (yield* metadataHash({
|
|
157
|
-
version: previous.version,
|
|
158
|
-
sources: previous.sources
|
|
159
|
-
})) !== previous.exportSha256) return yield* userError(`Local edits in ${path.join(root, ".cms-content.json")}; review them before exporting again.`);
|
|
160
|
-
for (const source of snapshot.sources) {
|
|
161
|
-
const file = contentPath(root, source.sourcePath, path);
|
|
162
|
-
if (!(yield* fileSystem.exists(file))) continue;
|
|
163
|
-
const local = yield* fileSystem.readFileString(file);
|
|
164
|
-
const old = oldEntries.get(source.sourcePath);
|
|
165
|
-
if (old ? (yield* hash(local)) !== old.sha256 : local !== source.markdown) return yield* userError(`Local edits in ${file}; review them before exporting again.`);
|
|
166
|
-
}
|
|
167
|
-
yield* fileSystem.makeDirectory(root, { recursive: true });
|
|
168
|
-
for (const source of snapshot.sources) {
|
|
169
|
-
const file = contentPath(root, source.sourcePath, path);
|
|
170
|
-
yield* fileSystem.makeDirectory(path.dirname(file), { recursive: true });
|
|
171
|
-
yield* fileSystem.writeFileString(file, source.markdown);
|
|
172
|
-
}
|
|
173
|
-
const metadata = {
|
|
174
|
-
version: 1,
|
|
175
|
-
sources: yield* Effect.forEach(snapshot.sources, (source) => hash(source.markdown).pipe(Effect.map((sha256) => ({
|
|
176
|
-
sourcePath: source.sourcePath,
|
|
177
|
-
published: source.published,
|
|
178
|
-
sha256
|
|
179
|
-
}))))
|
|
180
|
-
};
|
|
181
|
-
const index = ContentIndex.make({
|
|
182
|
-
...metadata,
|
|
183
|
-
exportSha256: yield* metadataHash(metadata)
|
|
184
|
-
});
|
|
185
|
-
yield* fileSystem.writeFileString(path.join(root, ".cms-content.json"), `${yield* Schema.encodeEffect(Schema.fromJsonString(ContentIndex))(index)}\n`);
|
|
186
|
-
yield* Console.log(`Exported ${index.sources.length} documents to ${root}`);
|
|
31
|
+
const content = yield* client(connection);
|
|
32
|
+
const source = yield* attempt(() => content.export({ directory }));
|
|
33
|
+
yield* Console.log(`Exported ${source.sources.length} documents to ${directory}`);
|
|
187
34
|
});
|
|
188
|
-
const plan = Effect.fn("CmsCli.content.plan")(function* (session, sources, options) {
|
|
189
|
-
return yield* Schema.decodeUnknownEffect(ContentPlan)(yield* request(session, "plan", {
|
|
190
|
-
sources,
|
|
191
|
-
...options
|
|
192
|
-
}));
|
|
193
|
-
});
|
|
194
|
-
function printPlan(value) {
|
|
195
|
-
return Effect.gen(function* () {
|
|
196
|
-
for (const entry of value.entries) yield* Console.log(`${entry.action.padEnd(9)} ${entry.sourcePath}`);
|
|
197
|
-
for (const issue of value.issues) yield* Console.error(`${issue.severity}: ${issue.sourcePath ?? ""} ${issue.message}`);
|
|
198
|
-
yield* Console.log(value.valid ? `Ready at generation ${value.generation}` : "Plan has conflicts or validation errors.");
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
35
|
const preparePlan = Effect.fn("CmsCli.content.preparePlan")(function* (connection, directory, options) {
|
|
202
|
-
const
|
|
203
|
-
const
|
|
204
|
-
const
|
|
205
|
-
yield* printPlan(
|
|
206
|
-
if (!
|
|
36
|
+
const source = yield* attempt(() => readCmsContentDirectory(directory));
|
|
37
|
+
const content = yield* client(connection);
|
|
38
|
+
const plan = yield* attempt(() => content.plan(source, options));
|
|
39
|
+
yield* printPlan(plan);
|
|
40
|
+
if (!plan.valid) return yield* userError("Content plan is not ready to apply.");
|
|
207
41
|
return {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
value
|
|
42
|
+
content,
|
|
43
|
+
plan
|
|
211
44
|
};
|
|
212
45
|
});
|
|
213
46
|
const planContent = Effect.fn("CmsCli.content.planCommand")(function* (connection, directory, options) {
|
|
214
47
|
yield* preparePlan(connection, directory, options);
|
|
215
48
|
});
|
|
49
|
+
const applyContent = Effect.fn("CmsCli.content.apply")(function* (connection, directory, options) {
|
|
50
|
+
const { content, plan } = yield* preparePlan(connection, directory, options);
|
|
51
|
+
const result = yield* attempt(() => content.apply(plan));
|
|
52
|
+
yield* Console.log(`Applied: ${result.created} created, ${result.updated} updated, ${result.published} published, ${result.unchanged} unchanged.`);
|
|
53
|
+
});
|
|
216
54
|
const diffContent = Effect.fn("CmsCli.content.diff")(function* (connection, directory) {
|
|
217
|
-
const
|
|
218
|
-
const
|
|
219
|
-
const comparison =
|
|
55
|
+
const source = yield* attempt(() => readCmsContentDirectory(directory));
|
|
56
|
+
const content = yield* client(connection);
|
|
57
|
+
const comparison = describeContentDiffResult(yield* attempt(() => content.diff(source)));
|
|
220
58
|
for (const line of comparison.lines) yield* Console.log(line);
|
|
221
59
|
yield* Console.log(`${comparison.changed} local document${comparison.changed === 1 ? " differs" : "s differ"} from the CMS.`);
|
|
222
60
|
});
|
|
223
61
|
function describeContentDiff(local, remote) {
|
|
224
|
-
|
|
225
|
-
|
|
62
|
+
return describeContentDiffResult(diffCmsContentSources(local, remote));
|
|
63
|
+
}
|
|
64
|
+
function describeContentDiffResult(comparison) {
|
|
226
65
|
const lines = [];
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const markdownChanged = current?.markdown !== source.markdown;
|
|
231
|
-
const publicationChanged = current !== void 0 && current.published !== source.published;
|
|
232
|
-
if (!markdownChanged && !publicationChanged) continue;
|
|
233
|
-
if (markdownChanged) {
|
|
234
|
-
const patch = createTwoFilesPatch(`remote/${source.sourcePath}`, `local/${source.sourcePath}`, current?.markdown ?? "", source.markdown);
|
|
235
|
-
lines.push(patch.trimEnd());
|
|
236
|
-
}
|
|
237
|
-
if (publicationChanged) lines.push(`Publication ${source.sourcePath}: CMS ${current.published ? "published" : "draft"}, local ${source.published ? "published (use --publish to apply)" : "draft (import keeps existing publication)"}`);
|
|
238
|
-
changed++;
|
|
66
|
+
for (const change of comparison.changes) {
|
|
67
|
+
if (change.markdownChanged && change.local) lines.push(createTwoFilesPatch(`remote/${change.sourcePath}`, `local/${change.sourcePath}`, change.target?.markdown ?? "", change.local.markdown).trimEnd());
|
|
68
|
+
if (change.publicationChanged && change.local && change.target) lines.push(`Publication ${change.sourcePath}: CMS ${change.target.published ? "published" : "draft"}, local ${change.local.published ? "published (use --publish to apply)" : "draft (import keeps existing publication)"}`);
|
|
239
69
|
}
|
|
240
|
-
for (const source of
|
|
70
|
+
for (const source of comparison.targetOnly) lines.push(`Remote only: ${source.sourcePath}`);
|
|
241
71
|
return {
|
|
242
72
|
lines,
|
|
243
|
-
changed
|
|
73
|
+
changed: comparison.changes.length
|
|
244
74
|
};
|
|
245
75
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
});
|
|
76
|
+
function printPlan(value) {
|
|
77
|
+
return Effect.gen(function* () {
|
|
78
|
+
for (const [action, entries] of [
|
|
79
|
+
["create", value.creates],
|
|
80
|
+
["update", value.updates],
|
|
81
|
+
["publish", value.publications],
|
|
82
|
+
["unchanged", value.unchanged],
|
|
83
|
+
["conflict", value.conflicts]
|
|
84
|
+
]) for (const entry of entries) yield* Console.log(`${action.padEnd(9)} ${entry.sourcePath}`);
|
|
85
|
+
for (const issue of value.issues) yield* Console.error(`${issue.severity}: ${issue.sourcePath ?? ""} ${issue.message}`);
|
|
86
|
+
yield* Console.log(value.valid ? `Ready at generation ${value.generation}` : "Plan has conflicts or validation errors.");
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function attempt(operation) {
|
|
90
|
+
return Effect.tryPromise({
|
|
91
|
+
try: () => Promise.resolve(operation()),
|
|
92
|
+
catch: (cause) => {
|
|
93
|
+
return userError(cause instanceof CmsContentError ? `${cause.code}: ${cause.message}` : String(cause));
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
255
97
|
|
|
256
98
|
//#endregion
|
|
257
|
-
export { applyContent,
|
|
99
|
+
export { applyContent, describeContentDiff, diffContent, exportContent, planContent, readSources };
|
|
258
100
|
//# sourceMappingURL=content.js.map
|
package/src/cli/content.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content.js","names":["ContentSourceSchema"],"sources":["../../../../src/cli/content.ts"],"sourcesContent":["import type { ContentSource } from '@internal/protocol/ContentTransfer'\nimport {\n ContentApplyResult,\n ContentPlan,\n ContentSnapshot,\n ContentSource as ContentSourceSchema,\n} from '@internal/protocol/ContentTransfer'\nimport type { ContentApplyInput, ContentTransferInput } from '@internal/protocol/ContentTransfer'\nimport { CMS_AUTHENTICATION_PATH, STUDIO_API_PATH } from '@internal/protocol/HttpPaths'\nimport { Config, Console, Crypto, Effect, FileSystem, Option, Path, Redacted, Result, Schema } from 'effect'\nimport type { PlatformError } from 'effect'\nimport { CliError, Prompt } from 'effect/unstable/cli'\nimport { Cookies, HttpClient, HttpClientRequest } from 'effect/unstable/http'\nimport { createTwoFilesPatch } from 'diff'\n\nconst IndexEntry = Schema.Struct({\n sourcePath: ContentSourceSchema.fields.sourcePath,\n published: Schema.Boolean,\n sha256: Schema.String.check(Schema.isPattern(/^[a-f0-9]{64}$/u)),\n})\n\nconst IndexMetadata = Schema.Struct({ version: Schema.Literal(1), sources: Schema.Array(IndexEntry) })\n\nconst ContentIndex = Schema.Struct({ ...IndexMetadata.fields, exportSha256: IndexEntry.fields.sha256 })\n\ntype ContentIndex = typeof ContentIndex.Type\n\nexport interface ConnectionOptions {\n readonly url: string\n readonly email: string\n}\n\nexport interface ImportOptions {\n readonly overwrite: boolean\n readonly publish: boolean\n}\n\nfunction userError(message: string) {\n return new CliError.UserError({ cause: new Error(message), userMessage: message })\n}\n\nconst hash = Effect.fn('CmsCli.content.hash')(function* (markdown: string) {\n const crypto = yield* Crypto.Crypto\n const digest = yield* crypto.digest('SHA-256', new TextEncoder().encode(markdown))\n\n return Array.from(digest, (byte) => byte.toString(16).padStart(2, '0')).join('')\n})\n\nconst metadataHash = Effect.fn('CmsCli.content.metadataHash')(function* (metadata: typeof IndexMetadata.Type) {\n return yield* hash(yield* Schema.encodeEffect(Schema.fromJsonString(IndexMetadata))(metadata))\n})\n\nconst resolveConnection = Effect.fn('CmsCli.resolveConnection')(function* (options: ConnectionOptions) {\n const configuredUrl = options.url || (yield* Config.String('CMS_URL').pipe(Config.withDefault('')))\n const configuredEmail = options.email || (yield* Config.String('CMS_EMAIL').pipe(Config.withDefault('')))\n const rawUrl = configuredUrl || (yield* Prompt.String({ message: 'CMS URL' }))\n const email = configuredEmail || (yield* Prompt.String({ message: 'Administrator email' }))\n\n if (!rawUrl || !email) return yield* userError('CMS URL and administrator email are required.')\n\n const url = yield* Effect.try({ try: () => new URL(rawUrl), catch: () => userError('CMS URL is invalid.') })\n\n if (\n url.protocol !== 'https:' &&\n !(url.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname))\n )\n throw userError('Use HTTPS for CMS credentials; HTTP is allowed only on localhost.')\n\n if (url.username || url.password || url.search || url.hash)\n throw userError('CMS URL must be an origin without credentials, query, or fragment.')\n\n return { url: new URL(url.origin), email }\n})\n\ninterface Session {\n readonly url: URL\n readonly cookie: string\n}\n\nconst SignIn = Schema.Struct({ email: Schema.String, password: Schema.String })\n\nconst ApiError = Schema.Struct({ message: Schema.optionalKey(Schema.String) })\n\nexport const connect = Effect.fn('CmsCli.connect')(function* (options: ConnectionOptions) {\n const { url, email } = yield* resolveConnection(options)\n\n const configured = yield* Config.Redacted('CMS_PASSWORD').pipe(Config.option)\n\n const password = Option.isSome(configured)\n ? configured.value\n : yield* Prompt.Password({ message: `Password for ${email} at ${url.origin}` })\n\n const client = yield* HttpClient.HttpClient\n\n const signInRequest = yield* HttpClientRequest.post(new URL(`${CMS_AUTHENTICATION_PATH}/sign-in/email`, url)).pipe(\n HttpClientRequest.setHeader('accept', 'application/json'),\n HttpClientRequest.setHeader('origin', url.origin),\n HttpClientRequest.schemaBodyJson(SignIn)({ email, password: Redacted.value(password) }),\n )\n\n const response = yield* client.execute(signInRequest)\n\n if (response.status < 200 || response.status >= 300)\n return yield* userError(`CMS sign-in failed (${response.status}).`)\n\n const cookie = Cookies.toCookieHeader(response.cookies)\n\n if (!cookie) return yield* userError('CMS sign-in did not return a session cookie.')\n\n return { url, cookie } satisfies Session\n})\n\nconst request = Effect.fn('CmsCli.request')(function* (\n session: Session,\n path: string,\n body?: ContentTransferInput | ContentApplyInput,\n) {\n const client = yield* HttpClient.HttpClient\n const url = new URL(`${STUDIO_API_PATH}/content/${path}`, session.url)\n\n const base = (body === undefined ? HttpClientRequest.get(url) : HttpClientRequest.post(url)).pipe(\n HttpClientRequest.setHeader('accept', 'application/json'),\n HttpClientRequest.setHeader('cookie', session.cookie),\n )\n\n const prepared = body === undefined ? base : yield* HttpClientRequest.bodyJson(base, body)\n const response = yield* client.execute(prepared)\n\n const payload = yield* response.json\n\n if (response.status < 200 || response.status >= 300) {\n const decoded = Schema.decodeUnknownResult(ApiError)(payload)\n\n const detail = Result.isSuccess(decoded)\n ? (decoded.success.message ?? `HTTP ${response.status}`)\n : `HTTP ${response.status}`\n\n return yield* userError(`CMS content ${path} failed: ${detail}`)\n }\n\n return payload\n})\n\nfunction contentPath(directory: string, sourcePath: string, path: Path.Path): string {\n const resolved = path.resolve(directory, sourcePath)\n\n if (!resolved.startsWith(`${directory}${path.sep}`)) throw userError(`Unsafe content path: ${sourcePath}`)\n\n return resolved\n}\n\nconst readIndex = Effect.fn('CmsCli.readIndex')(function* (directory: string) {\n const fileSystem = yield* FileSystem.FileSystem\n const path = yield* Path.Path\n const indexPath = path.join(directory, '.cms-content.json')\n\n if (!(yield* fileSystem.exists(indexPath))) return undefined\n\n return yield* Schema.decodeEffect(Schema.fromJsonString(ContentIndex))(yield* fileSystem.readFileString(indexPath))\n})\n\nexport const readSources = Effect.fn('CmsCli.readSources')(function* (directory: string) {\n const fileSystem = yield* FileSystem.FileSystem\n const path = yield* Path.Path\n const root = path.resolve(directory)\n const index = yield* readIndex(root)\n\n if (!index) {\n const paths = yield* markdownFiles(root)\n\n if (paths.length === 0) return yield* userError(`No Markdown files found in ${root}.`)\n\n return yield* Effect.forEach(paths, (file) =>\n Effect.gen(function* () {\n const sourcePath = path.relative(root, file).replaceAll('\\\\', '/')\n const markdown = yield* fileSystem.readFileString(file)\n\n return yield* Schema.decodeEffect(ContentSourceSchema)({ sourcePath, markdown, published: true })\n }),\n )\n }\n\n const seen = new Set<string>()\n const sources: ContentSource[] = []\n\n for (const entry of index.sources) {\n if (seen.has(entry.sourcePath)) return yield* userError(`Duplicate source path in index: ${entry.sourcePath}`)\n seen.add(entry.sourcePath)\n const markdown = yield* fileSystem.readFileString(contentPath(root, entry.sourcePath, path))\n sources.push(ContentSourceSchema.make({ sourcePath: entry.sourcePath, markdown, published: entry.published }))\n }\n\n return sources\n})\n\nfunction markdownFiles(\n directory: string,\n): Effect.Effect<ReadonlyArray<string>, PlatformError.PlatformError, FileSystem.FileSystem | Path.Path> {\n return Effect.gen(function* () {\n const fileSystem = yield* FileSystem.FileSystem\n const path = yield* Path.Path\n const names = yield* fileSystem.readDirectory(directory)\n\n const nested = yield* Effect.forEach(names, (name) =>\n Effect.gen(function* () {\n const file = path.join(directory, name)\n const info = yield* fileSystem.stat(file)\n\n if (info.type === 'Directory') return yield* markdownFiles(file)\n\n return info.type === 'File' && ['.md', '.mdc'].includes(path.extname(file)) ? [file] : []\n }),\n )\n\n return nested.flat().sort((left, right) => left.localeCompare(right))\n })\n}\n\nexport const exportContent = Effect.fn('CmsCli.content.export')(function* (\n connection: ConnectionOptions,\n directory: string,\n) {\n const fileSystem = yield* FileSystem.FileSystem\n const path = yield* Path.Path\n const root = path.resolve(directory)\n const session = yield* connect(connection)\n const snapshot = yield* Schema.decodeUnknownEffect(ContentSnapshot)(yield* request(session, 'snapshot'))\n const previous = yield* readIndex(root)\n const oldEntries = new Map(previous?.sources.map((entry) => [entry.sourcePath, entry]))\n\n if (\n previous &&\n (yield* metadataHash({ version: previous.version, sources: previous.sources })) !== previous.exportSha256\n )\n return yield* userError(\n `Local edits in ${path.join(root, '.cms-content.json')}; review them before exporting again.`,\n )\n\n // Preflight every file before replacing any of them.\n for (const source of snapshot.sources) {\n const file = contentPath(root, source.sourcePath, path)\n\n if (!(yield* fileSystem.exists(file))) continue\n\n const local = yield* fileSystem.readFileString(file)\n const old = oldEntries.get(source.sourcePath)\n\n if (old ? (yield* hash(local)) !== old.sha256 : local !== source.markdown)\n return yield* userError(`Local edits in ${file}; review them before exporting again.`)\n }\n\n yield* fileSystem.makeDirectory(root, { recursive: true })\n\n for (const source of snapshot.sources) {\n const file = contentPath(root, source.sourcePath, path)\n yield* fileSystem.makeDirectory(path.dirname(file), { recursive: true })\n yield* fileSystem.writeFileString(file, source.markdown)\n }\n\n const metadata: typeof IndexMetadata.Type = {\n version: 1,\n sources: yield* Effect.forEach(snapshot.sources, (source) =>\n hash(source.markdown).pipe(\n Effect.map((sha256) => ({\n sourcePath: source.sourcePath,\n published: source.published,\n sha256,\n })),\n ),\n ),\n }\n\n const index = ContentIndex.make({ ...metadata, exportSha256: yield* metadataHash(metadata) })\n\n yield* fileSystem.writeFileString(\n path.join(root, '.cms-content.json'),\n `${yield* Schema.encodeEffect(Schema.fromJsonString(ContentIndex))(index)}\\n`,\n )\n yield* Console.log(`Exported ${index.sources.length} documents to ${root}`)\n})\n\nconst plan = Effect.fn('CmsCli.content.plan')(function* (\n session: Session,\n sources: ReadonlyArray<ContentSource>,\n options: ImportOptions,\n) {\n return yield* Schema.decodeUnknownEffect(ContentPlan)(yield* request(session, 'plan', { sources, ...options }))\n})\n\nfunction printPlan(value: ContentPlan) {\n return Effect.gen(function* () {\n for (const entry of value.entries) yield* Console.log(`${entry.action.padEnd(9)} ${entry.sourcePath}`)\n\n for (const issue of value.issues)\n yield* Console.error(`${issue.severity}: ${issue.sourcePath ?? ''} ${issue.message}`)\n\n yield* Console.log(\n value.valid ? `Ready at generation ${value.generation}` : 'Plan has conflicts or validation errors.',\n )\n })\n}\n\nconst preparePlan = Effect.fn('CmsCli.content.preparePlan')(function* (\n connection: ConnectionOptions,\n directory: string,\n options: ImportOptions,\n) {\n const sources = yield* readSources(directory)\n const session = yield* connect(connection)\n const value = yield* plan(session, sources, options)\n yield* printPlan(value)\n\n if (!value.valid) return yield* userError('Content plan is not ready to apply.')\n\n return { session, sources, value }\n})\n\nexport const planContent = Effect.fn('CmsCli.content.planCommand')(function* (\n connection: ConnectionOptions,\n directory: string,\n options: ImportOptions,\n) {\n yield* preparePlan(connection, directory, options)\n})\n\nexport const diffContent = Effect.fn('CmsCli.content.diff')(function* (\n connection: ConnectionOptions,\n directory: string,\n) {\n const local = yield* readSources(directory)\n const session = yield* connect(connection)\n const remote = yield* Schema.decodeUnknownEffect(ContentSnapshot)(yield* request(session, 'snapshot'))\n const comparison = describeContentDiff(local, remote.sources)\n\n for (const line of comparison.lines) yield* Console.log(line)\n\n yield* Console.log(\n `${comparison.changed} local document${comparison.changed === 1 ? ' differs' : 's differ'} from the CMS.`,\n )\n})\n\nexport function describeContentDiff(local: ReadonlyArray<ContentSource>, remote: ReadonlyArray<ContentSource>) {\n const remoteByPath = new Map(remote.map((source) => [source.sourcePath, source]))\n const localPaths = new Set(local.map((source) => source.sourcePath))\n const lines: string[] = []\n let changed = 0\n\n for (const source of local) {\n const current = remoteByPath.get(source.sourcePath)\n const markdownChanged = current?.markdown !== source.markdown\n const publicationChanged = current !== undefined && current.published !== source.published\n\n if (!markdownChanged && !publicationChanged) continue\n\n if (markdownChanged) {\n const patch = createTwoFilesPatch(\n `remote/${source.sourcePath}`,\n `local/${source.sourcePath}`,\n current?.markdown ?? '',\n source.markdown,\n )\n\n lines.push(patch.trimEnd())\n }\n\n if (publicationChanged)\n lines.push(\n `Publication ${source.sourcePath}: CMS ${current.published ? 'published' : 'draft'}, local ${source.published ? 'published (use --publish to apply)' : 'draft (import keeps existing publication)'}`,\n )\n\n changed++\n }\n\n for (const source of remote) if (!localPaths.has(source.sourcePath)) lines.push(`Remote only: ${source.sourcePath}`)\n\n return { lines, changed }\n}\n\nexport const applyContent = Effect.fn('CmsCli.content.apply')(function* (\n connection: ConnectionOptions,\n directory: string,\n options: ImportOptions,\n) {\n const { session, sources, value } = yield* preparePlan(connection, directory, options)\n\n const result = yield* Schema.decodeUnknownEffect(ContentApplyResult)(\n yield* request(session, 'apply', { sources, ...options, expectedGeneration: value.generation }),\n )\n\n yield* Console.log(\n `Applied: ${result.created} created, ${result.updated} updated, ${result.published} published, ${result.unchanged} unchanged.`,\n )\n})\n"],"mappings":";;;;;;;;AAeA,MAAM,aAAa,OAAO,OAAO;CAC/B,YAAYA,cAAoB,OAAO;CACvC,WAAW,OAAO;CAClB,QAAQ,OAAO,OAAO,MAAM,OAAO,UAAU,iBAAiB,CAAC;AACjE,CAAC;AAED,MAAM,gBAAgB,OAAO,OAAO;CAAE,SAAS,OAAO,QAAQ,CAAC;CAAG,SAAS,OAAO,MAAM,UAAU;AAAE,CAAC;AAErG,MAAM,eAAe,OAAO,OAAO;CAAE,GAAG,cAAc;CAAQ,cAAc,WAAW,OAAO;AAAO,CAAC;AActG,SAAS,UAAU,SAAiB;CAClC,OAAO,IAAI,SAAS,UAAU;EAAE,OAAO,IAAI,MAAM,OAAO;EAAG,aAAa;CAAQ,CAAC;AACnF;AAEA,MAAM,OAAO,O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AAM,UAAU,aAAa,IAAI,OAAO,UAAU;EAClD,MAAM,kBAAkB,SAAS,aAAa,OAAO;EACrD,MAAM,qBAAqB,YAAY,UAAa,QAAQ,cAAc,OAAO;EAEjF,IAAI,CAAC,mBAAmB,CAAC,oBAAoB;EAE7C,IAAI,iBAAiB;GACnB,MAAM,QAAQ,oBACZ,UAAU,OAAO,cACjB,SAAS,OAAO,cAChB,SAAS,YAAY,IACrB,OAAO,QACT;GAEA,MAAM,KAAK,MAAM,QAAQ,CAAC;EAC5B;EAEA,IAAI,oBACF,MAAM,KACJ,eAAe,OAAO,WAAW,QAAQ,QAAQ,YAAY,cAAc,QAAQ,UAAU,OAAO,YAAY,uCAAuC,6CACzJ;EAEF;CACF;CAEA,KAAK,MAAM,UAAU,QAAQ,IAAI,CAAC,WAAW,IAAI,OAAO,UAAU,GAAG,MAAM,KAAK,gBAAgB,OAAO,YAAY;CAEnH,OAAO;EAAE;EAAO;CAAQ;AAC1B;AAEA,MAAa,eAAe,OAAO,GAAG,sBAAsB,CAAC,CAAC,WAC5D,YACA,WACA,SACA;CACA,MAAM,EAAE,SAAS,SAAS,UAAU,OAAO,YAAY,YAAY,WAAW,OAAO;CAErF,MAAM,SAAS,OAAO,OAAO,oBAAoB,kBAAkB,CAAC,CAClE,OAAO,QAAQ,SAAS,SAAS;EAAE;EAAS,GAAG;EAAS,oBAAoB,MAAM;CAAW,CAAC,CAChG;CAEA,OAAO,QAAQ,IACb,YAAY,OAAO,QAAQ,YAAY,OAAO,QAAQ,YAAY,OAAO,UAAU,cAAc,OAAO,UAAU,YACpH;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"content.js","names":[],"sources":["../../../../src/cli/content.ts"],"sourcesContent":["import type { ContentSource } from '@internal/protocol/ContentTransfer'\nimport { CmsContentError, createCmsContentClient, diffCmsContentSources, readCmsContentDirectory } from '../Content'\nimport type { CmsContentDiff, CmsContentPlan } from '../Content'\nimport { Config, Console, Effect, Option, Redacted } from 'effect'\nimport { CliError, Prompt } from 'effect/unstable/cli'\nimport { createTwoFilesPatch } from 'diff'\n\nexport interface ConnectionOptions {\n readonly url: string\n readonly email: string\n}\n\nexport interface ImportOptions {\n readonly overwrite: boolean\n readonly publish: boolean\n}\n\nfunction userError(message: string) {\n return new CliError.UserError({ cause: new Error(message), userMessage: message })\n}\n\nconst client = Effect.fn('CmsCli.content.client')(function* (options: ConnectionOptions) {\n const baseUrl =\n options.url ||\n (yield* Config.String('CMS_URL').pipe(Config.withDefault(''))) ||\n (yield* Prompt.String({ message: 'CMS URL' }))\n\n const email =\n options.email ||\n (yield* Config.String('CMS_EMAIL').pipe(Config.withDefault(''))) ||\n (yield* Prompt.String({ message: 'Administrator email' }))\n\n if (!baseUrl || !email) return yield* userError('CMS URL and administrator email are required.')\n const configured = yield* Config.Redacted('CMS_PASSWORD').pipe(Config.option)\n\n const password = Option.isSome(configured)\n ? Redacted.value(configured.value)\n : Redacted.value(yield* Prompt.Password({ message: `Password for ${email} at ${baseUrl}` }))\n\n return yield* attempt(() => createCmsContentClient({ baseUrl, credentials: { email, password } }))\n})\n\nexport const readSources = Effect.fn('CmsCli.content.readSources')(function* (directory: string) {\n return (yield* attempt(() => readCmsContentDirectory(directory))).sources\n})\n\nexport const exportContent = Effect.fn('CmsCli.content.export')(function* (\n connection: ConnectionOptions,\n directory: string,\n) {\n const content = yield* client(connection)\n const source = yield* attempt(() => content.export({ directory }))\n yield* Console.log(`Exported ${source.sources.length} documents to ${directory}`)\n})\n\nconst preparePlan = Effect.fn('CmsCli.content.preparePlan')(function* (\n connection: ConnectionOptions,\n directory: string,\n options: ImportOptions,\n) {\n const source = yield* attempt(() => readCmsContentDirectory(directory))\n const content = yield* client(connection)\n const plan = yield* attempt(() => content.plan(source, options))\n yield* printPlan(plan)\n\n if (!plan.valid) return yield* userError('Content plan is not ready to apply.')\n\n return { content, plan }\n})\n\nexport const planContent = Effect.fn('CmsCli.content.planCommand')(function* (\n connection: ConnectionOptions,\n directory: string,\n options: ImportOptions,\n) {\n yield* preparePlan(connection, directory, options)\n})\n\nexport const applyContent = Effect.fn('CmsCli.content.apply')(function* (\n connection: ConnectionOptions,\n directory: string,\n options: ImportOptions,\n) {\n const { content, plan } = yield* preparePlan(connection, directory, options)\n const result = yield* attempt(() => content.apply(plan))\n yield* Console.log(\n `Applied: ${result.created} created, ${result.updated} updated, ${result.published} published, ${result.unchanged} unchanged.`,\n )\n})\n\nexport const diffContent = Effect.fn('CmsCli.content.diff')(function* (\n connection: ConnectionOptions,\n directory: string,\n) {\n const source = yield* attempt(() => readCmsContentDirectory(directory))\n const content = yield* client(connection)\n const comparison = describeContentDiffResult(yield* attempt(() => content.diff(source)))\n\n for (const line of comparison.lines) yield* Console.log(line)\n\n yield* Console.log(\n `${comparison.changed} local document${comparison.changed === 1 ? ' differs' : 's differ'} from the CMS.`,\n )\n})\n\nexport function describeContentDiff(local: ReadonlyArray<ContentSource>, remote: ReadonlyArray<ContentSource>) {\n return describeContentDiffResult(diffCmsContentSources(local, remote))\n}\n\nfunction describeContentDiffResult(comparison: CmsContentDiff) {\n const lines: string[] = []\n\n for (const change of comparison.changes) {\n if (change.markdownChanged && change.local) {\n lines.push(\n createTwoFilesPatch(\n `remote/${change.sourcePath}`,\n `local/${change.sourcePath}`,\n change.target?.markdown ?? '',\n change.local.markdown,\n ).trimEnd(),\n )\n }\n\n if (change.publicationChanged && change.local && change.target)\n lines.push(\n `Publication ${change.sourcePath}: CMS ${change.target.published ? 'published' : 'draft'}, local ${change.local.published ? 'published (use --publish to apply)' : 'draft (import keeps existing publication)'}`,\n )\n }\n\n for (const source of comparison.targetOnly) lines.push(`Remote only: ${source.sourcePath}`)\n\n return { lines, changed: comparison.changes.length }\n}\n\nfunction printPlan(value: CmsContentPlan) {\n return Effect.gen(function* () {\n for (const [action, entries] of [\n ['create', value.creates],\n ['update', value.updates],\n ['publish', value.publications],\n ['unchanged', value.unchanged],\n ['conflict', value.conflicts],\n ] as const)\n for (const entry of entries) yield* Console.log(`${action.padEnd(9)} ${entry.sourcePath}`)\n\n for (const issue of value.issues)\n yield* Console.error(`${issue.severity}: ${issue.sourcePath ?? ''} ${issue.message}`)\n\n yield* Console.log(\n value.valid ? `Ready at generation ${value.generation}` : 'Plan has conflicts or validation errors.',\n )\n })\n}\n\nfunction attempt<Value>(operation: () => Value | PromiseLike<Value>) {\n return Effect.tryPromise({\n try: () => Promise.resolve(operation()),\n catch: (cause) => {\n const message = cause instanceof CmsContentError ? `${cause.code}: ${cause.message}` : String(cause)\n\n return userError(message)\n },\n })\n}\n"],"mappings":";;;;;;AAiBA,SAAS,UAAU,SAAiB;CAClC,OAAO,IAAI,SAAS,UAAU;EAAE,OAAO,IAAI,MAAM,OAAO;EAAG,aAAa;CAAQ,CAAC;AACnF;AAEA,MAAM,SAAS,OAAO,GAAG,uBAAuB,CAAC,CAAC,WAAW,SAA4B;CACvF,MAAM,UACJ,QAAQ,QACP,OAAO,OAAO,OAAO,SAAS,CAAC,CAAC,KAAK,OAAO,YAAY,EAAE,CAAC,OAC3D,OAAO,OAAO,OAAO,EAAE,SAAS,UAAU,CAAC;CAE9C,MAAM,QACJ,QAAQ,UACP,OAAO,OAAO,OAAO,WAAW,CAAC,CAAC,KAAK,OAAO,YAAY,EAAE,CAAC,OAC7D,OAAO,OAAO,OAAO,EAAE,SAAS,sBAAsB,CAAC;CAE1D,IAAI,CAAC,WAAW,CAAC,OAAO,OAAO,OAAO,UAAU,+CAA+C;CAC/F,MAAM,aAAa,OAAO,OAAO,SAAS,cAAc,CAAC,CAAC,KAAK,OAAO,MAAM;CAE5E,MAAM,WAAW,OAAO,OAAO,UAAU,IACrC,SAAS,MAAM,WAAW,KAAK,IAC/B,SAAS,MAAM,OAAO,OAAO,SAAS,EAAE,SAAS,gBAAgB,MAAM,MAAM,UAAU,CAAC,CAAC;CAE7F,OAAO,OAAO,cAAc,uBAAuB;EAAE;EAAS,aAAa;GAAE;GAAO;EAAS;CAAE,CAAC,CAAC;AACnG,CAAC;AAED,MAAa,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O,OAAO,CAAC,EAAE,GAAG,MAAM,YAAY;EAE3F,KAAK,MAAM,SAAS,MAAM,QACxB,OAAO,QAAQ,MAAM,GAAG,MAAM,SAAS,IAAI,MAAM,cAAc,GAAG,GAAG,MAAM,SAAS;EAEtF,OAAO,QAAQ,IACb,MAAM,QAAQ,uBAAuB,MAAM,eAAe,0CAC5D;CACF,CAAC;AACH;AAEA,SAAS,QAAe,WAA6C;CACnE,OAAO,OAAO,WAAW;EACvB,WAAW,QAAQ,QAAQ,UAAU,CAAC;EACtC,QAAQ,UAAU;GAGhB,OAAO,UAFS,iBAAiB,kBAAkB,GAAG,MAAM,KAAK,IAAI,MAAM,YAAY,OAAO,KAAK,CAE3E;EAC1B;CACF,CAAC;AACH"}
|
package/src/cli.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { applyContent, diffContent, exportContent, planContent } from "./cli/content.js";
|
|
2
|
-
import {
|
|
2
|
+
import { n as doctorCmsDevelopment, r as resetCmsDevelopment, t as bootstrapCmsDevelopment } from "./dev-lifecycle-vQLxwCFq.js";
|
|
3
|
+
import { Config, Console, Effect, Layer, Match, Option, Redacted } from "effect";
|
|
3
4
|
import { NodeChildProcessSpawner, NodeCrypto, NodeFileSystem, NodePath, NodeRuntime, NodeStdio, NodeTerminal } from "@effect/platform-node-shared";
|
|
4
5
|
import { FetchHttpClient } from "effect/unstable/http";
|
|
5
|
-
import { Argument, Command, Flag } from "effect/unstable/cli";
|
|
6
|
+
import { Argument, CliError, Command, Flag, Prompt } from "effect/unstable/cli";
|
|
6
7
|
|
|
7
8
|
//#region package.json
|
|
8
|
-
var version = "0.
|
|
9
|
+
var version = "0.2.0-beta.1";
|
|
9
10
|
|
|
10
11
|
//#endregion
|
|
11
12
|
//#region src/cli.ts
|
|
@@ -64,7 +65,95 @@ const content = Command.make("content").pipe(Command.withSubcommands([
|
|
|
64
65
|
planCommand,
|
|
65
66
|
applyCommand
|
|
66
67
|
]));
|
|
67
|
-
|
|
68
|
+
const json = Flag.Boolean("json").pipe(Flag.withDefault(false));
|
|
69
|
+
const full = Flag.Boolean("full").pipe(Flag.withDefault(false));
|
|
70
|
+
const rootDirectory = Flag.String("root").pipe(Flag.withDefault("."));
|
|
71
|
+
const contentDirectory = Flag.String("content").pipe(Flag.withDefault(""));
|
|
72
|
+
const administratorName = Flag.String("name").pipe(Flag.withDefault(""));
|
|
73
|
+
const database = Flag.Boolean("database").pipe(Flag.withDefault(false));
|
|
74
|
+
const media = Flag.Boolean("media").pipe(Flag.withDefault(false));
|
|
75
|
+
const bootstrap = Flag.Boolean("bootstrap").pipe(Flag.withDefault(false));
|
|
76
|
+
const yes = Flag.Boolean("yes").pipe(Flag.withDefault(false));
|
|
77
|
+
const doctorCommand = Command.make("doctor", {
|
|
78
|
+
full,
|
|
79
|
+
json,
|
|
80
|
+
rootDirectory
|
|
81
|
+
}, ({ full, json, rootDirectory }) => Effect.gen(function* () {
|
|
82
|
+
const report = yield* attempt(() => doctorCmsDevelopment(rootDirectory, full));
|
|
83
|
+
if (json) yield* Console.log(JSON.stringify(report));
|
|
84
|
+
else for (const check of report.checks) {
|
|
85
|
+
const icon = Match.value(check.status).pipe(Match.when("pass", () => "✓"), Match.when("fail", () => "✗"), Match.when("not-verified", () => "−"), Match.exhaustive);
|
|
86
|
+
yield* Console.log(`${icon} ${check.label}${check.detail ? `\n\n ${check.detail}` : ""}`);
|
|
87
|
+
}
|
|
88
|
+
if (!report.ok) return yield* cliError("One or more CMS development checks failed.");
|
|
89
|
+
})).pipe(Command.withDescription("Check local CMS configuration and infrastructure"));
|
|
90
|
+
const bootstrapCommand = Command.make("bootstrap", {
|
|
91
|
+
contentDirectory,
|
|
92
|
+
administratorName,
|
|
93
|
+
publish,
|
|
94
|
+
rootDirectory
|
|
95
|
+
}, ({ contentDirectory, administratorName, publish, rootDirectory }) => Effect.gen(function* () {
|
|
96
|
+
const administrator = yield* administratorOptions(administratorName, contentDirectory, publish);
|
|
97
|
+
const result = yield* attempt(() => bootstrapCmsDevelopment({
|
|
98
|
+
rootDir: rootDirectory,
|
|
99
|
+
...administrator
|
|
100
|
+
}));
|
|
101
|
+
yield* Console.log(`Bootstrap complete: administrator ${result.administrator}${result.contentApplied ? ", content applied" : ""}.`);
|
|
102
|
+
})).pipe(Command.withDescription("Provision and initialize a local CMS installation"));
|
|
103
|
+
const resetCommand = Command.make("reset", {
|
|
104
|
+
bootstrap,
|
|
105
|
+
contentDirectory,
|
|
106
|
+
database,
|
|
107
|
+
json,
|
|
108
|
+
media,
|
|
109
|
+
publish,
|
|
110
|
+
rootDirectory,
|
|
111
|
+
yes
|
|
112
|
+
}, ({ bootstrap, contentDirectory, database, json, media, publish, rootDirectory, yes }) => Effect.gen(function* () {
|
|
113
|
+
if (!database && !media) return yield* cliError("Choose --database, --media, or both.");
|
|
114
|
+
const administrator = bootstrap && yes ? yield* administratorOptions("", contentDirectory, publish) : void 0;
|
|
115
|
+
const result = yield* attempt(() => resetCmsDevelopment({
|
|
116
|
+
rootDir: rootDirectory,
|
|
117
|
+
database,
|
|
118
|
+
media,
|
|
119
|
+
yes,
|
|
120
|
+
bootstrap: administrator
|
|
121
|
+
}));
|
|
122
|
+
if (json) return yield* Console.log(JSON.stringify(result));
|
|
123
|
+
for (const target of result.targets) yield* Console.log(`${result.executed ? "✓ Reset" : "Would reset"} ${target}`);
|
|
124
|
+
if (!result.executed) yield* Console.log("Dry run only. Add --yes to execute these exact targets.");
|
|
125
|
+
})).pipe(Command.withDescription("Reset CMS-owned local data with a dry-run default"));
|
|
126
|
+
const dev = Command.make("dev").pipe(Command.withSubcommands([
|
|
127
|
+
doctorCommand,
|
|
128
|
+
bootstrapCommand,
|
|
129
|
+
resetCommand
|
|
130
|
+
]));
|
|
131
|
+
const administratorOptions = Effect.fn("CmsCli.dev.administratorOptions")(function* (name, content, publishContent) {
|
|
132
|
+
const email = yield* Config.String("CMS_EMAIL").pipe(Config.withDefault(""));
|
|
133
|
+
const configuredPassword = yield* Config.Redacted("CMS_PASSWORD").pipe(Config.option);
|
|
134
|
+
const password = Option.isSome(configuredPassword) ? Redacted.value(configuredPassword.value) : Redacted.value(yield* Prompt.Password({ message: `Password for ${email || "local administrator"}` }));
|
|
135
|
+
if (!email || !password) return yield* cliError("CMS_EMAIL and CMS_PASSWORD are required.");
|
|
136
|
+
return {
|
|
137
|
+
content: content || void 0,
|
|
138
|
+
email,
|
|
139
|
+
name: name || (yield* Config.String("CMS_NAME").pipe(Config.withDefault("Administrator"))),
|
|
140
|
+
password,
|
|
141
|
+
publish: publishContent
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
function cliError(message) {
|
|
145
|
+
return new CliError.UserError({
|
|
146
|
+
cause: new Error(message),
|
|
147
|
+
userMessage: message
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function attempt(operation) {
|
|
151
|
+
return Effect.tryPromise({
|
|
152
|
+
try: () => Promise.resolve(operation()),
|
|
153
|
+
catch: (cause) => cliError(cause instanceof Error ? cause.message : String(cause))
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
root.pipe(Command.withSubcommands([content, dev]), Command.run({ version }), Effect.provide(Layer.mergeAll(NodeFileSystem.layer, NodePath.layer, NodeCrypto.layer, NodeTerminal.layer, NodeStdio.layer, NodeChildProcessSpawner.layer.pipe(Layer.provide(Layer.merge(NodeFileSystem.layer, NodePath.layer))), FetchHttpClient.layer)), NodeRuntime.runMain);
|
|
68
157
|
|
|
69
158
|
//#endregion
|
|
70
159
|
export { };
|
package/src/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","names":["packageManifest.version"],"sources":["../../../package.json","../../../src/cli.ts"],"sourcesContent":["","import {\n NodeChildProcessSpawner,\n NodeCrypto,\n NodeFileSystem,\n NodePath,\n NodeRuntime,\n NodeStdio,\n NodeTerminal,\n} from '@effect/platform-node-shared'\nimport { Effect, Layer } from 'effect'\nimport { Argument, Command, Flag } from 'effect/unstable/cli'\nimport { FetchHttpClient } from 'effect/unstable/http'\nimport packageManifest from '../package.json' with { type: 'json' }\nimport { applyContent, diffContent, exportContent, planContent } from './cli/content'\n\nconst root = Command.make('cms').pipe(\n Command.withSharedFlags({\n url: Flag.String('url').pipe(Flag.withDefault('')),\n email: Flag.String('email').pipe(Flag.withDefault('')),\n }),\n)\n\nconst directory = Argument.String('directory')\n\nconst overwrite = Flag.Boolean('overwrite').pipe(Flag.withDefault(false))\n\nconst publish = Flag.Boolean('publish').pipe(Flag.withDefault(false))\n\nconst exportCommand = Command.make('export', { directory }, ({ directory }) =>\n Effect.gen(function* () {\n const { url, email } = yield* root\n\n return yield* exportContent({ url, email }, directory)\n }),\n).pipe(Command.withDescription('Export authored Markdown from a CMS installation'))\n\nconst diffCommand = Command.make('diff', { directory }, ({ directory }) =>\n Effect.gen(function* () {\n const { url, email } = yield* root\n\n return yield* diffContent({ url, email }, directory)\n }),\n).pipe(Command.withDescription('Compare local Markdown with the current CMS content'))\n\nconst planCommand = Command.make('plan', { directory, overwrite, publish }, ({ directory, overwrite, publish }) =>\n Effect.gen(function* () {\n const { url, email } = yield* root\n\n return yield* planContent({ url, email }, directory, { overwrite, publish })\n }),\n).pipe(Command.withDescription('Preview content changes without writing to the CMS'))\n\nconst applyCommand = Command.make('apply', { directory, overwrite, publish }, ({ directory, overwrite, publish }) =>\n Effect.gen(function* () {\n const { url, email } = yield* root\n\n return yield* applyContent({ url, email }, directory, { overwrite, publish })\n }),\n).pipe(Command.withDescription('Apply a validated content plan to the CMS'))\n\nconst content = Command.make('content').pipe(\n Command.withSubcommands([exportCommand, diffCommand, planCommand, applyCommand]),\n)\n\nroot.pipe(\n Command.withSubcommands([content]),\n Command.run({ version: packageManifest.version }),\n Effect.provide(\n Layer.mergeAll(\n NodeFileSystem.layer,\n NodePath.layer,\n NodeCrypto.layer,\n NodeTerminal.layer,\n NodeStdio.layer,\n NodeChildProcessSpawner.layer.pipe(Layer.provide(Layer.merge(NodeFileSystem.layer, NodePath.layer))),\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,CACH,CAAC,CAAC,KAAK,QAAQ,gBAAgB,oDAAoD,CAAC;AAEpF,MAAM,eAAe,QAAQ,KAAK,SAAS;CAAE;CAAW;CAAW;AAAQ,IAAI,EAAE,WAAW,WAAW,cACrG,OAAO,IAAI,aAAa;CACtB,MAAM,EAAE,KAAK,UAAU,OAAO;CAE9B,OAAO,OAAO,aAAa;EAAE;EAAK;CAAM,GAAG,WAAW;EAAE;EAAW;CAAQ,CAAC;AAC9E,CAAC,CACH,CAAC,CAAC,KAAK,QAAQ,gBAAgB,2CAA2C,CAAC;AAE3E,MAAM,UAAU,QAAQ,KAAK,SAAS,CAAC,CAAC,KACtC,QAAQ,gBAAgB;CAAC;CAAe;CAAa;CAAa;AAAY,CAAC,CACjF;AAEA,KAAK,KACH,QAAQ,gBAAgB,CAAC,OAAO,CAAC,GACjC,QAAQ,IAAI,EAAWA,QAAwB,CAAC,GAChD,OAAO,QACL,MAAM,SACJ,eAAe,OACf,SAAS,OACT,WAAW,OACX,aAAa,OACb,UAAU,OACV,wBAAwB,MAAM,KAAK,MAAM,QAAQ,MAAM,MAAM,eAAe,OAAO,SAAS,KAAK,CAAC,CAAC,GACnG,gBAAgB,KAClB,CACF,GACA,YAAY,OACd"}
|
|
1
|
+
{"version":3,"file":"cli.js","names":["packageManifest.version"],"sources":["../../../package.json","../../../src/cli.ts"],"sourcesContent":["","import {\n NodeChildProcessSpawner,\n NodeCrypto,\n NodeFileSystem,\n NodePath,\n NodeRuntime,\n NodeStdio,\n NodeTerminal,\n} from '@effect/platform-node-shared'\nimport { Config, Console, Effect, Layer, Match, Option, Redacted } from 'effect'\nimport { Argument, CliError, Command, Flag, Prompt } from 'effect/unstable/cli'\nimport { FetchHttpClient } from 'effect/unstable/http'\nimport packageManifest from '../package.json' with { type: 'json' }\nimport { applyContent, diffContent, exportContent, planContent } from './cli/content'\nimport { bootstrapCmsDevelopment, doctorCmsDevelopment, resetCmsDevelopment } from './internal/dev-lifecycle'\n\nconst root = Command.make('cms').pipe(\n Command.withSharedFlags({\n url: Flag.String('url').pipe(Flag.withDefault('')),\n email: Flag.String('email').pipe(Flag.withDefault('')),\n }),\n)\n\nconst directory = Argument.String('directory')\n\nconst overwrite = Flag.Boolean('overwrite').pipe(Flag.withDefault(false))\n\nconst publish = Flag.Boolean('publish').pipe(Flag.withDefault(false))\n\nconst exportCommand = Command.make('export', { directory }, ({ directory }) =>\n Effect.gen(function* () {\n const { url, email } = yield* root\n\n return yield* exportContent({ url, email }, directory)\n }),\n).pipe(Command.withDescription('Export authored Markdown from a CMS installation'))\n\nconst diffCommand = Command.make('diff', { directory }, ({ directory }) =>\n Effect.gen(function* () {\n const { url, email } = yield* root\n\n return yield* diffContent({ url, email }, directory)\n }),\n).pipe(Command.withDescription('Compare local Markdown with the current CMS content'))\n\nconst planCommand = Command.make('plan', { directory, overwrite, publish }, ({ directory, overwrite, publish }) =>\n Effect.gen(function* () {\n const { url, email } = yield* root\n\n return yield* planContent({ url, email }, directory, { overwrite, publish })\n }),\n).pipe(Command.withDescription('Preview content changes without writing to the CMS'))\n\nconst applyCommand = Command.make('apply', { directory, overwrite, publish }, ({ directory, overwrite, publish }) =>\n Effect.gen(function* () {\n const { url, email } = yield* root\n\n return yield* applyContent({ url, email }, directory, { overwrite, publish })\n }),\n).pipe(Command.withDescription('Apply a validated content plan to the CMS'))\n\nconst content = Command.make('content').pipe(\n Command.withSubcommands([exportCommand, diffCommand, planCommand, applyCommand]),\n)\n\nconst json = Flag.Boolean('json').pipe(Flag.withDefault(false))\n\nconst full = Flag.Boolean('full').pipe(Flag.withDefault(false))\n\nconst rootDirectory = Flag.String('root').pipe(Flag.withDefault('.'))\n\nconst contentDirectory = Flag.String('content').pipe(Flag.withDefault(''))\n\nconst administratorName = Flag.String('name').pipe(Flag.withDefault(''))\n\nconst database = Flag.Boolean('database').pipe(Flag.withDefault(false))\n\nconst media = Flag.Boolean('media').pipe(Flag.withDefault(false))\n\nconst bootstrap = Flag.Boolean('bootstrap').pipe(Flag.withDefault(false))\n\nconst yes = Flag.Boolean('yes').pipe(Flag.withDefault(false))\n\nconst doctorCommand = Command.make('doctor', { full, json, rootDirectory }, ({ full, json, rootDirectory }) =>\n Effect.gen(function* () {\n const report = yield* attempt(() => doctorCmsDevelopment(rootDirectory, full))\n\n if (json) {\n // oxlint-disable-next-line effecttsgo/prefer-schema-over-json -- This flag intentionally emits the report's exact JSON representation.\n yield* Console.log(JSON.stringify(report))\n } else\n for (const check of report.checks) {\n const icon = Match.value(check.status).pipe(\n Match.when('pass', () => '✓'),\n Match.when('fail', () => '✗'),\n Match.when('not-verified', () => '−'),\n Match.exhaustive,\n )\n\n yield* Console.log(`${icon} ${check.label}${check.detail ? `\\n\\n ${check.detail}` : ''}`)\n }\n\n if (!report.ok) return yield* cliError('One or more CMS development checks failed.')\n }),\n).pipe(Command.withDescription('Check local CMS configuration and infrastructure'))\n\nconst bootstrapCommand = Command.make(\n 'bootstrap',\n { contentDirectory, administratorName, publish, rootDirectory },\n ({ contentDirectory, administratorName, publish, rootDirectory }) =>\n Effect.gen(function* () {\n const administrator = yield* administratorOptions(administratorName, contentDirectory, publish)\n const result = yield* attempt(() => bootstrapCmsDevelopment({ rootDir: rootDirectory, ...administrator }))\n yield* Console.log(\n `Bootstrap complete: administrator ${result.administrator}${result.contentApplied ? ', content applied' : ''}.`,\n )\n }),\n).pipe(Command.withDescription('Provision and initialize a local CMS installation'))\n\nconst resetCommand = Command.make(\n 'reset',\n { bootstrap, contentDirectory, database, json, media, publish, rootDirectory, yes },\n ({ bootstrap, contentDirectory, database, json, media, publish, rootDirectory, yes }) =>\n Effect.gen(function* () {\n if (!database && !media) return yield* cliError('Choose --database, --media, or both.')\n const administrator = bootstrap && yes ? yield* administratorOptions('', contentDirectory, publish) : undefined\n\n const result = yield* attempt(() =>\n resetCmsDevelopment({ rootDir: rootDirectory, database, media, yes, bootstrap: administrator }),\n )\n\n if (json) {\n // oxlint-disable-next-line effecttsgo/prefer-schema-over-json -- This flag intentionally emits the report's exact JSON representation.\n return yield* Console.log(JSON.stringify(result))\n }\n\n for (const target of result.targets)\n yield* Console.log(`${result.executed ? '✓ Reset' : 'Would reset'} ${target}`)\n\n if (!result.executed) yield* Console.log('Dry run only. Add --yes to execute these exact targets.')\n }),\n).pipe(Command.withDescription('Reset CMS-owned local data with a dry-run default'))\n\nconst dev = Command.make('dev').pipe(Command.withSubcommands([doctorCommand, bootstrapCommand, resetCommand]))\n\nconst administratorOptions = Effect.fn('CmsCli.dev.administratorOptions')(function* (\n name: string,\n content: string,\n publishContent: boolean,\n) {\n const email = yield* Config.String('CMS_EMAIL').pipe(Config.withDefault(''))\n const configuredPassword = yield* Config.Redacted('CMS_PASSWORD').pipe(Config.option)\n\n const password = Option.isSome(configuredPassword)\n ? Redacted.value(configuredPassword.value)\n : Redacted.value(yield* Prompt.Password({ message: `Password for ${email || 'local administrator'}` }))\n\n if (!email || !password) return yield* cliError('CMS_EMAIL and CMS_PASSWORD are required.')\n\n return {\n content: content || undefined,\n email,\n name: name || (yield* Config.String('CMS_NAME').pipe(Config.withDefault('Administrator'))),\n password,\n publish: publishContent,\n }\n})\n\nfunction cliError(message: string) {\n return new CliError.UserError({ cause: new Error(message), userMessage: message })\n}\n\nfunction attempt<Value>(operation: () => Value | PromiseLike<Value>) {\n return Effect.tryPromise({\n try: () => Promise.resolve(operation()),\n catch: (cause) => cliError(cause instanceof Error ? cause.message : String(cause)),\n })\n}\n\nroot.pipe(\n Command.withSubcommands([content, dev]),\n Command.run({ version: packageManifest.version }),\n Effect.provide(\n Layer.mergeAll(\n NodeFileSystem.layer,\n NodePath.layer,\n NodeCrypto.layer,\n NodeTerminal.layer,\n NodeStdio.layer,\n NodeChildProcessSpawner.layer.pipe(Layer.provide(Layer.merge(NodeFileSystem.layer, NodePath.layer))),\n FetchHttpClient.layer,\n ),\n ),\n NodeRuntime.runMain,\n)\n"],"mappings":";;;;;;;;;;;;ACgBA,MAAM,OAAO,QAAQ,KAAK,KAAK,CAAC,CAAC,KA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,QAAe,WAA6C;CACnE,OAAO,OAAO,WAAW;EACvB,WAAW,QAAQ,QAAQ,UAAU,CAAC;EACtC,QAAQ,UAAU,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;CACnF,CAAC;AACH;AAEA,KAAK,KACH,QAAQ,gBAAgB,CAAC,SAAS,GAAG,CAAC,GACtC,QAAQ,IAAI,EAAWA,QAAwB,CAAC,GAChD,OAAO,QACL,MAAM,SACJ,eAAe,OACf,SAAS,OACT,WAAW,OACX,aAAa,OACb,UAAU,OACV,wBAAwB,MAAM,KAAK,MAAM,QAAQ,MAAM,MAAM,eAAe,OAAO,SAAS,KAAK,CAAC,CAAC,GACnG,gBAAgB,KAClB,CACF,GACA,YAAY,OACd"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as DocumentPreviewMessage, o as isDocumentPreviewMessage, t as DOCUMENT_PREVIEW_CHANNEL } from "./models-
|
|
1
|
+
import { n as DocumentPreviewMessage, o as isDocumentPreviewMessage, t as DOCUMENT_PREVIEW_CHANNEL } from "./models-k2nXyD6q.js";
|
|
2
2
|
import { Effect, Exit, Result, Schedule, Schema, Scope } from "effect";
|
|
3
3
|
|
|
4
4
|
//#region ../protocol/src/internal/preview/bridge.ts
|
|
@@ -89,4 +89,4 @@ async function openSitePreviewConnection(options) {
|
|
|
89
89
|
|
|
90
90
|
//#endregion
|
|
91
91
|
export { openSitePreviewConnection as t };
|
|
92
|
-
//# sourceMappingURL=connection-
|
|
92
|
+
//# sourceMappingURL=connection-BPf4BQUZ.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection-CBAy2qGy.js","names":["DocumentPreviewMessageSchema"],"sources":["../../../../protocol/src/internal/preview/bridge.ts","../../../src/runtime/preview/connection.ts"],"sourcesContent":["import type {\n DocumentPreviewMessage,\n PreviewOrigin,\n PreviewSessionId,\n PreviewSitePayload,\n StudioPreviewPayload,\n} from './models'\nimport type { Scope } from 'effect'\nimport { Effect, Result, Schedule, Schema } from 'effect'\nimport {\n DOCUMENT_PREVIEW_CHANNEL,\n DocumentPreviewMessage as DocumentPreviewMessageSchema,\n isDocumentPreviewMessage,\n} from './models'\n\nexport interface PreviewMessageEvent {\n readonly data: unknown\n readonly origin: string\n readonly source: object | null\n}\n\nexport interface PreviewMessageSource {\n readonly addEventListener: (type: 'message', listener: (event: PreviewMessageEvent) => void) => void\n readonly removeEventListener: (type: 'message', listener: (event: PreviewMessageEvent) => void) => void\n}\n\nexport interface PreviewMessageTarget {\n readonly postMessage: (message: DocumentPreviewMessage, targetOrigin: string) => void\n}\n\nexport type PreviewBridgeState = 'closed' | 'connecting' | 'ready'\n\nexport interface PreviewHostBridge {\n readonly reconnect: () => void\n readonly send: (payload: Exclude<StudioPreviewPayload, { readonly type: 'studio:connect' }>) => boolean\n readonly state: () => PreviewBridgeState\n readonly subscribe: (listener: (message: PreviewSitePayload) => void) => () => void\n}\n\nexport interface PreviewSiteBridge {\n readonly send: (payload: Exclude<PreviewSitePayload, { readonly type: 'preview:ready' }>) => boolean\n readonly subscribe: (listener: (message: StudioPreviewPayload) => void) => () => void\n}\n\nexport interface PreviewHostBridgeOptions {\n readonly handshakeIntervalMs?: number\n readonly sessionId: PreviewSessionId\n readonly target: () => PreviewMessageTarget | undefined\n readonly targetOrigin: PreviewOrigin\n readonly view: PreviewMessageSource\n}\n\nexport interface PreviewSiteBridgeOptions {\n readonly parent: PreviewMessageTarget\n readonly parentOrigin: PreviewOrigin\n readonly route: PreviewSitePayload & { readonly type: 'preview:ready' }\n readonly sessionId: PreviewSessionId\n readonly view: PreviewMessageSource\n}\n\nexport function makePreviewHostBridge(\n options: PreviewHostBridgeOptions,\n): Effect.Effect<PreviewHostBridge, never, Scope.Scope> {\n return Effect.acquireRelease(\n Effect.context<never>().pipe(\n Effect.map((context) => createPreviewHostBridge(options, Effect.runForkWith(context))),\n ),\n (bridge) => Effect.sync(() => bridge.close()),\n ).pipe(Effect.map(({ close: _close, ...bridge }) => bridge))\n}\n\nexport function makePreviewSiteBridge(\n options: PreviewSiteBridgeOptions,\n): Effect.Effect<PreviewSiteBridge, never, Scope.Scope> {\n return Effect.acquireRelease(\n Effect.sync(() => createPreviewSiteBridge(options)),\n (bridge) => Effect.sync(() => bridge.close()),\n ).pipe(Effect.map(({ close: _close, ...bridge }) => bridge))\n}\n\nfunction createPreviewHostBridge(options: PreviewHostBridgeOptions, runFork: typeof Effect.runFork) {\n const listeners = new Set<(message: PreviewSitePayload) => void>()\n let retry: ReturnType<typeof Effect.runFork> | undefined\n let state: PreviewBridgeState = 'connecting'\n\n const stopRetry = (): void => {\n if (retry === undefined) return\n retry.interruptUnsafe()\n retry = undefined\n }\n\n const post = (payload: StudioPreviewPayload): boolean => {\n const target = options.target()\n\n if (!target) return false\n target.postMessage(envelope(options.sessionId, payload), options.targetOrigin)\n\n return true\n }\n\n const connect = (): void => {\n post({ type: 'studio:connect' })\n }\n\n const reconnect = (): void => {\n if (state === 'closed') return\n state = 'connecting'\n stopRetry()\n const interval = options.handshakeIntervalMs ?? 250\n retry = runFork(Effect.sync(connect).pipe(Effect.repeat(Schedule.spaced(interval)), Effect.delay(interval)))\n connect()\n }\n\n const receive = (event: PreviewMessageEvent): void => {\n const target = options.target()\n\n if (!target) return\n const message = receivePreviewMessage(event, target, options.targetOrigin, options.sessionId)\n\n if (!message || !isPreviewSiteMessage(message)) return\n\n if (message.type === 'preview:ready') {\n state = 'ready'\n stopRetry()\n }\n\n for (const listener of listeners) listener(message)\n }\n\n const close = (): void => {\n if (state === 'closed') return\n state = 'closed'\n stopRetry()\n options.view.removeEventListener('message', receive)\n listeners.clear()\n }\n\n options.view.addEventListener('message', receive)\n reconnect()\n\n return {\n close,\n reconnect,\n send: (payload: Exclude<StudioPreviewPayload, { readonly type: 'studio:connect' }>) =>\n state === 'ready' && post(payload),\n state: () => state,\n subscribe: (listener: (message: PreviewSitePayload) => void) => {\n listeners.add(listener)\n\n return () => listeners.delete(listener)\n },\n }\n}\n\nfunction createPreviewSiteBridge(options: PreviewSiteBridgeOptions) {\n const listeners = new Set<(message: StudioPreviewPayload) => void>()\n let active = false\n let closed = false\n\n const post = (payload: PreviewSitePayload): boolean => {\n if (!active || closed) return false\n options.parent.postMessage(envelope(options.sessionId, payload), options.parentOrigin)\n\n return true\n }\n\n const receive = (event: PreviewMessageEvent): void => {\n if (closed) return\n const message = receivePreviewMessage(event, options.parent, options.parentOrigin, options.sessionId)\n\n if (!message || !isStudioPreviewMessage(message)) return\n\n if (message.type === 'studio:connect') {\n active = true\n post(options.route)\n }\n\n for (const listener of listeners) listener(message)\n }\n\n const close = (): void => {\n if (closed) return\n closed = true\n active = false\n options.view.removeEventListener('message', receive)\n listeners.clear()\n }\n\n options.view.addEventListener('message', receive)\n\n return {\n close,\n send: post,\n subscribe: (listener: (message: StudioPreviewPayload) => void) => {\n listeners.add(listener)\n\n return () => listeners.delete(listener)\n },\n }\n}\n\nfunction envelope(\n sessionId: PreviewSessionId,\n payload: PreviewSitePayload | StudioPreviewPayload,\n): DocumentPreviewMessage {\n return Result.getOrThrow(\n Schema.decodeResult(Schema.toType(DocumentPreviewMessageSchema))({\n channel: DOCUMENT_PREVIEW_CHANNEL,\n sessionId,\n ...payload,\n }),\n )\n}\n\nfunction receivePreviewMessage(\n event: PreviewMessageEvent,\n source: PreviewMessageTarget,\n origin: PreviewOrigin,\n sessionId: PreviewSessionId,\n): DocumentPreviewMessage | undefined {\n if (event.origin !== origin || event.source !== source || !isDocumentPreviewMessage(event.data)) return undefined\n\n return event.data.sessionId === sessionId ? event.data : undefined\n}\n\nfunction isPreviewSiteMessage(\n message: DocumentPreviewMessage,\n): message is Extract<DocumentPreviewMessage, { readonly type: `preview:${string}` }> {\n return message.type.startsWith('preview:')\n}\n\nfunction isStudioPreviewMessage(\n message: DocumentPreviewMessage,\n): message is Extract<DocumentPreviewMessage, { readonly type: `studio:${string}` }> {\n return message.type.startsWith('studio:')\n}\n","import type { PublicPath } from '@internal/protocol/Documents'\nimport type { PreviewOrigin, PreviewSessionId, PreviewSiteBridge } from '@internal/protocol/Preview'\nimport { makePreviewSiteBridge } from '@internal/protocol/Preview'\nimport { Effect, Exit, Scope } from 'effect'\n\nexport interface SitePreviewConnection extends PreviewSiteBridge {\n readonly close: () => Promise<void>\n}\n\nexport async function openSitePreviewConnection(options: {\n readonly editorOrigin: PreviewOrigin\n readonly route: PublicPath\n readonly sessionId: PreviewSessionId\n}): Promise<SitePreviewConnection> {\n const scope = await Effect.runPromise(Scope.make())\n\n try {\n const bridge = await Effect.runPromise(\n makePreviewSiteBridge({\n parent: window.parent,\n parentOrigin: options.editorOrigin,\n route: { path: options.route, type: 'preview:ready' },\n sessionId: options.sessionId,\n view: window,\n }).pipe(Effect.provideService(Scope.Scope, scope)),\n )\n\n let closed = false\n\n return {\n async close() {\n if (closed) return\n closed = true\n await Effect.runPromise(Scope.close(scope, Exit.void))\n },\n send: bridge.send,\n subscribe: bridge.subscribe,\n }\n } catch (cause) {\n await Effect.runPromise(Scope.close(scope, Exit.fail(cause)))\n throw cause\n }\n}\n"],"mappings":";;;;AAuEA,SAAgB,sBACd,SACsD;CACtD,OAAO,OAAO,eACZ,OAAO,WAAW,wBAAwB,OAAO,CAAC,IACjD,WAAW,OAAO,WAAW,OAAO,MAAM,CAAC,CAC9C,CAAC,CAAC,KAAK,OAAO,KAAK,EAAE,OAAO,QAAQ,GAAG,aAAa,MAAM,CAAC;AAC7D;AA4EA,SAAS,wBAAwB,SAAmC;CAClE,MAAM,4BAAY,IAAI,IAA6C;CACnE,IAAI,SAAS;CACb,IAAI,SAAS;CAEb,MAAM,QAAQ,YAAyC;EACrD,IAAI,CAAC,UAAU,QAAQ,OAAO;EAC9B,QAAQ,OAAO,YAAY,SAAS,QAAQ,WAAW,OAAO,GAAG,QAAQ,YAAY;EAErF,OAAO;CACT;CAEA,MAAM,WAAW,UAAqC;EACpD,IAAI,QAAQ;EACZ,MAAM,UAAU,sBAAsB,OAAO,QAAQ,QAAQ,QAA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AS;EAEb,OAAO;GACL,MAAM,QAAQ;IACZ,IAAI,QAAQ;IACZ,SAAS;IACT,MAAM,OAAO,WAAW,MAAM,MAAM,OAAO,KAAK,IAAI,CAAC;GACvD;GACA,MAAM,OAAO;GACb,WAAW,OAAO;EACpB;CACF,SAAS,OAAO;EACd,MAAM,OAAO,WAAW,MAAM,MAAM,OAAO,KAAK,KAAK,KAAK,CAAC,CAAC;EAC5D,MAAM;CACR;AACF"}
|
|
1
|
+
{"version":3,"file":"connection-BPf4BQUZ.js","names":["DocumentPreviewMessageSchema"],"sources":["../../../../protocol/src/internal/preview/bridge.ts","../../../src/runtime/preview/connection.ts"],"sourcesContent":["import type {\n DocumentPreviewMessage,\n PreviewOrigin,\n PreviewSessionId,\n PreviewSitePayload,\n StudioPreviewPayload,\n} from './models'\nimport type { Scope } from 'effect'\nimport { Effect, Result, Schedule, Schema } from 'effect'\nimport {\n DOCUMENT_PREVIEW_CHANNEL,\n DocumentPreviewMessage as DocumentPreviewMessageSchema,\n isDocumentPreviewMessage,\n} from './models'\n\nexport interface PreviewMessageEvent {\n readonly data: unknown\n readonly origin: string\n readonly source: object | null\n}\n\nexport interface PreviewMessageSource {\n readonly addEventListener: (type: 'message', listener: (event: PreviewMessageEvent) => void) => void\n readonly removeEventListener: (type: 'message', listener: (event: PreviewMessageEvent) => void) => void\n}\n\nexport interface PreviewMessageTarget {\n readonly postMessage: (message: DocumentPreviewMessage, targetOrigin: string) => void\n}\n\nexport type PreviewBridgeState = 'closed' | 'connecting' | 'ready'\n\nexport interface PreviewHostBridge {\n readonly reconnect: () => void\n readonly send: (payload: Exclude<StudioPreviewPayload, { readonly type: 'studio:connect' }>) => boolean\n readonly state: () => PreviewBridgeState\n readonly subscribe: (listener: (message: PreviewSitePayload) => void) => () => void\n}\n\nexport interface PreviewSiteBridge {\n readonly send: (payload: Exclude<PreviewSitePayload, { readonly type: 'preview:ready' }>) => boolean\n readonly subscribe: (listener: (message: StudioPreviewPayload) => void) => () => void\n}\n\nexport interface PreviewHostBridgeOptions {\n readonly handshakeIntervalMs?: number\n readonly sessionId: PreviewSessionId\n readonly target: () => PreviewMessageTarget | undefined\n readonly targetOrigin: PreviewOrigin\n readonly view: PreviewMessageSource\n}\n\nexport interface PreviewSiteBridgeOptions {\n readonly parent: PreviewMessageTarget\n readonly parentOrigin: PreviewOrigin\n readonly route: PreviewSitePayload & { readonly type: 'preview:ready' }\n readonly sessionId: PreviewSessionId\n readonly view: PreviewMessageSource\n}\n\nexport function makePreviewHostBridge(\n options: PreviewHostBridgeOptions,\n): Effect.Effect<PreviewHostBridge, never, Scope.Scope> {\n return Effect.acquireRelease(\n Effect.context<never>().pipe(\n Effect.map((context) => createPreviewHostBridge(options, Effect.runForkWith(context))),\n ),\n (bridge) => Effect.sync(() => bridge.close()),\n ).pipe(Effect.map(({ close: _close, ...bridge }) => bridge))\n}\n\nexport function makePreviewSiteBridge(\n options: PreviewSiteBridgeOptions,\n): Effect.Effect<PreviewSiteBridge, never, Scope.Scope> {\n return Effect.acquireRelease(\n Effect.sync(() => createPreviewSiteBridge(options)),\n (bridge) => Effect.sync(() => bridge.close()),\n ).pipe(Effect.map(({ close: _close, ...bridge }) => bridge))\n}\n\nfunction createPreviewHostBridge(options: PreviewHostBridgeOptions, runFork: typeof Effect.runFork) {\n const listeners = new Set<(message: PreviewSitePayload) => void>()\n let retry: ReturnType<typeof Effect.runFork> | undefined\n let state: PreviewBridgeState = 'connecting'\n\n const stopRetry = (): void => {\n if (retry === undefined) return\n retry.interruptUnsafe()\n retry = undefined\n }\n\n const post = (payload: StudioPreviewPayload): boolean => {\n const target = options.target()\n\n if (!target) return false\n target.postMessage(envelope(options.sessionId, payload), options.targetOrigin)\n\n return true\n }\n\n const connect = (): void => {\n post({ type: 'studio:connect' })\n }\n\n const reconnect = (): void => {\n if (state === 'closed') return\n state = 'connecting'\n stopRetry()\n const interval = options.handshakeIntervalMs ?? 250\n retry = runFork(Effect.sync(connect).pipe(Effect.repeat(Schedule.spaced(interval)), Effect.delay(interval)))\n connect()\n }\n\n const receive = (event: PreviewMessageEvent): void => {\n const target = options.target()\n\n if (!target) return\n const message = receivePreviewMessage(event, target, options.targetOrigin, options.sessionId)\n\n if (!message || !isPreviewSiteMessage(message)) return\n\n if (message.type === 'preview:ready') {\n state = 'ready'\n stopRetry()\n }\n\n for (const listener of listeners) listener(message)\n }\n\n const close = (): void => {\n if (state === 'closed') return\n state = 'closed'\n stopRetry()\n options.view.removeEventListener('message', receive)\n listeners.clear()\n }\n\n options.view.addEventListener('message', receive)\n reconnect()\n\n return {\n close,\n reconnect,\n send: (payload: Exclude<StudioPreviewPayload, { readonly type: 'studio:connect' }>) =>\n state === 'ready' && post(payload),\n state: () => state,\n subscribe: (listener: (message: PreviewSitePayload) => void) => {\n listeners.add(listener)\n\n return () => listeners.delete(listener)\n },\n }\n}\n\nfunction createPreviewSiteBridge(options: PreviewSiteBridgeOptions) {\n const listeners = new Set<(message: StudioPreviewPayload) => void>()\n let active = false\n let closed = false\n\n const post = (payload: PreviewSitePayload): boolean => {\n if (!active || closed) return false\n options.parent.postMessage(envelope(options.sessionId, payload), options.parentOrigin)\n\n return true\n }\n\n const receive = (event: PreviewMessageEvent): void => {\n if (closed) return\n const message = receivePreviewMessage(event, options.parent, options.parentOrigin, options.sessionId)\n\n if (!message || !isStudioPreviewMessage(message)) return\n\n if (message.type === 'studio:connect') {\n active = true\n post(options.route)\n }\n\n for (const listener of listeners) listener(message)\n }\n\n const close = (): void => {\n if (closed) return\n closed = true\n active = false\n options.view.removeEventListener('message', receive)\n listeners.clear()\n }\n\n options.view.addEventListener('message', receive)\n\n return {\n close,\n send: post,\n subscribe: (listener: (message: StudioPreviewPayload) => void) => {\n listeners.add(listener)\n\n return () => listeners.delete(listener)\n },\n }\n}\n\nfunction envelope(\n sessionId: PreviewSessionId,\n payload: PreviewSitePayload | StudioPreviewPayload,\n): DocumentPreviewMessage {\n return Result.getOrThrow(\n Schema.decodeResult(Schema.toType(DocumentPreviewMessageSchema))({\n channel: DOCUMENT_PREVIEW_CHANNEL,\n sessionId,\n ...payload,\n }),\n )\n}\n\nfunction receivePreviewMessage(\n event: PreviewMessageEvent,\n source: PreviewMessageTarget,\n origin: PreviewOrigin,\n sessionId: PreviewSessionId,\n): DocumentPreviewMessage | undefined {\n if (event.origin !== origin || event.source !== source || !isDocumentPreviewMessage(event.data)) return undefined\n\n return event.data.sessionId === sessionId ? event.data : undefined\n}\n\nfunction isPreviewSiteMessage(\n message: DocumentPreviewMessage,\n): message is Extract<DocumentPreviewMessage, { readonly type: `preview:${string}` }> {\n return message.type.startsWith('preview:')\n}\n\nfunction isStudioPreviewMessage(\n message: DocumentPreviewMessage,\n): message is Extract<DocumentPreviewMessage, { readonly type: `studio:${string}` }> {\n return message.type.startsWith('studio:')\n}\n","import type { PublicPath } from '@internal/protocol/Documents'\nimport type { PreviewOrigin, PreviewSessionId, PreviewSiteBridge } from '@internal/protocol/Preview'\nimport { makePreviewSiteBridge } from '@internal/protocol/Preview'\nimport { Effect, Exit, Scope } from 'effect'\n\nexport interface SitePreviewConnection extends PreviewSiteBridge {\n readonly close: () => Promise<void>\n}\n\nexport async function openSitePreviewConnection(options: {\n readonly editorOrigin: PreviewOrigin\n readonly route: PublicPath\n readonly sessionId: PreviewSessionId\n}): Promise<SitePreviewConnection> {\n const scope = await Effect.runPromise(Scope.make())\n\n try {\n const bridge = await Effect.runPromise(\n makePreviewSiteBridge({\n parent: window.parent,\n parentOrigin: options.editorOrigin,\n route: { path: options.route, type: 'preview:ready' },\n sessionId: options.sessionId,\n view: window,\n }).pipe(Effect.provideService(Scope.Scope, scope)),\n )\n\n let closed = false\n\n return {\n async close() {\n if (closed) return\n closed = true\n await Effect.runPromise(Scope.close(scope, Exit.void))\n },\n send: bridge.send,\n subscribe: bridge.subscribe,\n }\n } catch (cause) {\n await Effect.runPromise(Scope.close(scope, Exit.fail(cause)))\n throw cause\n }\n}\n"],"mappings":";;;;AAuEA,SAAgB,sBACd,SACsD;CACt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