@velastack/patterns 0.0.22 → 0.0.23
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-Bzyt18yv.js +52 -0
- package/dist/chunks/collections-Bzyt18yv.js.map +1 -0
- package/dist/chunks/constants-B5Ub2oFy.js +6 -0
- package/dist/chunks/constants-B5Ub2oFy.js.map +1 -0
- package/dist/chunks/dedent-BFNYR8cb.js +87 -0
- package/dist/chunks/dedent-BFNYR8cb.js.map +1 -0
- package/dist/chunks/env.preview-Cpr4Im0G.js +19 -0
- package/dist/chunks/env.preview-Cpr4Im0G.js.map +1 -0
- package/dist/chunks/env.runtime-iw-bWvXM.js +32 -0
- package/dist/chunks/env.runtime-iw-bWvXM.js.map +1 -0
- package/dist/chunks/generate.runtime-BEuc5hDN.js +225 -0
- package/dist/chunks/generate.runtime-BEuc5hDN.js.map +1 -0
- package/dist/chunks/generate.runtime-CHmm4bBZ.js +154 -0
- package/dist/chunks/generate.runtime-CHmm4bBZ.js.map +1 -0
- package/dist/chunks/generate.runtime-DXkJgkP4.js +232 -0
- package/dist/chunks/generate.runtime-DXkJgkP4.js.map +1 -0
- package/dist/chunks/generate.runtime-egq8I3EA.js +61 -0
- package/dist/chunks/generate.runtime-egq8I3EA.js.map +1 -0
- package/dist/chunks/generate.runtime-uPCB71Ki.js +522 -0
- package/dist/chunks/generate.runtime-uPCB71Ki.js.map +1 -0
- package/dist/chunks/pocketbase-CPRrN7Qs.js +106 -0
- package/dist/chunks/pocketbase-CPRrN7Qs.js.map +1 -0
- package/dist/chunks/pocketbase-CSl9ydRr.js +47 -0
- package/dist/chunks/pocketbase-CSl9ydRr.js.map +1 -0
- package/dist/chunks/ts-morph-helpers-BiOU9sUx.js +150 -0
- package/dist/chunks/ts-morph-helpers-BiOU9sUx.js.map +1 -0
- package/dist/chunks/util-k-qPjL06.js +21 -0
- package/dist/chunks/util-k-qPjL06.js.map +1 -0
- package/dist/chunks/write-result-CSxmq40z.js +216 -0
- package/dist/chunks/write-result-CSxmq40z.js.map +1 -0
- package/dist/index.js +3532 -8
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/dist/chunks/collections-Ch4kZjuV.js +0 -67
- package/dist/chunks/collections-Ch4kZjuV.js.map +0 -1
- package/dist/chunks/env.preview-HlXZOEL4.js +0 -17
- package/dist/chunks/env.preview-HlXZOEL4.js.map +0 -1
- package/dist/chunks/env.runtime-BauEimvd.js +0 -36
- package/dist/chunks/env.runtime-BauEimvd.js.map +0 -1
- package/dist/chunks/generate.runtime-C7g-zL0o.js +0 -55
- package/dist/chunks/generate.runtime-C7g-zL0o.js.map +0 -1
- package/dist/chunks/generate.runtime-CnxTssBQ.js +0 -334
- package/dist/chunks/generate.runtime-CnxTssBQ.js.map +0 -1
- package/dist/chunks/generate.runtime-D5bvTbSL.js +0 -172
- package/dist/chunks/generate.runtime-D5bvTbSL.js.map +0 -1
- package/dist/chunks/generate.runtime-Tk3iuGQE.js +0 -259
- package/dist/chunks/generate.runtime-Tk3iuGQE.js.map +0 -1
- package/dist/chunks/generate.runtime-hZV63PD9.js +0 -610
- package/dist/chunks/generate.runtime-hZV63PD9.js.map +0 -1
- package/dist/chunks/index-CoeyNVkU.js +0 -9549
- package/dist/chunks/index-CoeyNVkU.js.map +0 -1
- package/dist/chunks/pocketbase-rvyHmLFw.js +0 -140
- package/dist/chunks/pocketbase-rvyHmLFw.js.map +0 -1
- package/dist/chunks/ts-morph-helpers-BNCluWTs.js +0 -166
- package/dist/chunks/ts-morph-helpers-BNCluWTs.js.map +0 -1
- package/dist/chunks/write-result-1Q3LB_k4.js +0 -1744
- package/dist/chunks/write-result-1Q3LB_k4.js.map +0 -1
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { n as e, r as t, t as n } from "./pocketbase-CPRrN7Qs.js";
|
|
2
|
+
import { i as r, t as i } from "./ts-morph-helpers-BiOU9sUx.js";
|
|
3
|
+
import a from "node:fs";
|
|
4
|
+
import o from "node:path";
|
|
5
|
+
import { Project as s, SyntaxKind as c } from "ts-morph";
|
|
6
|
+
//#region src/patterns/enable/api-keys/modifies/hooks.server.ts
|
|
7
|
+
function l(e) {
|
|
8
|
+
let t = new s().addSourceFileAtPath(e), n = t.getDescendantsOfKind(c.CallExpression).find((e) => e.getExpression().getText() === "handlePocketbase");
|
|
9
|
+
if (!n) {
|
|
10
|
+
t.saveSync();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
let r = n.getArguments();
|
|
14
|
+
if (r.length === 0) {
|
|
15
|
+
n.addArgument("{ api: { enabled: true, apiKeys: { enabled: true } } }"), t.formatText(), t.saveSync();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
let i = r[0];
|
|
19
|
+
if (i.getKind() !== c.ObjectLiteralExpression) {
|
|
20
|
+
n.insertArgument(0, "{ api: { enabled: true, apiKeys: { enabled: true } } }"), t.formatText(), t.saveSync();
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
let a = i.asKindOrThrow(c.ObjectLiteralExpression), o = a.getProperty("api");
|
|
24
|
+
if (!o) {
|
|
25
|
+
a.addPropertyAssignment({
|
|
26
|
+
name: "api",
|
|
27
|
+
initializer: "{ enabled: true, apiKeys: { enabled: true } }"
|
|
28
|
+
}), t.formatText(), t.saveSync();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (o.getKind() === c.PropertyAssignment) {
|
|
32
|
+
let e = o.getInitializer();
|
|
33
|
+
if (e && e.getKind() === c.ObjectLiteralExpression) {
|
|
34
|
+
let t = e;
|
|
35
|
+
t.getProperty("enabled") || t.addPropertyAssignment({
|
|
36
|
+
name: "enabled",
|
|
37
|
+
initializer: "true"
|
|
38
|
+
});
|
|
39
|
+
let n = t.getProperty("apiKeys");
|
|
40
|
+
if (!n) t.addPropertyAssignment({
|
|
41
|
+
name: "apiKeys",
|
|
42
|
+
initializer: "{ enabled: true }"
|
|
43
|
+
});
|
|
44
|
+
else if (n.getKind() === c.PropertyAssignment) {
|
|
45
|
+
let e = n.getInitializer();
|
|
46
|
+
if (e && e.getKind() === c.ObjectLiteralExpression) {
|
|
47
|
+
let t = e;
|
|
48
|
+
t.getProperty("enabled") || t.addPropertyAssignment({
|
|
49
|
+
name: "enabled",
|
|
50
|
+
initializer: "true"
|
|
51
|
+
});
|
|
52
|
+
} else n.setInitializer("{ enabled: true }");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
t.formatText(), t.saveSync();
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/patterns/enable/api-keys/modifies/nav-user.svelte.ts
|
|
60
|
+
function u(e, t, n, a, o) {
|
|
61
|
+
let s = new r(e), c = !1, l = (e) => {
|
|
62
|
+
c || e((e) => {
|
|
63
|
+
let r = i(e, t, n, a, o);
|
|
64
|
+
return r.wasAdded && (c = !0), r.source;
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
return l((e) => s.modifyModuleScript(e)), c || l((e) => s.modifyScript(e)), {
|
|
68
|
+
newNavUserSnippet: s.toString(),
|
|
69
|
+
wasAdded: c
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function d(e) {
|
|
73
|
+
if (!a.existsSync(e)) return !1;
|
|
74
|
+
let { newNavUserSnippet: t, wasAdded: n } = u(a.readFileSync(e, "utf8"), "API Keys", "/api-keys", "KeyRoundIcon", "@lucide/svelte/icons/key-round");
|
|
75
|
+
return n && a.writeFileSync(e, t, "utf8"), n;
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/patterns/enable/api-keys/generate.runtime.ts
|
|
79
|
+
async function f(r) {
|
|
80
|
+
let i = [];
|
|
81
|
+
await t(r.root, async (t) => {
|
|
82
|
+
let o = await t.collections.getOne("users");
|
|
83
|
+
await t.collections.create({
|
|
84
|
+
listRule: "@request.auth.id = user.id",
|
|
85
|
+
viewRule: "@request.auth.id = user.id",
|
|
86
|
+
createRule: "@request.auth.id = user.id",
|
|
87
|
+
updateRule: "@request.auth.id = user.id",
|
|
88
|
+
deleteRule: "@request.auth.id = user.id",
|
|
89
|
+
name: "api_keys",
|
|
90
|
+
type: "base",
|
|
91
|
+
fields: [
|
|
92
|
+
{
|
|
93
|
+
collectionId: o.id,
|
|
94
|
+
maxSelect: 1,
|
|
95
|
+
name: "user",
|
|
96
|
+
type: "relation"
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: "key_hash",
|
|
100
|
+
type: "text"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "label",
|
|
104
|
+
type: "text"
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: "last_used",
|
|
108
|
+
type: "date"
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "created",
|
|
112
|
+
onCreate: !0,
|
|
113
|
+
onUpdate: !1,
|
|
114
|
+
type: "autodate"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "updated",
|
|
118
|
+
onCreate: !0,
|
|
119
|
+
onUpdate: !0,
|
|
120
|
+
type: "autodate"
|
|
121
|
+
}
|
|
122
|
+
]
|
|
123
|
+
}), await e();
|
|
124
|
+
let s = n("api_keys", "created", r);
|
|
125
|
+
s && i.push({
|
|
126
|
+
path: s,
|
|
127
|
+
language: "ts",
|
|
128
|
+
content: a.readFileSync(s, "utf8")
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
let s = [], c = o.join(r.root, "src", "hooks.server.ts");
|
|
132
|
+
a.existsSync(c) && (l(c), s.push({
|
|
133
|
+
path: c,
|
|
134
|
+
language: "ts",
|
|
135
|
+
content: a.readFileSync(c, "utf8")
|
|
136
|
+
}));
|
|
137
|
+
let u = o.join(r.root, "src", "lib", "components", "nav-user.svelte");
|
|
138
|
+
return d(u) && s.push({
|
|
139
|
+
path: u,
|
|
140
|
+
language: "svelte",
|
|
141
|
+
content: a.readFileSync(u, "utf8")
|
|
142
|
+
}), {
|
|
143
|
+
creates: i,
|
|
144
|
+
modifies: s,
|
|
145
|
+
deletes: [],
|
|
146
|
+
components: [],
|
|
147
|
+
packages: [],
|
|
148
|
+
collections: []
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
//#endregion
|
|
152
|
+
export { f as generate };
|
|
153
|
+
|
|
154
|
+
//# sourceMappingURL=generate.runtime-CHmm4bBZ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate.runtime-CHmm4bBZ.js","names":[],"sources":["../../src/patterns/enable/api-keys/modifies/hooks.server.ts","../../src/patterns/enable/api-keys/modifies/nav-user.svelte.ts","../../src/patterns/enable/api-keys/generate.runtime.ts"],"sourcesContent":["import { Project, SyntaxKind } from \"ts-morph\";\n\nexport function modifyHooksServer(hooksServerPath: string) {\n const project = new Project();\n const sourceFile = project.addSourceFileAtPath(hooksServerPath);\n\n const callExpr = sourceFile\n .getDescendantsOfKind(SyntaxKind.CallExpression)\n .find((ce) => ce.getExpression().getText() === \"handlePocketbase\");\n\n if (!callExpr) {\n sourceFile.saveSync();\n return;\n }\n\n const args = callExpr.getArguments();\n if (args.length === 0) {\n callExpr.addArgument(\n `{ api: { enabled: true, apiKeys: { enabled: true } } }`,\n );\n sourceFile.formatText();\n sourceFile.saveSync();\n return;\n }\n\n const firstArg = args[0];\n if (firstArg.getKind() !== SyntaxKind.ObjectLiteralExpression) {\n callExpr.insertArgument(\n 0,\n `{ api: { enabled: true, apiKeys: { enabled: true } } }`,\n );\n sourceFile.formatText();\n sourceFile.saveSync();\n return;\n }\n\n const optionsObj = firstArg.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);\n\n const apiProp = optionsObj.getProperty(\"api\");\n if (!apiProp) {\n optionsObj.addPropertyAssignment({\n name: \"api\",\n initializer: `{ enabled: true, apiKeys: { enabled: true } }`,\n });\n sourceFile.formatText();\n sourceFile.saveSync();\n return;\n }\n\n if (apiProp.getKind() === SyntaxKind.PropertyAssignment) {\n const apiInit = (\n apiProp as import(\"ts-morph\").PropertyAssignment\n ).getInitializer();\n if (apiInit && apiInit.getKind() === SyntaxKind.ObjectLiteralExpression) {\n const apiObj = apiInit as import(\"ts-morph\").ObjectLiteralExpression;\n const enabledProp = apiObj.getProperty(\"enabled\");\n if (!enabledProp) {\n apiObj.addPropertyAssignment({ name: \"enabled\", initializer: \"true\" });\n }\n\n const apiKeysProp = apiObj.getProperty(\"apiKeys\");\n if (!apiKeysProp) {\n apiObj.addPropertyAssignment({\n name: \"apiKeys\",\n initializer: `{ enabled: true }`,\n });\n } else if (apiKeysProp.getKind() === SyntaxKind.PropertyAssignment) {\n const keysInit = (\n apiKeysProp as import(\"ts-morph\").PropertyAssignment\n ).getInitializer();\n if (\n keysInit &&\n keysInit.getKind() === SyntaxKind.ObjectLiteralExpression\n ) {\n const keysObj =\n keysInit as import(\"ts-morph\").ObjectLiteralExpression;\n const keysEnabled = keysObj.getProperty(\"enabled\");\n if (!keysEnabled) {\n keysObj.addPropertyAssignment({\n name: \"enabled\",\n initializer: \"true\",\n });\n }\n } else {\n (apiKeysProp as import(\"ts-morph\").PropertyAssignment).setInitializer(\n `{ enabled: true }`,\n );\n }\n }\n }\n }\n\n sourceFile.formatText();\n sourceFile.saveSync();\n}\n","import fs from \"node:fs\";\nimport { SvelteFile } from \"../../../../runtime/svelte-file\";\nimport { addNavItemToScript } from \"../../../../runtime/ts-morph-helpers\";\n\nexport function addItemToNavUser(\n navUserSnippet: string,\n title: string,\n url: string,\n icon: string,\n iconImportPath: string,\n): { newNavUserSnippet: string; wasAdded: boolean } {\n const file = new SvelteFile(navUserSnippet);\n let wasAdded = false;\n\n const tryAdd = (run: (fn: (s: string) => string) => boolean) => {\n if (wasAdded) return;\n run((source) => {\n const result = addNavItemToScript(\n source,\n title,\n url,\n icon,\n iconImportPath,\n );\n if (result.wasAdded) wasAdded = true;\n return result.source;\n });\n };\n\n tryAdd((fn) => file.modifyModuleScript(fn));\n if (!wasAdded) tryAdd((fn) => file.modifyScript(fn));\n\n return { newNavUserSnippet: file.toString(), wasAdded };\n}\n\nexport function modifyNavUser(navUserPath: string): boolean {\n if (!fs.existsSync(navUserPath)) return false;\n\n const navUserSnippet = fs.readFileSync(navUserPath, \"utf8\");\n const { newNavUserSnippet, wasAdded } = addItemToNavUser(\n navUserSnippet,\n \"API Keys\",\n \"/api-keys\",\n \"KeyRoundIcon\",\n \"@lucide/svelte/icons/key-round\",\n );\n\n if (wasAdded) {\n fs.writeFileSync(navUserPath, newNavUserSnippet, \"utf8\");\n }\n\n return wasAdded;\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport type { File, Options, Result } from \"../../../core/types\";\nimport {\n getMigrationFile,\n migrationDelay,\n withPocketbase,\n} from \"../../../runtime/pocketbase\";\nimport { modifyHooksServer } from \"./modifies/hooks.server\";\nimport { modifyNavUser } from \"./modifies/nav-user.svelte\";\n\nexport async function generate(options: Options) {\n const creates: File[] = [];\n\n await withPocketbase(options.root, async (pb) => {\n const userCollection = await pb.collections.getOne(\"users\");\n\n await pb.collections.create({\n listRule: \"@request.auth.id = user.id\",\n viewRule: \"@request.auth.id = user.id\",\n createRule: \"@request.auth.id = user.id\",\n updateRule: \"@request.auth.id = user.id\",\n deleteRule: \"@request.auth.id = user.id\",\n name: \"api_keys\",\n type: \"base\",\n fields: [\n {\n collectionId: userCollection.id,\n maxSelect: 1,\n name: \"user\",\n type: \"relation\",\n },\n {\n name: \"key_hash\",\n type: \"text\",\n },\n {\n name: \"label\",\n type: \"text\",\n },\n {\n name: \"last_used\",\n type: \"date\",\n },\n {\n name: \"created\",\n onCreate: true,\n onUpdate: false,\n type: \"autodate\",\n },\n {\n name: \"updated\",\n onCreate: true,\n onUpdate: true,\n type: \"autodate\",\n },\n ],\n });\n await migrationDelay();\n\n const migrationFile = getMigrationFile(\"api_keys\", \"created\", options);\n if (migrationFile) {\n creates.push({\n path: migrationFile,\n language: \"ts\",\n content: fs.readFileSync(migrationFile, \"utf8\"),\n });\n }\n });\n\n const modifies: File[] = [];\n\n const hooksServerPath = path.join(options.root, \"src\", \"hooks.server.ts\");\n if (fs.existsSync(hooksServerPath)) {\n modifyHooksServer(hooksServerPath);\n modifies.push({\n path: hooksServerPath,\n language: \"ts\",\n content: fs.readFileSync(hooksServerPath, \"utf8\"),\n });\n }\n\n const navUserPath = path.join(\n options.root,\n \"src\",\n \"lib\",\n \"components\",\n \"nav-user.svelte\",\n );\n if (modifyNavUser(navUserPath)) {\n modifies.push({\n path: navUserPath,\n language: \"svelte\",\n content: fs.readFileSync(navUserPath, \"utf8\"),\n });\n }\n\n return {\n creates,\n modifies,\n deletes: [],\n components: [],\n packages: [],\n collections: [],\n } satisfies Result;\n}\n"],"mappings":";;;;;;AAEA,SAAgB,EAAkB,GAAyB;CAEzD,IAAM,IADU,IAAI,GAAS,CACF,oBAAoB,EAAgB,EAEzD,IAAW,EACd,qBAAqB,EAAW,eAAe,CAC/C,MAAM,MAAO,EAAG,eAAe,CAAC,SAAS,KAAK,mBAAmB;AAEpE,KAAI,CAAC,GAAU;AACb,IAAW,UAAU;AACrB;;CAGF,IAAM,IAAO,EAAS,cAAc;AACpC,KAAI,EAAK,WAAW,GAAG;AAKrB,EAJA,EAAS,YACP,yDACD,EACD,EAAW,YAAY,EACvB,EAAW,UAAU;AACrB;;CAGF,IAAM,IAAW,EAAK;AACtB,KAAI,EAAS,SAAS,KAAK,EAAW,yBAAyB;AAM7D,EALA,EAAS,eACP,GACA,yDACD,EACD,EAAW,YAAY,EACvB,EAAW,UAAU;AACrB;;CAGF,IAAM,IAAa,EAAS,cAAc,EAAW,wBAAwB,EAEvE,IAAU,EAAW,YAAY,MAAM;AAC7C,KAAI,CAAC,GAAS;AAMZ,EALA,EAAW,sBAAsB;GAC/B,MAAM;GACN,aAAa;GACd,CAAC,EACF,EAAW,YAAY,EACvB,EAAW,UAAU;AACrB;;AAGF,KAAI,EAAQ,SAAS,KAAK,EAAW,oBAAoB;EACvD,IAAM,IACJ,EACA,gBAAgB;AA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
+
import { n as t, r as n, t as r } from "./pocketbase-CPRrN7Qs.js";
|
|
3
|
+
import { i, n as a, r as o } from "./ts-morph-helpers-BiOU9sUx.js";
|
|
4
|
+
import s from "node:fs";
|
|
5
|
+
import c from "node:path";
|
|
6
|
+
import { Project as l, SyntaxKind as u } from "ts-morph";
|
|
7
|
+
//#region src/patterns/enable/auth/modifies/+layout.server.ts
|
|
8
|
+
function d(t) {
|
|
9
|
+
if (!s.existsSync(t)) {
|
|
10
|
+
let n = e`
|
|
11
|
+
export const load = async ({ locals }) => {
|
|
12
|
+
return { user: locals.pb.authStore.record };
|
|
13
|
+
};
|
|
14
|
+
`;
|
|
15
|
+
s.writeFileSync(t, n);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
let n = new l().addSourceFileAtPath(t), r = (e) => {
|
|
19
|
+
e.getProperty("user") || e.addPropertyAssignment({
|
|
20
|
+
name: "user",
|
|
21
|
+
initializer: "locals.pb.authStore.record"
|
|
22
|
+
});
|
|
23
|
+
}, i, a, o = n.getVariableDeclaration("load");
|
|
24
|
+
if (o && o.getFirstAncestorByKind(u.VariableStatement)?.hasExportKeyword()) {
|
|
25
|
+
let e = o.getInitializerIfKind(u.ArrowFunction);
|
|
26
|
+
if (e) i = e;
|
|
27
|
+
else {
|
|
28
|
+
let e = o.getInitializer();
|
|
29
|
+
if (e) {
|
|
30
|
+
let t = e.getFirstDescendantByKind(u.ArrowFunction);
|
|
31
|
+
if (t) i = t;
|
|
32
|
+
else {
|
|
33
|
+
let t = e.getFirstDescendantByKind(u.FunctionExpression);
|
|
34
|
+
t && (a = t);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!i) {
|
|
40
|
+
let e = n.getFunction("load");
|
|
41
|
+
e && e.isExported() && (a = e);
|
|
42
|
+
}
|
|
43
|
+
if (!i && !a) {
|
|
44
|
+
n.addFunction({
|
|
45
|
+
isExported: !0,
|
|
46
|
+
isAsync: !0,
|
|
47
|
+
name: "load",
|
|
48
|
+
parameters: [{ name: "{ locals }" }],
|
|
49
|
+
statements: "return { user: locals.pb.authStore.record };"
|
|
50
|
+
}), n.formatText(), n.saveSync();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
let c = i ?? a, d = c.getParameters();
|
|
54
|
+
if (d.length === 0) c.insertParameter(0, { name: "{ locals }" });
|
|
55
|
+
else {
|
|
56
|
+
let e = d[0], t = e.getStructure(), n = e.getNameNode();
|
|
57
|
+
if (n.getKind() === u.ObjectBindingPattern) {
|
|
58
|
+
let r = n;
|
|
59
|
+
if (!r.getElements().some((e) => e.getName() === "locals")) {
|
|
60
|
+
let n = r.getElements().map((e) => e.getText()).filter(Boolean), i = n.length > 0 ? ", " + n.join(", ") : "";
|
|
61
|
+
e.set({
|
|
62
|
+
name: `{ locals${i} }`,
|
|
63
|
+
type: t.type,
|
|
64
|
+
initializer: t.initializer
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
let n = e.getName();
|
|
69
|
+
n !== "locals" && e.set({
|
|
70
|
+
name: `{ locals, ...${n} }`,
|
|
71
|
+
type: t.type,
|
|
72
|
+
initializer: t.initializer
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (i) {
|
|
77
|
+
let e = i.getBody();
|
|
78
|
+
if (e.getKind() === u.Block) e.getDescendantsOfKind(u.ReturnStatement).forEach((e) => {
|
|
79
|
+
let t = e.getExpression();
|
|
80
|
+
if (t) {
|
|
81
|
+
if (t.getKind() === u.ObjectLiteralExpression) r(t);
|
|
82
|
+
else if (t.getKind() === u.ParenthesizedExpression) {
|
|
83
|
+
let e = t.getExpression();
|
|
84
|
+
e.getKind() === u.ObjectLiteralExpression && r(e);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
else {
|
|
89
|
+
let t = e;
|
|
90
|
+
if (t.getKind() === u.ParenthesizedExpression) {
|
|
91
|
+
let e = t.getExpression();
|
|
92
|
+
e.getKind() === u.ObjectLiteralExpression && r(e);
|
|
93
|
+
} else t.getKind() === u.ObjectLiteralExpression && r(t);
|
|
94
|
+
}
|
|
95
|
+
} else if (a) {
|
|
96
|
+
let e = a.getBody();
|
|
97
|
+
e && e.getDescendantsOfKind(u.ReturnStatement).forEach((e) => {
|
|
98
|
+
let t = e.getExpression();
|
|
99
|
+
if (t) {
|
|
100
|
+
if (t.getKind() === u.ObjectLiteralExpression) r(t);
|
|
101
|
+
else if (t.getKind() === u.ParenthesizedExpression) {
|
|
102
|
+
let e = t.getExpression();
|
|
103
|
+
e.getKind() === u.ObjectLiteralExpression && r(e);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
n.formatText(), n.saveSync();
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/patterns/enable/auth/modifies/root-layout.svelte.ts
|
|
112
|
+
var f = "\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 ";
|
|
113
|
+
function p(e) {
|
|
114
|
+
if (!s.existsSync(e)) return !1;
|
|
115
|
+
let t = i.fromPath(e);
|
|
116
|
+
return !t.hasElement("Navbar.List") || t.hasElement("AuthMenu.Root") ? !1 : (t.modifyScript((e) => {
|
|
117
|
+
let { source: t } = o(e, (e) => {
|
|
118
|
+
a(e, [{
|
|
119
|
+
namespaceImport: "AuthMenu",
|
|
120
|
+
moduleSpecifier: "$lib/components/ui/auth-menu"
|
|
121
|
+
}, {
|
|
122
|
+
namespaceImport: "Avatar",
|
|
123
|
+
moduleSpecifier: "$lib/components/ui/avatar"
|
|
124
|
+
}]);
|
|
125
|
+
});
|
|
126
|
+
return t;
|
|
127
|
+
}), t.insertBeforeClosingTag("Navbar.List", f), t.writeTo(e), t.hasChanged());
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/patterns/enable/auth/modifies/hooks.server.ts
|
|
131
|
+
function m(e) {
|
|
132
|
+
let t = new l().addSourceFileAtPath(e), n = t.getDescendantsOfKind(u.CallExpression).find((e) => e.getExpression().getText() === "handlePocketbase");
|
|
133
|
+
if (!n) {
|
|
134
|
+
t.saveSync();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
let r = n.getArguments();
|
|
138
|
+
if (r.length === 0) {
|
|
139
|
+
n.addArgument("{ auth: { protectedRoutes: ['/(app)'] } }"), t.formatText(), t.saveSync();
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
let i = r[0];
|
|
143
|
+
if (i.getKind() !== u.ObjectLiteralExpression) {
|
|
144
|
+
n.insertArgument(0, "{ auth: { protectedRoutes: ['/(app)'] } }"), t.formatText(), t.saveSync();
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
let a = i.asKindOrThrow(u.ObjectLiteralExpression), o = a.getProperty("auth");
|
|
148
|
+
if (!o) {
|
|
149
|
+
a.addPropertyAssignment({
|
|
150
|
+
name: "auth",
|
|
151
|
+
initializer: "{ protectedRoutes: ['/(app)'] }"
|
|
152
|
+
}), t.formatText(), t.saveSync();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (o.getKind() === u.PropertyAssignment) {
|
|
156
|
+
let e = o.getInitializer();
|
|
157
|
+
if (e && e.getKind() === u.ObjectLiteralExpression) {
|
|
158
|
+
let t = e, n = t.getProperty("protectedRoutes");
|
|
159
|
+
if (!n) t.addPropertyAssignment({
|
|
160
|
+
name: "protectedRoutes",
|
|
161
|
+
initializer: "['/(app)']"
|
|
162
|
+
});
|
|
163
|
+
else if (n.getKind() === u.PropertyAssignment) {
|
|
164
|
+
let e = n.getInitializer();
|
|
165
|
+
if (e && e.getKind() === u.ArrayLiteralExpression) {
|
|
166
|
+
let t = e;
|
|
167
|
+
t.getElements().some((e) => e.getText().replace(/['"`]/g, "") === "/(app)") || t.addElement("'/(app)'");
|
|
168
|
+
} else n.setInitializer("['/(app)']");
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
t.formatText(), t.saveSync();
|
|
173
|
+
}
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region src/patterns/enable/auth/generate.runtime.ts
|
|
176
|
+
async function h(e) {
|
|
177
|
+
let { input: i } = e, a = [];
|
|
178
|
+
await n(e.root, async (n) => {
|
|
179
|
+
await n.settings.update({ meta: {
|
|
180
|
+
senderName: i.senderName,
|
|
181
|
+
senderAddress: i.senderAddress
|
|
182
|
+
} }), await t();
|
|
183
|
+
let o = await n.collections.getOne("users");
|
|
184
|
+
if (!o) throw Error("Users collection not found");
|
|
185
|
+
let c = o.verificationTemplate.body.replace("{APP_URL}/_/#/auth/confirm-verification/{TOKEN}", "{APP_URL}/confirm-verification/{TOKEN}"), l = o.resetPasswordTemplate.body.replace("{APP_URL}/_/#/auth/confirm-reset/{TOKEN}", "{APP_URL}/confirm-reset/{TOKEN}"), u = o.confirmEmailChangeTemplate.body.replace("{APP_URL}/_/#/auth/confirm-email-change/{TOKEN}", "{APP_URL}/confirm-email-change/{TOKEN}");
|
|
186
|
+
await n.collections.update("users", {
|
|
187
|
+
listRule: "@request.auth.id = id",
|
|
188
|
+
viewRule: "@request.auth.id = id",
|
|
189
|
+
createRule: "@request.auth.id = id",
|
|
190
|
+
updateRule: "@request.auth.id = id",
|
|
191
|
+
deleteRule: "@request.auth.id = id",
|
|
192
|
+
verificationTemplate: { body: c },
|
|
193
|
+
resetPasswordTemplate: { body: l },
|
|
194
|
+
confirmEmailChangeTemplate: { body: u }
|
|
195
|
+
}), await t();
|
|
196
|
+
let d = r("users", "updated", e);
|
|
197
|
+
d && a.push({
|
|
198
|
+
path: d,
|
|
199
|
+
language: "ts",
|
|
200
|
+
content: s.readFileSync(d, "utf8")
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
let o = [], l = c.join(e.root, "src", "routes", "+layout.server.ts");
|
|
204
|
+
await d(l), o.push({
|
|
205
|
+
path: l,
|
|
206
|
+
language: "ts",
|
|
207
|
+
content: s.readFileSync(l, "utf8")
|
|
208
|
+
});
|
|
209
|
+
let u = c.join(e.root, "src", "routes", "(public)", "root-layout.svelte");
|
|
210
|
+
s.existsSync(u) || (u = c.join(e.root, "src", "routes", "root-layout.svelte")), s.existsSync(u) || (u = c.join(e.root, "src", "routes", "(public)", "+layout.svelte")), s.existsSync(u) || (u = c.join(e.root, "src", "routes", "+layout.svelte")), s.existsSync(u) && (p(u), o.push({
|
|
211
|
+
path: u,
|
|
212
|
+
language: "svelte",
|
|
213
|
+
content: s.readFileSync(u, "utf8")
|
|
214
|
+
}));
|
|
215
|
+
let f = c.join(e.root, "src", "hooks.server.ts");
|
|
216
|
+
return s.existsSync(f) && (m(f), o.push({
|
|
217
|
+
path: f,
|
|
218
|
+
language: "ts",
|
|
219
|
+
content: s.readFileSync(f, "utf8")
|
|
220
|
+
})), {
|
|
221
|
+
creates: [],
|
|
222
|
+
modifies: o,
|
|
223
|
+
deletes: [],
|
|
224
|
+
components: [],
|
|
225
|
+
packages: [],
|
|
226
|
+
collections: []
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
//#endregion
|
|
230
|
+
export { h as generate };
|
|
231
|
+
|
|
232
|
+
//# sourceMappingURL=generate.runtime-DXkJgkP4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate.runtime-DXkJgkP4.js","names":[],"sources":["../../src/patterns/enable/auth/modifies/+layout.server.ts","../../src/patterns/enable/auth/modifies/root-layout.svelte.ts","../../src/patterns/enable/auth/modifies/hooks.server.ts","../../src/patterns/enable/auth/generate.runtime.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport dedent from \"dedent\";\nimport { Project, SyntaxKind } from \"ts-morph\";\n\nexport function modifyLayoutServer(layoutPath: string) {\n if (!fs.existsSync(layoutPath)) {\n // write file directly\n\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;\n }\n\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;\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}\n","import fs from \"node:fs\";\nimport { SvelteFile } from \"../../../../runtime/svelte-file\";\nimport {\n ensureImports,\n withInMemoryScript,\n} from \"../../../../runtime/ts-morph-helpers\";\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\nexport function modifyRootLayoutSvelte(layoutPath: string): boolean {\n if (!fs.existsSync(layoutPath)) return false;\n\n const file = SvelteFile.fromPath(layoutPath);\n if (!file.hasElement(\"Navbar.List\")) return false;\n if (file.hasElement(\"AuthMenu.Root\")) return false;\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 file.hasChanged();\n}\n","import { Project, SyntaxKind } from \"ts-morph\";\n\nexport function modifyHooksServer(hooksServerPath: string) {\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 sourceFile.saveSync();\n return;\n }\n\n const args = callExpr.getArguments();\n if (args.length === 0) {\n // Create an options object if missing\n callExpr.addArgument(`{ auth: { protectedRoutes: ['/(app)'] } }`);\n sourceFile.formatText();\n sourceFile.saveSync();\n return;\n }\n\n const firstArg = args[0];\n if (firstArg.getKind() !== SyntaxKind.ObjectLiteralExpression) {\n // Replace non-object first arg with an object including auth\n callExpr.insertArgument(0, `{ auth: { protectedRoutes: ['/(app)'] } }`);\n sourceFile.formatText();\n sourceFile.saveSync();\n return;\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;\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 // Replace non-array with desired value\n (prProp as import(\"ts-morph\").PropertyAssignment).setInitializer(\n `['/(app)']`,\n );\n }\n }\n }\n }\n\n sourceFile.formatText();\n sourceFile.saveSync();\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\";\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 });\n }\n });\n\n // Modify files\n let modifies: File[] = [];\n\n const layoutServerFile = path.join(\n options.root,\n \"src\",\n \"routes\",\n \"+layout.server.ts\",\n );\n\n await modifyLayoutServer(layoutServerFile);\n modifies.push({\n path: layoutServerFile,\n language: \"ts\",\n content: fs.readFileSync(layoutServerFile, \"utf8\"),\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 let layoutFile = path.join(\n options.root,\n \"src\",\n \"routes\",\n \"(public)\",\n \"root-layout.svelte\",\n );\n\n if (!fs.existsSync(layoutFile)) {\n layoutFile = path.join(options.root, \"src\", \"routes\", \"root-layout.svelte\");\n }\n\n if (!fs.existsSync(layoutFile)) {\n layoutFile = path.join(\n options.root,\n \"src\",\n \"routes\",\n \"(public)\",\n \"+layout.svelte\",\n );\n }\n\n if (!fs.existsSync(layoutFile)) {\n layoutFile = path.join(options.root, \"src\", \"routes\", \"+layout.svelte\");\n }\n\n if (fs.existsSync(layoutFile)) {\n modifyRootLayoutSvelte(layoutFile);\n modifies.push({\n path: layoutFile,\n language: \"svelte\",\n content: fs.readFileSync(layoutFile, \"utf8\"),\n });\n }\n\n const hooksServerFile = path.join(options.root, \"src\", \"hooks.server.ts\");\n\n if (fs.existsSync(hooksServerFile)) {\n modifyHooksServer(hooksServerFile);\n modifies.push({\n path: hooksServerFile,\n language: \"ts\",\n content: fs.readFileSync(hooksServerFile, \"utf8\"),\n });\n }\n\n return {\n creates: [],\n modifies,\n deletes: [],\n components: [],\n packages: [],\n collections: [],\n } satisfies Result;\n}\n"],"mappings":";;;;;;;AAIA,SAAgB,EAAmB,GAAoB;AACrD,KAAI,CAAC,EAAG,WAAW,EAAW,EAAE;EAG9B,IAAM,IAAU,CAAM;;;;;AAMtB,IAAG,cAAc,GAAY,EAAQ;AACrC;;CAIF,IAAM,IADU,IAAI,GAAS,CACF,oBAAoB,EAAW,EAGpD,KAAe,MAAoD;AAEvE,EADkB,EAAI,YAAY,OAAO,IAEvC,EAAI,sBAAsB;GACxB,MAAM;GACN,aAAa;GACd,C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nE,EAAI,KACF,EAAQ,KAAK;GACX,MAAM;GACN,UAAU;GACV,SAAS,EAAG,aAAa,GAAe,OAAO;GAChD,CAAC;GAEJ;CAGF,IAAI,IAAmB,EAAE,EAEnB,IAAmB,EAAK,KAC5B,EAAQ,MACR,OACA,UACA,oBACD;AAGD,CADA,MAAM,EAAmB,EAAiB,EAC1C,EAAS,KAAK;EACZ,MAAM;EACN,UAAU;EACV,SAAS,EAAG,aAAa,GAAkB,OAAO;EACnD,CAAC;CAIF,IAAI,IAAa,EAAK,KACpB,EAAQ,MACR,OACA,UACA,YACA,qBACD;AAoBD,CAlBK,EAAG,WAAW,EAAW,KAC5B,IAAa,EAAK,KAAK,EAAQ,MAAM,OAAO,UAAU,qBAAqB,GAGxE,EAAG,WAAW,EAAW,KAC5B,IAAa,EAAK,KAChB,EAAQ,MACR,OACA,UACA,YACA,iBACD,GAGE,EAAG,WAAW,EAAW,KAC5B,IAAa,EAAK,KAAK,EAAQ,MAAM,OAAO,UAAU,iBAAiB,GAGrE,EAAG,WAAW,EAAW,KAC3B,EAAuB,EAAW,EAClC,EAAS,KAAK;EACZ,MAAM;EACN,UAAU;EACV,SAAS,EAAG,aAAa,GAAY,OAAO;EAC7C,CAAC;CAGJ,IAAM,IAAkB,EAAK,KAAK,EAAQ,MAAM,OAAO,kBAAkB;AAWzE,QATI,EAAG,WAAW,EAAgB,KAChC,EAAkB,EAAgB,EAClC,EAAS,KAAK;EACZ,MAAM;EACN,UAAU;EACV,SAAS,EAAG,aAAa,GAAiB,OAAO;EAClD,CAAC,GAGG;EACL,SAAS,EAAE;EACX;EACA,SAAS,EAAE;EACX,YAAY,EAAE;EACd,UAAU,EAAE;EACZ,aAAa,EAAE;EAChB"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import e from "node:fs";
|
|
2
|
+
import t from "node:path";
|
|
3
|
+
import { Project as n, SyntaxKind as r } from "ts-morph";
|
|
4
|
+
//#region src/patterns/enable/api/modifies/hooks.server.ts
|
|
5
|
+
function i(e) {
|
|
6
|
+
let t = new n().addSourceFileAtPath(e), i = t.getDescendantsOfKind(r.CallExpression).find((e) => e.getExpression().getText() === "handlePocketbase");
|
|
7
|
+
if (!i) {
|
|
8
|
+
t.saveSync();
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
let a = i.getArguments();
|
|
12
|
+
if (a.length === 0) {
|
|
13
|
+
i.addArgument("{ api: { enabled: true } }"), t.formatText(), t.saveSync();
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
let o = a[0];
|
|
17
|
+
if (o.getKind() !== r.ObjectLiteralExpression) {
|
|
18
|
+
i.insertArgument(0, "{ api: { enabled: true } }"), t.formatText(), t.saveSync();
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
let s = o.asKindOrThrow(r.ObjectLiteralExpression), c = s.getProperty("api");
|
|
22
|
+
if (!c) {
|
|
23
|
+
s.addPropertyAssignment({
|
|
24
|
+
name: "api",
|
|
25
|
+
initializer: "{ enabled: true }"
|
|
26
|
+
}), t.formatText(), t.saveSync();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (c.getKind() === r.PropertyAssignment) {
|
|
30
|
+
let e = c.getInitializer();
|
|
31
|
+
if (e && e.getKind() === r.ObjectLiteralExpression) {
|
|
32
|
+
let t = e;
|
|
33
|
+
t.getProperty("enabled") || t.addPropertyAssignment({
|
|
34
|
+
name: "enabled",
|
|
35
|
+
initializer: "true"
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
t.formatText(), t.saveSync();
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/patterns/enable/api/generate.runtime.ts
|
|
43
|
+
async function a(n) {
|
|
44
|
+
let r = [], a = t.join(n.root, "src", "hooks.server.ts");
|
|
45
|
+
return e.existsSync(a) && (i(a), r.push({
|
|
46
|
+
path: a,
|
|
47
|
+
language: "ts",
|
|
48
|
+
content: e.readFileSync(a, "utf8")
|
|
49
|
+
})), {
|
|
50
|
+
creates: [],
|
|
51
|
+
modifies: r,
|
|
52
|
+
deletes: [],
|
|
53
|
+
components: [],
|
|
54
|
+
packages: [],
|
|
55
|
+
collections: []
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { a as generate };
|
|
60
|
+
|
|
61
|
+
//# sourceMappingURL=generate.runtime-egq8I3EA.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate.runtime-egq8I3EA.js","names":[],"sources":["../../src/patterns/enable/api/modifies/hooks.server.ts","../../src/patterns/enable/api/generate.runtime.ts"],"sourcesContent":["import { Project, SyntaxKind } from \"ts-morph\";\n\nexport function modifyHooksServer(hooksServerPath: string) {\n const project = new Project();\n const sourceFile = project.addSourceFileAtPath(hooksServerPath);\n\n const callExpr = sourceFile\n .getDescendantsOfKind(SyntaxKind.CallExpression)\n .find((ce) => ce.getExpression().getText() === \"handlePocketbase\");\n\n if (!callExpr) {\n sourceFile.saveSync();\n return;\n }\n\n const args = callExpr.getArguments();\n if (args.length === 0) {\n callExpr.addArgument(`{ api: { enabled: true } }`);\n sourceFile.formatText();\n sourceFile.saveSync();\n return;\n }\n\n const firstArg = args[0];\n if (firstArg.getKind() !== SyntaxKind.ObjectLiteralExpression) {\n callExpr.insertArgument(0, `{ api: { enabled: true } }`);\n sourceFile.formatText();\n sourceFile.saveSync();\n return;\n }\n\n const optionsObj = firstArg.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);\n const apiProp = optionsObj.getProperty(\"api\");\n\n if (!apiProp) {\n optionsObj.addPropertyAssignment({\n name: \"api\",\n initializer: `{ enabled: true }`,\n });\n sourceFile.formatText();\n sourceFile.saveSync();\n return;\n }\n\n if (apiProp.getKind() === SyntaxKind.PropertyAssignment) {\n const apiInit = (\n apiProp as import(\"ts-morph\").PropertyAssignment\n ).getInitializer();\n if (apiInit && apiInit.getKind() === SyntaxKind.ObjectLiteralExpression) {\n const apiObj = apiInit as import(\"ts-morph\").ObjectLiteralExpression;\n const enabledProp = apiObj.getProperty(\"enabled\");\n if (!enabledProp) {\n apiObj.addPropertyAssignment({ name: \"enabled\", initializer: \"true\" });\n }\n }\n }\n\n sourceFile.formatText();\n sourceFile.saveSync();\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport type { File, Options, Result } from \"../../../core/types\";\nimport { modifyHooksServer } from \"./modifies/hooks.server\";\n\nexport async function generate(options: Options) {\n const modifies: File[] = [];\n\n const hooksServerPath = path.join(options.root, \"src\", \"hooks.server.ts\");\n if (fs.existsSync(hooksServerPath)) {\n modifyHooksServer(hooksServerPath);\n modifies.push({\n path: hooksServerPath,\n language: \"ts\",\n content: fs.readFileSync(hooksServerPath, \"utf8\"),\n });\n }\n\n return {\n creates: [],\n modifies,\n deletes: [],\n components: [],\n packages: [],\n collections: [],\n } satisfies Result;\n}\n"],"mappings":";;;;AAEA,SAAgB,EAAkB,GAAyB;CAEzD,IAAM,IADU,IAAI,GAAS,CACF,oBAAoB,EAAgB,EAEzD,IAAW,EACd,qBAAqB,EAAW,eAAe,CAC/C,MAAM,MAAO,EAAG,eAAe,CAAC,SAAS,KAAK,mBAAmB;AAEpE,KAAI,CAAC,GAAU;AACb,IAAW,UAAU;AACrB;;CAGF,IAAM,IAAO,EAAS,cAAc;AACpC,KAAI,EAAK,WAAW,GAAG;AAGrB,EAFA,EAAS,YAAY,6BAA6B,EAClD,EAAW,YAAY,EACvB,EAAW,UAAU;AACrB;;CAGF,IAAM,IAAW,EAAK;AACtB,KAAI,EAAS,SAAS,KAAK,EAAW,yBAAyB;AAG7D,EAFA,EAAS,eAAe,GAAG,6BAA6B,EACxD,EAAW,YAAY,EACvB,EAAW,UAAU;AACrB;;CAGF,IAAM,IAAa,EAAS,cAAc,EAAW,wBAAwB,EACvE,IAAU,EAAW,YAAY,MAAM;AAE7C,KAAI,CAAC,GAAS;AAMZ,EALA,EAAW,sBAAsB;GAC/B,MAAM;GACN,aA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