@nuvler/cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/dist/api/client.js +84 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/theme-context.js +190 -0
- package/dist/api/theme-context.js.map +1 -0
- package/dist/api/theme-registry.js +77 -0
- package/dist/api/theme-registry.js.map +1 -0
- package/dist/commands/theme/build.js +116 -0
- package/dist/commands/theme/build.js.map +1 -0
- package/dist/commands/theme/create.js +92 -0
- package/dist/commands/theme/create.js.map +1 -0
- package/dist/commands/theme/dev.js +46 -0
- package/dist/commands/theme/dev.js.map +1 -0
- package/dist/commands/theme/pack.js +75 -0
- package/dist/commands/theme/pack.js.map +1 -0
- package/dist/commands/theme/publish.js +37 -0
- package/dist/commands/theme/publish.js.map +1 -0
- package/dist/commands/theme/validate.js +15 -0
- package/dist/commands/theme/validate.js.map +1 -0
- package/dist/index.js +144 -0
- package/dist/index.js.map +1 -0
- package/dist/nuvler.js +7553 -0
- package/dist/nuvler.js.map +1 -0
- package/dist/preview/launcher.js +104 -0
- package/dist/preview/launcher.js.map +1 -0
- package/dist/theme/artifact-policy.js +119 -0
- package/dist/theme/artifact-policy.js.map +1 -0
- package/dist/theme/artifact.js +39 -0
- package/dist/theme/artifact.js.map +1 -0
- package/dist/theme/project.js +71 -0
- package/dist/theme/project.js.map +1 -0
- package/package.json +44 -0
- package/preview-host/index.html +13 -0
- package/preview-host/package.json +23 -0
- package/preview-host/src/app/globals.css +16 -0
- package/preview-host/src/fallback-context.json +53 -0
- package/preview-host/src/main.tsx +145 -0
- package/preview-host/src/virtual-modules.d.ts +11 -0
- package/preview-host/tsconfig.json +28 -0
- package/preview-host/vendor/theme-runtime/default-theme.tsx +577 -0
- package/preview-host/vendor/theme-runtime/index.ts +25 -0
- package/preview-host/vite.config.ts +125 -0
- package/templates/starter-theme/.env.example +3 -0
- package/templates/starter-theme/README.md +100 -0
- package/templates/starter-theme/gitignore.template +9 -0
- package/templates/starter-theme/nuvler.theme.json +10 -0
- package/templates/starter-theme/package.json +29 -0
- package/templates/starter-theme/src/definition.tsx +54 -0
- package/templates/starter-theme/src/styles.d.ts +1 -0
- package/templates/starter-theme/src/theme.css +21 -0
- package/templates/starter-theme/src/theme.tsx +34 -0
- package/templates/starter-theme/tsconfig.json +17 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { StrictMode, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { createRoot } from "react-dom/client";
|
|
3
|
+
import "./app/globals.css";
|
|
4
|
+
import type {
|
|
5
|
+
ThemeImageProps,
|
|
6
|
+
ThemeLinkProps,
|
|
7
|
+
ThemeRoute,
|
|
8
|
+
ThemeRuntimeAdapters,
|
|
9
|
+
} from "@nuvler/theme-runtime";
|
|
10
|
+
import type { ThemePagination, ThemeProduct } from "@nuvler/theme-sdk";
|
|
11
|
+
import ActiveTheme from "virtual:nuvler-active-theme";
|
|
12
|
+
import context from "virtual:nuvler-theme-context";
|
|
13
|
+
|
|
14
|
+
function PreviewImage({ image, fallbackAlt, className }: ThemeImageProps) {
|
|
15
|
+
if (!image?.url) return <span className={`store-image-placeholder theme-image-placeholder ${className ?? ""}`} aria-hidden="true"><span>N</span></span>;
|
|
16
|
+
return <img className={className} src={image.url} alt={image.alt || fallbackAlt} />;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function normalizeProduct(value: unknown): ThemeProduct {
|
|
20
|
+
const product = value as Partial<ThemeProduct>;
|
|
21
|
+
return {
|
|
22
|
+
title: product.title || "Produto",
|
|
23
|
+
slug: product.slug || "produto",
|
|
24
|
+
description: product.description || "",
|
|
25
|
+
sku: product.sku || "",
|
|
26
|
+
price: product.price ?? null,
|
|
27
|
+
promotionalPrice: product.promotionalPrice ?? null,
|
|
28
|
+
stockQuantity: product.stockQuantity ?? null,
|
|
29
|
+
brand: product.brand || "",
|
|
30
|
+
model: product.model || "",
|
|
31
|
+
ean: product.ean || "",
|
|
32
|
+
image: product.image || null,
|
|
33
|
+
images: product.images || [],
|
|
34
|
+
categories: product.categories || [],
|
|
35
|
+
variantOptions: product.variantOptions || [],
|
|
36
|
+
variants: product.variants || [],
|
|
37
|
+
publishedAt: product.publishedAt ?? null,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function previewApi<T>(path: string): Promise<T> {
|
|
42
|
+
const response = await fetch(`/__nuvler-api/${path.replace(/^\/+/, "")}`, {
|
|
43
|
+
headers: { accept: "application/json" },
|
|
44
|
+
});
|
|
45
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
46
|
+
return response.json() as Promise<T>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function routeFromLocalData(url: URL): ThemeRoute | null {
|
|
50
|
+
if (url.pathname === "/") return { type: "home" };
|
|
51
|
+
const editorial = [
|
|
52
|
+
{ basePath: "/blog", title: "Blog", items: context.editorial.posts },
|
|
53
|
+
{ basePath: "/servicos", title: "Serviços", items: context.editorial.services },
|
|
54
|
+
{ basePath: "/cases", title: "Cases", items: context.editorial.cases },
|
|
55
|
+
];
|
|
56
|
+
for (const group of editorial) {
|
|
57
|
+
if (url.pathname === group.basePath) return { type: "editorial-list", ...group };
|
|
58
|
+
if (url.pathname.startsWith(`${group.basePath}/`)) {
|
|
59
|
+
const slug = decodeURIComponent(url.pathname.slice(group.basePath.length + 1));
|
|
60
|
+
const item = group.items.find((candidate) => candidate.slug === slug);
|
|
61
|
+
return item ? { type: "editorial-detail", basePath: group.basePath, item } : { type: "not-found", pathname: url.pathname };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const page = context.navigation.pages.find((item) => `/${item.slug}` === url.pathname);
|
|
65
|
+
return page ? { type: "page", title: page.title } : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function PreviewApp() {
|
|
69
|
+
const [route, setRoute] = useState<ThemeRoute>({ type: "loading", pathname: window.location.pathname });
|
|
70
|
+
const requestId = useRef(0);
|
|
71
|
+
|
|
72
|
+
const loadRoute = useCallback(async (url: URL) => {
|
|
73
|
+
const currentRequest = ++requestId.current;
|
|
74
|
+
const localRoute = routeFromLocalData(url);
|
|
75
|
+
if (localRoute) { setRoute(localRoute); return; }
|
|
76
|
+
setRoute({ type: "loading", pathname: url.pathname });
|
|
77
|
+
try {
|
|
78
|
+
if (url.pathname.startsWith("/produto/")) {
|
|
79
|
+
const slug = decodeURIComponent(url.pathname.slice("/produto/".length));
|
|
80
|
+
const product = normalizeProduct(await previewApi<unknown>(`products/${encodeURIComponent(slug)}`));
|
|
81
|
+
if (currentRequest === requestId.current) setRoute({ type: "product", product });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (url.pathname === "/produtos" || url.pathname.startsWith("/categoria/")) {
|
|
85
|
+
const categoryPath = url.pathname.startsWith("/categoria/")
|
|
86
|
+
? url.pathname.slice("/categoria/".length).split("/").filter(Boolean).map(decodeURIComponent)
|
|
87
|
+
: [];
|
|
88
|
+
const params = new URLSearchParams();
|
|
89
|
+
const page = Math.max(1, Number(url.searchParams.get("pagina")) || 1);
|
|
90
|
+
const search = url.searchParams.get("busca")?.trim() || "";
|
|
91
|
+
const variants = url.searchParams.getAll("variante");
|
|
92
|
+
params.set("page", String(page));
|
|
93
|
+
params.set("limit", String(context.site.settings?.catalog?.productsPerPage || 24));
|
|
94
|
+
params.set("facets", "true");
|
|
95
|
+
if (categoryPath.length) params.set("category", categoryPath.at(-1) || "");
|
|
96
|
+
if (search) params.set("search", search);
|
|
97
|
+
variants.forEach((variant) => params.append("variant", variant));
|
|
98
|
+
if (context.site.settings?.catalog?.showProductsWithoutImage === false) params.set("withImageOnly", "true");
|
|
99
|
+
const response = await previewApi<{ items?: unknown[]; pagination?: ThemePagination; facets?: { variants?: Array<{ name: string; values: Array<{ value: string; count: number }> }> } }>(`products?${params}`);
|
|
100
|
+
const products = (response.items || []).map(normalizeProduct);
|
|
101
|
+
const pagination = response.pagination || { page, limit: products.length || 1, total: products.length, totalPages: 1, hasPreviousPage: false, hasNextPage: false };
|
|
102
|
+
if (currentRequest === requestId.current) setRoute({ type: "catalog", products, categories: context.navigation.categories, pagination, search, categorySlug: categoryPath.at(-1) || null, categoryPath, variants, facets: response.facets?.variants || [] });
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
setRoute({ type: "not-found", pathname: url.pathname });
|
|
106
|
+
} catch {
|
|
107
|
+
if (currentRequest === requestId.current) setRoute({ type: "not-found", pathname: url.pathname });
|
|
108
|
+
}
|
|
109
|
+
}, []);
|
|
110
|
+
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
void loadRoute(new URL(window.location.href));
|
|
113
|
+
const pop = () => void loadRoute(new URL(window.location.href));
|
|
114
|
+
window.addEventListener("popstate", pop);
|
|
115
|
+
return () => window.removeEventListener("popstate", pop);
|
|
116
|
+
}, [loadRoute]);
|
|
117
|
+
|
|
118
|
+
const adapters = useMemo<ThemeRuntimeAdapters>(() => {
|
|
119
|
+
function PreviewLink({ href, children, className }: ThemeLinkProps) {
|
|
120
|
+
const external = /^(?:https?:|mailto:|tel:)/.test(href);
|
|
121
|
+
return <a className={className} href={href} onClick={(event) => {
|
|
122
|
+
if (external || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
123
|
+
event.preventDefault();
|
|
124
|
+
const next = new URL(href, window.location.origin);
|
|
125
|
+
window.history.pushState({}, "", next);
|
|
126
|
+
window.scrollTo({ top: 0 });
|
|
127
|
+
void loadRoute(next);
|
|
128
|
+
}}>{children}</a>;
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
Link: PreviewLink,
|
|
132
|
+
Image: PreviewImage,
|
|
133
|
+
searchProducts: async (query) => {
|
|
134
|
+
const results = await previewApi<unknown[]>(`products/suggestions?search=${encodeURIComponent(query)}`);
|
|
135
|
+
return results.map(normalizeProduct).slice(0, 5);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}, [loadRoute]);
|
|
139
|
+
|
|
140
|
+
return <ActiveTheme adapters={adapters} context={context} route={route} />;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const container = document.getElementById("root");
|
|
144
|
+
if (!container) throw new Error("Elemento #root não encontrado.");
|
|
145
|
+
createRoot(container).render(<StrictMode><PreviewApp /></StrictMode>);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare module "virtual:nuvler-active-theme" {
|
|
2
|
+
import type { NuvlerThemeComponent } from "@nuvler/theme-runtime";
|
|
3
|
+
const Theme: NuvlerThemeComponent;
|
|
4
|
+
export default Theme;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare module "virtual:nuvler-theme-context" {
|
|
8
|
+
import type { ThemeContext } from "@nuvler/theme-sdk";
|
|
9
|
+
const context: ThemeContext;
|
|
10
|
+
export default context;
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": [
|
|
5
|
+
"dom",
|
|
6
|
+
"dom.iterable",
|
|
7
|
+
"esnext"
|
|
8
|
+
],
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"noEmit": true,
|
|
12
|
+
"module": "esnext",
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"moduleResolution": "bundler",
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"isolatedModules": true,
|
|
17
|
+
"jsx": "react-jsx",
|
|
18
|
+
"types": ["vite/client"]
|
|
19
|
+
},
|
|
20
|
+
"include": [
|
|
21
|
+
"vite.config.ts",
|
|
22
|
+
"**/*.ts",
|
|
23
|
+
"**/*.tsx"
|
|
24
|
+
],
|
|
25
|
+
"exclude": [
|
|
26
|
+
"node_modules"
|
|
27
|
+
]
|
|
28
|
+
}
|