@soroush.design/cli 0.2.0-alpha.3 → 0.2.0-alpha.4
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/bin.mjs +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +1 -1
- package/dist/{io-Bybgct-f.mjs → io-C_8_JerZ.mjs} +368 -1039
- package/package.json +13 -5
- package/templates/astro/js/base/README.md +93 -0
- package/templates/astro/js/base/_gitignore +5 -0
- package/templates/astro/js/base/astro.config.mjs +23 -0
- package/templates/astro/js/base/src/components/About.jsx +24 -0
- package/templates/astro/js/base/src/components/Counter.jsx +26 -0
- package/templates/astro/js/base/src/components/Greeting.jsx +60 -0
- package/templates/astro/js/base/src/components/Header.jsx +99 -0
- package/templates/astro/js/base/src/components/Home.jsx +32 -0
- package/templates/astro/js/base/src/components/Home.test.jsx +39 -0
- package/templates/astro/js/base/src/components/NotFound.jsx +17 -0
- package/templates/astro/js/base/src/data/api.js +84 -0
- package/templates/astro/js/base/src/layouts/Layout.astro +52 -0
- package/templates/astro/js/base/src/pages/404.astro +12 -0
- package/templates/astro/js/base/src/pages/about.astro +12 -0
- package/templates/astro/js/base/src/pages/index.astro +23 -0
- package/templates/astro/js/base/src/theme/Island.jsx +89 -0
- package/templates/astro/js/base/src/theme/scheme.js +32 -0
- package/templates/astro/js/base/vitest.config.js +12 -0
- package/templates/astro/js/sample-data/src/components/ItemDetails.jsx +64 -0
- package/templates/astro/js/sample-data/src/components/ItemList.jsx +100 -0
- package/templates/astro/js/sample-data/src/components/StatusChip.jsx +8 -0
- package/templates/astro/js/sample-data/src/data/items.js +65 -0
- package/templates/astro/js/sample-data/src/pages/items/[id].astro +37 -0
- package/templates/astro/js/sample-data/src/pages/items/index.astro +20 -0
- package/templates/astro/js/with/jotai/src/components/Counter.jsx +26 -0
- package/templates/astro/js/with/jotai/src/state/counter.js +4 -0
- package/templates/astro/js/with/msw/_env +3 -0
- package/templates/astro/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/astro/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/astro/js/with/msw/src/mocks/server.js +5 -0
- package/templates/astro/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/astro/js/with/msw/src/mocks/start.js +14 -0
- package/templates/astro/js/with/react-hook-form/src/components/Form.jsx +313 -0
- package/templates/astro/js/with/react-hook-form/src/pages/form.astro +8 -0
- package/templates/astro/js/with/redux-toolkit/src/components/Counter.jsx +27 -0
- package/templates/astro/js/with/redux-toolkit/src/state/store.js +18 -0
- package/templates/astro/js/with/tanstack-query/src/data/queryClient.js +13 -0
- package/templates/astro/js/with/zustand/src/components/Counter.jsx +24 -0
- package/templates/astro/js/with/zustand/src/state/counter.js +14 -0
- package/templates/astro/ts/base/README.md +93 -0
- package/templates/astro/ts/base/_gitignore +5 -0
- package/templates/astro/ts/base/astro.config.mjs +23 -0
- package/templates/astro/ts/base/src/components/About.tsx +24 -0
- package/templates/astro/ts/base/src/components/Counter.tsx +26 -0
- package/templates/astro/ts/base/src/components/Greeting.tsx +61 -0
- package/templates/astro/ts/base/src/components/Header.tsx +99 -0
- package/templates/astro/ts/base/src/components/Home.test.tsx +39 -0
- package/templates/astro/ts/base/src/components/Home.tsx +33 -0
- package/templates/astro/ts/base/src/components/NotFound.tsx +17 -0
- package/templates/astro/ts/base/src/data/api.ts +92 -0
- package/templates/astro/ts/base/src/layouts/Layout.astro +52 -0
- package/templates/astro/ts/base/src/pages/404.astro +12 -0
- package/templates/astro/ts/base/src/pages/about.astro +12 -0
- package/templates/astro/ts/base/src/pages/index.astro +23 -0
- package/templates/astro/ts/base/src/theme/Island.tsx +90 -0
- package/templates/astro/ts/base/src/theme/scheme.ts +34 -0
- package/templates/astro/ts/base/tsconfig.json +9 -0
- package/templates/astro/ts/base/vitest.config.ts +12 -0
- package/templates/astro/ts/sample-data/src/components/ItemDetails.tsx +65 -0
- package/templates/astro/ts/sample-data/src/components/ItemList.tsx +109 -0
- package/templates/astro/ts/sample-data/src/components/StatusChip.tsx +9 -0
- package/templates/astro/ts/sample-data/src/data/items.ts +71 -0
- package/templates/astro/ts/sample-data/src/pages/items/[id].astro +39 -0
- package/templates/astro/ts/sample-data/src/pages/items/index.astro +20 -0
- package/templates/astro/ts/with/jotai/src/components/Counter.tsx +26 -0
- package/templates/astro/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/astro/ts/with/msw/_env +3 -0
- package/templates/astro/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/astro/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/astro/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/astro/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/astro/ts/with/msw/src/mocks/start.ts +14 -0
- package/templates/astro/ts/with/react-hook-form/src/components/Form.tsx +317 -0
- package/templates/astro/ts/with/react-hook-form/src/pages/form.astro +8 -0
- package/templates/astro/ts/with/redux-toolkit/src/components/Counter.tsx +27 -0
- package/templates/astro/ts/with/redux-toolkit/src/state/store.ts +19 -0
- package/templates/astro/ts/with/tanstack-query/src/data/queryClient.ts +13 -0
- package/templates/astro/ts/with/zustand/src/components/Counter.tsx +24 -0
- package/templates/astro/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/templates.md +66 -0
- package/templates/vike/js/base/README.md +87 -0
- package/templates/vike/js/base/_gitignore +4 -0
- package/templates/vike/js/base/src/data/api.js +76 -0
- package/templates/vike/js/base/src/pages/+Layout.jsx +183 -0
- package/templates/vike/js/base/src/pages/+config.js +12 -0
- package/templates/vike/js/base/src/pages/+onRenderClient.jsx +39 -0
- package/templates/vike/js/base/src/pages/+onRenderHtml.jsx +27 -0
- package/templates/vike/js/base/src/pages/PageShell.jsx +47 -0
- package/templates/vike/js/base/src/pages/_error/+Page.jsx +14 -0
- package/templates/vike/js/base/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/base/src/pages/index/+Page.jsx +33 -0
- package/templates/vike/js/base/src/pages/index/Counter.jsx +20 -0
- package/templates/vike/js/base/src/pages/index/Greeting.jsx +48 -0
- package/templates/vike/js/base/src/pages/index/Page.test.jsx +67 -0
- package/templates/vike/js/base/src/pages/usePageContext.jsx +20 -0
- package/templates/vike/js/base/vite.config.js +14 -0
- package/templates/vike/js/base/vitest.config.js +13 -0
- package/templates/vike/js/sample-data/src/data/items.js +58 -0
- package/templates/vike/js/sample-data/src/pages/items/+Page.jsx +49 -0
- package/templates/vike/js/sample-data/src/pages/items/+data.js +26 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+Page.jsx +40 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+data.js +33 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+onBeforePrerenderStart.js +8 -0
- package/templates/vike/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
- package/templates/vike/js/spa/src/pages/+config.js +12 -0
- package/templates/vike/js/spa/src/pages/+onRenderClient.jsx +25 -0
- package/templates/vike/js/spa/src/pages/+onRenderHtml.jsx +16 -0
- package/templates/vike/js/spa/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/ssg/src/pages/+config.js +11 -0
- package/templates/vike/js/ssg/src/pages/about/+Page.jsx +13 -0
- package/templates/vike/js/ssr/src/pages/+config.js +12 -0
- package/templates/vike/js/ssr/src/pages/+onRenderHtml.jsx +27 -0
- package/templates/vike/js/ssr/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/with/jotai/src/pages/index/Counter.jsx +19 -0
- package/templates/vike/js/with/jotai/src/state/counter.js +4 -0
- package/templates/vike/js/with/jotai/src/state/scheme.js +4 -0
- package/templates/vike/js/with/msw/_env +3 -0
- package/templates/vike/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/vike/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/vike/js/with/msw/src/mocks/server.js +5 -0
- package/templates/vike/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/vike/js/with/msw/src/mocks/start.js +10 -0
- package/templates/vike/js/with/react-hook-form/src/pages/form/+Page.jsx +294 -0
- package/templates/vike/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
- package/templates/vike/js/with/redux-toolkit/src/state/store.js +27 -0
- package/templates/vike/js/with/zustand/src/pages/index/Counter.jsx +17 -0
- package/templates/vike/js/with/zustand/src/state/counter.js +14 -0
- package/templates/vike/js/with/zustand/src/state/scheme.js +14 -0
- package/templates/vike/ts/base/README.md +87 -0
- package/templates/vike/ts/base/_gitignore +4 -0
- package/templates/vike/ts/base/src/data/api.ts +84 -0
- package/templates/vike/ts/base/src/pages/+Layout.tsx +184 -0
- package/templates/vike/ts/base/src/pages/+config.ts +14 -0
- package/templates/vike/ts/base/src/pages/+onRenderClient.tsx +41 -0
- package/templates/vike/ts/base/src/pages/+onRenderHtml.tsx +28 -0
- package/templates/vike/ts/base/src/pages/PageShell.tsx +48 -0
- package/templates/vike/ts/base/src/pages/_error/+Page.tsx +14 -0
- package/templates/vike/ts/base/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/base/src/pages/index/+Page.tsx +33 -0
- package/templates/vike/ts/base/src/pages/index/Counter.tsx +20 -0
- package/templates/vike/ts/base/src/pages/index/Greeting.tsx +49 -0
- package/templates/vike/ts/base/src/pages/index/Page.test.tsx +67 -0
- package/templates/vike/ts/base/src/pages/usePageContext.tsx +27 -0
- package/templates/vike/ts/base/src/vike.d.ts +20 -0
- package/templates/vike/ts/base/tsconfig.json +18 -0
- package/templates/vike/ts/base/vite.config.ts +14 -0
- package/templates/vike/ts/base/vitest.config.ts +13 -0
- package/templates/vike/ts/sample-data/src/data/items.ts +64 -0
- package/templates/vike/ts/sample-data/src/pages/items/+Page.tsx +50 -0
- package/templates/vike/ts/sample-data/src/pages/items/+data.ts +28 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+Page.tsx +41 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+data.ts +37 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+onBeforePrerenderStart.ts +8 -0
- package/templates/vike/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
- package/templates/vike/ts/spa/src/pages/+config.ts +14 -0
- package/templates/vike/ts/spa/src/pages/+onRenderClient.tsx +27 -0
- package/templates/vike/ts/spa/src/pages/+onRenderHtml.tsx +17 -0
- package/templates/vike/ts/spa/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/ssg/src/pages/+config.ts +13 -0
- package/templates/vike/ts/ssg/src/pages/about/+Page.tsx +13 -0
- package/templates/vike/ts/ssr/src/pages/+config.ts +14 -0
- package/templates/vike/ts/ssr/src/pages/+onRenderHtml.tsx +28 -0
- package/templates/vike/ts/ssr/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
- package/templates/vike/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/vike/ts/with/jotai/src/state/scheme.ts +4 -0
- package/templates/vike/ts/with/msw/_env +3 -0
- package/templates/vike/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/vike/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/vike/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/vike/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/vike/ts/with/msw/src/mocks/start.ts +10 -0
- package/templates/vike/ts/with/react-hook-form/src/pages/form/+Page.tsx +298 -0
- package/templates/vike/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
- package/templates/vike/ts/with/redux-toolkit/src/state/store.ts +29 -0
- package/templates/vike/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
- package/templates/vike/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/vike/ts/with/zustand/src/state/scheme.ts +19 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { parseArgs } from "node:util";
|
|
2
2
|
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, rmdirSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { dirname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
4
5
|
import { spawnSync } from "node:child_process";
|
|
5
6
|
import { emitKeypressEvents } from "node:readline";
|
|
6
7
|
import { createInterface } from "node:readline/promises";
|
|
@@ -917,10 +918,6 @@ const LIBRARIES = [
|
|
|
917
918
|
{
|
|
918
919
|
name: "react-hook-form",
|
|
919
920
|
label: "React Hook Form + Zod: a form, validated"
|
|
920
|
-
},
|
|
921
|
-
{
|
|
922
|
-
name: "vitest",
|
|
923
|
-
label: "Vitest + Testing Library: tests"
|
|
924
921
|
}
|
|
925
922
|
];
|
|
926
923
|
const LIBRARY_NAMES = LIBRARIES.map(({ name }) => name);
|
|
@@ -941,11 +938,197 @@ function findLibraryConflict(libraries) {
|
|
|
941
938
|
return `${listNames(names)} ${all} ${GROUP_JOBS[group]}: pick one.`;
|
|
942
939
|
}
|
|
943
940
|
}
|
|
941
|
+
// src/create/template/template.ts
|
|
942
|
+
const COMMENTS = [
|
|
943
|
+
["//", ""],
|
|
944
|
+
["{/*", "*/}"],
|
|
945
|
+
["<!--", "-->"]
|
|
946
|
+
];
|
|
947
|
+
/** The marker `line` holds - its word and its condition - or `undefined` when it holds none. */
|
|
948
|
+
function readMarker(line) {
|
|
949
|
+
const text = line.trim();
|
|
950
|
+
for (const [open, close] of COMMENTS) {
|
|
951
|
+
if (!text.startsWith(open) || !text.endsWith(close)) continue;
|
|
952
|
+
const inner = text.slice(open.length, text.length - close.length).trim();
|
|
953
|
+
const match = /^@(if|elif|else|endif)\b(.*)$/.exec(inner);
|
|
954
|
+
if (match) return {
|
|
955
|
+
word: match[1],
|
|
956
|
+
expression: match[2].trim()
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* Whether `expression` holds for `flags`: names, `!`, `&&`, `||` and parentheses, as in
|
|
962
|
+
* `tanstack-query && !ssg`.
|
|
963
|
+
*/
|
|
964
|
+
function isConditionMet(expression, flags) {
|
|
965
|
+
const tokens = expression.match(/[a-z0-9-]+|&&|\|\||[!()]/g) ?? [];
|
|
966
|
+
if (tokens.join("") !== expression.replaceAll(/\s/g, "")) throw new Error(`The condition "${expression}" holds something other than names and ! && || ( ).`);
|
|
967
|
+
let position = 0;
|
|
968
|
+
const next = () => tokens[position++];
|
|
969
|
+
const readOr = () => {
|
|
970
|
+
let value = readAnd();
|
|
971
|
+
while (tokens[position] === "||") {
|
|
972
|
+
next();
|
|
973
|
+
value = readAnd() || value;
|
|
974
|
+
}
|
|
975
|
+
return value;
|
|
976
|
+
};
|
|
977
|
+
const readAnd = () => {
|
|
978
|
+
let value = readNot();
|
|
979
|
+
while (tokens[position] === "&&") {
|
|
980
|
+
next();
|
|
981
|
+
value = readNot() && value;
|
|
982
|
+
}
|
|
983
|
+
return value;
|
|
984
|
+
};
|
|
985
|
+
const readNot = () => {
|
|
986
|
+
const token = next();
|
|
987
|
+
if (token === "!") return !readNot();
|
|
988
|
+
if (token === "(") {
|
|
989
|
+
const value = readOr();
|
|
990
|
+
if (next() !== ")") throw new Error(`The condition "${expression}" leaves a ( open.`);
|
|
991
|
+
return value;
|
|
992
|
+
}
|
|
993
|
+
if (token === void 0 || !/^[a-z0-9-]+$/.test(token)) throw new Error(`The condition "${expression}" lacks a name where it has ${token ?? "none"}.`);
|
|
994
|
+
return flags.has(token);
|
|
995
|
+
};
|
|
996
|
+
const value = readOr();
|
|
997
|
+
if (position !== tokens.length) throw new Error(`The condition "${expression}" goes on after its end.`);
|
|
998
|
+
return value;
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* A template's text for an app: the lines whose conditions hold, with each placeholder replaced by
|
|
1002
|
+
* its value. Returns `undefined` when nothing is left, so the file is not written.
|
|
1003
|
+
*/
|
|
1004
|
+
function renderTemplate(source, flags, values, name = "the template") {
|
|
1005
|
+
const open = [];
|
|
1006
|
+
const isKept = () => open.every((block) => block.isKept);
|
|
1007
|
+
const lines = [];
|
|
1008
|
+
for (const line of source.split("\n")) {
|
|
1009
|
+
const marker = readMarker(line);
|
|
1010
|
+
if (!marker) {
|
|
1011
|
+
if (isKept()) lines.push(line);
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
const { word, expression } = marker;
|
|
1015
|
+
const block = open.at(-1);
|
|
1016
|
+
if (word === "if") {
|
|
1017
|
+
const holds = isConditionMet(expression, flags);
|
|
1018
|
+
open.push({
|
|
1019
|
+
isKept: holds,
|
|
1020
|
+
isTaken: holds
|
|
1021
|
+
});
|
|
1022
|
+
} else if (!block) throw new Error(`${name} has an @${word} with no @if.`);
|
|
1023
|
+
else if (word === "elif") {
|
|
1024
|
+
block.isKept = !block.isTaken && isConditionMet(expression, flags);
|
|
1025
|
+
block.isTaken ||= block.isKept;
|
|
1026
|
+
} else if (word === "else") {
|
|
1027
|
+
block.isKept = !block.isTaken;
|
|
1028
|
+
block.isTaken = true;
|
|
1029
|
+
} else open.pop();
|
|
1030
|
+
}
|
|
1031
|
+
if (open.length > 0) throw new Error(`${name} leaves an @if open.`);
|
|
1032
|
+
let text = lines.join("\n");
|
|
1033
|
+
for (const [placeholder, value] of Object.entries(values)) text = text.replaceAll(placeholder, value);
|
|
1034
|
+
return text.trim() === "" ? void 0 : text;
|
|
1035
|
+
}
|
|
1036
|
+
/** The names npm or this repository would drop from a template, and what they become. */
|
|
1037
|
+
const RENAMES = {
|
|
1038
|
+
_gitignore: ".gitignore",
|
|
1039
|
+
_env: ".env"
|
|
1040
|
+
};
|
|
1041
|
+
const toOutputPath = (path) => {
|
|
1042
|
+
const name = path.split("/").at(-1);
|
|
1043
|
+
return Object.hasOwn(RENAMES, name) ? `${path.slice(0, -name.length)}${RENAMES[name]}` : path;
|
|
1044
|
+
};
|
|
1045
|
+
/** Every file under `folder`, by its path from there, `/`-separated. */
|
|
1046
|
+
function readFolder(folder) {
|
|
1047
|
+
const files = /* @__PURE__ */ new Map();
|
|
1048
|
+
const visit = (directory) => {
|
|
1049
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
1050
|
+
const path = join(directory, entry.name);
|
|
1051
|
+
if (entry.isDirectory()) visit(path);
|
|
1052
|
+
else files.set(relative(folder, path).replaceAll("\\", "/"), readFileSync(path, "utf8"));
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
visit(folder);
|
|
1056
|
+
return files;
|
|
1057
|
+
}
|
|
1058
|
+
/** The folder of this package that holds the templates, from where this module runs. */
|
|
1059
|
+
function findTemplatesRoot(start = dirname(fileURLToPath(import.meta.url))) {
|
|
1060
|
+
for (let directory = start;; directory = dirname(directory)) {
|
|
1061
|
+
const templates = join(directory, "templates");
|
|
1062
|
+
if (existsSync(join(templates, "templates.md"))) return templates;
|
|
1063
|
+
if (dirname(directory) === directory) throw new Error(`No templates folder above ${start}.`);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* The files of `layers`, read from `root` in order, each over the ones before it. A layer that is
|
|
1068
|
+
* not there adds nothing. Returns each file's source by the path it is written at.
|
|
1069
|
+
*/
|
|
1070
|
+
function readLayers(root, layers) {
|
|
1071
|
+
const files = /* @__PURE__ */ new Map();
|
|
1072
|
+
for (const layer of layers) {
|
|
1073
|
+
const folder = join(root, layer);
|
|
1074
|
+
if (!existsSync(folder)) continue;
|
|
1075
|
+
for (const [path, source] of readFolder(folder)) files.set(toOutputPath(path), source);
|
|
1076
|
+
}
|
|
1077
|
+
return files;
|
|
1078
|
+
}
|
|
1079
|
+
// src/create/template/starterFiles.ts
|
|
1080
|
+
/** The names that hold for the app `facts` describe, as the templates' markers read them. */
|
|
1081
|
+
function getFlags({ isTypeScript, rendering, libraries, hasSampleData }) {
|
|
1082
|
+
return /* @__PURE__ */ new Set([
|
|
1083
|
+
isTypeScript ? "ts" : "js",
|
|
1084
|
+
rendering,
|
|
1085
|
+
...libraries,
|
|
1086
|
+
...hasSampleData ? ["sample-data"] : []
|
|
1087
|
+
]);
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* The layers of a framework's template for the app `facts` describe, in the order they are laid:
|
|
1091
|
+
* the base, the rendering's own files, the sample data pages, then each library's, in the order
|
|
1092
|
+
* create offers them, whatever order they were given in.
|
|
1093
|
+
*/
|
|
1094
|
+
function getLayers({ rendering, libraries, hasSampleData }) {
|
|
1095
|
+
return [
|
|
1096
|
+
"base",
|
|
1097
|
+
...rendering === "isomorphic" ? [] : [rendering],
|
|
1098
|
+
...hasSampleData ? ["sample-data"] : [],
|
|
1099
|
+
...LIBRARY_NAMES.filter((name) => libraries.includes(name)).map((name) => `with/${name}`)
|
|
1100
|
+
];
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* The files the template of `framework` writes for the app `facts` describe: its layers, laid
|
|
1104
|
+
* one over the other, each file with the lines its markers keep and its placeholders filled.
|
|
1105
|
+
*/
|
|
1106
|
+
function createTemplateFiles(framework, facts, root = findTemplatesRoot()) {
|
|
1107
|
+
const folder = join(root, framework, facts.isTypeScript ? "ts" : "js");
|
|
1108
|
+
const flags = getFlags(facts);
|
|
1109
|
+
const values = {
|
|
1110
|
+
__TITLE_STRING__: facts.title.replaceAll("'", String.raw`\'`),
|
|
1111
|
+
__TITLE__: facts.title,
|
|
1112
|
+
__SCHEME__: facts.scheme
|
|
1113
|
+
};
|
|
1114
|
+
const files = [];
|
|
1115
|
+
for (const [path, source] of readLayers(folder, getLayers(facts))) {
|
|
1116
|
+
const content = renderTemplate(source, flags, values, `${framework}/${path}`);
|
|
1117
|
+
if (content !== void 0) files.push({
|
|
1118
|
+
path,
|
|
1119
|
+
content
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
return files;
|
|
1123
|
+
}
|
|
944
1124
|
// package.json
|
|
945
|
-
var version = "0.2.0-alpha.
|
|
946
|
-
// src/create/
|
|
947
|
-
/** The versions the
|
|
1125
|
+
var version = "0.2.0-alpha.4";
|
|
1126
|
+
// src/create/versions.ts
|
|
1127
|
+
/** The versions the starters install: the ones this package is tested with. */
|
|
948
1128
|
const VERSIONS = {
|
|
1129
|
+
"@astrojs/check": "^0.9.12",
|
|
1130
|
+
"@astrojs/node": "^11.1.13",
|
|
1131
|
+
"@astrojs/react": "^7.1.9",
|
|
949
1132
|
"@emotion/server": "^11.11.0",
|
|
950
1133
|
"@hookform/resolvers": "^7.0.6",
|
|
951
1134
|
"@reduxjs/toolkit": "^2.14.0",
|
|
@@ -958,6 +1141,7 @@ const VERSIONS = {
|
|
|
958
1141
|
"@types/react-dom": "^19.3.0",
|
|
959
1142
|
"@vitejs/plugin-react": "^6.3.0",
|
|
960
1143
|
"@vitest/coverage-v8": "^5.1.0",
|
|
1144
|
+
"astro": "^7.4.12",
|
|
961
1145
|
"jotai": "^3.0.2",
|
|
962
1146
|
"jsdom": "^31.0.1",
|
|
963
1147
|
"msw": "^3.0.5",
|
|
@@ -967,8 +1151,10 @@ const VERSIONS = {
|
|
|
967
1151
|
"react-hook-form": "^7.92.0",
|
|
968
1152
|
"react-redux": "^9.3.1",
|
|
969
1153
|
"swr": "^2.5.1",
|
|
1154
|
+
"ts-blank-space": "^0.9.0",
|
|
970
1155
|
"tsdown": "^0.23.7",
|
|
971
1156
|
"typescript": "~7.0.2",
|
|
1157
|
+
"typescript-astro": "npm:typescript@^6.0.3",
|
|
972
1158
|
"vike": "0.4.270",
|
|
973
1159
|
"vite": "~8.3.8",
|
|
974
1160
|
"vitest": "^5.1.0",
|
|
@@ -977,6 +1163,7 @@ const VERSIONS = {
|
|
|
977
1163
|
};
|
|
978
1164
|
/** A version from this package's own manifest, as a range an app takes. */
|
|
979
1165
|
const toRange = (version) => /^\d/.test(version) ? `^${version}` : version;
|
|
1166
|
+
/** The ranges of `names`, by name, as a package.json lists them. */
|
|
980
1167
|
const pickVersions = (names) => Object.fromEntries(names.map((name) => [name, toRange(VERSIONS[name])]));
|
|
981
1168
|
/** The packages each library installs: for the app's code, and for its development only. */
|
|
982
1169
|
const LIBRARY_PACKAGES = {
|
|
@@ -990,18 +1177,105 @@ const LIBRARY_PACKAGES = {
|
|
|
990
1177
|
"react-hook-form",
|
|
991
1178
|
"zod",
|
|
992
1179
|
"@hookform/resolvers"
|
|
993
|
-
] },
|
|
994
|
-
vitest: { devDependencies: [
|
|
995
|
-
"vitest",
|
|
996
|
-
"@testing-library/react",
|
|
997
|
-
"@testing-library/dom",
|
|
998
|
-
"jsdom"
|
|
999
1180
|
] }
|
|
1000
1181
|
};
|
|
1182
|
+
/** The packages of `libraries`: what the app's code imports, and what only its development does. */
|
|
1183
|
+
function getLibraryPackages(libraries) {
|
|
1184
|
+
return {
|
|
1185
|
+
dependencies: libraries.flatMap((library) => LIBRARY_PACKAGES[library].dependencies ?? []),
|
|
1186
|
+
devDependencies: libraries.flatMap((library) => LIBRARY_PACKAGES[library].devDependencies ?? [])
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
/** What every app's tests run on: Vitest, and Testing Library on jsdom. */
|
|
1190
|
+
const TEST_PACKAGES = [
|
|
1191
|
+
"vitest",
|
|
1192
|
+
"@testing-library/react",
|
|
1193
|
+
"@testing-library/dom",
|
|
1194
|
+
"jsdom"
|
|
1195
|
+
];
|
|
1196
|
+
// src/create/astro/astro.ts
|
|
1197
|
+
const TYPESCRIPT_RANGE = VERSIONS["typescript-astro"].replace("npm:typescript@", "");
|
|
1198
|
+
function createPackageJson$1({ packageName, isTypeScript, rendering, libraries, version }) {
|
|
1199
|
+
const isServer = rendering === "ssr";
|
|
1200
|
+
const { dependencies: libraryPackages, devDependencies: libraryDevPackages } = getLibraryPackages(libraries);
|
|
1201
|
+
const packageJson = {
|
|
1202
|
+
name: packageName,
|
|
1203
|
+
private: true,
|
|
1204
|
+
version: "0.0.0",
|
|
1205
|
+
type: "module",
|
|
1206
|
+
scripts: {
|
|
1207
|
+
dev: "astro dev",
|
|
1208
|
+
build: "astro build",
|
|
1209
|
+
preview: "astro preview",
|
|
1210
|
+
...isServer ? { start: "node dist/server/entry.mjs" } : {},
|
|
1211
|
+
...isTypeScript ? { typecheck: "astro check" } : {},
|
|
1212
|
+
test: "vitest run"
|
|
1213
|
+
},
|
|
1214
|
+
dependencies: {
|
|
1215
|
+
"@soroush.tech/design-system": toRange(version),
|
|
1216
|
+
...pickVersions([
|
|
1217
|
+
...isServer ? ["@astrojs/node"] : [],
|
|
1218
|
+
"@astrojs/react",
|
|
1219
|
+
...libraryPackages,
|
|
1220
|
+
"astro",
|
|
1221
|
+
"react",
|
|
1222
|
+
"react-dom"
|
|
1223
|
+
])
|
|
1224
|
+
},
|
|
1225
|
+
devDependencies: {
|
|
1226
|
+
...pickVersions([
|
|
1227
|
+
...isTypeScript ? [
|
|
1228
|
+
"@astrojs/check",
|
|
1229
|
+
"@types/react",
|
|
1230
|
+
"@types/react-dom"
|
|
1231
|
+
] : [],
|
|
1232
|
+
...libraryDevPackages,
|
|
1233
|
+
...TEST_PACKAGES
|
|
1234
|
+
]),
|
|
1235
|
+
...isTypeScript ? { typescript: TYPESCRIPT_RANGE } : {}
|
|
1236
|
+
}
|
|
1237
|
+
};
|
|
1238
|
+
return `${JSON.stringify(packageJson, null, 2)}\n`;
|
|
1239
|
+
}
|
|
1240
|
+
/** The files of the Astro app `facts` describe. */
|
|
1241
|
+
function createFiles$1(facts) {
|
|
1242
|
+
const { isTypeScript, scheme } = facts;
|
|
1243
|
+
const x = isTypeScript ? "ts" : "js";
|
|
1244
|
+
return [
|
|
1245
|
+
{
|
|
1246
|
+
path: "package.json",
|
|
1247
|
+
content: createPackageJson$1(facts)
|
|
1248
|
+
},
|
|
1249
|
+
{
|
|
1250
|
+
path: `src/theme/themes.${x}`,
|
|
1251
|
+
content: createThemesSource({ isTypeScript })
|
|
1252
|
+
},
|
|
1253
|
+
{
|
|
1254
|
+
path: `src/theme/Providers.${x}`,
|
|
1255
|
+
content: createProvidersSource({
|
|
1256
|
+
isTypeScript,
|
|
1257
|
+
scheme,
|
|
1258
|
+
themesImport: "./themes"
|
|
1259
|
+
})
|
|
1260
|
+
},
|
|
1261
|
+
...createTemplateFiles("astro", facts)
|
|
1262
|
+
];
|
|
1263
|
+
}
|
|
1264
|
+
const astroStarter = {
|
|
1265
|
+
title: "Astro",
|
|
1266
|
+
label: "Astro: HTML pages, with React islands",
|
|
1267
|
+
renderings: [
|
|
1268
|
+
"isomorphic",
|
|
1269
|
+
"ssr",
|
|
1270
|
+
"ssg"
|
|
1271
|
+
],
|
|
1272
|
+
hasLibraries: true,
|
|
1273
|
+
createFiles: createFiles$1
|
|
1274
|
+
};
|
|
1275
|
+
// src/create/vike/vike.ts
|
|
1001
1276
|
function createPackageJson({ packageName, isTypeScript, rendering, libraries, version }) {
|
|
1002
1277
|
const renderer = rendering === "spa" ? [] : ["@emotion/server"];
|
|
1003
|
-
const libraryPackages
|
|
1004
|
-
const libraryDevPackages = libraries.flatMap((library) => LIBRARY_PACKAGES[library].devDependencies ?? []);
|
|
1278
|
+
const { dependencies: libraryPackages, devDependencies: libraryDevPackages } = getLibraryPackages(libraries);
|
|
1005
1279
|
const packageJson = {
|
|
1006
1280
|
name: packageName,
|
|
1007
1281
|
private: true,
|
|
@@ -1011,8 +1285,9 @@ function createPackageJson({ packageName, isTypeScript, rendering, libraries, ve
|
|
|
1011
1285
|
dev: "vite",
|
|
1012
1286
|
build: "vite build",
|
|
1013
1287
|
preview: "vite preview",
|
|
1288
|
+
...rendering === "ssr" ? { start: "node dist/server/index.mjs" } : {},
|
|
1014
1289
|
...isTypeScript ? { typecheck: "tsc --noEmit" } : {},
|
|
1015
|
-
|
|
1290
|
+
test: "vitest run"
|
|
1016
1291
|
},
|
|
1017
1292
|
dependencies: {
|
|
1018
1293
|
"@soroush.tech/design-system": toRange(version),
|
|
@@ -1027,6 +1302,7 @@ function createPackageJson({ packageName, isTypeScript, rendering, libraries, ve
|
|
|
1027
1302
|
devDependencies: pickVersions([
|
|
1028
1303
|
"@vitejs/plugin-react",
|
|
1029
1304
|
...libraryDevPackages,
|
|
1305
|
+
...TEST_PACKAGES,
|
|
1030
1306
|
"vite",
|
|
1031
1307
|
...isTypeScript ? [
|
|
1032
1308
|
"@types/react",
|
|
@@ -1037,963 +1313,15 @@ function createPackageJson({ packageName, isTypeScript, rendering, libraries, ve
|
|
|
1037
1313
|
};
|
|
1038
1314
|
return `${JSON.stringify(packageJson, null, 2)}\n`;
|
|
1039
1315
|
}
|
|
1040
|
-
/** The Vite config: with msw, its plugin serves the mock API's worker, in development and in the build. */
|
|
1041
|
-
const createViteConfig = ({ libraries }) => libraries.includes("msw") ? `import react from '@vitejs/plugin-react'
|
|
1042
|
-
import { msw } from 'msw/vite'
|
|
1043
|
-
import vike from 'vike/plugin'
|
|
1044
|
-
import { defineConfig } from 'vite'
|
|
1045
|
-
|
|
1046
|
-
export default defineConfig({
|
|
1047
|
-
plugins: [react(), vike(), msw({ mode: 'worker-only' })],
|
|
1048
|
-
})
|
|
1049
|
-
` : `import react from '@vitejs/plugin-react'
|
|
1050
|
-
import vike from 'vike/plugin'
|
|
1051
|
-
import { defineConfig } from 'vite'
|
|
1052
|
-
|
|
1053
|
-
export default defineConfig({
|
|
1054
|
-
plugins: [react(), vike()],
|
|
1055
|
-
})
|
|
1056
|
-
`;
|
|
1057
|
-
const TSCONFIG = `{
|
|
1058
|
-
"compilerOptions": {
|
|
1059
|
-
"target": "ES2022",
|
|
1060
|
-
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
|
1061
|
-
"module": "ESNext",
|
|
1062
|
-
"moduleResolution": "bundler",
|
|
1063
|
-
"verbatimModuleSyntax": true,
|
|
1064
|
-
"moduleDetection": "force",
|
|
1065
|
-
"noEmit": true,
|
|
1066
|
-
"strict": true,
|
|
1067
|
-
"noUnusedLocals": true,
|
|
1068
|
-
"noUnusedParameters": true,
|
|
1069
|
-
"skipLibCheck": true,
|
|
1070
|
-
"jsx": "react-jsx",
|
|
1071
|
-
"types": ["vite/client"]
|
|
1072
|
-
},
|
|
1073
|
-
"include": ["src", "vite.config.ts"]
|
|
1074
|
-
}
|
|
1075
|
-
`;
|
|
1076
|
-
const GITIGNORE$1 = `node_modules/
|
|
1077
|
-
dist/
|
|
1078
|
-
*.log
|
|
1079
|
-
.DS_Store
|
|
1080
|
-
`;
|
|
1081
|
-
const VIKE_TYPES = `// Types for what this app adds to Vike: each page's component, and the +Layout around it.
|
|
1082
|
-
import type { ReactNode } from 'react'
|
|
1083
|
-
|
|
1084
|
-
type Layout = (props: { children: ReactNode }) => ReactNode
|
|
1085
|
-
|
|
1086
|
-
declare global {
|
|
1087
|
-
namespace Vike {
|
|
1088
|
-
interface PageContext {
|
|
1089
|
-
Page: () => ReactNode
|
|
1090
|
-
}
|
|
1091
|
-
interface Config {
|
|
1092
|
-
Layout?: Layout
|
|
1093
|
-
}
|
|
1094
|
-
interface ConfigResolved {
|
|
1095
|
-
Layout?: Layout
|
|
1096
|
-
}
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
|
|
1100
|
-
export {}
|
|
1101
|
-
`;
|
|
1102
|
-
/** The layout's link to the sample data pages, when the app has them. */
|
|
1103
|
-
const ITEMS_LINK = ` <Link href="/items" underline="hover">
|
|
1104
|
-
Items
|
|
1105
|
-
</Link>
|
|
1106
|
-
`;
|
|
1107
|
-
/** The layout's light and dark switch, in a page that has JavaScript. */
|
|
1108
|
-
const SCHEME_SWITCH = ` <Button size="sm" variant="outlined" onClick={() => setScheme(otherScheme)}>
|
|
1109
|
-
{otherScheme}
|
|
1110
|
-
</Button>
|
|
1111
|
-
`;
|
|
1112
|
-
/** The settings of each rendering, in +config. */
|
|
1113
|
-
const CONFIG_SETTINGS = {
|
|
1114
|
-
isomorphic: ` // Isomorphic: every page is HTML at build time, then the app moves between pages in the
|
|
1115
|
-
// browser, with no reload.
|
|
1116
|
-
prerender: true,
|
|
1117
|
-
clientRouting: true,
|
|
1118
|
-
hydrationCanBeAborted: true,
|
|
1119
|
-
// +Layout is a setting of this app's own: the component every page renders inside.
|
|
1120
|
-
meta: {
|
|
1121
|
-
Layout: { env: { server: true, client: true } },
|
|
1122
|
-
},`,
|
|
1123
|
-
spa: ` // SPA: every page is an empty HTML shell from the build, which the browser renders; the app
|
|
1124
|
-
// moves between pages with no reload.
|
|
1125
|
-
prerender: true,
|
|
1126
|
-
clientRouting: true,
|
|
1127
|
-
// The page and +Layout, a setting of this app's own, load in the browser only.
|
|
1128
|
-
meta: {
|
|
1129
|
-
Page: { env: { server: false, client: true } },
|
|
1130
|
-
Layout: { env: { server: false, client: true } },
|
|
1131
|
-
},`,
|
|
1132
|
-
ssg: ` // SSG: every page is HTML at build time, with no JavaScript in the browser: a link loads the
|
|
1133
|
-
// next page.
|
|
1134
|
-
prerender: true,
|
|
1135
|
-
// The page and +Layout, a setting of this app's own, load at build time only.
|
|
1136
|
-
meta: {
|
|
1137
|
-
Page: { env: { server: true, client: false } },
|
|
1138
|
-
Layout: { env: { server: true, client: false } },
|
|
1139
|
-
},`
|
|
1140
|
-
};
|
|
1141
|
-
/** What the second page says of how it got there. */
|
|
1142
|
-
const ABOUT_TEXT = {
|
|
1143
|
-
isomorphic: `A second page: it is HTML with its styles from the build, and the app moves to it without a
|
|
1144
|
-
reload.`,
|
|
1145
|
-
spa: `A second page: the browser renders it, with its styles, and the app moves to it without a
|
|
1146
|
-
reload.`,
|
|
1147
|
-
ssg: "A second page: it is HTML with its styles from the build, and a link loads it as a new page."
|
|
1148
|
-
};
|
|
1149
|
-
/** Indents each line by `spaces`: the layout's tree, which has no blank line. */
|
|
1150
|
-
const indentLines = (text, spaces) => text.split("\n").map((line) => `${" ".repeat(spaces)}${line}`).join("\n");
|
|
1151
|
-
/**
|
|
1152
|
-
* Where the layout keeps the scheme it switches - a zustand store, a Jotai atom or the Redux
|
|
1153
|
-
* store, else its own state - as the imports and the lines that read it.
|
|
1154
|
-
*/
|
|
1155
|
-
function getSchemeHolder(holder, t, scheme) {
|
|
1156
|
-
if (holder === "zustand") return {
|
|
1157
|
-
imports: "import { useScheme } from '../state/scheme'\n",
|
|
1158
|
-
lines: " const { scheme, setScheme } = useScheme()\n"
|
|
1159
|
-
};
|
|
1160
|
-
if (holder === "jotai") return {
|
|
1161
|
-
imports: "import { useAtom } from 'jotai'\nimport { schemeAtom } from '../state/scheme'\n",
|
|
1162
|
-
lines: " const [scheme, setScheme] = useAtom(schemeAtom)\n"
|
|
1163
|
-
};
|
|
1164
|
-
if (holder === "redux-toolkit") return {
|
|
1165
|
-
imports: `import { useDispatch, useSelector } from 'react-redux'\nimport { schemeChanged${t(", type RootState")} } from '../state/store'\n`,
|
|
1166
|
-
lines: ` const scheme = useSelector((state${t(": RootState")}) => state.scheme)
|
|
1167
|
-
const dispatch = useDispatch()
|
|
1168
|
-
const setScheme = (next${t(": 'light' | 'dark'")}) => dispatch(schemeChanged(next))
|
|
1169
|
-
`
|
|
1170
|
-
};
|
|
1171
|
-
return {
|
|
1172
|
-
imports: "",
|
|
1173
|
-
lines: ` const [scheme, setScheme] = useState${t("<'light' | 'dark'>")}('${scheme}')\n`
|
|
1174
|
-
};
|
|
1175
|
-
}
|
|
1176
|
-
/**
|
|
1177
|
-
* The +Layout, the frame every page renders in: it holds the app's providers, the light and dark
|
|
1178
|
-
* switch when the page has JavaScript, and the query client of TanStack Query.
|
|
1179
|
-
*/
|
|
1180
|
-
function createLayout({ title, isTypeScript, rendering, libraries, scheme, hasSampleData }) {
|
|
1181
|
-
const t = (typeScriptOnly) => isTypeScript ? typeScriptOnly : "";
|
|
1182
|
-
const x = isTypeScript ? "ts" : "js";
|
|
1183
|
-
const hasSwitch = rendering !== "ssg";
|
|
1184
|
-
const hasQuery = libraries.includes("tanstack-query");
|
|
1185
|
-
const holder = [
|
|
1186
|
-
"zustand",
|
|
1187
|
-
"jotai",
|
|
1188
|
-
"redux-toolkit"
|
|
1189
|
-
].find((library) => libraries.includes(library));
|
|
1190
|
-
const schemeHolder = getSchemeHolder(holder, t, scheme);
|
|
1191
|
-
const imports = [
|
|
1192
|
-
hasQuery || hasSwitch && holder === void 0 ? `import { useState${t(", type ReactNode")} } from 'react'\n` : t("import type { ReactNode } from 'react'\n"),
|
|
1193
|
-
hasSwitch ? "import { Button } from '@soroush.tech/design-system/Button'\n" : "",
|
|
1194
|
-
"import { Flex } from '@soroush.tech/design-system/Flex'\n",
|
|
1195
|
-
"import { Link } from '@soroush.tech/design-system/Link'\n",
|
|
1196
|
-
"import { Typography } from '@soroush.tech/design-system/Typography'\n",
|
|
1197
|
-
"import { View } from '@soroush.tech/design-system/View'\n",
|
|
1198
|
-
hasQuery ? "import { QueryClient, QueryClientProvider } from '@tanstack/react-query'\n" : "",
|
|
1199
|
-
hasSwitch ? schemeHolder.imports : "",
|
|
1200
|
-
"import { Providers } from '../theme/Providers'\n"
|
|
1201
|
-
].join("");
|
|
1202
|
-
const about = !hasSwitch ? `The frame every page renders in, inside the app's providers. A page has no JavaScript in the
|
|
1203
|
-
* browser, so it is drawn in the default scheme, which src/theme/Providers.${x} sets.` : holder ? `The frame every page renders in. The app's providers live here, in the app itself, so the
|
|
1204
|
-
* scheme, held in src/state/${holder === "redux-toolkit" ? "store" : "scheme"}.${x}, can switch the theme.` : `The frame every page renders in. The app's providers live here, in the app itself, so the
|
|
1205
|
-
* scheme it holds can switch the theme.`;
|
|
1206
|
-
const hooks = [hasQuery ? " const [queryClient] = useState(() => new QueryClient())\n" : "", hasSwitch ? `${schemeHolder.lines} const otherScheme = scheme === 'light' ? 'dark' : 'light'\n` : ""].join("");
|
|
1207
|
-
const frame = `<Providers${hasSwitch ? " scheme={scheme}" : ""}>
|
|
1208
|
-
<Flex
|
|
1209
|
-
as="header"
|
|
1210
|
-
flexDirection="row"
|
|
1211
|
-
alignItems="center"
|
|
1212
|
-
justifyContent="space-between"
|
|
1213
|
-
gap={2}
|
|
1214
|
-
px={3}
|
|
1215
|
-
py={2}
|
|
1216
|
-
bg="paper"
|
|
1217
|
-
>
|
|
1218
|
-
<Typography variant="h6" as="span">
|
|
1219
|
-
${title}
|
|
1220
|
-
</Typography>
|
|
1221
|
-
<Flex as="nav" flexDirection="row" alignItems="center" gap={2}>
|
|
1222
|
-
<Link href="/" underline="hover">
|
|
1223
|
-
Home
|
|
1224
|
-
</Link>
|
|
1225
|
-
<Link href="/about" underline="hover">
|
|
1226
|
-
About
|
|
1227
|
-
</Link>
|
|
1228
|
-
${`${hasSampleData ? ITEMS_LINK : ""}${libraries.includes("react-hook-form") ? FORM_LINK : ""}${hasSwitch ? SCHEME_SWITCH : ""}`.replaceAll(/^ {4}/gm, "")} </Flex>
|
|
1229
|
-
</Flex>
|
|
1230
|
-
<View as="main" px={3} py={4}>
|
|
1231
|
-
{children}
|
|
1232
|
-
</View>
|
|
1233
|
-
</Providers>`;
|
|
1234
|
-
const tree = hasQuery ? `<QueryClientProvider client={queryClient}>
|
|
1235
|
-
${indentLines(frame, 2)}
|
|
1236
|
-
</QueryClientProvider>` : frame;
|
|
1237
|
-
return `${imports}
|
|
1238
|
-
/**
|
|
1239
|
-
* ${about}
|
|
1240
|
-
*/
|
|
1241
|
-
export default function Layout({ children }${t(": { readonly children: ReactNode }")}) {
|
|
1242
|
-
${hooks} return (
|
|
1243
|
-
${indentLines(tree, 4)}
|
|
1244
|
-
)
|
|
1245
|
-
}
|
|
1246
|
-
`;
|
|
1247
|
-
}
|
|
1248
|
-
function createSources(facts) {
|
|
1249
|
-
const { title, isTypeScript, rendering } = facts;
|
|
1250
|
-
const t = (typeScriptOnly) => isTypeScript ? typeScriptOnly : "";
|
|
1251
|
-
const x = isTypeScript ? "ts" : "js";
|
|
1252
|
-
return {
|
|
1253
|
-
config: `${t("import type { Config } from 'vike/types'\n\n")}// The settings of every page: https://vike.dev/config
|
|
1254
|
-
export default {
|
|
1255
|
-
${CONFIG_SETTINGS[rendering]}
|
|
1256
|
-
}${t(" satisfies Config")}
|
|
1257
|
-
`,
|
|
1258
|
-
PageShell: `import { StrictMode } from 'react'
|
|
1259
|
-
import { CacheProvider, styleCache } from '@soroush.tech/design-system/engine'
|
|
1260
|
-
${t("import type { PageContext } from 'vike/types'\n")}import { PageContextProvider } from './usePageContext'
|
|
1261
|
-
|
|
1262
|
-
/**
|
|
1263
|
-
* The page inside its +Layout. The Emotion cache is the design system's, so the styles the page
|
|
1264
|
-
* uses are found where it is drawn, and every component of the page can read its pageContext.
|
|
1265
|
-
*/
|
|
1266
|
-
export function PageShell({ pageContext }${t(": { readonly pageContext: PageContext }")}) {
|
|
1267
|
-
const { Page, config } = pageContext
|
|
1268
|
-
const { Layout } = config
|
|
1269
|
-
return (
|
|
1270
|
-
<StrictMode>
|
|
1271
|
-
<CacheProvider value={styleCache}>
|
|
1272
|
-
<PageContextProvider pageContext={pageContext}>
|
|
1273
|
-
{Layout ? (
|
|
1274
|
-
<Layout>
|
|
1275
|
-
<Page />
|
|
1276
|
-
</Layout>
|
|
1277
|
-
) : (
|
|
1278
|
-
<Page />
|
|
1279
|
-
)}
|
|
1280
|
-
</PageContextProvider>
|
|
1281
|
-
</CacheProvider>
|
|
1282
|
-
</StrictMode>
|
|
1283
|
-
)
|
|
1284
|
-
}
|
|
1285
|
-
`,
|
|
1286
|
-
spaOnRenderHtml: `import { escapeInject } from 'vike/server'
|
|
1287
|
-
${t("import type { OnRenderHtmlSync } from 'vike/types'\n")}
|
|
1288
|
-
/** The page's HTML shell, from the build: the browser renders the page in #root. */
|
|
1289
|
-
export const onRenderHtml${t(": OnRenderHtmlSync")} = () => {
|
|
1290
|
-
return escapeInject\`<!doctype html>
|
|
1291
|
-
<html lang="en">
|
|
1292
|
-
<head>
|
|
1293
|
-
<meta charset="UTF-8" />
|
|
1294
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
1295
|
-
<title>${title}</title>
|
|
1296
|
-
</head>
|
|
1297
|
-
<body>
|
|
1298
|
-
<div id="root"></div>
|
|
1299
|
-
</body>
|
|
1300
|
-
</html>\`
|
|
1301
|
-
}
|
|
1302
|
-
`,
|
|
1303
|
-
spaOnRenderClient: `import { createRoot${t(", type Root")} } from 'react-dom/client'
|
|
1304
|
-
${t("import type { OnRenderClientSync } from 'vike/types'\n")}import { PageShell } from './PageShell'
|
|
1305
|
-
|
|
1306
|
-
let root${t(": Root | undefined")}
|
|
1307
|
-
|
|
1308
|
-
/** Draws the page in the browser, and each page the app moves to. */
|
|
1309
|
-
export const onRenderClient${t(": OnRenderClientSync")} = (pageContext) => {
|
|
1310
|
-
root ??= createRoot(document.getElementById('root')${t("!")})
|
|
1311
|
-
root.render(<PageShell pageContext={pageContext} />)
|
|
1312
|
-
}
|
|
1313
|
-
`,
|
|
1314
|
-
onRenderHtml: `import createEmotionServer from '@emotion/server/create-instance'
|
|
1315
|
-
import { renderToString } from 'react-dom/server'
|
|
1316
|
-
import { styleCache } from '@soroush.tech/design-system/engine'
|
|
1317
|
-
import { dangerouslySkipEscape, escapeInject } from 'vike/server'
|
|
1318
|
-
${t("import type { OnRenderHtmlSync } from 'vike/types'\n")}import { PageShell } from './PageShell'
|
|
1319
|
-
|
|
1320
|
-
/**
|
|
1321
|
-
* Renders the page to HTML - at build time, as the app is prerendered - with the styles it uses
|
|
1322
|
-
* in the head, so it is drawn styled before any script runs.
|
|
1323
|
-
*/
|
|
1324
|
-
export const onRenderHtml${t(": OnRenderHtmlSync")} = (pageContext) => {
|
|
1325
|
-
const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(styleCache)
|
|
1326
|
-
const html = renderToString(<PageShell pageContext={pageContext} />)
|
|
1327
|
-
const styles = constructStyleTagsFromChunks(extractCriticalToChunks(html))
|
|
1328
|
-
return escapeInject\`<!doctype html>
|
|
1329
|
-
<html lang="en">
|
|
1330
|
-
<head>
|
|
1331
|
-
<meta charset="UTF-8" />
|
|
1332
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
1333
|
-
<title>${title}</title>
|
|
1334
|
-
\${dangerouslySkipEscape(styles)}
|
|
1335
|
-
</head>
|
|
1336
|
-
<body>
|
|
1337
|
-
<div id="root">\${dangerouslySkipEscape(html)}</div>
|
|
1338
|
-
</body>
|
|
1339
|
-
</html>\`
|
|
1340
|
-
}
|
|
1341
|
-
`,
|
|
1342
|
-
onRenderClient: `import { createRoot, hydrateRoot${t(", type Root")} } from 'react-dom/client'
|
|
1343
|
-
${t("import type { OnRenderClientSync } from 'vike/types'\n")}import { PageShell } from './PageShell'
|
|
1344
|
-
|
|
1345
|
-
let root${t(": Root | undefined")}
|
|
1346
|
-
|
|
1347
|
-
/** Takes over the page the server drew, then draws each page the app moves to. */
|
|
1348
|
-
export const onRenderClient${t(": OnRenderClientSync")} = (pageContext) => {
|
|
1349
|
-
const page = <PageShell pageContext={pageContext} />
|
|
1350
|
-
const container = document.getElementById('root')${t("!")}
|
|
1351
|
-
if (pageContext.isHydration) {
|
|
1352
|
-
root = hydrateRoot(container, page)
|
|
1353
|
-
} else {
|
|
1354
|
-
root ??= createRoot(container)
|
|
1355
|
-
root.render(page)
|
|
1356
|
-
}
|
|
1357
|
-
}
|
|
1358
|
-
`,
|
|
1359
|
-
Layout: createLayout(facts),
|
|
1360
|
-
home: `import { Button } from '@soroush.tech/design-system/Button'
|
|
1361
|
-
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
1362
|
-
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
1363
|
-
|
|
1364
|
-
export default function Page() {
|
|
1365
|
-
return (
|
|
1366
|
-
<Flex gap={2} alignItems="flex-start">
|
|
1367
|
-
<Typography variant="h2">${title}</Typography>
|
|
1368
|
-
<Typography color="secondary">
|
|
1369
|
-
A Vike app on the design system. Edit src/pages/index/+Page.${x}x, and make the themes your
|
|
1370
|
-
own in src/theme/themes.${x}.
|
|
1371
|
-
</Typography>
|
|
1372
|
-
<Flex flexDirection="row" gap={1}>
|
|
1373
|
-
<Button href="https://docs.soroush.tech">Components</Button>
|
|
1374
|
-
<Button href="https://vike.dev" variant="outlined">
|
|
1375
|
-
Vike
|
|
1376
|
-
</Button>
|
|
1377
|
-
</Flex>
|
|
1378
|
-
</Flex>
|
|
1379
|
-
)
|
|
1380
|
-
}
|
|
1381
|
-
`,
|
|
1382
|
-
about: `import { Flex } from '@soroush.tech/design-system/Flex'
|
|
1383
|
-
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
1384
|
-
|
|
1385
|
-
export default function Page() {
|
|
1386
|
-
return (
|
|
1387
|
-
<Flex gap={2}>
|
|
1388
|
-
<Typography variant="h2">About</Typography>
|
|
1389
|
-
<Typography color="secondary">
|
|
1390
|
-
${ABOUT_TEXT[rendering]}
|
|
1391
|
-
</Typography>
|
|
1392
|
-
</Flex>
|
|
1393
|
-
)
|
|
1394
|
-
}
|
|
1395
|
-
`,
|
|
1396
|
-
usePageContext: `import { createContext, useContext${t(", type ReactNode")} } from 'react'
|
|
1397
|
-
${t("import type { PageContext } from 'vike/types'\n")}
|
|
1398
|
-
const Context = createContext${t("<PageContext | undefined>")}(undefined)
|
|
1399
|
-
|
|
1400
|
-
/** Gives every component of the page its pageContext: https://vike.dev/pageContext */
|
|
1401
|
-
export function PageContextProvider({
|
|
1402
|
-
pageContext,
|
|
1403
|
-
children,
|
|
1404
|
-
}${t(`: {
|
|
1405
|
-
readonly pageContext: PageContext
|
|
1406
|
-
readonly children: ReactNode
|
|
1407
|
-
}`)}) {
|
|
1408
|
-
return <Context.Provider value={pageContext}>{children}</Context.Provider>
|
|
1409
|
-
}
|
|
1410
|
-
|
|
1411
|
-
/** The pageContext of the page on screen: its URL, its route's parameters, its data. */
|
|
1412
|
-
export function usePageContext() {
|
|
1413
|
-
const pageContext = useContext(Context)
|
|
1414
|
-
if (!pageContext) throw new Error('usePageContext runs only inside a page.')
|
|
1415
|
-
return pageContext
|
|
1416
|
-
}
|
|
1417
|
-
|
|
1418
|
-
/** What the page's +data returned: https://vike.dev/data */
|
|
1419
|
-
export function useData${t("<Data>")}() {
|
|
1420
|
-
return usePageContext().data${t(" as Data")}
|
|
1421
|
-
}
|
|
1422
|
-
`,
|
|
1423
|
-
error: `import { Flex } from '@soroush.tech/design-system/Flex'
|
|
1424
|
-
import { Link } from '@soroush.tech/design-system/Link'
|
|
1425
|
-
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
1426
|
-
|
|
1427
|
-
/** Shown for a page that does not exist, and when a page fails to render. */
|
|
1428
|
-
export default function Page() {
|
|
1429
|
-
return (
|
|
1430
|
-
<Flex gap={2} alignItems="flex-start">
|
|
1431
|
-
<Typography variant="h2">Page not found</Typography>
|
|
1432
|
-
<Typography color="secondary">This page does not exist, or it could not be shown.</Typography>
|
|
1433
|
-
<Link href="/">Back home</Link>
|
|
1434
|
-
</Flex>
|
|
1435
|
-
)
|
|
1436
|
-
}
|
|
1437
|
-
`
|
|
1438
|
-
};
|
|
1439
|
-
}
|
|
1440
|
-
/**
|
|
1441
|
-
* The sample data pages: a list of items and a page for each, which read `src/data/items` in their
|
|
1442
|
-
* +data hooks, at build time.
|
|
1443
|
-
*/
|
|
1444
|
-
function createSampleDataFiles({ isTypeScript, libraries }) {
|
|
1445
|
-
const t = (typeScriptOnly) => isTypeScript ? typeScriptOnly : "";
|
|
1446
|
-
const hasQuery = libraries.includes("tanstack-query");
|
|
1447
|
-
const hasSwr = libraries.includes("swr");
|
|
1448
|
-
const queryImport = (api, fetcher) => hasQuery ? `import { useQuery } from '@tanstack/react-query'\nimport { ${fetcher} } from '${api}'\n` : hasSwr ? `import useSWR from 'swr'\nimport { ${fetcher} } from '${api}'\n` : "";
|
|
1449
|
-
const listRead = hasQuery ? ` const { data: items } = useQuery({
|
|
1450
|
-
queryKey: ['items'],
|
|
1451
|
-
queryFn: fetchItems,
|
|
1452
|
-
initialData: useData${t("<Data>")}(),
|
|
1453
|
-
})` : hasSwr ? ` const initialItems = useData${t("<Data>")}()
|
|
1454
|
-
const { data: items = initialItems } = useSWR('items', fetchItems, {
|
|
1455
|
-
fallbackData: initialItems,
|
|
1456
|
-
})` : ` const items = useData${t("<Data>")}()`;
|
|
1457
|
-
const itemRead = hasQuery ? ` const initialItem = useData${t("<Data>")}()
|
|
1458
|
-
const { data: item } = useQuery({
|
|
1459
|
-
queryKey: ['items', initialItem.id],
|
|
1460
|
-
queryFn: () => fetchItem(initialItem.id),
|
|
1461
|
-
initialData: initialItem,
|
|
1462
|
-
})` : hasSwr ? ` const initialItem = useData${t("<Data>")}()
|
|
1463
|
-
const { data: item = initialItem } = useSWR(
|
|
1464
|
-
['items', initialItem.id],
|
|
1465
|
-
() => fetchItem(initialItem.id),
|
|
1466
|
-
{ fallbackData: initialItem }
|
|
1467
|
-
)` : ` const item = useData${t("<Data>")}()`;
|
|
1468
|
-
const x = isTypeScript ? "ts" : "js";
|
|
1469
|
-
const dataType = t("\nexport type Data = ReturnType<typeof data>\n");
|
|
1470
|
-
const items = `${t(`/** One sample item. */
|
|
1471
|
-
export interface Item {
|
|
1472
|
-
id: string
|
|
1473
|
-
title: string
|
|
1474
|
-
summary: string
|
|
1475
|
-
status: 'todo' | 'doing' | 'done'
|
|
1476
|
-
}
|
|
1477
|
-
|
|
1478
|
-
`)}/**
|
|
1479
|
-
* Sample data, so the items pages have something to show: each page's +data reads it at build
|
|
1480
|
-
* time. Make the +data hooks fetch your own data instead, and remove this file.
|
|
1481
|
-
*/
|
|
1482
|
-
export const items${t(": readonly Item[]")} = [
|
|
1483
|
-
{
|
|
1484
|
-
id: 'theme',
|
|
1485
|
-
title: 'Make the theme your own',
|
|
1486
|
-
summary: 'Change the colors and fonts of both themes in src/theme/themes.${x}.',
|
|
1487
|
-
status: 'doing',
|
|
1488
|
-
},
|
|
1489
|
-
{
|
|
1490
|
-
id: 'fonts',
|
|
1491
|
-
title: 'Load the fonts',
|
|
1492
|
-
summary: 'The theme names Space Grotesk and JetBrains Mono, and does not load them.',
|
|
1493
|
-
status: 'todo',
|
|
1494
|
-
},
|
|
1495
|
-
{
|
|
1496
|
-
id: 'pages',
|
|
1497
|
-
title: 'Add a page',
|
|
1498
|
-
summary: 'A folder in src/pages with a +Page in it is a new route.',
|
|
1499
|
-
status: 'todo',
|
|
1500
|
-
},
|
|
1501
|
-
{
|
|
1502
|
-
id: 'data',
|
|
1503
|
-
title: 'Read your own data',
|
|
1504
|
-
summary: 'Each +data here reads this file. Make it fetch from your API, at build time.',
|
|
1505
|
-
status: 'todo',
|
|
1506
|
-
},
|
|
1507
|
-
{
|
|
1508
|
-
id: 'build',
|
|
1509
|
-
title: 'Build it',
|
|
1510
|
-
summary: 'The build prerenders every page, these items among them, to HTML with its styles.',
|
|
1511
|
-
status: 'done',
|
|
1512
|
-
},
|
|
1513
|
-
{
|
|
1514
|
-
id: 'deploy',
|
|
1515
|
-
title: 'Deploy it',
|
|
1516
|
-
summary: 'The build is static files in dist/client, which any static host serves.',
|
|
1517
|
-
status: 'todo',
|
|
1518
|
-
},
|
|
1519
|
-
]
|
|
1520
|
-
`;
|
|
1521
|
-
const statusChip = `import { Chip } from '@soroush.tech/design-system/Chip'
|
|
1522
|
-
${t("import type { Item } from '../../data/items'\n")}
|
|
1523
|
-
const COLORS = { todo: 'default', doing: 'primary', done: 'success' }${t(" as const")}
|
|
1524
|
-
|
|
1525
|
-
/** An item's status, in its palette color. */
|
|
1526
|
-
export function StatusChip({ status }${t(": { readonly status: Item['status'] }")}) {
|
|
1527
|
-
return <Chip label={status} size="sm" variant="outlined" color={COLORS[status]} />
|
|
1528
|
-
}
|
|
1529
|
-
`;
|
|
1530
|
-
const listData = `import { items } from '../../data/items'
|
|
1531
|
-
|
|
1532
|
-
/**
|
|
1533
|
-
* The list page's data: what it shows of each item. It runs at build time, as the page is
|
|
1534
|
-
* prerendered. https://vike.dev/data
|
|
1535
|
-
*/
|
|
1536
|
-
export function data() {
|
|
1537
|
-
return items.map(({ id, title, status }) => ({ id, title, status }))
|
|
1538
|
-
}
|
|
1539
|
-
${dataType}`;
|
|
1540
|
-
const listPage = `import { Flex } from '@soroush.tech/design-system/Flex'
|
|
1541
|
-
import { Link } from '@soroush.tech/design-system/Link'
|
|
1542
|
-
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
1543
|
-
${queryImport("../../data/api", "fetchItems")}import { useData } from '../usePageContext'
|
|
1544
|
-
${t("import type { Data } from './+data'\n")}import { StatusChip } from './StatusChip'
|
|
1545
|
-
|
|
1546
|
-
export default function Page() {
|
|
1547
|
-
${listRead}
|
|
1548
|
-
return (
|
|
1549
|
-
<Flex gap={2}>
|
|
1550
|
-
<Typography variant="h2">Items</Typography>
|
|
1551
|
-
<Typography color="secondary">
|
|
1552
|
-
Sample data from src/data/items.${x}, which this page's +data reads at build time.
|
|
1553
|
-
</Typography>
|
|
1554
|
-
<Flex gap={1}>
|
|
1555
|
-
{items.map((item) => (
|
|
1556
|
-
<Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
|
|
1557
|
-
<Link href={\`/items/\${item.id}\`}>{item.title}</Link>
|
|
1558
|
-
<StatusChip status={item.status} />
|
|
1559
|
-
</Flex>
|
|
1560
|
-
))}
|
|
1561
|
-
</Flex>
|
|
1562
|
-
</Flex>
|
|
1563
|
-
)
|
|
1564
|
-
}
|
|
1565
|
-
`;
|
|
1566
|
-
const itemData = `import { render } from 'vike/abort'
|
|
1567
|
-
${t("import type { PageContextServer } from 'vike/types'\n")}import { items } from '../../../data/items'
|
|
1568
|
-
|
|
1569
|
-
/**
|
|
1570
|
-
* One item's data, by the id in the URL, or the 404 page for an id with no item. It runs at build
|
|
1571
|
-
* time, for each URL +onBeforePrerenderStart lists. https://vike.dev/data
|
|
1572
|
-
*/
|
|
1573
|
-
export function data(pageContext${t(": PageContextServer")}) {
|
|
1574
|
-
const item = items.find(({ id }) => id === pageContext.routeParams.id)
|
|
1575
|
-
if (!item) throw render(404)
|
|
1576
|
-
return item
|
|
1577
|
-
}
|
|
1578
|
-
${dataType}`;
|
|
1579
|
-
const itemPrerender = `import { items } from '../../../data/items'
|
|
1580
|
-
|
|
1581
|
-
/** The URLs of this route the build prerenders: one per item. https://vike.dev/onBeforePrerenderStart */
|
|
1582
|
-
export function onBeforePrerenderStart() {
|
|
1583
|
-
return items.map(({ id }) => \`/items/\${id}\`)
|
|
1584
|
-
}
|
|
1585
|
-
`;
|
|
1586
|
-
const itemPage = `import { Flex } from '@soroush.tech/design-system/Flex'
|
|
1587
|
-
import { Link } from '@soroush.tech/design-system/Link'
|
|
1588
|
-
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
1589
|
-
${queryImport("../../../data/api", "fetchItem")}import { useData } from '../../usePageContext'
|
|
1590
|
-
import { StatusChip } from '../StatusChip'
|
|
1591
|
-
${t("import type { Data } from './+data'\n")}
|
|
1592
|
-
export default function Page() {
|
|
1593
|
-
${itemRead}
|
|
1594
|
-
return (
|
|
1595
|
-
<Flex gap={2} alignItems="flex-start">
|
|
1596
|
-
<Typography variant="h2">{item.title}</Typography>
|
|
1597
|
-
<StatusChip status={item.status} />
|
|
1598
|
-
<Typography color="secondary">{item.summary}</Typography>
|
|
1599
|
-
<Link href="/items">All items</Link>
|
|
1600
|
-
</Flex>
|
|
1601
|
-
)
|
|
1602
|
-
}
|
|
1603
|
-
`;
|
|
1604
|
-
return [
|
|
1605
|
-
{
|
|
1606
|
-
path: `src/data/items.${x}`,
|
|
1607
|
-
content: items
|
|
1608
|
-
},
|
|
1609
|
-
{
|
|
1610
|
-
path: `src/pages/items/StatusChip.${x}x`,
|
|
1611
|
-
content: statusChip
|
|
1612
|
-
},
|
|
1613
|
-
{
|
|
1614
|
-
path: `src/pages/items/+data.${x}`,
|
|
1615
|
-
content: listData
|
|
1616
|
-
},
|
|
1617
|
-
{
|
|
1618
|
-
path: `src/pages/items/+Page.${x}x`,
|
|
1619
|
-
content: listPage
|
|
1620
|
-
},
|
|
1621
|
-
{
|
|
1622
|
-
path: `src/pages/items/@id/+data.${x}`,
|
|
1623
|
-
content: itemData
|
|
1624
|
-
},
|
|
1625
|
-
{
|
|
1626
|
-
path: `src/pages/items/@id/+onBeforePrerenderStart.${x}`,
|
|
1627
|
-
content: itemPrerender
|
|
1628
|
-
},
|
|
1629
|
-
{
|
|
1630
|
-
path: `src/pages/items/@id/+Page.${x}x`,
|
|
1631
|
-
content: itemPage
|
|
1632
|
-
}
|
|
1633
|
-
];
|
|
1634
|
-
}
|
|
1635
|
-
/** How the README says each rendering works, and which files render the pages. */
|
|
1636
|
-
const README_RENDERING = {
|
|
1637
|
-
isomorphic: {
|
|
1638
|
-
summary: "prerendered at build time with its styles in the HTML, then moving between pages with no reload.",
|
|
1639
|
-
renderers: "`+onRenderHtml` and `+onRenderClient` render the pages, with the styles each page uses in its HTML."
|
|
1640
|
-
},
|
|
1641
|
-
spa: {
|
|
1642
|
-
summary: "a single-page app: each page is an HTML shell from the build, rendered in the browser, which moves between pages with no reload.",
|
|
1643
|
-
renderers: "`+onRenderHtml` writes the HTML shell of each page, and `+onRenderClient` renders the page in it."
|
|
1644
|
-
},
|
|
1645
|
-
ssg: {
|
|
1646
|
-
summary: "every page prerendered to HTML at build time, with its styles, and no JavaScript in the browser.",
|
|
1647
|
-
renderers: "`+onRenderHtml` renders the pages to HTML, with the styles each page uses."
|
|
1648
|
-
}
|
|
1649
|
-
};
|
|
1650
|
-
function createReadme({ title, isTypeScript, rendering, libraries, hasSampleData }) {
|
|
1651
|
-
const x = isTypeScript ? "ts" : "js";
|
|
1652
|
-
const { summary, renderers } = README_RENDERING[rendering];
|
|
1653
|
-
const sampleData = hasSampleData ? `- \`src/pages/items/\` lists the sample items of \`src/data/items.${x}\`, with a page for each.
|
|
1654
|
-
Their \`+data\` hooks read the data at build time: make them fetch your own.
|
|
1655
|
-
` : "";
|
|
1656
|
-
const libraryLines = {
|
|
1657
|
-
"tanstack-query": `- [TanStack Query](https://tanstack.com/query): the query client lives in \`+Layout\`.${hasSampleData ? ` The items pages start on their build-time data, then query \`src/data/api.${x}\` in the browser.` : ""}\n`,
|
|
1658
|
-
msw: `- [msw](https://mswjs.io): \`src/mocks/handlers.${x}\` is a mock API, which answers in the browser while \`VITE_MSW\` in \`.env\` is true.\n`,
|
|
1659
|
-
zustand: `- [zustand](https://zustand.docs.pmnd.rs): \`src/state/scheme.${x}\` holds the color scheme, in a store.\n`,
|
|
1660
|
-
jotai: `- [Jotai](https://jotai.org): \`src/state/scheme.${x}\` holds the color scheme, in an atom.\n`,
|
|
1661
|
-
swr: `- [SWR](https://swr.vercel.app): fetches and caches data.${hasSampleData ? ` The items pages start on their build-time data, then fetch \`src/data/api.${x}\` in the browser.` : ""}\n`,
|
|
1662
|
-
"redux-toolkit": `- [Redux Toolkit](https://redux-toolkit.js.org): \`src/state/store.${x}\` holds the color scheme, in a slice of the store \`PageShell\` provides.\n`,
|
|
1663
|
-
"react-hook-form": `- [React Hook Form](https://react-hook-form.com) and [Zod](https://zod.dev): \`/form\` is a form whose values a Zod schema checks.\n`,
|
|
1664
|
-
vitest: `- [Vitest](https://vitest.dev) and [Testing Library](https://testing-library.com): \`npm run test\` runs \`src/pages/index/Page.test.${x}x\`.\n`
|
|
1665
|
-
};
|
|
1666
|
-
return `# ${title}
|
|
1667
|
-
|
|
1668
|
-
A [Vike](https://vike.dev) app on the [@soroush.tech design system](https://docs.soroush.tech):
|
|
1669
|
-
${summary}
|
|
1670
|
-
|
|
1671
|
-
- \`npm run dev\` starts it, \`npm run build\` builds it, and \`npm run preview\` serves the build.
|
|
1672
|
-
- \`src/pages/\` holds the pages: a folder per route, each with its \`+Page\`.
|
|
1673
|
-
- \`src/pages/+Layout.${x}x\` is the frame every page renders in, inside the app's providers.
|
|
1674
|
-
- \`src/theme/themes.${x}\` holds the light and the dark theme: make them your own.
|
|
1675
|
-
- In \`src/pages/\`, ${renderers}
|
|
1676
|
-
${sampleData}${libraries.map((library) => libraryLines[library]).join("")}
|
|
1677
|
-
Fonts: the theme names Space Grotesk and JetBrains Mono, and does not load them. See
|
|
1678
|
-
https://docs.soroush.tech/design-system/getting-started/installation/#fonts
|
|
1679
|
-
`;
|
|
1680
|
-
}
|
|
1681
|
-
/** The layout's link to the form page, when the app has React Hook Form. */
|
|
1682
|
-
const FORM_LINK = ` <Link href="/form" underline="hover">
|
|
1683
|
-
Form
|
|
1684
|
-
</Link>
|
|
1685
|
-
`;
|
|
1686
|
-
/**
|
|
1687
|
-
* The form page of React Hook Form: two fields of the design system's TextInput, which a Zod
|
|
1688
|
-
* schema checks before the form is sent.
|
|
1689
|
-
*/
|
|
1690
|
-
function createFormPage(isTypeScript) {
|
|
1691
|
-
const t = (typeScriptOnly) => isTypeScript ? typeScriptOnly : "";
|
|
1692
|
-
return `import { useState } from 'react'
|
|
1693
|
-
import { zodResolver } from '@hookform/resolvers/zod'
|
|
1694
|
-
import { Controller, useForm } from 'react-hook-form'
|
|
1695
|
-
import { z } from 'zod'
|
|
1696
|
-
import { Button } from '@soroush.tech/design-system/Button'
|
|
1697
|
-
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
1698
|
-
import { TextInput } from '@soroush.tech/design-system/TextInput'
|
|
1699
|
-
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
1700
|
-
|
|
1701
|
-
/** What the form takes, and what it says when a value will not do. */
|
|
1702
|
-
const schema = z.object({
|
|
1703
|
-
name: z.string().trim().min(1, 'Your name, please.'),
|
|
1704
|
-
email: z.email('An email address, please.'),
|
|
1705
|
-
})
|
|
1706
|
-
${t(`
|
|
1707
|
-
type Values = z.infer<typeof schema>
|
|
1708
|
-
`)}
|
|
1709
|
-
export default function Page() {
|
|
1710
|
-
const [sent, setSent] = useState${t("<Values | null>")}(null)
|
|
1711
|
-
const { control, handleSubmit } = useForm${t("<Values>")}({
|
|
1712
|
-
resolver: zodResolver(schema),
|
|
1713
|
-
defaultValues: { name: '', email: '' },
|
|
1714
|
-
})
|
|
1715
|
-
return (
|
|
1716
|
-
<Flex gap={2} alignItems="flex-start">
|
|
1717
|
-
<Typography variant="h2">Form</Typography>
|
|
1718
|
-
<Typography color="secondary">
|
|
1719
|
-
React Hook Form holds the values, and a Zod schema checks them when the form is sent.
|
|
1720
|
-
</Typography>
|
|
1721
|
-
<form noValidate onSubmit={handleSubmit(setSent)}>
|
|
1722
|
-
<Flex gap={2} alignItems="flex-start">
|
|
1723
|
-
<Controller
|
|
1724
|
-
name="name"
|
|
1725
|
-
control={control}
|
|
1726
|
-
render={({ field, fieldState }) => (
|
|
1727
|
-
<Flex gap={1}>
|
|
1728
|
-
<TextInput
|
|
1729
|
-
name={field.name}
|
|
1730
|
-
value={field.value}
|
|
1731
|
-
onChange={field.onChange}
|
|
1732
|
-
placeholder="Name"
|
|
1733
|
-
error={fieldState.invalid}
|
|
1734
|
-
inputProps={{ 'aria-label': 'Name', onBlur: field.onBlur }}
|
|
1735
|
-
/>
|
|
1736
|
-
<Typography variant="caption" color="error">
|
|
1737
|
-
{fieldState.error?.message}
|
|
1738
|
-
</Typography>
|
|
1739
|
-
</Flex>
|
|
1740
|
-
)}
|
|
1741
|
-
/>
|
|
1742
|
-
<Controller
|
|
1743
|
-
name="email"
|
|
1744
|
-
control={control}
|
|
1745
|
-
render={({ field, fieldState }) => (
|
|
1746
|
-
<Flex gap={1}>
|
|
1747
|
-
<TextInput
|
|
1748
|
-
name={field.name}
|
|
1749
|
-
value={field.value}
|
|
1750
|
-
onChange={field.onChange}
|
|
1751
|
-
type="email"
|
|
1752
|
-
placeholder="Email"
|
|
1753
|
-
error={fieldState.invalid}
|
|
1754
|
-
inputProps={{ 'aria-label': 'Email', onBlur: field.onBlur }}
|
|
1755
|
-
/>
|
|
1756
|
-
<Typography variant="caption" color="error">
|
|
1757
|
-
{fieldState.error?.message}
|
|
1758
|
-
</Typography>
|
|
1759
|
-
</Flex>
|
|
1760
|
-
)}
|
|
1761
|
-
/>
|
|
1762
|
-
<Button type="submit">Send</Button>
|
|
1763
|
-
</Flex>
|
|
1764
|
-
</form>
|
|
1765
|
-
{sent ? <Typography>Thanks, {sent.name}: the form was valid.</Typography> : null}
|
|
1766
|
-
</Flex>
|
|
1767
|
-
)
|
|
1768
|
-
}
|
|
1769
|
-
`;
|
|
1770
|
-
}
|
|
1771
|
-
/**
|
|
1772
|
-
* PageShell with Redux Toolkit: the store's provider wraps every page, so +Layout and the pages
|
|
1773
|
-
* read the store.
|
|
1774
|
-
*/
|
|
1775
|
-
function withStore(pageShell) {
|
|
1776
|
-
const lines = pageShell.replace("import { CacheProvider, styleCache } from '@soroush.tech/design-system/engine'\n", "import { CacheProvider, styleCache } from '@soroush.tech/design-system/engine'\nimport { Provider as StoreProvider } from 'react-redux'\n").replace("import { PageContextProvider } from './usePageContext'\n", "import { store } from '../state/store'\nimport { PageContextProvider } from './usePageContext'\n").split("\n");
|
|
1777
|
-
const start = lines.findIndex((line) => line.trimStart().startsWith("<PageContextProvider"));
|
|
1778
|
-
const end = lines.findIndex((line) => line.trimStart() === "</PageContextProvider>");
|
|
1779
|
-
const indent = lines[start].slice(0, lines[start].indexOf("<"));
|
|
1780
|
-
return [
|
|
1781
|
-
...lines.slice(0, start),
|
|
1782
|
-
`${indent}<StoreProvider store={store}>`,
|
|
1783
|
-
...lines.slice(start, end + 1).map((line) => ` ${line}`),
|
|
1784
|
-
`${indent}</StoreProvider>`,
|
|
1785
|
-
...lines.slice(end + 1)
|
|
1786
|
-
].join("\n");
|
|
1787
|
-
}
|
|
1788
|
-
/**
|
|
1789
|
-
* The client renderer with msw: the mock API starts as the module loads, and the first page waits
|
|
1790
|
-
* for it, so no request leaves before the mock can answer it.
|
|
1791
|
-
*/
|
|
1792
|
-
function withMocks(onRenderClient) {
|
|
1793
|
-
return onRenderClient.replaceAll("OnRenderClientSync", "OnRenderClientAsync").replace("import { PageShell } from './PageShell'\n", "import { startMocks } from '../mocks/start'\nimport { PageShell } from './PageShell'\n").replace(/^(let root.*\n)/m, "$1const mocks = startMocks()\n").replace("= (pageContext) => {\n", "= async (pageContext) => {\n await mocks\n");
|
|
1794
|
-
}
|
|
1795
|
-
/** The files each library adds: its setup, and the example that uses it. */
|
|
1796
|
-
function createLibraryFiles({ title, isTypeScript, libraries, scheme, hasSampleData }) {
|
|
1797
|
-
const t = (typeScriptOnly) => isTypeScript ? typeScriptOnly : "";
|
|
1798
|
-
const x = isTypeScript ? "ts" : "js";
|
|
1799
|
-
const files = [];
|
|
1800
|
-
const hasMocks = libraries.includes("msw");
|
|
1801
|
-
if (libraries.includes("zustand")) files.push({
|
|
1802
|
-
path: `src/state/scheme.${x}`,
|
|
1803
|
-
content: `import { create } from 'zustand'
|
|
1804
|
-
${t(`
|
|
1805
|
-
interface SchemeState {
|
|
1806
|
-
scheme: 'light' | 'dark'
|
|
1807
|
-
setScheme: (scheme: 'light' | 'dark') => void
|
|
1808
|
-
}
|
|
1809
|
-
`)}
|
|
1810
|
-
/** The color scheme the app is drawn in: a zustand store, which any component can read. */
|
|
1811
|
-
export const useScheme = create${t("<SchemeState>()")}((set) => ({
|
|
1812
|
-
scheme: '${scheme}',
|
|
1813
|
-
setScheme: (scheme) => set({ scheme }),
|
|
1814
|
-
}))
|
|
1815
|
-
`
|
|
1816
|
-
});
|
|
1817
|
-
if (libraries.includes("jotai")) files.push({
|
|
1818
|
-
path: `src/state/scheme.${x}`,
|
|
1819
|
-
content: `import { atom } from 'jotai'
|
|
1820
|
-
|
|
1821
|
-
/** The color scheme the app is drawn in: a Jotai atom, which any component can read. */
|
|
1822
|
-
export const schemeAtom = atom${t("<'light' | 'dark'>")}('${scheme}')
|
|
1823
|
-
`
|
|
1824
|
-
});
|
|
1825
|
-
if (hasMocks) {
|
|
1826
|
-
const handlers = hasSampleData ? `import { http, HttpResponse } from 'msw'
|
|
1827
|
-
import { items } from '../data/items'
|
|
1828
|
-
|
|
1829
|
-
/** The mock API: what it answers, by method and path. Add a handler for each call the app makes. */
|
|
1830
|
-
export const handlers = [
|
|
1831
|
-
http.get('/api/items', () =>
|
|
1832
|
-
HttpResponse.json(items.map(({ id, title, status }) => ({ id, title, status })))
|
|
1833
|
-
),
|
|
1834
|
-
http.get('/api/items/:id', ({ params }) => {
|
|
1835
|
-
const item = items.find(({ id }) => id === params.id)
|
|
1836
|
-
return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 })
|
|
1837
|
-
}),
|
|
1838
|
-
]
|
|
1839
|
-
` : `import { http, HttpResponse } from 'msw'
|
|
1840
|
-
|
|
1841
|
-
/** The mock API: what it answers, by method and path. Add a handler for each call the app makes. */
|
|
1842
|
-
export const handlers = [
|
|
1843
|
-
http.get('/api/hello', () => HttpResponse.json({ message: 'Hello from the mock API' })),
|
|
1844
|
-
]
|
|
1845
|
-
`;
|
|
1846
|
-
files.push({
|
|
1847
|
-
path: `src/mocks/handlers.${x}`,
|
|
1848
|
-
content: handlers
|
|
1849
|
-
}, {
|
|
1850
|
-
path: `src/mocks/browser.${x}`,
|
|
1851
|
-
content: `import { setupWorker } from 'msw/browser'
|
|
1852
|
-
import { handlers } from './handlers'
|
|
1853
|
-
|
|
1854
|
-
/** The mock API in the browser: a service worker answers the requests the handlers match. */
|
|
1855
|
-
export const worker = setupWorker(...handlers)
|
|
1856
|
-
`
|
|
1857
|
-
}, {
|
|
1858
|
-
path: `src/mocks/start.${x}`,
|
|
1859
|
-
content: `/**
|
|
1860
|
-
* Starts the mock API in the browser while VITE_MSW, in .env, is true. Set it to false once the
|
|
1861
|
-
* app's own API answers.
|
|
1862
|
-
*/
|
|
1863
|
-
export async function startMocks() {
|
|
1864
|
-
if (import.meta.env.VITE_MSW !== 'true') return
|
|
1865
|
-
const { worker } = await import('./browser')
|
|
1866
|
-
await worker.start()
|
|
1867
|
-
}
|
|
1868
|
-
`
|
|
1869
|
-
}, {
|
|
1870
|
-
path: ".env",
|
|
1871
|
-
content: `# The mock API in src/mocks answers in the browser while this is true. Set it to false once
|
|
1872
|
-
# the app's own API answers.
|
|
1873
|
-
VITE_MSW=true
|
|
1874
|
-
`
|
|
1875
|
-
});
|
|
1876
|
-
}
|
|
1877
|
-
if (libraries.includes("redux-toolkit")) files.push({
|
|
1878
|
-
path: `src/state/store.${x}`,
|
|
1879
|
-
content: `import { configureStore, createSlice${t(", type PayloadAction")} } from '@reduxjs/toolkit'
|
|
1880
|
-
|
|
1881
|
-
/** The color scheme the app is drawn in: a slice of the store. */
|
|
1882
|
-
const schemeSlice = createSlice({
|
|
1883
|
-
name: 'scheme',
|
|
1884
|
-
initialState: '${scheme}'${t(" as 'light' | 'dark'")},
|
|
1885
|
-
reducers: {
|
|
1886
|
-
schemeChanged: (_scheme, action${t(": PayloadAction<'light' | 'dark'>")}) => action.payload,
|
|
1887
|
-
},
|
|
1888
|
-
})
|
|
1889
|
-
|
|
1890
|
-
export const { schemeChanged } = schemeSlice.actions
|
|
1891
|
-
|
|
1892
|
-
/** The app's Redux store, which PageShell provides to every page. */
|
|
1893
|
-
export const store = configureStore({ reducer: { scheme: schemeSlice.reducer } })
|
|
1894
|
-
${t(`
|
|
1895
|
-
export type RootState = ReturnType<typeof store.getState>
|
|
1896
|
-
`)}`
|
|
1897
|
-
});
|
|
1898
|
-
if (libraries.includes("react-hook-form")) files.push({
|
|
1899
|
-
path: `src/pages/form/+Page.${x}x`,
|
|
1900
|
-
content: createFormPage(isTypeScript)
|
|
1901
|
-
});
|
|
1902
|
-
if (libraries.includes("vitest")) files.push({
|
|
1903
|
-
path: `vitest.config.${x}`,
|
|
1904
|
-
content: `import react from '@vitejs/plugin-react'
|
|
1905
|
-
import { defineConfig } from 'vitest/config'
|
|
1906
|
-
|
|
1907
|
-
// The tests render components in jsdom, with no Vike: a page is a component like any other.
|
|
1908
|
-
export default defineConfig({
|
|
1909
|
-
plugins: [react()],
|
|
1910
|
-
test: { environment: 'jsdom' },
|
|
1911
|
-
})
|
|
1912
|
-
`
|
|
1913
|
-
}, {
|
|
1914
|
-
path: `src/pages/index/Page.test.${x}x`,
|
|
1915
|
-
content: `import { render, screen } from '@testing-library/react'
|
|
1916
|
-
import { describe, expect, it } from 'vitest'
|
|
1917
|
-
import { Providers } from '../../theme/Providers'
|
|
1918
|
-
import Page from './+Page'
|
|
1919
|
-
|
|
1920
|
-
describe('the home page', () => {
|
|
1921
|
-
it('shows the app title', () => {
|
|
1922
|
-
render(
|
|
1923
|
-
<Providers>
|
|
1924
|
-
<Page />
|
|
1925
|
-
</Providers>
|
|
1926
|
-
)
|
|
1927
|
-
expect(screen.getByRole('heading', { name: '${title.replaceAll("'", String.raw`\'`)}' })).toBeTruthy()
|
|
1928
|
-
})
|
|
1929
|
-
})
|
|
1930
|
-
`
|
|
1931
|
-
});
|
|
1932
|
-
if ((libraries.includes("tanstack-query") || libraries.includes("swr")) && hasSampleData) files.push({
|
|
1933
|
-
path: `src/data/api.${x}`,
|
|
1934
|
-
content: hasMocks ? `${t("import type { Item } from './items'\n\n")}/** The items, from the API: while VITE_MSW is true, the mock API of src/mocks answers. */
|
|
1935
|
-
export async function fetchItems()${t(": Promise<Pick<Item, 'id' | 'title' | 'status'>[]>")} {
|
|
1936
|
-
const response = await fetch('/api/items')
|
|
1937
|
-
if (!response.ok) throw new Error(\`GET /api/items answered \${response.status}\`)
|
|
1938
|
-
return response.json()
|
|
1939
|
-
}
|
|
1940
|
-
|
|
1941
|
-
/** One item, from the API. */
|
|
1942
|
-
export async function fetchItem(id${t(": string")})${t(": Promise<Item>")} {
|
|
1943
|
-
const response = await fetch(\`/api/items/\${id}\`)
|
|
1944
|
-
if (!response.ok) throw new Error(\`GET /api/items/\${id} answered \${response.status}\`)
|
|
1945
|
-
return response.json()
|
|
1946
|
-
}
|
|
1947
|
-
` : `import { items } from './items'
|
|
1948
|
-
|
|
1949
|
-
/** The items, as an API would answer: make it fetch from yours. */
|
|
1950
|
-
export async function fetchItems() {
|
|
1951
|
-
return items.map(({ id, title, status }) => ({ id, title, status }))
|
|
1952
|
-
}
|
|
1953
|
-
|
|
1954
|
-
/** One item, as an API would answer: make it fetch from yours. */
|
|
1955
|
-
export async function fetchItem(id${t(": string")}) {
|
|
1956
|
-
const item = items.find((candidate) => candidate.id === id)
|
|
1957
|
-
if (!item) throw new Error(\`There is no item \${id}.\`)
|
|
1958
|
-
return item
|
|
1959
|
-
}
|
|
1960
|
-
`
|
|
1961
|
-
});
|
|
1962
|
-
return files;
|
|
1963
|
-
}
|
|
1964
1316
|
/** The files of the Vike app `facts` describe. */
|
|
1965
1317
|
function createFiles(facts) {
|
|
1966
|
-
const { isTypeScript,
|
|
1318
|
+
const { isTypeScript, scheme } = facts;
|
|
1967
1319
|
const x = isTypeScript ? "ts" : "js";
|
|
1968
|
-
const sources = createSources(facts);
|
|
1969
|
-
const isSpa = rendering === "spa";
|
|
1970
|
-
const renderer = rendering === "ssg" ? void 0 : isSpa ? sources.spaOnRenderClient : sources.onRenderClient;
|
|
1971
|
-
const onRenderClient = renderer && libraries.includes("msw") ? withMocks(renderer) : renderer;
|
|
1972
1320
|
return [
|
|
1973
1321
|
{
|
|
1974
1322
|
path: "package.json",
|
|
1975
1323
|
content: createPackageJson(facts)
|
|
1976
1324
|
},
|
|
1977
|
-
{
|
|
1978
|
-
path: `vite.config.${x}`,
|
|
1979
|
-
content: createViteConfig(facts)
|
|
1980
|
-
},
|
|
1981
|
-
...isTypeScript ? [{
|
|
1982
|
-
path: "tsconfig.json",
|
|
1983
|
-
content: TSCONFIG
|
|
1984
|
-
}] : [],
|
|
1985
|
-
{
|
|
1986
|
-
path: ".gitignore",
|
|
1987
|
-
content: GITIGNORE$1
|
|
1988
|
-
},
|
|
1989
|
-
{
|
|
1990
|
-
path: "README.md",
|
|
1991
|
-
content: createReadme(facts)
|
|
1992
|
-
},
|
|
1993
|
-
...isTypeScript ? [{
|
|
1994
|
-
path: "src/vike.d.ts",
|
|
1995
|
-
content: VIKE_TYPES
|
|
1996
|
-
}] : [],
|
|
1997
1325
|
{
|
|
1998
1326
|
path: `src/theme/themes.${x}`,
|
|
1999
1327
|
content: createThemesSource({ isTypeScript })
|
|
@@ -2006,59 +1334,34 @@ function createFiles(facts) {
|
|
|
2006
1334
|
themesImport: "./themes"
|
|
2007
1335
|
})
|
|
2008
1336
|
},
|
|
2009
|
-
|
|
2010
|
-
path: `src/pages/+config.${x}`,
|
|
2011
|
-
content: sources.config
|
|
2012
|
-
},
|
|
2013
|
-
{
|
|
2014
|
-
path: `src/pages/PageShell.${x}x`,
|
|
2015
|
-
content: libraries.includes("redux-toolkit") ? withStore(sources.PageShell) : sources.PageShell
|
|
2016
|
-
},
|
|
2017
|
-
{
|
|
2018
|
-
path: `src/pages/usePageContext.${x}x`,
|
|
2019
|
-
content: sources.usePageContext
|
|
2020
|
-
},
|
|
2021
|
-
{
|
|
2022
|
-
path: `src/pages/+onRenderHtml.${x}x`,
|
|
2023
|
-
content: isSpa ? sources.spaOnRenderHtml : sources.onRenderHtml
|
|
2024
|
-
},
|
|
2025
|
-
...onRenderClient ? [{
|
|
2026
|
-
path: `src/pages/+onRenderClient.${x}x`,
|
|
2027
|
-
content: onRenderClient
|
|
2028
|
-
}] : [],
|
|
2029
|
-
{
|
|
2030
|
-
path: `src/pages/+Layout.${x}x`,
|
|
2031
|
-
content: sources.Layout
|
|
2032
|
-
},
|
|
2033
|
-
{
|
|
2034
|
-
path: `src/pages/index/+Page.${x}x`,
|
|
2035
|
-
content: sources.home
|
|
2036
|
-
},
|
|
2037
|
-
{
|
|
2038
|
-
path: `src/pages/about/+Page.${x}x`,
|
|
2039
|
-
content: sources.about
|
|
2040
|
-
},
|
|
2041
|
-
{
|
|
2042
|
-
path: `src/pages/_error/+Page.${x}x`,
|
|
2043
|
-
content: sources.error
|
|
2044
|
-
},
|
|
2045
|
-
...facts.hasSampleData ? createSampleDataFiles(facts) : [],
|
|
2046
|
-
...createLibraryFiles(facts)
|
|
1337
|
+
...createTemplateFiles("vike", facts)
|
|
2047
1338
|
];
|
|
2048
1339
|
}
|
|
2049
1340
|
const vikeStarter = {
|
|
2050
1341
|
title: "Vike",
|
|
1342
|
+
label: "Vike (recommended): React pages, rendered as you pick",
|
|
1343
|
+
renderings: [
|
|
1344
|
+
"isomorphic",
|
|
1345
|
+
"ssr",
|
|
1346
|
+
"spa",
|
|
1347
|
+
"ssg"
|
|
1348
|
+
],
|
|
1349
|
+
hasLibraries: true,
|
|
2051
1350
|
createFiles
|
|
2052
1351
|
};
|
|
2053
1352
|
// src/create/starters.ts
|
|
2054
1353
|
/** The renderings, in the order create offers them. The first is the default. */
|
|
2055
1354
|
const RENDERINGS = [
|
|
2056
1355
|
"isomorphic",
|
|
1356
|
+
"ssr",
|
|
2057
1357
|
"spa",
|
|
2058
1358
|
"ssg"
|
|
2059
1359
|
];
|
|
2060
1360
|
/** The starters, by the name `--framework` takes. The first is the default. */
|
|
2061
|
-
const STARTERS = {
|
|
1361
|
+
const STARTERS = {
|
|
1362
|
+
vike: vikeStarter,
|
|
1363
|
+
astro: astroStarter
|
|
1364
|
+
};
|
|
2062
1365
|
// src/create/workspace.ts
|
|
2063
1366
|
/** The layouts, in the order create offers them. The first is the default. */
|
|
2064
1367
|
const LAYOUTS = ["workspace", "standalone"];
|
|
@@ -2080,16 +1383,14 @@ const GITIGNORE = `node_modules/
|
|
|
2080
1383
|
*.log
|
|
2081
1384
|
.DS_Store
|
|
2082
1385
|
`;
|
|
2083
|
-
|
|
2084
|
-
|
|
1386
|
+
/** The names of the scripts in the app's package.json, which the root runs by the same names. */
|
|
1387
|
+
function readAppScripts(appFiles) {
|
|
1388
|
+
const packageJson = appFiles.find((file) => file.path === "package.json");
|
|
1389
|
+
return Object.keys(JSON.parse(packageJson?.content ?? "{}").scripts ?? {});
|
|
1390
|
+
}
|
|
1391
|
+
function createRootFiles(facts, scripts) {
|
|
1392
|
+
const { packageName, title, packageManager } = facts;
|
|
2085
1393
|
const app = toAppPackageName(packageName);
|
|
2086
|
-
const scripts = [
|
|
2087
|
-
"dev",
|
|
2088
|
-
"build",
|
|
2089
|
-
"preview",
|
|
2090
|
-
...isTypeScript ? ["typecheck"] : [],
|
|
2091
|
-
...libraries.includes("vitest") ? ["test"] : []
|
|
2092
|
-
];
|
|
2093
1394
|
const isPnpm = packageManager === "pnpm";
|
|
2094
1395
|
const packageJson = {
|
|
2095
1396
|
name: packageName,
|
|
@@ -2133,7 +1434,7 @@ serves the build.
|
|
|
2133
1434
|
}
|
|
2134
1435
|
/** The files of a workspace holding the app whose files are `appFiles`, in `apps/web`. */
|
|
2135
1436
|
function createWorkspaceFiles(appFiles, facts) {
|
|
2136
|
-
return [...createRootFiles(facts), ...appFiles.map((file) => ({
|
|
1437
|
+
return [...createRootFiles(facts, readAppScripts(appFiles)), ...appFiles.map((file) => ({
|
|
2137
1438
|
...file,
|
|
2138
1439
|
path: `${APP_FOLDER}/${file.path}`
|
|
2139
1440
|
}))];
|
|
@@ -2248,28 +1549,48 @@ async function askName(io) {
|
|
|
2248
1549
|
/** What the rendering question says of each one. */
|
|
2249
1550
|
const RENDERING_LABELS = {
|
|
2250
1551
|
isomorphic: "Isomorphic (recommended): HTML from the build, then no reload",
|
|
1552
|
+
ssr: "SSR: HTML from a server, on each request, then no reload",
|
|
2251
1553
|
spa: "SPA: rendered in the browser",
|
|
2252
1554
|
ssg: "SSG: HTML only, no JavaScript"
|
|
2253
1555
|
};
|
|
2254
|
-
async function askRendering(io) {
|
|
2255
|
-
const choices =
|
|
1556
|
+
async function askRendering(io, renderings) {
|
|
1557
|
+
const choices = renderings.map((value) => ({
|
|
2256
1558
|
value,
|
|
2257
1559
|
label: RENDERING_LABELS[value]
|
|
2258
1560
|
}));
|
|
2259
1561
|
return await io.select("Rendering", choices);
|
|
2260
1562
|
}
|
|
2261
|
-
/** Asks for the
|
|
2262
|
-
async function
|
|
2263
|
-
const choices =
|
|
2264
|
-
value
|
|
2265
|
-
label
|
|
1563
|
+
/** Asks for the framework, of the ones `names` lists. */
|
|
1564
|
+
async function askFramework(io, starters, names) {
|
|
1565
|
+
const choices = names.map((value) => ({
|
|
1566
|
+
value,
|
|
1567
|
+
label: starters[value].label
|
|
2266
1568
|
}));
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
1569
|
+
return io.select("Framework", choices);
|
|
1570
|
+
}
|
|
1571
|
+
/** The choices of one job's question: the libraries of `group`, or those of none. */
|
|
1572
|
+
const getLibraryChoices = (group) => LIBRARIES.filter((library) => library.group === group).map(({ name, label }) => ({
|
|
1573
|
+
value: name,
|
|
1574
|
+
label
|
|
1575
|
+
}));
|
|
1576
|
+
const NO_LIBRARY = {
|
|
1577
|
+
value: "none",
|
|
1578
|
+
label: "None"
|
|
1579
|
+
};
|
|
1580
|
+
/**
|
|
1581
|
+
* Asks for the libraries by job: one that fetches data, one that holds state - each of them or
|
|
1582
|
+
* none - then the others, toggled. Returns them in the order create offers them.
|
|
1583
|
+
*/
|
|
1584
|
+
async function askLibraries(io) {
|
|
1585
|
+
const data = await io.select("Data fetching", [NO_LIBRARY, ...getLibraryChoices("data")]);
|
|
1586
|
+
const state = await io.select("State management", [NO_LIBRARY, ...getLibraryChoices("state")]);
|
|
1587
|
+
const more = await io.selectMany("More", getLibraryChoices(void 0));
|
|
1588
|
+
const picked = /* @__PURE__ */ new Set([
|
|
1589
|
+
data,
|
|
1590
|
+
state,
|
|
1591
|
+
...more
|
|
1592
|
+
]);
|
|
1593
|
+
return LIBRARY_NAMES.filter((name) => picked.has(name));
|
|
2273
1594
|
}
|
|
2274
1595
|
async function askSampleData(io) {
|
|
2275
1596
|
return await io.select("Add sample data pages?", YES_OR_NO, 1) === "yes";
|
|
@@ -2336,13 +1657,14 @@ function writeAndInstall({ name, target, wasMissing, plan, packageManager }, opt
|
|
|
2336
1657
|
async function runCreate(options, io, designSystem, { starters = STARTERS, fileSystem = nodeFileSystem, userAgent = process.env.npm_config_user_agent } = {}) {
|
|
2337
1658
|
const isPreviewOnly = options.isDryRun || !io.isInteractive && !options.isConfirmed;
|
|
2338
1659
|
const canAsk = !isPreviewOnly && !options.isConfirmed;
|
|
2339
|
-
const
|
|
2340
|
-
const
|
|
2341
|
-
if (!
|
|
2342
|
-
const names =
|
|
2343
|
-
io.write(`create has no starter for ${
|
|
1660
|
+
const frameworks = Object.keys(starters);
|
|
1661
|
+
const given = options.framework;
|
|
1662
|
+
if (frameworks.length === 0 || given !== void 0 && !Object.hasOwn(starters, given)) {
|
|
1663
|
+
const names = frameworks.join(", ") || "none yet";
|
|
1664
|
+
io.write(`create has no starter for ${given ?? "any framework"}. It has: ${names}.\n`);
|
|
2344
1665
|
return 1;
|
|
2345
1666
|
}
|
|
1667
|
+
const pool = given === void 0 ? frameworks : [given];
|
|
2346
1668
|
const name = options.name ?? (canAsk ? await askName(io) : DEFAULT_NAME);
|
|
2347
1669
|
const nameRefusal = findNameRefusal(name);
|
|
2348
1670
|
if (nameRefusal) {
|
|
@@ -2369,13 +1691,22 @@ async function runCreate(options, io, designSystem, { starters = STARTERS, fileS
|
|
|
2369
1691
|
return 1;
|
|
2370
1692
|
}
|
|
2371
1693
|
const isWorkspace = (options.layout ?? (canAsk ? await askLayout(io) : LAYOUTS[0])) === "workspace";
|
|
2372
|
-
const
|
|
2373
|
-
|
|
1694
|
+
const offered = RENDERINGS.filter((rendering) => pool.some((framework) => starters[framework].renderings.includes(rendering)));
|
|
1695
|
+
if (options.rendering !== void 0 && !offered.includes(options.rendering)) {
|
|
1696
|
+
const titles = pool.map((framework) => starters[framework].title).join(" or ");
|
|
1697
|
+
io.write(`${titles} does not render ${options.rendering}: it renders ${offered.join(" or ")}.\n`);
|
|
1698
|
+
return 1;
|
|
1699
|
+
}
|
|
1700
|
+
const rendering = options.rendering ?? (canAsk && offered.length > 1 ? await askRendering(io, offered) : offered[0]);
|
|
1701
|
+
const candidates = pool.filter((framework) => starters[framework].renderings.includes(rendering));
|
|
1702
|
+
const starter = starters[canAsk && candidates.length > 1 ? await askFramework(io, starters, candidates) : candidates[0]];
|
|
1703
|
+
const libraryRefusal = !options.libraries?.length ? void 0 : rendering === "ssg" ? "An SSG app has no JavaScript in the browser, so it takes no library: leave out --with." : starter.hasLibraries ? findLibraryConflict(options.libraries) : `${starter.title} takes no library yet: leave out --with.`;
|
|
2374
1704
|
if (libraryRefusal) {
|
|
2375
1705
|
io.write(`${libraryRefusal}\n`);
|
|
2376
1706
|
return 1;
|
|
2377
1707
|
}
|
|
2378
|
-
const
|
|
1708
|
+
const takesLibraries = starter.hasLibraries && rendering !== "ssg";
|
|
1709
|
+
const libraries = options.libraries ?? (canAsk && takesLibraries ? await askLibraries(io) : []);
|
|
2379
1710
|
const scheme = options.scheme ?? (canAsk ? await askScheme(io) : "light");
|
|
2380
1711
|
const hasSampleData = options.hasSampleData ?? (canAsk ? await askSampleData(io) : false);
|
|
2381
1712
|
const appFiles = starter.createFiles({
|
|
@@ -2393,8 +1724,6 @@ async function runCreate(options, io, designSystem, { starters = STARTERS, fileS
|
|
|
2393
1724
|
creates: isWorkspace ? createWorkspaceFiles(appFiles, {
|
|
2394
1725
|
packageName,
|
|
2395
1726
|
title,
|
|
2396
|
-
isTypeScript,
|
|
2397
|
-
libraries,
|
|
2398
1727
|
packageManager: packageManager.name
|
|
2399
1728
|
}) : appFiles,
|
|
2400
1729
|
edits: [],
|
|
@@ -2405,7 +1734,7 @@ async function runCreate(options, io, designSystem, { starters = STARTERS, fileS
|
|
|
2405
1734
|
const language = isTypeScript ? "TypeScript" : "JavaScript";
|
|
2406
1735
|
const packageNote = packageName === name ? "" : ` (package ${packageName})`;
|
|
2407
1736
|
const renderingName = rendering === "isomorphic" ? rendering : rendering.toUpperCase();
|
|
2408
|
-
io.write(`create will make ${name}${packageNote}, a ${starter.title} app in ${language} (${renderingName}), ${isWorkspace ? "in a workspace, " : ""}in ./${name}:\n\n`);
|
|
1737
|
+
io.write(`create will make ${name}${packageNote}, ${/^[AEIOU]/.test(starter.title) ? "an" : "a"} ${starter.title} app in ${language} (${renderingName}), ${isWorkspace ? "in a workspace, " : ""}in ./${name}:\n\n`);
|
|
2409
1738
|
io.write(`${formatPlan(plan)}\n`);
|
|
2410
1739
|
if (!await isApproved(options, isPreviewOnly, io)) return 0;
|
|
2411
1740
|
return writeAndInstall({
|
|
@@ -2511,7 +1840,7 @@ Options:
|
|
|
2511
1840
|
when not given
|
|
2512
1841
|
--layout <name> workspace (the default): the app in apps/web, with room for more
|
|
2513
1842
|
apps and services; or standalone: the app alone
|
|
2514
|
-
--render <name> how pages render: isomorphic (the default), spa or ssg
|
|
1843
|
+
--render <name> how pages render: isomorphic (the default), ssr, spa or ssg
|
|
2515
1844
|
--with <names> the libraries to add, by comma: ${LIBRARY_NAMES.join(", ")}
|
|
2516
1845
|
--scheme <name> the default color scheme: light (the default) or dark
|
|
2517
1846
|
--sample-data add a list of sample items, and a page for each
|