@velastack/patterns 0.13.2 → 0.13.3
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/app-d-ts-h33RrYIv.js +198 -0
- package/dist/chunks/app-d-ts-h33RrYIv.js.map +1 -0
- package/dist/chunks/app-layout.svelte-CZ0hw-pi.js +51 -0
- package/dist/chunks/app-layout.svelte-CZ0hw-pi.js.map +1 -0
- package/dist/chunks/{generate.runtime-Dfl-07VS.js → generate.runtime-CZKIKP3B.js} +214 -178
- package/dist/chunks/generate.runtime-CZKIKP3B.js.map +1 -0
- package/dist/chunks/{generate.runtime-B6FeCL5I.js → generate.runtime-CsP4c8GY.js} +78 -77
- package/dist/chunks/generate.runtime-CsP4c8GY.js.map +1 -0
- package/dist/chunks/generate.runtime-DZUPtJVF.js +429 -0
- package/dist/chunks/generate.runtime-DZUPtJVF.js.map +1 -0
- package/dist/chunks/{generate.runtime-i3tMIA1A.js → generate.runtime-pbIrEsa7.js} +45 -45
- package/dist/chunks/generate.runtime-pbIrEsa7.js.map +1 -0
- package/dist/chunks/i18n-BHCytiiO.js +28 -0
- package/dist/chunks/i18n-BHCytiiO.js.map +1 -0
- package/dist/chunks/translations-CiJnl574.js +189 -0
- package/dist/chunks/translations-CiJnl574.js.map +1 -0
- package/dist/index.js +677 -664
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunks/_layout-xBzFhszN.js +0 -78
- package/dist/chunks/_layout-xBzFhszN.js.map +0 -1
- package/dist/chunks/generate.runtime-B6FeCL5I.js.map +0 -1
- package/dist/chunks/generate.runtime-DLg2CKxQ.js +0 -341
- package/dist/chunks/generate.runtime-DLg2CKxQ.js.map +0 -1
- package/dist/chunks/generate.runtime-Dfl-07VS.js.map +0 -1
- package/dist/chunks/generate.runtime-i3tMIA1A.js.map +0 -1
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { t as e } from "./dedent-CCUOcPFW.js";
|
|
2
|
+
import { i as t, o as n, r } from "./ts-morph-helpers-B4KZXP44.js";
|
|
3
|
+
import { t as i } from "./package-imports-CcIIHbp5.js";
|
|
4
|
+
import { a, c as o, i as s, l as c, r as l, s as u } from "./load-function-BFvIVsh7.js";
|
|
5
|
+
import d from "node:fs";
|
|
6
|
+
import f from "node:path";
|
|
7
|
+
import { Node as p, Project as m, QuoteKind as h } from "ts-morph";
|
|
8
|
+
// src/patterns/enable/i18n/modifies/prettierignore.ts
|
|
9
|
+
var g = "src/locales/", _ = "# Wuchale";
|
|
10
|
+
function v(e) {
|
|
11
|
+
if (!d.existsSync(e)) return {
|
|
12
|
+
status: "success",
|
|
13
|
+
changed: !1
|
|
14
|
+
};
|
|
15
|
+
let t = d.readFileSync(e, "utf8");
|
|
16
|
+
if (t.split("\n").some((e) => e.trim().replace(/^\//, "") === "src/locales/")) return {
|
|
17
|
+
status: "success",
|
|
18
|
+
changed: !1
|
|
19
|
+
};
|
|
20
|
+
let n = t.length > 0 && !t.endsWith("\n"), r = t.length > 0 ? "\n" : "";
|
|
21
|
+
return d.writeFileSync(e, `${t}${n ? "\n" : ""}${r}${_}\n${g}\n`), {
|
|
22
|
+
status: "success",
|
|
23
|
+
changed: !0
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
// src/patterns/enable/i18n/modifies/package-imports.ts
|
|
27
|
+
var y = { "#locales/*": "./src/locales/*" };
|
|
28
|
+
function b(e) {
|
|
29
|
+
return i(e, y);
|
|
30
|
+
}
|
|
31
|
+
// src/patterns/enable/i18n/modifies/+layout.ts
|
|
32
|
+
var x = e`
|
|
33
|
+
import { browser } from '$app/env';
|
|
34
|
+
import { loadLocale } from 'wuchale/load-utils';
|
|
35
|
+
|
|
36
|
+
import { getLocale } from '#locales/main.url.js';
|
|
37
|
+
import { hasLocalizedUrl } from '#lib/url.js';
|
|
38
|
+
import '#locales/main.loader.svelte.js';
|
|
39
|
+
import '#locales/js.loader.js';
|
|
40
|
+
`, S = e`
|
|
41
|
+
export const load = async ({ url, data }) => {
|
|
42
|
+
// A public page's language is in its URL. The others follow the visitor's
|
|
43
|
+
// preference, which only the server knows: see src/lib/locale.ts.
|
|
44
|
+
const locale = hasLocalizedUrl(url.pathname) ? getLocale(url) : data.locale;
|
|
45
|
+
|
|
46
|
+
if (browser) {
|
|
47
|
+
await loadLocale(locale);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { ...data, locale };
|
|
51
|
+
};
|
|
52
|
+
`, C = e`
|
|
53
|
+
export const load = async ({ url, data }) => {
|
|
54
|
+
const locale = getLocale(url);
|
|
55
|
+
|
|
56
|
+
if (browser) {
|
|
57
|
+
await loadLocale(locale);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return data;
|
|
61
|
+
};
|
|
62
|
+
`, w = `${x}\n\n${S}`, T = [
|
|
63
|
+
"Merge the wuchale loader into your existing load function in +layout.ts:",
|
|
64
|
+
"",
|
|
65
|
+
w
|
|
66
|
+
].join("\n");
|
|
67
|
+
function E(e) {
|
|
68
|
+
if (!d.existsSync(e)) return d.mkdirSync(f.dirname(e), { recursive: !0 }), d.writeFileSync(e, w + "\n"), {
|
|
69
|
+
status: "success",
|
|
70
|
+
changed: !0
|
|
71
|
+
};
|
|
72
|
+
let i = d.readFileSync(e, "utf8");
|
|
73
|
+
if (i.includes("loadLocale") || i.includes("translateUrl")) return {
|
|
74
|
+
status: "success",
|
|
75
|
+
changed: !1
|
|
76
|
+
};
|
|
77
|
+
if (/\bexport\s+(const|function)\s+load\b/.test(i)) return {
|
|
78
|
+
status: "failed",
|
|
79
|
+
message: T
|
|
80
|
+
};
|
|
81
|
+
let a = new m({
|
|
82
|
+
compilerOptions: { allowJs: !0 },
|
|
83
|
+
manipulationSettings: { quoteKind: h.Single }
|
|
84
|
+
}).addSourceFileAtPath(e);
|
|
85
|
+
return t(a, "$app/env", "browser"), t(a, "wuchale/load-utils", "loadLocale"), t(a, "#locales/main.url.js", "getLocale"), t(a, "#lib/url.js", "hasLocalizedUrl"), r(a, [{ moduleSpecifier: "#locales/main.loader.svelte.js" }, { moduleSpecifier: "#locales/js.loader.js" }]), a.addStatements(`\n${S}\n`), n(a), a.saveSync(), {
|
|
86
|
+
status: "success",
|
|
87
|
+
changed: !0
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// src/patterns/enable/i18n/modifies/layout.server.ts
|
|
91
|
+
var D = e`
|
|
92
|
+
import type { LayoutServerLoad } from './$types';
|
|
93
|
+
|
|
94
|
+
export const load: LayoutServerLoad = async ({ locals }) => {
|
|
95
|
+
// Decided on the server (src/hooks.server.ts), because pages without a
|
|
96
|
+
// language in their URL follow the visitor's preference.
|
|
97
|
+
return { locale: locals.locale };
|
|
98
|
+
};
|
|
99
|
+
`, O = "Return the request's language from the load in src/routes/+layout.server.ts:\n\nreturn {\n // ...what it returns today\n locale: locals.locale\n};";
|
|
100
|
+
function k(e) {
|
|
101
|
+
let t = e.getBody();
|
|
102
|
+
if (!t) return null;
|
|
103
|
+
if (p.isBlock(t)) {
|
|
104
|
+
let n = l(e, t);
|
|
105
|
+
return n ? c(n) : null;
|
|
106
|
+
}
|
|
107
|
+
let n = t;
|
|
108
|
+
for (; p.isParenthesizedExpression(n);) n = n.getExpression();
|
|
109
|
+
return p.isObjectLiteralExpression(n) ? n : null;
|
|
110
|
+
}
|
|
111
|
+
function A(e) {
|
|
112
|
+
if (!d.existsSync(e)) return d.mkdirSync(f.dirname(e), { recursive: !0 }), d.writeFileSync(e, D + "\n"), {
|
|
113
|
+
status: "success",
|
|
114
|
+
changed: !0
|
|
115
|
+
};
|
|
116
|
+
let t = d.readFileSync(e, "utf8");
|
|
117
|
+
if (t.includes("locals.locale")) return {
|
|
118
|
+
status: "success",
|
|
119
|
+
changed: !1
|
|
120
|
+
};
|
|
121
|
+
let r = o().addSourceFileAtPath(e), i = s(r);
|
|
122
|
+
if (!i) return /\bload\b/.test(t) ? {
|
|
123
|
+
status: "failed",
|
|
124
|
+
message: O
|
|
125
|
+
} : (r.addStatements("\n" + D.split("\n").slice(2).join("\n") + "\n"), r.insertStatements(0, D.split("\n")[0]), n(r), r.saveSync(), {
|
|
126
|
+
status: "success",
|
|
127
|
+
changed: !0
|
|
128
|
+
});
|
|
129
|
+
let c = u(i), l = k(i);
|
|
130
|
+
if (c.kind === "unsupported" || !l || a(l, "locale")) return {
|
|
131
|
+
status: "failed",
|
|
132
|
+
message: O
|
|
133
|
+
};
|
|
134
|
+
let p = "locals";
|
|
135
|
+
if (c.kind === "identifier") p = `${c.name}.locals`;
|
|
136
|
+
else if (c.kind === "none") i.addParameter({ name: "{ locals }" });
|
|
137
|
+
else {
|
|
138
|
+
let e = c.pattern.getElements(), t = e.find((e) => (e.getPropertyNameNode()?.getText() ?? e.getName()) === "locals");
|
|
139
|
+
if (t) p = t.getName();
|
|
140
|
+
else if (e.some((e) => e.getDotDotDotToken())) return {
|
|
141
|
+
status: "failed",
|
|
142
|
+
message: O
|
|
143
|
+
};
|
|
144
|
+
else {
|
|
145
|
+
let t = e.map((e) => e.getText());
|
|
146
|
+
c.pattern.replaceWithText(`{ ${["locals", ...t].join(", ")} }`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return l.addPropertyAssignment({
|
|
150
|
+
name: "locale",
|
|
151
|
+
initializer: `${p}.locale`,
|
|
152
|
+
leadingTrivia: (e) => e.writeLine("// Decided on the server (src/hooks.server.ts), because pages without a").writeLine("// language in their URL follow the visitor's preference.")
|
|
153
|
+
}), n(r), r.saveSync(), {
|
|
154
|
+
status: "success",
|
|
155
|
+
changed: !0
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
// src/patterns/enable/i18n/modifies/app-d-ts.ts
|
|
159
|
+
var j = "locale: import('#locales/data.js').Locale;", M = "/** The language of this request: see src/lib/locale.ts. */", N = [
|
|
160
|
+
"Declare the request's language on App.Locals in src/app.d.ts:",
|
|
161
|
+
"",
|
|
162
|
+
"interface Locals {",
|
|
163
|
+
` ${M}`,
|
|
164
|
+
` ${j}`,
|
|
165
|
+
"}"
|
|
166
|
+
].join("\n"), P = /^([ \t]*)\/\/\s*interface Locals\s*\{\s*\}[ \t]*$/m, F = /^([ \t]*)interface Locals\s*\{/m;
|
|
167
|
+
function I(e) {
|
|
168
|
+
if (!d.existsSync(e)) return {
|
|
169
|
+
status: "not-found",
|
|
170
|
+
message: N
|
|
171
|
+
};
|
|
172
|
+
let t = d.readFileSync(e, "utf8"), n = F.exec(t);
|
|
173
|
+
if (n) {
|
|
174
|
+
let e = n.index + n[0].length, r = t.indexOf("}", e);
|
|
175
|
+
if (r !== -1 && /\blocale\??\s*:/.test(t.slice(e, r))) return {
|
|
176
|
+
status: "success",
|
|
177
|
+
changed: !1
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
let r = /^\t/m.test(t) ? " " : " ", i = (e) => `${e}${r}${M}\n${e}${r}${j}\n`, a, o = P.exec(t);
|
|
181
|
+
if (n) {
|
|
182
|
+
let e = n.index + n[0].length, r = t.slice(e);
|
|
183
|
+
a = t.slice(0, e) + "\n" + i(n[1]) + (r.startsWith("}") ? n[1] : "") + r.replace(/^[ \t]*\n/, "");
|
|
184
|
+
} else if (o) {
|
|
185
|
+
let e = o[1];
|
|
186
|
+
a = t.replace(P, `${e}interface Locals {\n${i(e)}${e}}`);
|
|
187
|
+
} else return {
|
|
188
|
+
status: "failed",
|
|
189
|
+
message: N
|
|
190
|
+
};
|
|
191
|
+
return d.writeFileSync(e, a), {
|
|
192
|
+
status: "success",
|
|
193
|
+
changed: !0
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
export { A as a, E as c, g as d, _ as f, D as i, y as l, j as n, C as o, v as p, I as r, S as s, M as t, b as u };
|
|
197
|
+
|
|
198
|
+
//# sourceMappingURL=app-d-ts-h33RrYIv.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-d-ts-h33RrYIv.js","names":["FAILURE_HINT","FAILURE_HINT","dedent","ensureNamedImport","formatLikeSource","ensurePackageImports","newProject","findLoadFunction","returnedObject","findFinalReturn","inspectEventParameter","fs","path","LOCALES_ENTRY","PRETTIERIGNORE_HEADER","modifyPrettierignore","filePath","original","line","needsNewline","separator","LOCALES_IMPORTS","modifyPackageImportsI18n","root","IMPORT_SNIPPET","LOAD_STATEMENT","LEGACY_LOAD_STATEMENT","LOAD_SNIPPET","FAILURE_HINT$2","ensureRootLayoutI18n","layoutPath","existing","sourceFile","Project","QuoteKind","ensureImports","LAYOUT_SERVER_SNIPPET","FAILURE_HINT$1","loadResult","body","fn","Node","returned","expression","modifyLayoutServerLocale","layoutServerPath","event","object","hasProperty","locals","elements","element","kept","writer","LOCALS_MEMBER","LOCALS_COMMENT","FAILURE_HINT","COMMENTED","DECLARED","modifyAppDts","appDtsPath","declared","start","end","unit","member","indent","updated","commented","at","rest"],"sources":["../../src/patterns/enable/i18n/modifies/prettierignore.ts","../../src/patterns/enable/i18n/modifies/package-imports.ts","../../src/patterns/enable/i18n/modifies/+layout.ts","../../src/patterns/enable/i18n/modifies/layout.server.ts","../../src/patterns/enable/i18n/modifies/app-d-ts.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\n\n/** Where wuchale writes its loaders, proxies and catalogs on every extract. */\nexport const LOCALES_ENTRY = \"src/locales/\";\nexport const PRETTIERIGNORE_HEADER = \"# Wuchale\";\n\n/**\n * wuchale rewrites `src/locales/` in its own style on every extract, which\n * the project's `prettier --check .` would flag. A project without a\n * `.prettierignore` is left alone: without prettier there is nothing to\n * ignore, and with it the file is the project's to start.\n */\nexport function modifyPrettierignore(filePath: string): ModifyOutcome {\n if (!fs.existsSync(filePath)) {\n return { status: \"success\", changed: false };\n }\n\n const original = fs.readFileSync(filePath, \"utf8\");\n const ignored = original\n .split(\"\\n\")\n .some((line) => line.trim().replace(/^\\//, \"\") === LOCALES_ENTRY);\n if (ignored) {\n return { status: \"success\", changed: false };\n }\n\n const needsNewline = original.length > 0 && !original.endsWith(\"\\n\");\n const separator = original.length > 0 ? \"\\n\" : \"\";\n fs.writeFileSync(\n filePath,\n `${original}${needsNewline ? \"\\n\" : \"\"}${separator}${PRETTIERIGNORE_HEADER}\\n${LOCALES_ENTRY}\\n`,\n );\n return { status: \"success\", changed: true };\n}\n","import {\n ensurePackageImports,\n type PackageJsonEditResult,\n} from \"../../../../runtime/package-imports\";\n\n/**\n * The package.json `imports` entry the app's `#locales/*.js` specifiers\n * resolve through. wuchale writes its loaders and data to `src/locales`;\n * SvelteKit 3 deprecates `alias`, so the directory is mapped the way `#lib`\n * is. wuchale's own generated imports are relative and never go through it.\n */\nexport const LOCALES_IMPORTS = { \"#locales/*\": \"./src/locales/*\" };\n\n/** Add `#locales/*` to package.json `imports`; a different value already there is reported. */\nexport function modifyPackageImportsI18n(root: string): PackageJsonEditResult {\n return ensurePackageImports(root, LOCALES_IMPORTS);\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport dedent from \"dedent\";\nimport { Project, QuoteKind } from \"ts-morph\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\nimport {\n ensureImports,\n ensureNamedImport,\n formatLikeSource,\n} from \"../../../../runtime/ts-morph-helpers\";\n\nconst IMPORT_SNIPPET = dedent`\n import { browser } from '$app/env';\n import { loadLocale } from 'wuchale/load-utils';\n\n import { getLocale } from '#locales/main.url.js';\n import { hasLocalizedUrl } from '#lib/url.js';\n import '#locales/main.loader.svelte.js';\n import '#locales/js.loader.js';\n`;\n\n/** The load `disable-i18n` looks for to take it out again. */\nexport const LOAD_STATEMENT = dedent`\n export const load = async ({ url, data }) => {\n // A public page's language is in its URL. The others follow the visitor's\n // preference, which only the server knows: see src/lib/locale.ts.\n const locale = hasLocalizedUrl(url.pathname) ? getLocale(url) : data.locale;\n\n if (browser) {\n await loadLocale(locale);\n }\n\n return { ...data, locale };\n };\n`;\n\n/**\n * The load enable-i18n wrote while every page took its language from the\n * URL. `disable-i18n` still takes it out of a project enabled back then.\n */\nexport const LEGACY_LOAD_STATEMENT = dedent`\n export const load = async ({ url, data }) => {\n const locale = getLocale(url);\n\n if (browser) {\n await loadLocale(locale);\n }\n\n return data;\n };\n`;\n\n/** The whole file, for a project that had no root `+layout.ts`. */\nexport const LOAD_SNIPPET = `${IMPORT_SNIPPET}\\n\\n${LOAD_STATEMENT}`;\n\nconst FAILURE_HINT = [\n \"Merge the wuchale loader into your existing load function in +layout.ts:\",\n \"\",\n LOAD_SNIPPET,\n].join(\"\\n\");\n\nexport function ensureRootLayoutI18n(layoutPath: string): ModifyOutcome {\n if (!fs.existsSync(layoutPath)) {\n fs.mkdirSync(path.dirname(layoutPath), { recursive: true });\n fs.writeFileSync(layoutPath, LOAD_SNIPPET + \"\\n\");\n return { status: \"success\", changed: true };\n }\n\n const existing = fs.readFileSync(layoutPath, \"utf8\");\n if (existing.includes(\"loadLocale\") || existing.includes(\"translateUrl\")) {\n return { status: \"success\", changed: false };\n }\n\n if (/\\bexport\\s+(const|function)\\s+load\\b/.test(existing)) {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n\n // No load to merge with, but the file may hold other route options\n // (`export const prerender = true`, say), so the load is added to it\n // rather than written over it.\n const project = new Project({\n compilerOptions: { allowJs: true },\n manipulationSettings: { quoteKind: QuoteKind.Single },\n });\n const sourceFile = project.addSourceFileAtPath(layoutPath);\n\n ensureNamedImport(sourceFile, \"$app/env\", \"browser\");\n ensureNamedImport(sourceFile, \"wuchale/load-utils\", \"loadLocale\");\n ensureNamedImport(sourceFile, \"#locales/main.url.js\", \"getLocale\");\n ensureNamedImport(sourceFile, \"#lib/url.js\", \"hasLocalizedUrl\");\n ensureImports(sourceFile, [\n { moduleSpecifier: \"#locales/main.loader.svelte.js\" },\n { moduleSpecifier: \"#locales/js.loader.js\" },\n ]);\n\n sourceFile.addStatements(`\\n${LOAD_STATEMENT}\\n`);\n formatLikeSource(sourceFile);\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport dedent from \"dedent\";\nimport { Node, type ObjectLiteralExpression } from \"ts-morph\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\nimport {\n findFinalReturn,\n findLoadFunction,\n hasProperty,\n inspectEventParameter,\n newProject,\n returnedObject,\n} from \"../../../../runtime/load-function\";\nimport { formatLikeSource } from \"../../../../runtime/ts-morph-helpers\";\n\n/**\n * The whole file, for a project that had no root `+layout.server.ts`.\n * `disable-i18n` deletes it again while it is still exactly this.\n */\nexport const LAYOUT_SERVER_SNIPPET = dedent`\n import type { LayoutServerLoad } from './$types';\n\n export const load: LayoutServerLoad = async ({ locals }) => {\n \t// Decided on the server (src/hooks.server.ts), because pages without a\n \t// language in their URL follow the visitor's preference.\n \treturn { locale: locals.locale };\n };\n`;\n\nconst FAILURE_HINT = [\n \"Return the request's language from the load in src/routes/+layout.server.ts:\",\n \"\",\n \"return {\",\n \" // ...what it returns today\",\n \" locale: locals.locale\",\n \"};\",\n].join(\"\\n\");\n\n/** The object a load yields: its final `return`, or an arrow's `({ ... })` body. */\nfunction loadResult(\n fn: NonNullable<ReturnType<typeof findLoadFunction>>,\n): ObjectLiteralExpression | null {\n const body = fn.getBody();\n if (!body) return null;\n if (Node.isBlock(body)) {\n const returned = findFinalReturn(fn, body);\n return returned ? returnedObject(returned) : null;\n }\n let expression: Node = body;\n while (Node.isParenthesizedExpression(expression)) {\n expression = expression.getExpression();\n }\n return Node.isObjectLiteralExpression(expression) ? expression : null;\n}\n\n/**\n * The root layout's server load returns `locale`, the language the hook chose\n * for the request, for `+layout.ts` and the language select to read. Created\n * when the project has no `+layout.server.ts`; otherwise the property joins\n * what the load returns already. A load with several returns, or one that\n * returns something other than an object literal, is reported as `failed`\n * with the line to add and left as it was.\n */\nexport function modifyLayoutServerLocale(\n layoutServerPath: string,\n): ModifyOutcome {\n if (!fs.existsSync(layoutServerPath)) {\n fs.mkdirSync(path.dirname(layoutServerPath), { recursive: true });\n fs.writeFileSync(layoutServerPath, LAYOUT_SERVER_SNIPPET + \"\\n\");\n return { status: \"success\", changed: true };\n }\n\n const original = fs.readFileSync(layoutServerPath, \"utf8\");\n if (original.includes(\"locals.locale\")) {\n return { status: \"success\", changed: false };\n }\n\n const sourceFile = newProject().addSourceFileAtPath(layoutServerPath);\n const fn = findLoadFunction(sourceFile);\n if (!fn) {\n // Route options only (`export const prerender = true`, say).\n if (/\\bload\\b/.test(original)) {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n sourceFile.addStatements(\n \"\\n\" + LAYOUT_SERVER_SNIPPET.split(\"\\n\").slice(2).join(\"\\n\") + \"\\n\",\n );\n sourceFile.insertStatements(0, LAYOUT_SERVER_SNIPPET.split(\"\\n\")[0]);\n formatLikeSource(sourceFile);\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n }\n\n const event = inspectEventParameter(fn);\n const object = loadResult(fn);\n if (\n event.kind === \"unsupported\" ||\n !object ||\n hasProperty(object, \"locale\")\n ) {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n\n let locals = \"locals\";\n if (event.kind === \"identifier\") {\n locals = `${event.name}.locals`;\n } else if (event.kind === \"none\") {\n fn.addParameter({ name: \"{ locals }\" });\n } else {\n const elements = event.pattern.getElements();\n const existing = elements.find(\n (element) =>\n (element.getPropertyNameNode()?.getText() ?? element.getName()) ===\n \"locals\",\n );\n if (existing) {\n locals = existing.getName();\n } else if (elements.some((element) => element.getDotDotDotToken())) {\n return { status: \"failed\", message: FAILURE_HINT };\n } else {\n const kept = elements.map((element) => element.getText());\n event.pattern.replaceWithText(`{ ${[\"locals\", ...kept].join(\", \")} }`);\n }\n }\n\n // After any spread, so nothing spread in can shadow it.\n object.addPropertyAssignment({\n name: \"locale\",\n initializer: `${locals}.locale`,\n leadingTrivia: (writer) =>\n writer\n .writeLine(\n \"// Decided on the server (src/hooks.server.ts), because pages without a\",\n )\n .writeLine(\"// language in their URL follow the visitor's preference.\"),\n });\n\n formatLikeSource(sourceFile);\n sourceFile.saveSync();\n return { status: \"success\", changed: true };\n}\n","import fs from \"node:fs\";\nimport type { ModifyOutcome } from \"../../../../core/types\";\n\n/** The member `disable-i18n` looks for to take it out again. */\nexport const LOCALS_MEMBER = \"locale: import('#locales/data.js').Locale;\";\nexport const LOCALS_COMMENT =\n \"/** The language of this request: see src/lib/locale.ts. */\";\n\nconst FAILURE_HINT = [\n \"Declare the request's language on App.Locals in src/app.d.ts:\",\n \"\",\n \"interface Locals {\",\n ` ${LOCALS_COMMENT}`,\n ` ${LOCALS_MEMBER}`,\n \"}\",\n].join(\"\\n\");\n\n/** `// interface Locals {}`, as `sv create` leaves it. */\nconst COMMENTED = /^([ \\t]*)\\/\\/\\s*interface Locals\\s*\\{\\s*\\}[ \\t]*$/m;\nconst DECLARED = /^([ \\t]*)interface Locals\\s*\\{/m;\n\n/**\n * `event.locals.locale`, which the i18n handle sets, is declared on\n * `App.Locals`. The interface `sv create` leaves commented out is switched\n * on; one that is declared already gains the member.\n */\nexport function modifyAppDts(appDtsPath: string): ModifyOutcome {\n if (!fs.existsSync(appDtsPath)) {\n return { status: \"not-found\", message: FAILURE_HINT };\n }\n\n const original = fs.readFileSync(appDtsPath, \"utf8\");\n // Whatever its type, a `locale` on Locals is there.\n const declared = DECLARED.exec(original);\n if (declared) {\n const start = declared.index + declared[0].length;\n const end = original.indexOf(\"}\", start);\n if (end !== -1 && /\\blocale\\??\\s*:/.test(original.slice(start, end))) {\n return { status: \"success\", changed: false };\n }\n }\n\n const unit = /^\\t/m.test(original) ? \"\\t\" : \" \";\n const member = (indent: string) =>\n `${indent}${unit}${LOCALS_COMMENT}\\n${indent}${unit}${LOCALS_MEMBER}\\n`;\n\n let updated: string;\n const commented = COMMENTED.exec(original);\n if (declared) {\n const at = declared.index + declared[0].length;\n const rest = original.slice(at);\n updated =\n original.slice(0, at) +\n \"\\n\" +\n member(declared[1]) +\n // `interface Locals {}` on one line: the brace moves to its own.\n (rest.startsWith(\"}\") ? declared[1] : \"\") +\n rest.replace(/^[ \\t]*\\n/, \"\");\n } else if (commented) {\n const indent = commented[1];\n updated = original.replace(\n COMMENTED,\n `${indent}interface Locals {\\n${member(indent)}${indent}}`,\n );\n } else {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n\n fs.writeFileSync(appDtsPath, updated);\n return { status: \"success\", changed: true };\n}\n"],"mappings":";;;;;;;;AAIA,IAAaa,IAAgB,gBAChBC,IAAwB;AAQrC,SAAgBC,EAAqB,GAAiC;CACpE,IAAI,CAACJ,EAAG,WAAWK,CAAQ,GACzB,OAAO;EAAE,QAAQ;EAAW,SAAS;CAAM;CAG7C,IAAMC,IAAWN,EAAG,aAAaK,GAAU,MAAM;CAIjD,IAHgBC,EACb,MAAM,IAAI,CAAC,CACX,MAAM,MAASC,EAAK,KAAK,CAAC,CAAC,QAAQ,OAAO,EAAE,MAAA,cAC3C,GACF,OAAO;EAAE,QAAQ;EAAW,SAAS;CAAM;CAG7C,IAAMC,IAAeF,EAAS,SAAS,KAAK,CAACA,EAAS,SAAS,IAAI,GAC7DG,IAAYH,EAAS,SAAS,IAAI,OAAO;CAK/C,OAJAN,EAAG,cACDK,GACA,GAAGC,IAAWE,IAAe,OAAO,KAAKC,IAAYN,EAAsB,IAAID,EAAc,GAC/F,GACO;EAAE,QAAQ;EAAW,SAAS;CAAK;AAC5C;;ACtBA,IAAaQ,IAAkB,EAAE,cAAc,kBAAkB;AAGjE,SAAgBC,EAAyB,GAAqC;CAC5E,OAAOjB,EAAqBkB,GAAMF,CAAe;AACnD;;ACLA,IAAMG,IAAiB,CAAM;;;;;;;;GAWhBC,IAAiB,CAAM;;;;;;;;;;;;GAkBvBC,IAAwB,CAAM;;;;;;;;;;GAa9BC,IAAe,GAAGH,EAAe,MAAMC,KAE9CzB,IAAe;CACnB;CACA;CACA2B;AACF,CAAC,CAAC,KAAK,IAAI;AAEX,SAAgBE,EAAqB,GAAmC;CACtE,IAAI,CAAClB,EAAG,WAAWmB,CAAU,GAG3B,OAFAnB,EAAG,UAAUC,EAAK,QAAQkB,CAAU,GAAG,EAAE,WAA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,MAAO,MACtCgD,KAAU,MACd,GAAGC,IAASF,IAAOT,EAAe,IAAIW,IAASF,IAAOV,EAAc,KAElEa,GACEC,IAAYX,EAAU,KAAKxC,CAAQ;CACzC,IAAI4C,GAAU;EACZ,IAAMQ,IAAKR,EAAS,QAAQA,EAAS,EAAE,CAAC,QAClCS,IAAOrD,EAAS,MAAMoD,CAAE;EAC9B,IACEpD,EAAS,MAAM,GAAGoD,CAAE,IACpB,OACAJ,EAAOJ,EAAS,EAAE,KAEjBS,EAAK,WAAW,GAAG,IAAIT,EAAS,KAAK,MACtCS,EAAK,QAAQ,aAAa,EAAE;CAChC,OAAO,IAAIF,GAAW;EACpB,IAAMF,IAASE,EAAU;EACzB,IAAUnD,EAAS,QACjBwC,GACA,GAAGS,EAAO,sBAAsBD,EAAOC,CAAM,IAAIA,EAAO,EAC1D;CACF,OACE,OAAO;EAAE,QAAQ;EAAU,SAASV;CAAa;CAInD,OADA7C,EAAG,cAAciD,GAAYO,CAAO,GAC7B;EAAE,QAAQ;EAAW,SAAS;CAAK;AAC5C"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { t as e } from "./dedent-CCUOcPFW.js";
|
|
2
|
+
import { t } from "./svelte-file-DfplF3BU.js";
|
|
3
|
+
import { _ as n, r } from "./ts-morph-helpers-B4KZXP44.js";
|
|
4
|
+
import i from "node:fs";
|
|
5
|
+
import a from "node:path";
|
|
6
|
+
// src/patterns/enable/i18n/modifies/app-layout.svelte.ts
|
|
7
|
+
var o = a.join("src", "routes", "(app)", "+layout.svelte"), s = " <div class=\"ml-auto px-4\">\n <LanguageSelect />\n </div>\n ", c = /[ \t]*<div class="ml-auto px-4">\s*<LanguageSelect\s*\/>\s*<\/div>[ \t]*\r?\n?/g, l = [
|
|
8
|
+
"Add the language select to the <header> of the signed-in layout,",
|
|
9
|
+
"src/routes/(app)/+layout.svelte. Signed-in pages have one URL for every",
|
|
10
|
+
"language, so the select is how a visitor changes it there.",
|
|
11
|
+
"",
|
|
12
|
+
"Import to add to the <script> tag:",
|
|
13
|
+
"",
|
|
14
|
+
e`
|
|
15
|
+
import LanguageSelect from '#lib/components/language-select.svelte';
|
|
16
|
+
`,
|
|
17
|
+
"",
|
|
18
|
+
"Markup to add at the end of <header>:",
|
|
19
|
+
"",
|
|
20
|
+
"<div class=\"ml-auto px-4\">",
|
|
21
|
+
" <LanguageSelect />",
|
|
22
|
+
"</div>"
|
|
23
|
+
].join("\n");
|
|
24
|
+
function u(e) {
|
|
25
|
+
if (!i.existsSync(e)) return {
|
|
26
|
+
status: "success",
|
|
27
|
+
changed: !1
|
|
28
|
+
};
|
|
29
|
+
let a = t.fromPath(e);
|
|
30
|
+
return a.hasElement("LanguageSelect") ? {
|
|
31
|
+
status: "success",
|
|
32
|
+
changed: !1
|
|
33
|
+
} : !a.getScript() || !a.hasElement("header") ? {
|
|
34
|
+
status: "failed",
|
|
35
|
+
message: l
|
|
36
|
+
} : (a.modifyScript((e) => {
|
|
37
|
+
let { source: t } = n(e, (e) => {
|
|
38
|
+
r(e, [{
|
|
39
|
+
defaultImport: "LanguageSelect",
|
|
40
|
+
moduleSpecifier: "#lib/components/language-select.svelte"
|
|
41
|
+
}]);
|
|
42
|
+
});
|
|
43
|
+
return t;
|
|
44
|
+
}), a.insertBeforeClosingTag("header", s), a.writeTo(e), {
|
|
45
|
+
status: "success",
|
|
46
|
+
changed: a.hasChanged()
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
export { o as n, u as r, c as t };
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=app-layout.svelte-CZ0hw-pi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-layout.svelte-CZ0hw-pi.js","names":["dedent","withInMemoryScript","fs","path","APP_LAYOUT_PATH","LANGUAGE_SELECT_SNIPPET","APP_LANGUAGE_SELECT","FAILURE_HINT","modifyAppLayoutLanguageSelect","layoutPath","file","SvelteFile","out","source","sf"],"sources":["../../src/patterns/enable/i18n/modifies/app-layout.svelte.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport path from \"node:path\";\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\n/** The signed-in layout enable-auth writes, relative to the project root. */\nexport const APP_LAYOUT_PATH = path.join(\n \"src\",\n \"routes\",\n \"(app)\",\n \"+layout.svelte\",\n);\n\n/** At the end of the header, pushed to its right edge. */\nconst LANGUAGE_SELECT_SNIPPET = `\t<div class=\"ml-auto px-4\">\n\t\t\t\t\t<LanguageSelect />\n\t\t\t\t</div>\n\t\t\t`;\n\n/** What `disable-i18n` takes back out. */\nexport const APP_LANGUAGE_SELECT =\n /[ \\t]*<div class=\"ml-auto px-4\">\\s*<LanguageSelect\\s*\\/>\\s*<\\/div>[ \\t]*\\r?\\n?/g;\n\nconst IMPORT_SNIPPET = dedent`\n import LanguageSelect from '#lib/components/language-select.svelte';\n`;\n\nconst FAILURE_HINT = [\n \"Add the language select to the <header> of the signed-in layout,\",\n \"src/routes/(app)/+layout.svelte. Signed-in pages have one URL for every\",\n \"language, so the select is how a visitor changes it there.\",\n \"\",\n \"Import to add to the <script> tag:\",\n \"\",\n IMPORT_SNIPPET,\n \"\",\n \"Markup to add at the end of <header>:\",\n \"\",\n '<div class=\"ml-auto px-4\">',\n \" <LanguageSelect />\",\n \"</div>\",\n].join(\"\\n\");\n\n/**\n * The language select at the end of the signed-in layout's header. Signed-in\n * pages have no language in their URL and no public navbar, so without it a\n * visitor could only change language by going back to a public page.\n *\n * Shared by the two patterns that can be the second one enabled: enable-i18n\n * adds it to the layout enable-auth wrote, and enable-auth adds it to its own\n * layout when the project already has i18n. A project without the layout is\n * left alone.\n */\nexport function modifyAppLayoutLanguageSelect(\n layoutPath: string,\n): ModifyOutcome {\n if (!fs.existsSync(layoutPath)) {\n return { status: \"success\", changed: false };\n }\n\n const file = SvelteFile.fromPath(layoutPath);\n if (file.hasElement(\"LanguageSelect\")) {\n return { status: \"success\", changed: false };\n }\n if (!file.getScript() || !file.hasElement(\"header\")) {\n return { status: \"failed\", message: FAILURE_HINT };\n }\n\n file.modifyScript((source) => {\n const { source: out } = withInMemoryScript(source, (sf) => {\n ensureImports(sf, [\n {\n defaultImport: \"LanguageSelect\",\n moduleSpecifier: \"#lib/components/language-select.svelte\",\n },\n ]);\n });\n return out;\n });\n file.insertBeforeClosingTag(\"header\", LANGUAGE_SELECT_SNIPPET);\n file.writeTo(layoutPath);\n return { status: \"success\", changed: file.hasChanged() };\n}\n"],"mappings":";;;;;;AAWA,IAAaI,IAAkBD,EAAK,KAClC,OACA,UACA,SACA,gBACF,GAGME,IAA0B,2EAMnBC,IACX,mFAMIC,IAAe;CACnB;CACA;CACA;CACA;CACA;CACA;CACA,CAX2B;;;CAY3B;CACA;CACA;CACA;CACA;CACA;AACF,CAAC,CAAC,KAAK,IAAI;AAYX,SAAgBC,EACd,GACe;CACf,IAAI,CAACN,EAAG,WAAWO,CAAU,GAC3B,OAAO;EAAE,QAAQ;EAAW,SAAS;CAAM;CAG7C,IAAMC,IAAOC,EAAW,SAASF,CAAU;CAqB3C,OApBIC,EAAK,WAAW,gBAAgB,IAC3B;EAAE,QAAQ;EAAW,SAAS;CAAM,IAEzC,CAACA,EAAK,UAAU,KAAK,CAACA,EAAK,WAAW,QAAQ,IACzC;EAAE,QAAQ;EAAU,SAASH;CAAa,KAGnDG,EAAK,cAAc,MAAW;EAC5B,IAAM,EAAE,QAAQE,MAAQX,EAAmBY,IAAS,MAAO;GACzD,EAAcC,GAAI,CAChB;IACE,eAAe;IACf,iBAAiB;GACnB,CACF,CAAC;EACH,CAAC;EACD,OAAOF;CACT,CAAC,GACDF,EAAK,uBAAuB,UAAUL,CAAuB,GAC7DK,EAAK,QAAQD,CAAU,GAChB;EAAE,QAAQ;EAAW,SAASC,EAAK,WAAW;CAAE;AACzD"}
|