@velastack/patterns 0.0.32 → 0.0.34
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/dist/chunks/collections-Czm1C8ow.js +108 -0
- package/dist/chunks/collections-Czm1C8ow.js.map +1 -0
- package/dist/chunks/{env.preview-NYMljYNr.js → env.preview-BZwjhgoi.js} +1 -1
- package/dist/chunks/{env.preview-NYMljYNr.js.map → env.preview-BZwjhgoi.js.map} +1 -1
- package/dist/chunks/{env.runtime-ZUZgxjxf.js → env.runtime-BEdollKV.js} +1 -1
- package/dist/chunks/{env.runtime-ZUZgxjxf.js.map → env.runtime-BEdollKV.js.map} +1 -1
- package/dist/chunks/{generate.runtime-uk9-KOP-.js → generate.runtime-BCz-NNqq.js} +123 -117
- package/dist/chunks/generate.runtime-BCz-NNqq.js.map +1 -0
- package/dist/chunks/{generate.runtime-4zd9-8H_.js → generate.runtime-BZ-KaCUr.js} +118 -102
- package/dist/chunks/generate.runtime-BZ-KaCUr.js.map +1 -0
- package/dist/chunks/generate.runtime-Caqa5ndO.js +85 -0
- package/dist/chunks/{generate.runtime-CbiQhqdL.js.map → generate.runtime-Caqa5ndO.js.map} +1 -1
- package/dist/chunks/{generate.runtime-DpeB3h-P.js → generate.runtime-Cbq2649r.js} +94 -91
- package/dist/chunks/{generate.runtime-DpeB3h-P.js.map → generate.runtime-Cbq2649r.js.map} +1 -1
- package/dist/chunks/{generate.runtime-CXqD8em8.js → generate.runtime-D7ksYerH.js} +133 -130
- package/dist/chunks/{generate.runtime-CXqD8em8.js.map → generate.runtime-D7ksYerH.js.map} +1 -1
- package/dist/chunks/{generate.runtime-BKrtBCRk.js → generate.runtime-DTZ1PeDS.js} +94 -91
- package/dist/chunks/{generate.runtime-BKrtBCRk.js.map → generate.runtime-DTZ1PeDS.js.map} +1 -1
- package/dist/chunks/{generate.runtime-BkjQiaP_.js → generate.runtime-y1Ga_5QI.js} +70 -64
- package/dist/chunks/{generate.runtime-BkjQiaP_.js.map → generate.runtime-y1Ga_5QI.js.map} +1 -1
- package/dist/chunks/logger-BpgCb-ZG.js +9 -0
- package/dist/chunks/logger-BpgCb-ZG.js.map +1 -0
- package/dist/chunks/{modify-file-D3OuKUEp.js → modify-file-DT_1_kvI.js} +1 -1
- package/dist/chunks/{modify-file-D3OuKUEp.js.map → modify-file-DT_1_kvI.js.map} +1 -1
- package/dist/chunks/{modify-svelte-config-remote-Dzgk1OIN.js → modify-svelte-config-remote-CFsA9Mom.js} +1 -1
- package/dist/chunks/{modify-svelte-config-remote-Dzgk1OIN.js.map → modify-svelte-config-remote-CFsA9Mom.js.map} +1 -1
- package/dist/chunks/{pocketbase--dNkFDJ5.js → pocketbase-BiRWz-OQ.js} +1 -1
- package/dist/chunks/{pocketbase--dNkFDJ5.js.map → pocketbase-BiRWz-OQ.js.map} +1 -1
- package/dist/chunks/{pocketbase-W_FJaYZ3.js → pocketbase-iFJBbNJu.js} +1 -1
- package/dist/chunks/{pocketbase-W_FJaYZ3.js.map → pocketbase-iFJBbNJu.js.map} +1 -1
- package/dist/chunks/{ts-morph-helpers-digNcfBD.js → ts-morph-helpers-C0HeJDjR.js} +1 -1
- package/dist/chunks/{ts-morph-helpers-digNcfBD.js.map → ts-morph-helpers-C0HeJDjR.js.map} +1 -1
- package/dist/chunks/write-result-B0-q-egx.js +219 -0
- package/dist/chunks/{write-result-C80dpjr-.js.map → write-result-B0-q-egx.js.map} +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.js +664 -659
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/dist/chunks/collections-BRIAYJ-Q.js +0 -94
- package/dist/chunks/collections-BRIAYJ-Q.js.map +0 -1
- package/dist/chunks/generate.runtime-4zd9-8H_.js.map +0 -1
- package/dist/chunks/generate.runtime-CbiQhqdL.js +0 -82
- package/dist/chunks/generate.runtime-uk9-KOP-.js.map +0 -1
- package/dist/chunks/write-result-C80dpjr-.js +0 -216
|
@@ -1,40 +1,41 @@
|
|
|
1
1
|
import { t as e } from "./dedent-C4qKBQXh.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import u from "
|
|
7
|
-
import d from "node:
|
|
8
|
-
import
|
|
2
|
+
import { n as t } from "./logger-BpgCb-ZG.js";
|
|
3
|
+
import { getMigrationFile as n, migrationDelay as r, withPocketbase as i } from "./pocketbase-iFJBbNJu.js";
|
|
4
|
+
import { modifyOutcomeToFile as a } from "./modify-file-DT_1_kvI.js";
|
|
5
|
+
import { i as o, n as s, r as c } from "./ts-morph-helpers-C0HeJDjR.js";
|
|
6
|
+
import { findSvelteConfigPath as l, modifySvelteConfigRemote as u } from "./modify-svelte-config-remote-CFsA9Mom.js";
|
|
7
|
+
import d from "node:fs";
|
|
8
|
+
import f from "node:path";
|
|
9
|
+
import { Project as p, SyntaxKind as m } from "ts-morph";
|
|
9
10
|
//#region src/patterns/enable/auth-remote/modifies/+layout.server.ts
|
|
10
|
-
function
|
|
11
|
-
if (!
|
|
11
|
+
function h(t) {
|
|
12
|
+
if (!d.existsSync(t)) {
|
|
12
13
|
let n = e`
|
|
13
14
|
export const load = async ({ locals }) => {
|
|
14
15
|
return { user: locals.pb.authStore.record };
|
|
15
16
|
};
|
|
16
17
|
`;
|
|
17
|
-
return
|
|
18
|
+
return d.writeFileSync(t, n), {
|
|
18
19
|
status: "success",
|
|
19
20
|
changed: !0
|
|
20
21
|
};
|
|
21
22
|
}
|
|
22
|
-
let n =
|
|
23
|
+
let n = d.readFileSync(t, "utf8"), r = new p().addSourceFileAtPath(t), i = (e) => {
|
|
23
24
|
e.getProperty("user") || e.addPropertyAssignment({
|
|
24
25
|
name: "user",
|
|
25
26
|
initializer: "locals.pb.authStore.record"
|
|
26
27
|
});
|
|
27
28
|
}, a, o, s = r.getVariableDeclaration("load");
|
|
28
|
-
if (s && s.getFirstAncestorByKind(
|
|
29
|
-
let e = s.getInitializerIfKind(
|
|
29
|
+
if (s && s.getFirstAncestorByKind(m.VariableStatement)?.hasExportKeyword()) {
|
|
30
|
+
let e = s.getInitializerIfKind(m.ArrowFunction);
|
|
30
31
|
if (e) a = e;
|
|
31
32
|
else {
|
|
32
33
|
let e = s.getInitializer();
|
|
33
34
|
if (e) {
|
|
34
|
-
let t = e.getFirstDescendantByKind(
|
|
35
|
+
let t = e.getFirstDescendantByKind(m.ArrowFunction);
|
|
35
36
|
if (t) a = t;
|
|
36
37
|
else {
|
|
37
|
-
let t = e.getFirstDescendantByKind(
|
|
38
|
+
let t = e.getFirstDescendantByKind(m.FunctionExpression);
|
|
38
39
|
t && (o = t);
|
|
39
40
|
}
|
|
40
41
|
}
|
|
@@ -58,7 +59,7 @@ function m(t) {
|
|
|
58
59
|
if (l.length === 0) c.insertParameter(0, { name: "{ locals }" });
|
|
59
60
|
else {
|
|
60
61
|
let e = l[0], t = e.getStructure(), n = e.getNameNode();
|
|
61
|
-
if (n.getKind() ===
|
|
62
|
+
if (n.getKind() === m.ObjectBindingPattern) {
|
|
62
63
|
let r = n;
|
|
63
64
|
if (!r.getElements().some((e) => e.getName() === "locals")) {
|
|
64
65
|
let n = r.getElements().map((e) => e.getText()).filter(Boolean), i = n.length > 0 ? ", " + n.join(", ") : "";
|
|
@@ -79,32 +80,32 @@ function m(t) {
|
|
|
79
80
|
}
|
|
80
81
|
if (a) {
|
|
81
82
|
let e = a.getBody();
|
|
82
|
-
if (e.getKind() ===
|
|
83
|
+
if (e.getKind() === m.Block) e.getDescendantsOfKind(m.ReturnStatement).forEach((e) => {
|
|
83
84
|
let t = e.getExpression();
|
|
84
85
|
if (t) {
|
|
85
|
-
if (t.getKind() ===
|
|
86
|
-
else if (t.getKind() ===
|
|
86
|
+
if (t.getKind() === m.ObjectLiteralExpression) i(t);
|
|
87
|
+
else if (t.getKind() === m.ParenthesizedExpression) {
|
|
87
88
|
let e = t.getExpression();
|
|
88
|
-
e.getKind() ===
|
|
89
|
+
e.getKind() === m.ObjectLiteralExpression && i(e);
|
|
89
90
|
}
|
|
90
91
|
}
|
|
91
92
|
});
|
|
92
93
|
else {
|
|
93
94
|
let t = e;
|
|
94
|
-
if (t.getKind() ===
|
|
95
|
+
if (t.getKind() === m.ParenthesizedExpression) {
|
|
95
96
|
let e = t.getExpression();
|
|
96
|
-
e.getKind() ===
|
|
97
|
-
} else t.getKind() ===
|
|
97
|
+
e.getKind() === m.ObjectLiteralExpression && i(e);
|
|
98
|
+
} else t.getKind() === m.ObjectLiteralExpression && i(t);
|
|
98
99
|
}
|
|
99
100
|
} else if (o) {
|
|
100
101
|
let e = o.getBody();
|
|
101
|
-
e && e.getDescendantsOfKind(
|
|
102
|
+
e && e.getDescendantsOfKind(m.ReturnStatement).forEach((e) => {
|
|
102
103
|
let t = e.getExpression();
|
|
103
104
|
if (t) {
|
|
104
|
-
if (t.getKind() ===
|
|
105
|
-
else if (t.getKind() ===
|
|
105
|
+
if (t.getKind() === m.ObjectLiteralExpression) i(t);
|
|
106
|
+
else if (t.getKind() === m.ParenthesizedExpression) {
|
|
106
107
|
let e = t.getExpression();
|
|
107
|
-
e.getKind() ===
|
|
108
|
+
e.getKind() === m.ObjectLiteralExpression && i(e);
|
|
108
109
|
}
|
|
109
110
|
}
|
|
110
111
|
});
|
|
@@ -116,40 +117,40 @@ function m(t) {
|
|
|
116
117
|
}
|
|
117
118
|
//#endregion
|
|
118
119
|
//#region src/patterns/enable/auth-remote/modifies/root-layout.svelte.ts
|
|
119
|
-
var
|
|
120
|
+
var g = "\n <Navbar.Item\n class=\"flex flex-col md:flex-row items-center space-y-2 md:space-y-0 md:space-x-4\"\n >\n <AuthMenu.Root>\n {#snippet user()}\n <AuthMenu.Trigger>\n <Avatar.Root>\n {#if data.user?.avatar}\n <Avatar.Image\n src=\"/api/files/users/{data.user?.id}/{data.user?.avatar}\"\n alt={data.user?.email}\n />\n {/if}\n <Avatar.Fallback>\n {data.user?.email?.charAt(0).toUpperCase()}\n </Avatar.Fallback>\n </Avatar.Root>\n </AuthMenu.Trigger>\n\n <AuthMenu.Content align=\"end\">\n <AuthMenu.Item href=\"/dashboard\">Dashboard</AuthMenu.Item>\n <AuthMenu.Group>\n <AuthMenu.Item href=\"/settings\">Settings</AuthMenu.Item>\n <AuthMenu.Item>\n <form action=\"/logout\" method=\"post\">\n <button type=\"submit\" class=\"contents\">Logout</button>\n </form>\n </AuthMenu.Item>\n </AuthMenu.Group>\n </AuthMenu.Content>\n {/snippet}\n\n {#snippet guest()}\n <Button\n class=\"w-full justify-start md:justify-center md:w-auto\"\n variant=\"ghost\"\n href=\"/login\">Login</Button\n >\n <Button\n class=\"w-full justify-start md:justify-center md:w-auto\"\n variant=\"outline\"\n href=\"/signup\">Sign up</Button\n >\n {/snippet}\n </AuthMenu.Root>\n </Navbar.Item>\n ", _ = e`
|
|
120
121
|
import * as AuthMenu from '$lib/components/ui/auth-menu';
|
|
121
122
|
import * as Avatar from '$lib/components/ui/avatar';
|
|
122
|
-
`,
|
|
123
|
+
`, v = [
|
|
123
124
|
"Add the auth menu inside your <Navbar.List> in the root layout.",
|
|
124
125
|
"",
|
|
125
126
|
"Imports to add to the <script> tag:",
|
|
126
127
|
"",
|
|
127
|
-
|
|
128
|
+
_,
|
|
128
129
|
"",
|
|
129
130
|
"Markup to add inside <Navbar.List>:",
|
|
130
|
-
|
|
131
|
-
].join("\n"),
|
|
131
|
+
g
|
|
132
|
+
].join("\n"), y = [
|
|
132
133
|
"Create a root layout that includes an auth menu inside <Navbar.List>.",
|
|
133
134
|
"",
|
|
134
135
|
"Imports for the <script> tag:",
|
|
135
136
|
"",
|
|
136
|
-
|
|
137
|
+
_,
|
|
137
138
|
"",
|
|
138
139
|
"Markup inside <Navbar.List>:",
|
|
139
|
-
|
|
140
|
+
g
|
|
140
141
|
].join("\n");
|
|
141
|
-
function
|
|
142
|
-
if (!
|
|
142
|
+
function b(e) {
|
|
143
|
+
if (!d.existsSync(e)) return {
|
|
143
144
|
status: "not-found",
|
|
144
|
-
message:
|
|
145
|
+
message: y
|
|
145
146
|
};
|
|
146
|
-
let t =
|
|
147
|
+
let t = o.fromPath(e);
|
|
147
148
|
return t.hasElement("Navbar.List") ? t.hasElement("AuthMenu.Root") ? {
|
|
148
149
|
status: "success",
|
|
149
150
|
changed: !1
|
|
150
151
|
} : (t.modifyScript((e) => {
|
|
151
|
-
let { source: t } =
|
|
152
|
-
|
|
152
|
+
let { source: t } = c(e, (e) => {
|
|
153
|
+
s(e, [{
|
|
153
154
|
namespaceImport: "AuthMenu",
|
|
154
155
|
moduleSpecifier: "$lib/components/ui/auth-menu"
|
|
155
156
|
}, {
|
|
@@ -158,23 +159,23 @@ function y(e) {
|
|
|
158
159
|
}]);
|
|
159
160
|
});
|
|
160
161
|
return t;
|
|
161
|
-
}), t.insertBeforeClosingTag("Navbar.List",
|
|
162
|
+
}), t.insertBeforeClosingTag("Navbar.List", g), t.writeTo(e), {
|
|
162
163
|
status: "success",
|
|
163
164
|
changed: t.hasChanged()
|
|
164
165
|
}) : {
|
|
165
166
|
status: "failed",
|
|
166
|
-
message:
|
|
167
|
+
message: v
|
|
167
168
|
};
|
|
168
169
|
}
|
|
169
170
|
//#endregion
|
|
170
171
|
//#region src/patterns/enable/auth-remote/modifies/hooks.server.ts
|
|
171
|
-
var
|
|
172
|
+
var x = e`
|
|
172
173
|
Configure handlePocketbase() with the auth option in hooks.server.ts:
|
|
173
174
|
|
|
174
175
|
handlePocketbase({
|
|
175
176
|
auth: { protectedRoutes: ['/(app)'] },
|
|
176
177
|
});
|
|
177
|
-
`,
|
|
178
|
+
`, S = e`
|
|
178
179
|
Create src/hooks.server.ts with handlePocketbase:
|
|
179
180
|
|
|
180
181
|
import { handlePocketbase } from 'velastack/handle';
|
|
@@ -183,15 +184,15 @@ var b = e`
|
|
|
183
184
|
auth: { protectedRoutes: ['/(app)'] },
|
|
184
185
|
});
|
|
185
186
|
`;
|
|
186
|
-
function
|
|
187
|
-
if (!
|
|
187
|
+
function C(e) {
|
|
188
|
+
if (!d.existsSync(e)) return {
|
|
188
189
|
status: "not-found",
|
|
189
|
-
message:
|
|
190
|
+
message: S
|
|
190
191
|
};
|
|
191
|
-
let t =
|
|
192
|
+
let t = d.readFileSync(e, "utf8"), n = new p().addSourceFileAtPath(e), r = n.getDescendantsOfKind(m.CallExpression).find((e) => e.getExpression().getText() === "handlePocketbase");
|
|
192
193
|
if (!r) return {
|
|
193
194
|
status: "failed",
|
|
194
|
-
message:
|
|
195
|
+
message: x
|
|
195
196
|
};
|
|
196
197
|
let i = r.getArguments();
|
|
197
198
|
if (i.length === 0) return r.addArgument("{ auth: { protectedRoutes: ['/(app)'] } }"), n.formatText(), n.saveSync(), {
|
|
@@ -199,11 +200,11 @@ function S(e) {
|
|
|
199
200
|
changed: !0
|
|
200
201
|
};
|
|
201
202
|
let a = i[0];
|
|
202
|
-
if (a.getKind() !==
|
|
203
|
+
if (a.getKind() !== m.ObjectLiteralExpression) return r.insertArgument(0, "{ auth: { protectedRoutes: ['/(app)'] } }"), n.formatText(), n.saveSync(), {
|
|
203
204
|
status: "success",
|
|
204
205
|
changed: !0
|
|
205
206
|
};
|
|
206
|
-
let o = a.asKindOrThrow(
|
|
207
|
+
let o = a.asKindOrThrow(m.ObjectLiteralExpression), s = o.getProperty("auth");
|
|
207
208
|
if (!s) return o.addPropertyAssignment({
|
|
208
209
|
name: "auth",
|
|
209
210
|
initializer: "{ protectedRoutes: ['/(app)'] }"
|
|
@@ -211,17 +212,17 @@ function S(e) {
|
|
|
211
212
|
status: "success",
|
|
212
213
|
changed: !0
|
|
213
214
|
};
|
|
214
|
-
if (s.getKind() ===
|
|
215
|
+
if (s.getKind() === m.PropertyAssignment) {
|
|
215
216
|
let e = s.getInitializer();
|
|
216
|
-
if (e && e.getKind() ===
|
|
217
|
+
if (e && e.getKind() === m.ObjectLiteralExpression) {
|
|
217
218
|
let t = e, n = t.getProperty("protectedRoutes");
|
|
218
219
|
if (!n) t.addPropertyAssignment({
|
|
219
220
|
name: "protectedRoutes",
|
|
220
221
|
initializer: "['/(app)']"
|
|
221
222
|
});
|
|
222
|
-
else if (n.getKind() ===
|
|
223
|
+
else if (n.getKind() === m.PropertyAssignment) {
|
|
223
224
|
let e = n.getInitializer();
|
|
224
|
-
if (e && e.getKind() ===
|
|
225
|
+
if (e && e.getKind() === m.ArrayLiteralExpression) {
|
|
225
226
|
let t = e;
|
|
226
227
|
t.getElements().some((e) => e.getText().replace(/['"`]/g, "") === "/(app)") || t.addElement("'/(app)'");
|
|
227
228
|
} else n.setInitializer("['/(app)']");
|
|
@@ -235,51 +236,53 @@ function S(e) {
|
|
|
235
236
|
}
|
|
236
237
|
//#endregion
|
|
237
238
|
//#region src/patterns/enable/auth-remote/generate.runtime.ts
|
|
238
|
-
async function
|
|
239
|
-
let { input:
|
|
240
|
-
await
|
|
241
|
-
await
|
|
242
|
-
senderName:
|
|
243
|
-
senderAddress:
|
|
244
|
-
} }), await
|
|
245
|
-
let i = await
|
|
239
|
+
async function w(e) {
|
|
240
|
+
let { input: o } = e, s = t(e), c = [];
|
|
241
|
+
await i(e.root, async (t) => {
|
|
242
|
+
s.info("Updating PocketBase mail settings"), await t.settings.update({ meta: {
|
|
243
|
+
senderName: o.senderName,
|
|
244
|
+
senderAddress: o.senderAddress
|
|
245
|
+
} }), await r();
|
|
246
|
+
let i = await t.collections.getOne("users");
|
|
246
247
|
if (!i) throw Error("Users collection not found");
|
|
247
|
-
let
|
|
248
|
-
await
|
|
248
|
+
let a = i.verificationTemplate.body.replace("{APP_URL}/_/#/auth/confirm-verification/{TOKEN}", "{APP_URL}/confirm-verification/{TOKEN}"), l = i.resetPasswordTemplate.body.replace("{APP_URL}/_/#/auth/confirm-reset/{TOKEN}", "{APP_URL}/confirm-reset/{TOKEN}"), u = i.confirmEmailChangeTemplate.body.replace("{APP_URL}/_/#/auth/confirm-email-change/{TOKEN}", "{APP_URL}/confirm-email-change/{TOKEN}");
|
|
249
|
+
s.info("Locking down users collection rules and email templates"), await t.collections.update("users", {
|
|
249
250
|
listRule: "@request.auth.id = id",
|
|
250
251
|
viewRule: "@request.auth.id = id",
|
|
251
252
|
createRule: "@request.auth.id = id",
|
|
252
253
|
updateRule: "@request.auth.id = id",
|
|
253
254
|
deleteRule: "@request.auth.id = id",
|
|
254
|
-
verificationTemplate: { body:
|
|
255
|
-
resetPasswordTemplate: { body:
|
|
256
|
-
confirmEmailChangeTemplate: { body:
|
|
257
|
-
}), await
|
|
258
|
-
let
|
|
259
|
-
|
|
260
|
-
path:
|
|
255
|
+
verificationTemplate: { body: a },
|
|
256
|
+
resetPasswordTemplate: { body: l },
|
|
257
|
+
confirmEmailChangeTemplate: { body: u }
|
|
258
|
+
}), await r();
|
|
259
|
+
let f = n("users", "updated", e);
|
|
260
|
+
f && c.push({
|
|
261
|
+
path: f,
|
|
261
262
|
language: "ts",
|
|
262
|
-
content:
|
|
263
|
+
content: d.readFileSync(f, "utf8"),
|
|
263
264
|
status: "success"
|
|
264
265
|
});
|
|
265
266
|
});
|
|
266
|
-
let
|
|
267
|
-
e &&
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
let
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
let
|
|
280
|
-
|
|
267
|
+
let p = [], m = (e) => {
|
|
268
|
+
e && p.push(e);
|
|
269
|
+
};
|
|
270
|
+
s.info("Modifying +layout.server.ts");
|
|
271
|
+
let g = f.join(e.root, "src", "routes", "+layout.server.ts");
|
|
272
|
+
m(a(g, h(g))), s.info("Modifying root layout");
|
|
273
|
+
let _ = [
|
|
274
|
+
f.join(e.root, "src", "routes", "(public)", "root-layout.svelte"),
|
|
275
|
+
f.join(e.root, "src", "routes", "root-layout.svelte"),
|
|
276
|
+
f.join(e.root, "src", "routes", "(public)", "+layout.svelte"),
|
|
277
|
+
f.join(e.root, "src", "routes", "+layout.svelte")
|
|
278
|
+
], v = _.find((e) => d.existsSync(e)) ?? _[0];
|
|
279
|
+
m(a(v, b(v))), s.info("Modifying hooks.server.ts");
|
|
280
|
+
let y = f.join(e.root, "src", "hooks.server.ts");
|
|
281
|
+
m(a(y, C(y))), s.info("Modifying svelte.config for remote");
|
|
282
|
+
let x = l(e.root);
|
|
283
|
+
return m(a(x, u(x))), {
|
|
281
284
|
creates: [],
|
|
282
|
-
modifies:
|
|
285
|
+
modifies: p,
|
|
283
286
|
deletes: [],
|
|
284
287
|
components: [],
|
|
285
288
|
packages: [],
|
|
@@ -288,6 +291,6 @@ async function C(e) {
|
|
|
288
291
|
};
|
|
289
292
|
}
|
|
290
293
|
//#endregion
|
|
291
|
-
export {
|
|
294
|
+
export { w as generate };
|
|
292
295
|
|
|
293
|
-
//# sourceMappingURL=generate.runtime-
|
|
296
|
+
//# sourceMappingURL=generate.runtime-Cbq2649r.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.runtime-DpeB3h-P.js","names":[],"sources":["../../src/patterns/enable/auth-remote/modifies/+layout.server.ts","../../src/patterns/enable/auth-remote/modifies/root-layout.svelte.ts","../../src/patterns/enable/auth-remote/modifies/hooks.server.ts","../../src/patterns/enable/auth-remote/generate.runtime.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport dedent from \"dedent\";\nimport { Project, SyntaxKind } from \"ts-morph\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\n\nexport function modifyLayoutServer(layoutPath: string): ModifyOutcome {\n if (!fs.existsSync(layoutPath)) {\n const snippet = dedent`\n\t\t\texport const load = async ({ locals }) => {\n\t\t\t\treturn { user: locals.pb.authStore.record };\n\t\t\t};\n\t\t`;\n\n fs.writeFileSync(layoutPath, snippet);\n return { status: \"success\", changed: true };\n }\n\n const originalSource = fs.readFileSync(layoutPath, \"utf8\");\n const project = new Project();\n const sourceFile = project.addSourceFileAtPath(layoutPath);\n\n // helper to add user property to an object literal if missing\n const addUserProp = (obj: import(\"ts-morph\").ObjectLiteralExpression) => {\n const hasUser = !!obj.getProperty(\"user\");\n if (!hasUser) {\n obj.addPropertyAssignment({\n name: \"user\",\n initializer: \"locals.pb.authStore.record\",\n });\n }\n };\n\n // find existing exported load (function or const with arrow)\n let loadArrow: import(\"ts-morph\").ArrowFunction | undefined;\n let loadFn:\n | import(\"ts-morph\").FunctionDeclaration\n | import(\"ts-morph\").FunctionExpression\n | undefined;\n\n const varDecl = sourceFile.getVariableDeclaration(\"load\");\n if (varDecl) {\n const vs = varDecl.getFirstAncestorByKind(SyntaxKind.VariableStatement);\n const isExported = !!vs?.hasExportKeyword();\n if (isExported) {\n const directArrow = varDecl.getInitializerIfKind(\n SyntaxKind.ArrowFunction,\n );\n if (directArrow) {\n loadArrow = directArrow;\n } else {\n const init = varDecl.getInitializer();\n if (init) {\n const innerArrow = init.getFirstDescendantByKind(\n SyntaxKind.ArrowFunction,\n );\n if (innerArrow) {\n loadArrow = innerArrow;\n } else {\n const innerFnExpr = init.getFirstDescendantByKind(\n SyntaxKind.FunctionExpression,\n );\n if (innerFnExpr) {\n loadFn = innerFnExpr;\n }\n }\n }\n }\n }\n }\n\n if (!loadArrow) {\n const maybeFn = sourceFile.getFunction(\"load\");\n if (maybeFn && maybeFn.isExported()) {\n loadFn = maybeFn;\n }\n }\n\n // if no load found, create one\n if (!loadArrow && !loadFn) {\n sourceFile.addFunction({\n isExported: true,\n isAsync: true,\n name: \"load\",\n parameters: [{ name: \"{ locals }\" }],\n statements: `return { user: locals.pb.authStore.record };`,\n });\n sourceFile.formatText();\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n // normalize function-like to a single handle\n const fnLike:\n | import(\"ts-morph\").ArrowFunction\n | import(\"ts-morph\").FunctionDeclaration\n | import(\"ts-morph\").FunctionExpression = (loadArrow ?? loadFn!) as any;\n\n // ensure first parameter destructures locals, preserving existing information\n const params = fnLike.getParameters();\n if (params.length === 0) {\n fnLike.insertParameter(0, { name: \"{ locals }\" });\n } else {\n const first = params[0];\n const original = first.getStructure();\n const nameNode = first.getNameNode();\n if (nameNode.getKind() === SyntaxKind.ObjectBindingPattern) {\n const obp = nameNode as import(\"ts-morph\").ObjectBindingPattern;\n const hasLocals = obp\n .getElements()\n .some((el) => el.getName() === \"locals\");\n if (!hasLocals) {\n const parts = obp\n .getElements()\n .map((el) => el.getText())\n .filter(Boolean);\n const tail = parts.length > 0 ? \", \" + parts.join(\", \") : \"\";\n first.set({\n name: `{ locals${tail} }`,\n type: original.type,\n initializer: (original as any).initializer,\n });\n }\n } else {\n const currentName = first.getName();\n if (currentName !== \"locals\") {\n first.set({\n name: `{ locals, ...${currentName} }`,\n type: original.type,\n initializer: (original as any).initializer,\n });\n }\n }\n }\n\n // add user to returned object literals without altering other properties\n if (loadArrow) {\n const body = loadArrow.getBody();\n if (body.getKind() === SyntaxKind.Block) {\n const returns = body.getDescendantsOfKind(SyntaxKind.ReturnStatement);\n returns.forEach((ret) => {\n const expr = ret.getExpression();\n if (!expr) return;\n if (expr.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(expr as import(\"ts-morph\").ObjectLiteralExpression);\n } else if (expr.getKind() === SyntaxKind.ParenthesizedExpression) {\n const inner = (\n expr as import(\"ts-morph\").ParenthesizedExpression\n ).getExpression();\n if (inner.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(inner as import(\"ts-morph\").ObjectLiteralExpression);\n }\n }\n });\n } else {\n // concise body\n const bodyExpr = body as import(\"ts-morph\").Expression;\n if (bodyExpr.getKind() === SyntaxKind.ParenthesizedExpression) {\n const inner = (\n bodyExpr as import(\"ts-morph\").ParenthesizedExpression\n ).getExpression();\n if (inner.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(inner as import(\"ts-morph\").ObjectLiteralExpression);\n }\n } else if (bodyExpr.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(bodyExpr as import(\"ts-morph\").ObjectLiteralExpression);\n }\n }\n } else if (loadFn) {\n const body = loadFn.getBody();\n if (body) {\n const returns = body.getDescendantsOfKind(SyntaxKind.ReturnStatement);\n returns.forEach((ret) => {\n const expr = ret.getExpression();\n if (!expr) return;\n if (expr.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(expr as import(\"ts-morph\").ObjectLiteralExpression);\n } else if (expr.getKind() === SyntaxKind.ParenthesizedExpression) {\n const inner = (\n expr as import(\"ts-morph\").ParenthesizedExpression\n ).getExpression();\n if (inner.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(inner as import(\"ts-morph\").ObjectLiteralExpression);\n }\n }\n });\n }\n }\n\n sourceFile.formatText();\n sourceFile.saveSync();\n return {\n status: \"success\",\n changed: sourceFile.getFullText() !== originalSource,\n };\n}\n","import fs from \"node:fs\";\nimport dedent from \"dedent\";\nimport { SvelteFile } from \"../../../../runtime/svelte-file\";\nimport {\n ensureImports,\n withInMemoryScript,\n} from \"../../../../runtime/ts-morph-helpers\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\n\nconst AUTH_MENU_NAVBAR_ITEM = `\n\t\t<Navbar.Item\n\t\t\tclass=\"flex flex-col md:flex-row items-center space-y-2 md:space-y-0 md:space-x-4\"\n\t\t>\n\t\t\t<AuthMenu.Root>\n\t\t\t\t{#snippet user()}\n\t\t\t\t\t<AuthMenu.Trigger>\n\t\t\t\t\t\t<Avatar.Root>\n\t\t\t\t\t\t\t{#if data.user?.avatar}\n\t\t\t\t\t\t\t\t<Avatar.Image\n\t\t\t\t\t\t\t\t\tsrc=\"/api/files/users/{data.user?.id}/{data.user?.avatar}\"\n\t\t\t\t\t\t\t\t\talt={data.user?.email}\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t{/if}\n\t\t\t\t\t\t\t<Avatar.Fallback>\n\t\t\t\t\t\t\t\t{data.user?.email?.charAt(0).toUpperCase()}\n\t\t\t\t\t\t\t</Avatar.Fallback>\n\t\t\t\t\t\t</Avatar.Root>\n\t\t\t\t\t</AuthMenu.Trigger>\n\n\t\t\t\t\t<AuthMenu.Content align=\"end\">\n\t\t\t\t\t\t<AuthMenu.Item href=\"/dashboard\">Dashboard</AuthMenu.Item>\n\t\t\t\t\t\t<AuthMenu.Group>\n\t\t\t\t\t\t\t<AuthMenu.Item href=\"/settings\">Settings</AuthMenu.Item>\n\t\t\t\t\t\t\t<AuthMenu.Item>\n\t\t\t\t\t\t\t\t<form action=\"/logout\" method=\"post\">\n\t\t\t\t\t\t\t\t\t<button type=\"submit\" class=\"contents\">Logout</button>\n\t\t\t\t\t\t\t\t</form>\n\t\t\t\t\t\t\t</AuthMenu.Item>\n\t\t\t\t\t\t</AuthMenu.Group>\n\t\t\t\t\t</AuthMenu.Content>\n\t\t\t\t{/snippet}\n\n\t\t\t\t{#snippet guest()}\n\t\t\t\t\t<Button\n\t\t\t\t\t\tclass=\"w-full justify-start md:justify-center md:w-auto\"\n\t\t\t\t\t\tvariant=\"ghost\"\n\t\t\t\t\t\thref=\"/login\">Login</Button\n\t\t\t\t\t>\n\t\t\t\t\t<Button\n\t\t\t\t\t\tclass=\"w-full justify-start md:justify-center md:w-auto\"\n\t\t\t\t\t\tvariant=\"outline\"\n\t\t\t\t\t\thref=\"/signup\">Sign up</Button\n\t\t\t\t\t>\n\t\t\t\t{/snippet}\n\t\t\t</AuthMenu.Root>\n\t\t</Navbar.Item>\n\t`;\n\nconst IMPORT_SNIPPET = dedent`\n import * as AuthMenu from '$lib/components/ui/auth-menu';\n import * as Avatar from '$lib/components/ui/avatar';\n`;\n\nconst FAILURE_HINT = [\n \"Add the auth menu inside your <Navbar.List> in the root layout.\",\n \"\",\n \"Imports to add to the <script> tag:\",\n \"\",\n IMPORT_SNIPPET,\n \"\",\n \"Markup to add inside <Navbar.List>:\",\n AUTH_MENU_NAVBAR_ITEM,\n].join(\"\\n\");\n\nconst NOT_FOUND_HINT = [\n \"Create a root layout that includes an auth menu inside <Navbar.List>.\",\n \"\",\n \"Imports for the <script> tag:\",\n \"\",\n IMPORT_SNIPPET,\n \"\",\n \"Markup inside <Navbar.List>:\",\n AUTH_MENU_NAVBAR_ITEM,\n].join(\"\\n\");\n\nexport function modifyRootLayoutSvelte(layoutPath: string): ModifyOutcome {\n if (!fs.existsSync(layoutPath)) {\n return { status: \"not-found\", message: NOT_FOUND_HINT };\n }\n\n const file = SvelteFile.fromPath(layoutPath);\n if (!file.hasElement(\"Navbar.List\")) {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n if (file.hasElement(\"AuthMenu.Root\")) {\n return { status: \"success\", changed: false };\n }\n\n file.modifyScript((source) => {\n const { source: out } = withInMemoryScript(source, (sf) => {\n ensureImports(sf, [\n {\n namespaceImport: \"AuthMenu\",\n moduleSpecifier: \"$lib/components/ui/auth-menu\",\n },\n {\n namespaceImport: \"Avatar\",\n moduleSpecifier: \"$lib/components/ui/avatar\",\n },\n ]);\n });\n return out;\n });\n\n file.insertBeforeClosingTag(\"Navbar.List\", AUTH_MENU_NAVBAR_ITEM);\n file.writeTo(layoutPath);\n return { status: \"success\", changed: file.hasChanged() };\n}\n","import fs from \"node:fs\";\nimport dedent from \"dedent\";\nimport { Project, SyntaxKind } from \"ts-morph\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\n\nconst FAILURE_HINT = dedent`\n Configure handlePocketbase() with the auth option in hooks.server.ts:\n\n handlePocketbase({\n auth: { protectedRoutes: ['/(app)'] },\n });\n`;\n\nconst NOT_FOUND_HINT = dedent`\n Create src/hooks.server.ts with handlePocketbase:\n\n import { handlePocketbase } from 'velastack/handle';\n\n export const handle = handlePocketbase({\n auth: { protectedRoutes: ['/(app)'] },\n });\n`;\n\nexport function modifyHooksServer(hooksServerPath: string): ModifyOutcome {\n if (!fs.existsSync(hooksServerPath)) {\n return { status: \"not-found\", message: NOT_FOUND_HINT };\n }\n\n const originalSource = fs.readFileSync(hooksServerPath, \"utf8\");\n const project = new Project();\n const sourceFile = project.addSourceFileAtPath(hooksServerPath);\n\n // Find the handlePocketbase(...) call\n const callExpr = sourceFile\n .getDescendantsOfKind(SyntaxKind.CallExpression)\n .find((ce) => ce.getExpression().getText() === \"handlePocketbase\");\n\n if (!callExpr) {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n\n const args = callExpr.getArguments();\n if (args.length === 0) {\n callExpr.addArgument(`{ auth: { protectedRoutes: ['/(app)'] } }`);\n sourceFile.formatText();\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n const firstArg = args[0];\n if (firstArg.getKind() !== SyntaxKind.ObjectLiteralExpression) {\n callExpr.insertArgument(0, `{ auth: { protectedRoutes: ['/(app)'] } }`);\n sourceFile.formatText();\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n const optionsObj = firstArg.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);\n\n // Ensure auth property exists\n const authProp = optionsObj.getProperty(\"auth\");\n if (!authProp) {\n optionsObj.addPropertyAssignment({\n name: \"auth\",\n initializer: `{ protectedRoutes: ['/(app)'] }`,\n });\n sourceFile.formatText();\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n // If auth exists, ensure protectedRoutes includes '/(app)'\n if (authProp.getKind() === SyntaxKind.PropertyAssignment) {\n const authInit = (\n authProp as import(\"ts-morph\").PropertyAssignment\n ).getInitializer();\n if (authInit && authInit.getKind() === SyntaxKind.ObjectLiteralExpression) {\n const authObj = authInit as import(\"ts-morph\").ObjectLiteralExpression;\n const prProp = authObj.getProperty(\"protectedRoutes\");\n if (!prProp) {\n authObj.addPropertyAssignment({\n name: \"protectedRoutes\",\n initializer: `['/(app)']`,\n });\n } else if (prProp.getKind() === SyntaxKind.PropertyAssignment) {\n const prInit = (\n prProp as import(\"ts-morph\").PropertyAssignment\n ).getInitializer();\n if (prInit && prInit.getKind() === SyntaxKind.ArrayLiteralExpression) {\n const arr = prInit as import(\"ts-morph\").ArrayLiteralExpression;\n const hasApp = arr\n .getElements()\n .some((el) => el.getText().replace(/['\"`]/g, \"\") === \"/(app)\");\n if (!hasApp) {\n arr.addElement(`'/(app)'`);\n }\n } else {\n (prProp as import(\"ts-morph\").PropertyAssignment).setInitializer(\n `['/(app)']`,\n );\n }\n }\n }\n }\n\n sourceFile.formatText();\n sourceFile.saveSync();\n return {\n status: \"success\",\n changed: sourceFile.getFullText() !== originalSource,\n };\n}\n","import path from \"node:path\";\nimport fs from \"node:fs\";\nimport { Options, Result, File } from \"../../../core/types\";\nimport {\n getMigrationFile,\n migrationDelay,\n withPocketbase,\n} from \"../../../runtime/pocketbase\";\nimport { modifyOutcomeToFile } from \"../../../runtime/modify-file\";\nimport {\n findSvelteConfigPath,\n modifySvelteConfigRemote,\n} from \"../../../runtime/modify-svelte-config-remote\";\n\nimport { modifyLayoutServer } from \"./modifies/+layout.server\";\nimport { modifyRootLayoutSvelte } from \"./modifies/root-layout.svelte\";\nimport { modifyHooksServer } from \"./modifies/hooks.server\";\n\nexport async function generate(options: Options) {\n const { input } = options;\n\n let creates: File[] = [];\n\n // Update PocketBase\n await withPocketbase(options.root, async (pb) => {\n await pb.settings.update({\n meta: {\n senderName: input.senderName,\n senderAddress: input.senderAddress,\n },\n });\n await migrationDelay();\n\n const userCollection = await pb.collections.getOne(\"users\");\n\n if (!userCollection) {\n throw new Error(\"Users collection not found\");\n }\n\n const verificationTemplate =\n userCollection.verificationTemplate.body.replace(\n \"{APP_URL}/_/#/auth/confirm-verification/{TOKEN}\",\n \"{APP_URL}/confirm-verification/{TOKEN}\",\n );\n const resetPasswordTemplate =\n userCollection.resetPasswordTemplate.body.replace(\n \"{APP_URL}/_/#/auth/confirm-reset/{TOKEN}\",\n \"{APP_URL}/confirm-reset/{TOKEN}\",\n );\n const confirmEmailChangeTemplate =\n userCollection.confirmEmailChangeTemplate.body.replace(\n \"{APP_URL}/_/#/auth/confirm-email-change/{TOKEN}\",\n \"{APP_URL}/confirm-email-change/{TOKEN}\",\n );\n\n // Update user auth rules to lock down access to the user's own account\n await pb.collections.update(\"users\", {\n listRule: \"@request.auth.id = id\",\n viewRule: \"@request.auth.id = id\",\n createRule: \"@request.auth.id = id\",\n updateRule: \"@request.auth.id = id\",\n deleteRule: \"@request.auth.id = id\",\n\n // Update email templates to VelaStack auth urls\n verificationTemplate: {\n body: verificationTemplate,\n },\n resetPasswordTemplate: {\n body: resetPasswordTemplate,\n },\n confirmEmailChangeTemplate: {\n body: confirmEmailChangeTemplate,\n },\n });\n await migrationDelay();\n\n const migrationFile = getMigrationFile(\"users\", \"updated\", options);\n if (migrationFile) {\n creates.push({\n path: migrationFile,\n language: \"ts\",\n content: fs.readFileSync(migrationFile, \"utf8\"),\n status: \"success\",\n });\n }\n });\n\n // Modify files\n const modifies: File[] = [];\n const pushResult = (file: File | null) => {\n if (file) modifies.push(file);\n };\n\n const layoutServerFile = path.join(\n options.root,\n \"src\",\n \"routes\",\n \"+layout.server.ts\",\n );\n pushResult(\n modifyOutcomeToFile(layoutServerFile, modifyLayoutServer(layoutServerFile)),\n );\n\n // TODO: make this more robust by searching for Navbar.Root > Navbar.List\n // src/routes/(public)/root-layout.svelte is what vela creates by default\n const layoutCandidates = [\n path.join(options.root, \"src\", \"routes\", \"(public)\", \"root-layout.svelte\"),\n path.join(options.root, \"src\", \"routes\", \"root-layout.svelte\"),\n path.join(options.root, \"src\", \"routes\", \"(public)\", \"+layout.svelte\"),\n path.join(options.root, \"src\", \"routes\", \"+layout.svelte\"),\n ];\n const layoutFile =\n layoutCandidates.find((p) => fs.existsSync(p)) ?? layoutCandidates[0];\n pushResult(\n modifyOutcomeToFile(layoutFile, modifyRootLayoutSvelte(layoutFile)),\n );\n\n const hooksServerFile = path.join(options.root, \"src\", \"hooks.server.ts\");\n pushResult(\n modifyOutcomeToFile(hooksServerFile, modifyHooksServer(hooksServerFile)),\n );\n\n const svelteConfigPath = findSvelteConfigPath(options.root);\n pushResult(\n modifyOutcomeToFile(\n svelteConfigPath,\n modifySvelteConfigRemote(svelteConfigPath),\n ),\n );\n\n return {\n creates: [],\n modifies,\n deletes: [],\n components: [],\n packages: [],\n collections: [],\n collectionPatches: [],\n } satisfies Result;\n}\n"],"mappings":";;;;;;;;;AAKA,SAAgB,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AAM,EAAS,KAAK,EAAK;IAGzB,IAAmB,EAAK,KAC5B,EAAQ,MACR,OACA,UACA,oBACD;AACD,GACE,EAAoB,GAAkB,EAAmB,EAAiB,CAAC,CAC5E;CAID,IAAM,IAAmB;EACvB,EAAK,KAAK,EAAQ,MAAM,OAAO,UAAU,YAAY,qBAAqB;EAC1E,EAAK,KAAK,EAAQ,MAAM,OAAO,UAAU,qBAAqB;EAC9D,EAAK,KAAK,EAAQ,MAAM,OAAO,UAAU,YAAY,iBAAiB;EACtE,EAAK,KAAK,EAAQ,MAAM,OAAO,UAAU,iBAAiB;EAC3D,EACK,IACJ,EAAiB,MAAM,MAAM,EAAG,WAAW,EAAE,CAAC,IAAI,EAAiB;AACrE,GACE,EAAoB,GAAY,EAAuB,EAAW,CAAC,CACpE;CAED,IAAM,IAAkB,EAAK,KAAK,EAAQ,MAAM,OAAO,kBAAkB;AACzE,GACE,EAAoB,GAAiB,EAAkB,EAAgB,CAAC,CACzE;CAED,IAAM,IAAmB,EAAqB,EAAQ,KAAK;AAQ3D,QAPA,EACE,EACE,GACA,EAAyB,EAAiB,CAC3C,CACF,EAEM;EACL,SAAS,EAAE;EACX;EACA,SAAS,EAAE;EACX,YAAY,EAAE;EACd,UAAU,EAAE;EACZ,aAAa,EAAE;EACf,mBAAmB,EAAE;EACtB"}
|
|
1
|
+
{"version":3,"file":"generate.runtime-Cbq2649r.js","names":[],"sources":["../../src/patterns/enable/auth-remote/modifies/+layout.server.ts","../../src/patterns/enable/auth-remote/modifies/root-layout.svelte.ts","../../src/patterns/enable/auth-remote/modifies/hooks.server.ts","../../src/patterns/enable/auth-remote/generate.runtime.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport dedent from \"dedent\";\nimport { Project, SyntaxKind } from \"ts-morph\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\n\nexport function modifyLayoutServer(layoutPath: string): ModifyOutcome {\n if (!fs.existsSync(layoutPath)) {\n const snippet = dedent`\n\t\t\texport const load = async ({ locals }) => {\n\t\t\t\treturn { user: locals.pb.authStore.record };\n\t\t\t};\n\t\t`;\n\n fs.writeFileSync(layoutPath, snippet);\n return { status: \"success\", changed: true };\n }\n\n const originalSource = fs.readFileSync(layoutPath, \"utf8\");\n const project = new Project();\n const sourceFile = project.addSourceFileAtPath(layoutPath);\n\n // helper to add user property to an object literal if missing\n const addUserProp = (obj: import(\"ts-morph\").ObjectLiteralExpression) => {\n const hasUser = !!obj.getProperty(\"user\");\n if (!hasUser) {\n obj.addPropertyAssignment({\n name: \"user\",\n initializer: \"locals.pb.authStore.record\",\n });\n }\n };\n\n // find existing exported load (function or const with arrow)\n let loadArrow: import(\"ts-morph\").ArrowFunction | undefined;\n let loadFn:\n | import(\"ts-morph\").FunctionDeclaration\n | import(\"ts-morph\").FunctionExpression\n | undefined;\n\n const varDecl = sourceFile.getVariableDeclaration(\"load\");\n if (varDecl) {\n const vs = varDecl.getFirstAncestorByKind(SyntaxKind.VariableStatement);\n const isExported = !!vs?.hasExportKeyword();\n if (isExported) {\n const directArrow = varDecl.getInitializerIfKind(\n SyntaxKind.ArrowFunction,\n );\n if (directArrow) {\n loadArrow = directArrow;\n } else {\n const init = varDecl.getInitializer();\n if (init) {\n const innerArrow = init.getFirstDescendantByKind(\n SyntaxKind.ArrowFunction,\n );\n if (innerArrow) {\n loadArrow = innerArrow;\n } else {\n const innerFnExpr = init.getFirstDescendantByKind(\n SyntaxKind.FunctionExpression,\n );\n if (innerFnExpr) {\n loadFn = innerFnExpr;\n }\n }\n }\n }\n }\n }\n\n if (!loadArrow) {\n const maybeFn = sourceFile.getFunction(\"load\");\n if (maybeFn && maybeFn.isExported()) {\n loadFn = maybeFn;\n }\n }\n\n // if no load found, create one\n if (!loadArrow && !loadFn) {\n sourceFile.addFunction({\n isExported: true,\n isAsync: true,\n name: \"load\",\n parameters: [{ name: \"{ locals }\" }],\n statements: `return { user: locals.pb.authStore.record };`,\n });\n sourceFile.formatText();\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n // normalize function-like to a single handle\n const fnLike:\n | import(\"ts-morph\").ArrowFunction\n | import(\"ts-morph\").FunctionDeclaration\n | import(\"ts-morph\").FunctionExpression = (loadArrow ?? loadFn!) as any;\n\n // ensure first parameter destructures locals, preserving existing information\n const params = fnLike.getParameters();\n if (params.length === 0) {\n fnLike.insertParameter(0, { name: \"{ locals }\" });\n } else {\n const first = params[0];\n const original = first.getStructure();\n const nameNode = first.getNameNode();\n if (nameNode.getKind() === SyntaxKind.ObjectBindingPattern) {\n const obp = nameNode as import(\"ts-morph\").ObjectBindingPattern;\n const hasLocals = obp\n .getElements()\n .some((el) => el.getName() === \"locals\");\n if (!hasLocals) {\n const parts = obp\n .getElements()\n .map((el) => el.getText())\n .filter(Boolean);\n const tail = parts.length > 0 ? \", \" + parts.join(\", \") : \"\";\n first.set({\n name: `{ locals${tail} }`,\n type: original.type,\n initializer: (original as any).initializer,\n });\n }\n } else {\n const currentName = first.getName();\n if (currentName !== \"locals\") {\n first.set({\n name: `{ locals, ...${currentName} }`,\n type: original.type,\n initializer: (original as any).initializer,\n });\n }\n }\n }\n\n // add user to returned object literals without altering other properties\n if (loadArrow) {\n const body = loadArrow.getBody();\n if (body.getKind() === SyntaxKind.Block) {\n const returns = body.getDescendantsOfKind(SyntaxKind.ReturnStatement);\n returns.forEach((ret) => {\n const expr = ret.getExpression();\n if (!expr) return;\n if (expr.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(expr as import(\"ts-morph\").ObjectLiteralExpression);\n } else if (expr.getKind() === SyntaxKind.ParenthesizedExpression) {\n const inner = (\n expr as import(\"ts-morph\").ParenthesizedExpression\n ).getExpression();\n if (inner.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(inner as import(\"ts-morph\").ObjectLiteralExpression);\n }\n }\n });\n } else {\n // concise body\n const bodyExpr = body as import(\"ts-morph\").Expression;\n if (bodyExpr.getKind() === SyntaxKind.ParenthesizedExpression) {\n const inner = (\n bodyExpr as import(\"ts-morph\").ParenthesizedExpression\n ).getExpression();\n if (inner.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(inner as import(\"ts-morph\").ObjectLiteralExpression);\n }\n } else if (bodyExpr.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(bodyExpr as import(\"ts-morph\").ObjectLiteralExpression);\n }\n }\n } else if (loadFn) {\n const body = loadFn.getBody();\n if (body) {\n const returns = body.getDescendantsOfKind(SyntaxKind.ReturnStatement);\n returns.forEach((ret) => {\n const expr = ret.getExpression();\n if (!expr) return;\n if (expr.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(expr as import(\"ts-morph\").ObjectLiteralExpression);\n } else if (expr.getKind() === SyntaxKind.ParenthesizedExpression) {\n const inner = (\n expr as import(\"ts-morph\").ParenthesizedExpression\n ).getExpression();\n if (inner.getKind() === SyntaxKind.ObjectLiteralExpression) {\n addUserProp(inner as import(\"ts-morph\").ObjectLiteralExpression);\n }\n }\n });\n }\n }\n\n sourceFile.formatText();\n sourceFile.saveSync();\n return {\n status: \"success\",\n changed: sourceFile.getFullText() !== originalSource,\n };\n}\n","import fs from \"node:fs\";\nimport dedent from \"dedent\";\nimport { SvelteFile } from \"../../../../runtime/svelte-file\";\nimport {\n ensureImports,\n withInMemoryScript,\n} from \"../../../../runtime/ts-morph-helpers\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\n\nconst AUTH_MENU_NAVBAR_ITEM = `\n\t\t<Navbar.Item\n\t\t\tclass=\"flex flex-col md:flex-row items-center space-y-2 md:space-y-0 md:space-x-4\"\n\t\t>\n\t\t\t<AuthMenu.Root>\n\t\t\t\t{#snippet user()}\n\t\t\t\t\t<AuthMenu.Trigger>\n\t\t\t\t\t\t<Avatar.Root>\n\t\t\t\t\t\t\t{#if data.user?.avatar}\n\t\t\t\t\t\t\t\t<Avatar.Image\n\t\t\t\t\t\t\t\t\tsrc=\"/api/files/users/{data.user?.id}/{data.user?.avatar}\"\n\t\t\t\t\t\t\t\t\talt={data.user?.email}\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t{/if}\n\t\t\t\t\t\t\t<Avatar.Fallback>\n\t\t\t\t\t\t\t\t{data.user?.email?.charAt(0).toUpperCase()}\n\t\t\t\t\t\t\t</Avatar.Fallback>\n\t\t\t\t\t\t</Avatar.Root>\n\t\t\t\t\t</AuthMenu.Trigger>\n\n\t\t\t\t\t<AuthMenu.Content align=\"end\">\n\t\t\t\t\t\t<AuthMenu.Item href=\"/dashboard\">Dashboard</AuthMenu.Item>\n\t\t\t\t\t\t<AuthMenu.Group>\n\t\t\t\t\t\t\t<AuthMenu.Item href=\"/settings\">Settings</AuthMenu.Item>\n\t\t\t\t\t\t\t<AuthMenu.Item>\n\t\t\t\t\t\t\t\t<form action=\"/logout\" method=\"post\">\n\t\t\t\t\t\t\t\t\t<button type=\"submit\" class=\"contents\">Logout</button>\n\t\t\t\t\t\t\t\t</form>\n\t\t\t\t\t\t\t</AuthMenu.Item>\n\t\t\t\t\t\t</AuthMenu.Group>\n\t\t\t\t\t</AuthMenu.Content>\n\t\t\t\t{/snippet}\n\n\t\t\t\t{#snippet guest()}\n\t\t\t\t\t<Button\n\t\t\t\t\t\tclass=\"w-full justify-start md:justify-center md:w-auto\"\n\t\t\t\t\t\tvariant=\"ghost\"\n\t\t\t\t\t\thref=\"/login\">Login</Button\n\t\t\t\t\t>\n\t\t\t\t\t<Button\n\t\t\t\t\t\tclass=\"w-full justify-start md:justify-center md:w-auto\"\n\t\t\t\t\t\tvariant=\"outline\"\n\t\t\t\t\t\thref=\"/signup\">Sign up</Button\n\t\t\t\t\t>\n\t\t\t\t{/snippet}\n\t\t\t</AuthMenu.Root>\n\t\t</Navbar.Item>\n\t`;\n\nconst IMPORT_SNIPPET = dedent`\n import * as AuthMenu from '$lib/components/ui/auth-menu';\n import * as Avatar from '$lib/components/ui/avatar';\n`;\n\nconst FAILURE_HINT = [\n \"Add the auth menu inside your <Navbar.List> in the root layout.\",\n \"\",\n \"Imports to add to the <script> tag:\",\n \"\",\n IMPORT_SNIPPET,\n \"\",\n \"Markup to add inside <Navbar.List>:\",\n AUTH_MENU_NAVBAR_ITEM,\n].join(\"\\n\");\n\nconst NOT_FOUND_HINT = [\n \"Create a root layout that includes an auth menu inside <Navbar.List>.\",\n \"\",\n \"Imports for the <script> tag:\",\n \"\",\n IMPORT_SNIPPET,\n \"\",\n \"Markup inside <Navbar.List>:\",\n AUTH_MENU_NAVBAR_ITEM,\n].join(\"\\n\");\n\nexport function modifyRootLayoutSvelte(layoutPath: string): ModifyOutcome {\n if (!fs.existsSync(layoutPath)) {\n return { status: \"not-found\", message: NOT_FOUND_HINT };\n }\n\n const file = SvelteFile.fromPath(layoutPath);\n if (!file.hasElement(\"Navbar.List\")) {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n if (file.hasElement(\"AuthMenu.Root\")) {\n return { status: \"success\", changed: false };\n }\n\n file.modifyScript((source) => {\n const { source: out } = withInMemoryScript(source, (sf) => {\n ensureImports(sf, [\n {\n namespaceImport: \"AuthMenu\",\n moduleSpecifier: \"$lib/components/ui/auth-menu\",\n },\n {\n namespaceImport: \"Avatar\",\n moduleSpecifier: \"$lib/components/ui/avatar\",\n },\n ]);\n });\n return out;\n });\n\n file.insertBeforeClosingTag(\"Navbar.List\", AUTH_MENU_NAVBAR_ITEM);\n file.writeTo(layoutPath);\n return { status: \"success\", changed: file.hasChanged() };\n}\n","import fs from \"node:fs\";\nimport dedent from \"dedent\";\nimport { Project, SyntaxKind } from \"ts-morph\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\n\nconst FAILURE_HINT = dedent`\n Configure handlePocketbase() with the auth option in hooks.server.ts:\n\n handlePocketbase({\n auth: { protectedRoutes: ['/(app)'] },\n });\n`;\n\nconst NOT_FOUND_HINT = dedent`\n Create src/hooks.server.ts with handlePocketbase:\n\n import { handlePocketbase } from 'velastack/handle';\n\n export const handle = handlePocketbase({\n auth: { protectedRoutes: ['/(app)'] },\n });\n`;\n\nexport function modifyHooksServer(hooksServerPath: string): ModifyOutcome {\n if (!fs.existsSync(hooksServerPath)) {\n return { status: \"not-found\", message: NOT_FOUND_HINT };\n }\n\n const originalSource = fs.readFileSync(hooksServerPath, \"utf8\");\n const project = new Project();\n const sourceFile = project.addSourceFileAtPath(hooksServerPath);\n\n // Find the handlePocketbase(...) call\n const callExpr = sourceFile\n .getDescendantsOfKind(SyntaxKind.CallExpression)\n .find((ce) => ce.getExpression().getText() === \"handlePocketbase\");\n\n if (!callExpr) {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n\n const args = callExpr.getArguments();\n if (args.length === 0) {\n callExpr.addArgument(`{ auth: { protectedRoutes: ['/(app)'] } }`);\n sourceFile.formatText();\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n const firstArg = args[0];\n if (firstArg.getKind() !== SyntaxKind.ObjectLiteralExpression) {\n callExpr.insertArgument(0, `{ auth: { protectedRoutes: ['/(app)'] } }`);\n sourceFile.formatText();\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n const optionsObj = firstArg.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);\n\n // Ensure auth property exists\n const authProp = optionsObj.getProperty(\"auth\");\n if (!authProp) {\n optionsObj.addPropertyAssignment({\n name: \"auth\",\n initializer: `{ protectedRoutes: ['/(app)'] }`,\n });\n sourceFile.formatText();\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n // If auth exists, ensure protectedRoutes includes '/(app)'\n if (authProp.getKind() === SyntaxKind.PropertyAssignment) {\n const authInit = (\n authProp as import(\"ts-morph\").PropertyAssignment\n ).getInitializer();\n if (authInit && authInit.getKind() === SyntaxKind.ObjectLiteralExpression) {\n const authObj = authInit as import(\"ts-morph\").ObjectLiteralExpression;\n const prProp = authObj.getProperty(\"protectedRoutes\");\n if (!prProp) {\n authObj.addPropertyAssignment({\n name: \"protectedRoutes\",\n initializer: `['/(app)']`,\n });\n } else if (prProp.getKind() === SyntaxKind.PropertyAssignment) {\n const prInit = (\n prProp as import(\"ts-morph\").PropertyAssignment\n ).getInitializer();\n if (prInit && prInit.getKind() === SyntaxKind.ArrayLiteralExpression) {\n const arr = prInit as import(\"ts-morph\").ArrayLiteralExpression;\n const hasApp = arr\n .getElements()\n .some((el) => el.getText().replace(/['\"`]/g, \"\") === \"/(app)\");\n if (!hasApp) {\n arr.addElement(`'/(app)'`);\n }\n } else {\n (prProp as import(\"ts-morph\").PropertyAssignment).setInitializer(\n `['/(app)']`,\n );\n }\n }\n }\n }\n\n sourceFile.formatText();\n sourceFile.saveSync();\n return {\n status: \"success\",\n changed: sourceFile.getFullText() !== originalSource,\n };\n}\n","import path from \"node:path\";\nimport fs from \"node:fs\";\nimport { Options, Result, File } from \"../../../core/types\";\nimport { getLogger } from \"../../../core/logger\";\nimport {\n getMigrationFile,\n migrationDelay,\n withPocketbase,\n} from \"../../../runtime/pocketbase\";\nimport { modifyOutcomeToFile } from \"../../../runtime/modify-file\";\nimport {\n findSvelteConfigPath,\n modifySvelteConfigRemote,\n} from \"../../../runtime/modify-svelte-config-remote\";\n\nimport { modifyLayoutServer } from \"./modifies/+layout.server\";\nimport { modifyRootLayoutSvelte } from \"./modifies/root-layout.svelte\";\nimport { modifyHooksServer } from \"./modifies/hooks.server\";\n\nexport async function generate(options: Options) {\n const { input } = options;\n const logger = getLogger(options);\n\n let creates: File[] = [];\n\n // Update PocketBase\n await withPocketbase(options.root, async (pb) => {\n logger.info(\"Updating PocketBase mail settings\");\n await pb.settings.update({\n meta: {\n senderName: input.senderName,\n senderAddress: input.senderAddress,\n },\n });\n await migrationDelay();\n\n const userCollection = await pb.collections.getOne(\"users\");\n\n if (!userCollection) {\n throw new Error(\"Users collection not found\");\n }\n\n const verificationTemplate =\n userCollection.verificationTemplate.body.replace(\n \"{APP_URL}/_/#/auth/confirm-verification/{TOKEN}\",\n \"{APP_URL}/confirm-verification/{TOKEN}\",\n );\n const resetPasswordTemplate =\n userCollection.resetPasswordTemplate.body.replace(\n \"{APP_URL}/_/#/auth/confirm-reset/{TOKEN}\",\n \"{APP_URL}/confirm-reset/{TOKEN}\",\n );\n const confirmEmailChangeTemplate =\n userCollection.confirmEmailChangeTemplate.body.replace(\n \"{APP_URL}/_/#/auth/confirm-email-change/{TOKEN}\",\n \"{APP_URL}/confirm-email-change/{TOKEN}\",\n );\n\n logger.info(\"Locking down users collection rules and email templates\");\n // Update user auth rules to lock down access to the user's own account\n await pb.collections.update(\"users\", {\n listRule: \"@request.auth.id = id\",\n viewRule: \"@request.auth.id = id\",\n createRule: \"@request.auth.id = id\",\n updateRule: \"@request.auth.id = id\",\n deleteRule: \"@request.auth.id = id\",\n\n // Update email templates to VelaStack auth urls\n verificationTemplate: {\n body: verificationTemplate,\n },\n resetPasswordTemplate: {\n body: resetPasswordTemplate,\n },\n confirmEmailChangeTemplate: {\n body: confirmEmailChangeTemplate,\n },\n });\n await migrationDelay();\n\n const migrationFile = getMigrationFile(\"users\", \"updated\", options);\n if (migrationFile) {\n creates.push({\n path: migrationFile,\n language: \"ts\",\n content: fs.readFileSync(migrationFile, \"utf8\"),\n status: \"success\",\n });\n }\n });\n\n // Modify files\n const modifies: File[] = [];\n const pushResult = (file: File | null) => {\n if (file) modifies.push(file);\n };\n\n logger.info(\"Modifying +layout.server.ts\");\n const layoutServerFile = path.join(\n options.root,\n \"src\",\n \"routes\",\n \"+layout.server.ts\",\n );\n pushResult(\n modifyOutcomeToFile(layoutServerFile, modifyLayoutServer(layoutServerFile)),\n );\n\n logger.info(\"Modifying root layout\");\n // TODO: make this more robust by searching for Navbar.Root > Navbar.List\n // src/routes/(public)/root-layout.svelte is what vela creates by default\n const layoutCandidates = [\n path.join(options.root, \"src\", \"routes\", \"(public)\", \"root-layout.svelte\"),\n path.join(options.root, \"src\", \"routes\", \"root-layout.svelte\"),\n path.join(options.root, \"src\", \"routes\", \"(public)\", \"+layout.svelte\"),\n path.join(options.root, \"src\", \"routes\", \"+layout.svelte\"),\n ];\n const layoutFile =\n layoutCandidates.find((p) => fs.existsSync(p)) ?? layoutCandidates[0];\n pushResult(\n modifyOutcomeToFile(layoutFile, modifyRootLayoutSvelte(layoutFile)),\n );\n\n logger.info(\"Modifying hooks.server.ts\");\n const hooksServerFile = path.join(options.root, \"src\", \"hooks.server.ts\");\n pushResult(\n modifyOutcomeToFile(hooksServerFile, modifyHooksServer(hooksServerFile)),\n );\n\n logger.info(\"Modifying svelte.config for remote\");\n const svelteConfigPath = findSvelteConfigPath(options.root);\n pushResult(\n modifyOutcomeToFile(\n svelteConfigPath,\n modifySvelteConfigRemote(svelteConfigPath),\n ),\n );\n\n return {\n creates: [],\n modifies,\n deletes: [],\n components: [],\n packages: [],\n collections: [],\n collectionPatches: [],\n } satisfies Result;\n}\n"],"mappings":";;;;;;;;;;AAKA,SAAgB,EAAmB,GAAmC;AACpE,KAAI,CAAC,EAAG,WAAW,EAAW,EAAE;EAC9B,IAAM,IAAU,CAAM;;;;;AAOtB,SADA,EAAG,cAAc,GAAY,EAAQ,EAC9B;GAAE,QAAQ;GAAW,SAAS;GAAM;;CAG7C,IAAM,IAAiB,EAAG,aAAa,GAAY,OAAO,EAEpD,IADU,IAAI,GAAS,CACF,oBAAoB,EAAW,EAGpD,KAAe,MAAoD;AAEvE,EADkB,EAAI,YAAY,OAAO,IAEvC,EAAI,sBAAsB;GACxB,MAAM;GACN,aAAa;GACd,CAAC;IAKF,GACA,GAKE,IAAU,EAAW,uBAAuB,OAAO;AACzD,KAAI,KACS,EAAQ,uBAAuB,EAAW,kBAAkB,EAC9C,kBAAkB,EAC3B;EACd,IAAM,IAAc,EAAQ,qBAC1B,EAAW,cACZ;AACD,MAAI,EACF,KAAY;OACP;GACL,IAAM,IAAO,EAAQ,gBAAgB;AACrC,OAAI,GAAM;IACR,IAAM,IAAa,EAAK,yBACtB,EAAW,cACZ;AACD,QAAI,EACF,KAAY;SACP;KACL,IAAM,IAAc,EAAK,yBACvB,EAAW,mBACZ;AACD,KAAI,MACF,IAAS;;;;;AAQrB,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