|
| 1 | +import fs from "fs/promises"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +/** |
| 5 | + * @param {string} file file path |
| 6 | + * @param {string} content file content |
| 7 | + * @returns {Promise<void>} |
| 8 | + */ |
| 9 | +async function write(file, content) { |
| 10 | + await fs.mkdir(path.dirname(file), { recursive: true }); |
| 11 | + await fs.writeFile(file, content); |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * @param {number} feature feature index |
| 16 | + * @param {number} component component index |
| 17 | + * @returns {string} component module source |
| 18 | + */ |
| 19 | +function componentCode(feature, component) { |
| 20 | + const name = `Feature${feature}Component${component}`; |
| 21 | + |
| 22 | + return `import { createElement, useMemo, useState } from "react"; |
| 23 | +import { debounce, groupBy, sortBy } from "lodash-es"; |
| 24 | +import { addDays, format } from "date-fns"; |
| 25 | +import { Button, Card, formatCurrency, useToggle } from "@/shared"; |
| 26 | +import * as styles from "../styles.module.css"; |
| 27 | +import rows from "../data.json"; |
| 28 | +import icon from "../icon.svg"; |
| 29 | +
|
| 30 | +const columns = ["id", "label", "amount", "createdAt"]; |
| 31 | +
|
| 32 | +export function ${name}({ title = "${name}", onSelect }) { |
| 33 | + const [query, setQuery] = useState(""); |
| 34 | + const [expanded, toggle] = useToggle(false); |
| 35 | + const visible = useMemo(() => { |
| 36 | + const filtered = rows.filter(({ label }) => label.includes(query)); |
| 37 | + return groupBy(sortBy(filtered, ["amount"]), (row) => row.amount > 50); |
| 38 | + }, [query]); |
| 39 | + const onInput = debounce((event) => setQuery(event.target.value), 100); |
| 40 | +
|
| 41 | + return createElement( |
| 42 | + Card, |
| 43 | + { className: styles.card, title }, |
| 44 | + createElement("img", { src: icon, alt: "" }), |
| 45 | + createElement("input", { className: styles.input, onInput }), |
| 46 | + createElement(Button, { onClick: toggle }, expanded ? "Less" : "More"), |
| 47 | + expanded && |
| 48 | + Object.entries(visible).map(([key, group]) => |
| 49 | + createElement( |
| 50 | + "ul", |
| 51 | + { key, className: styles.list }, |
| 52 | + group.map((row) => |
| 53 | + createElement( |
| 54 | + "li", |
| 55 | + { key: row.id, onClick: () => onSelect && onSelect(row) }, |
| 56 | + columns.map((column) => String(row[column])).join(" | "), |
| 57 | + " ", |
| 58 | + formatCurrency(row.amount), |
| 59 | + " ", |
| 60 | + format(addDays(new Date(0), row.id), "yyyy-MM-dd") |
| 61 | + ) |
| 62 | + ) |
| 63 | + ) |
| 64 | + ) |
| 65 | + ); |
| 66 | +} |
| 67 | +
|
| 68 | +export default ${name}; |
| 69 | +`; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * @param {number} feature feature index |
| 74 | + * @returns {string} CSS module source |
| 75 | + */ |
| 76 | +function stylesCode(feature) { |
| 77 | + return `.card { |
| 78 | + display: grid; |
| 79 | + grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr)); |
| 80 | + gap: 1rem; |
| 81 | + padding: ${feature % 4}rem; |
| 82 | + border: 1px solid var(--border, #ddd); |
| 83 | +} |
| 84 | +
|
| 85 | +.card:hover { |
| 86 | + box-shadow: 0 2px 8px rgb(0 0 0 / 15%); |
| 87 | +} |
| 88 | +
|
| 89 | +.input { |
| 90 | + composes: card; |
| 91 | + width: 100%; |
| 92 | +} |
| 93 | +
|
| 94 | +.list > li:nth-child(odd) { |
| 95 | + background: color-mix(in srgb, currentColor 5%, transparent); |
| 96 | +} |
| 97 | +
|
| 98 | +@media (max-width: 600px) { |
| 99 | + .card { |
| 100 | + grid-template-columns: 1fr; |
| 101 | + } |
| 102 | +} |
| 103 | +`; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * @param {number} feature feature index |
| 108 | + * @returns {string} JSON source |
| 109 | + */ |
| 110 | +function dataCode(feature) { |
| 111 | + return JSON.stringify( |
| 112 | + Array.from({ length: 20 }, (_, id) => ({ |
| 113 | + id, |
| 114 | + label: `feature-${feature}-row-${id}`, |
| 115 | + amount: (id * 37 + feature * 11) % 100, |
| 116 | + createdAt: 1700000000000 + id * 86400000 |
| 117 | + })), |
| 118 | + null, |
| 119 | + 2 |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Writes a small single-page app: lazy routes, feature folders, barrel files, |
| 125 | + * an `@` alias, CSS modules, JSON and SVG assets and real npm packages. |
| 126 | + * @param {string} folder output folder |
| 127 | + * @param {number} features count of lazy-loaded feature routes |
| 128 | + * @param {number} components count of components in each feature |
| 129 | + * @returns {Promise<void>} |
| 130 | + */ |
| 131 | +async function createApp(folder, features, components) { |
| 132 | + await fs.rm(folder, { recursive: true, force: true }); |
| 133 | + |
| 134 | + const utils = ["format", "toggle", "request", "storage", "validate"]; |
| 135 | + |
| 136 | + for (const util of utils) { |
| 137 | + await write( |
| 138 | + path.join(folder, `shared/utils/${util}.js`), |
| 139 | + `export const ${util}Version = ${JSON.stringify(util)}; |
| 140 | +
|
| 141 | +export function ${util}(value, options = {}) { |
| 142 | + const { strict = false, fallback = null } = options; |
| 143 | + if (value == null) return fallback; |
| 144 | + return strict ? JSON.parse(JSON.stringify(value)) : value; |
| 145 | +} |
| 146 | +` |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + await write( |
| 151 | + path.join(folder, "shared/utils/currency.js"), |
| 152 | + `export const formatCurrency = (value, currency = "USD") => |
| 153 | + new Intl.NumberFormat("en-US", { style: "currency", currency }).format(value); |
| 154 | +` |
| 155 | + ); |
| 156 | + await write( |
| 157 | + path.join(folder, "shared/components/Button.js"), |
| 158 | + `import { createElement } from "react"; |
| 159 | +
|
| 160 | +export const Button = ({ children, ...props }) => |
| 161 | + createElement("button", { type: "button", ...props }, children); |
| 162 | +` |
| 163 | + ); |
| 164 | + await write( |
| 165 | + path.join(folder, "shared/components/Card.js"), |
| 166 | + `import { createElement } from "react"; |
| 167 | +
|
| 168 | +export const Card = ({ title, children, ...props }) => |
| 169 | + createElement("section", props, createElement("h2", null, title), children); |
| 170 | +` |
| 171 | + ); |
| 172 | + await write( |
| 173 | + path.join(folder, "shared/index.js"), |
| 174 | + `${utils.map((util) => `export * from "./utils/${util}.js";`).join("\n")} |
| 175 | +export * from "./utils/currency.js"; |
| 176 | +export { useToggle } from "./utils/toggle-hook.js"; |
| 177 | +export * from "./components/Button.js"; |
| 178 | +export * from "./components/Card.js"; |
| 179 | +` |
| 180 | + ); |
| 181 | + await write( |
| 182 | + path.join(folder, "shared/utils/toggle-hook.js"), |
| 183 | + `import { useCallback, useState } from "react"; |
| 184 | +
|
| 185 | +export function useToggle(initial) { |
| 186 | + const [value, setValue] = useState(initial); |
| 187 | + return [value, useCallback(() => setValue((current) => !current), [])]; |
| 188 | +} |
| 189 | +` |
| 190 | + ); |
| 191 | + |
| 192 | + const routes = []; |
| 193 | + |
| 194 | + for (let feature = 0; feature < features; feature++) { |
| 195 | + const dir = path.join(folder, `features/feature-${feature}`); |
| 196 | + const names = []; |
| 197 | + |
| 198 | + await write(path.join(dir, "styles.module.css"), stylesCode(feature)); |
| 199 | + await write(path.join(dir, "data.json"), dataCode(feature)); |
| 200 | + await write( |
| 201 | + path.join(dir, "icon.svg"), |
| 202 | + `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><circle cx="12" cy="12" r="${(feature % 10) + 1}"/></svg>\n` |
| 203 | + ); |
| 204 | + |
| 205 | + for (let component = 0; component < components; component++) { |
| 206 | + names.push(`Feature${feature}Component${component}`); |
| 207 | + await write( |
| 208 | + path.join(dir, `components/Component${component}.js`), |
| 209 | + componentCode(feature, component) |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + await write( |
| 214 | + path.join(dir, "index.js"), |
| 215 | + `import { createElement } from "react"; |
| 216 | +${names.map((name, i) => `import ${name} from "./components/Component${i}.js";`).join("\n")} |
| 217 | +
|
| 218 | +export default function Feature${feature}() { |
| 219 | + return createElement( |
| 220 | + "main", |
| 221 | + null, |
| 222 | + ${names.map((name) => `createElement(${name}, { key: ${JSON.stringify(name)} })`).join(",\n\t\t")} |
| 223 | + ); |
| 224 | +} |
| 225 | +` |
| 226 | + ); |
| 227 | + routes.push( |
| 228 | + `\t${JSON.stringify(`/feature-${feature}`)}: () => import(/* webpackChunkName: "feature-${feature}" */ "./features/feature-${feature}/index.js")` |
| 229 | + ); |
| 230 | + } |
| 231 | + |
| 232 | + await write( |
| 233 | + path.join(folder, "index.js"), |
| 234 | + `import { createElement, lazy, Suspense } from "react"; |
| 235 | +import { Card } from "@/shared"; |
| 236 | +
|
| 237 | +const routes = { |
| 238 | +${routes.join(",\n")} |
| 239 | +}; |
| 240 | +
|
| 241 | +const App = ({ path }) => |
| 242 | + createElement( |
| 243 | + Suspense, |
| 244 | + { fallback: createElement(Card, { title: "Loading" }) }, |
| 245 | + createElement(lazy(routes[path] || routes["/feature-0"])) |
| 246 | + ); |
| 247 | +
|
| 248 | +export default createElement(App, { path: window.location.pathname }); |
| 249 | +` |
| 250 | + ); |
| 251 | +} |
| 252 | + |
| 253 | +export default createApp; |
0 commit comments