weifuwu 0.27.8 → 0.27.10

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.
Files changed (2) hide show
  1. package/dist/cli.js +7 -222
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // cli.ts
4
- import { mkdir, writeFile } from "node:fs/promises";
4
+ import { mkdir, writeFile, readFile, cp } from "node:fs/promises";
5
5
  import { existsSync } from "node:fs";
6
6
  import { execSync } from "node:child_process";
7
7
  import { join, dirname, resolve } from "node:path";
@@ -65,229 +65,14 @@ async function generateMinimal(targetDir, name, version, skipInstall) {
65
65
  await finishInit(targetDir, skipInstall);
66
66
  }
67
67
  async function generateFull(targetDir, name, version, skipInstall) {
68
- await mkdir(targetDir, { recursive: true });
69
- await mkdir(join(targetDir, "ui", "app"), { recursive: true });
70
- await mkdir(join(targetDir, "ui", "lib"), { recursive: true });
71
- await mkdir(join(targetDir, "locales"), { recursive: true });
68
+ const templateDir = existsSync(join(__dirname, "cli", "template")) ? join(__dirname, "cli", "template") : join(__dirname, "template");
69
+ await cp(templateDir, targetDir, { recursive: true });
70
+ const pkgPath = join(targetDir, "package.json");
71
+ const pkgContent = await readFile(pkgPath, "utf-8");
72
72
  await writeFile(
73
- join(targetDir, "app.ts"),
74
- [
75
- `import { Router, layout, view, theme, i18n, cssContext, cssRouter, assetRouter } from 'weifuwu'`,
76
- ``,
77
- `export const app = new Router()`,
78
- ``,
79
- `// Middleware`,
80
- `app.use(theme())`,
81
- `app.use(i18n({ dir: './locales' }))`,
82
- `app.use(cssContext('./ui'))`,
83
- ``,
84
- `// Layout \u2014 wraps all pages`,
85
- `app.use(layout('./ui/app/layout.ts'))`,
86
- ``,
87
- `// Static assets (HTMX, Alpine)`,
88
- `app.use('/', assetRouter())`,
89
- ``,
90
- `// CSS serving`,
91
- `app.use('/', cssRouter('./ui'))`,
92
- ``,
93
- `// Pages`,
94
- `app.get('/', view('./ui/app/page.ts'))`,
95
- ``,
96
- `// API route`,
97
- `app.get('/api/ping', () => Response.json({ pong: true, time: new Date().toISOString() }))`,
98
- ``
99
- ].join("\n")
73
+ pkgPath,
74
+ pkgContent.replace("__PROJECT_NAME__", name).replace("__VERSION__", version)
100
75
  );
101
- await writeFile(
102
- join(targetDir, "index.ts"),
103
- [
104
- `import { loadEnv, serve } from 'weifuwu'`,
105
- `import { app } from './app.ts'`,
106
- ``,
107
- `loadEnv()`,
108
- `const port = Number(process.env.PORT) || 3000`,
109
- `serve(app.handler(), { port })`,
110
- ``
111
- ].join("\n")
112
- );
113
- await writeFile(
114
- join(targetDir, "ui", "app", "globals.css"),
115
- [`@import "tailwindcss";`, `@custom-variant dark (&:is(.dark *));`, ``].join("\n")
116
- );
117
- await writeFile(
118
- join(targetDir, "ui", "app", "layout.ts"),
119
- [
120
- `import { html, raw, assetScripts } from 'weifuwu'`,
121
- ``,
122
- `export default function(body: string, ctx: any) {`,
123
- ` // Theme: read from ctx at server, resolve system on client`,
124
- ` const themeVal = ctx.theme?.value || 'system'`,
125
- ` const isDark = themeVal === 'dark' || (themeVal === 'system' && false)`,
126
- ` const htmlClass = isDark ? 'dark' : ''`,
127
- ` const themeScript = raw(\`<script>`,
128
- `!function(){`,
129
- `var t=(document.cookie.match(/(?:^|;\\s*)theme=([^;]+)/)||[])[1]||'system';`,
130
- `if(t==='system')t=window.matchMedia('(prefers-color-scheme:dark)').matches?'dark':'light';`,
131
- `document.documentElement.classList.toggle('dark',t==='dark');`,
132
- `}()`,
133
- `</script>\`)`,
134
- ``,
135
- ` // i18n: set lang attribute`,
136
- ` const lang = ctx.i18n?.locale || 'en'`,
137
- ``,
138
- ` // CSS: include compiled stylesheet`,
139
- ` const cssLink = ctx.css?.url`,
140
- ` ? raw(\`<link rel="stylesheet" href="\${ctx.css.url}">\`)`,
141
- ` : ''`,
142
- ``,
143
- ` return html\`<!DOCTYPE html>`,
144
- `<html lang="\${lang}" class="\${htmlClass}">`,
145
- `<head>`,
146
- ` <meta charset="utf-8" />`,
147
- ` <meta name="viewport" content="width=device-width, initial-scale=1" />`,
148
- ` \${themeScript}`,
149
- ` \${assetScripts()}`,
150
- ` \${cssLink}`,
151
- `</head>`,
152
- `<body class="min-h-screen bg-white text-gray-900 dark:bg-gray-950 dark:text-gray-100">`,
153
- ` \${raw(body)}`,
154
- `</body>`,
155
- `</html>\``,
156
- `}`,
157
- ``
158
- ].join("\n")
159
- );
160
- await writeFile(
161
- join(targetDir, "ui", "app", "page.ts"),
162
- [
163
- `import { html } from 'weifuwu'`,
164
- ``,
165
- `export default function(ctx: any) {`,
166
- ` const t = ctx.i18n?.t || ((k: string) => k)`,
167
- ` const theme = ctx.theme?.value || 'system'`,
168
- ` const locale = ctx.i18n?.locale || 'en'`,
169
- ``,
170
- ` return html\`<div x-data="{ open: false }" class="min-h-screen">`,
171
- ` <!-- Navbar -->`,
172
- ` <nav class="border-b border-gray-200 dark:border-gray-800">`,
173
- ` <div class="max-w-5xl mx-auto flex items-center justify-between h-14 px-4">`,
174
- ` <span class="font-bold text-lg">weifuwu</span>`,
175
- ` <div class="flex items-center gap-3 text-sm">`,
176
- ` <!-- Locale toggle -->`,
177
- ` <a href="/__lang/\${locale === 'en' ? 'zh-CN' : 'en'}"`,
178
- ` class="px-2 py-1 rounded border border-gray-300 dark:border-gray-600`,
179
- ` hover:bg-gray-100 dark:hover:bg-gray-800 transition">`,
180
- ` \${locale === 'en' ? '\u4E2D\u6587' : 'EN'}`,
181
- ` </a>`,
182
- ` <!-- Theme toggle -->`,
183
- ` <a href="/__theme/\${theme === 'dark' ? 'light' : 'dark'}"`,
184
- ` class="px-2 py-1 rounded border border-gray-300 dark:border-gray-600`,
185
- ` hover:bg-gray-100 dark:hover:bg-gray-800 transition">`,
186
- ` \${theme === 'dark' ? '\u2600\uFE0F' : '\u{1F319}'}`,
187
- ` </a>`,
188
- ` </div>`,
189
- ` </div>`,
190
- ` </nav>`,
191
- ``,
192
- ` <!-- Hero -->`,
193
- ` <section class="max-w-3xl mx-auto px-4 py-16 text-center">`,
194
- ` <h1 class="text-4xl font-bold tracking-tight mb-3">\${t('title')}</h1>`,
195
- ` <p class="text-gray-500 dark:text-gray-400 text-lg mb-8">`,
196
- ` Pure Node.js, no build step`,
197
- ` </p>`,
198
- ``,
199
- ` <div class="flex justify-center gap-3">`,
200
- ` <button @click="open = !open"`,
201
- ` class="rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white`,
202
- ` hover:bg-gray-700 dark:bg-gray-100 dark:text-gray-900 dark:hover:bg-gray-300">`,
203
- ` \${t('cta')}`,
204
- ` </button>`,
205
- ` <a href="/docs"`,
206
- ` class="rounded-md border border-gray-300 px-4 py-2 text-sm font-medium`,
207
- ` hover:bg-gray-100 dark:border-gray-600 dark:hover:bg-gray-800">`,
208
- ` \${t('docs')}`,
209
- ` </a>`,
210
- ` </div>`,
211
- ``,
212
- ` <!-- Alpine demo: click to reveal -->`,
213
- ` <div x-show="open" x-cloak`,
214
- ` class="mt-6 p-4 bg-gray-100 dark:bg-gray-800 rounded-lg text-sm text-left">`,
215
- ` \${t('demo')}`,
216
- ` </div>`,
217
- ` </section>`,
218
- ` </div>\``,
219
- `}`,
220
- ``
221
- ].join("\n")
222
- );
223
- await writeFile(
224
- join(targetDir, "ui", "lib", "utils.ts"),
225
- [
226
- `/**`,
227
- ` * cn() \u2014 Merge class names, handling conditional and array inputs.`,
228
- ` * Lightweight alternative to clsx + tailwind-merge.`,
229
- ` */`,
230
- `export function cn(...classes: (string | false | null | undefined)[]): string {`,
231
- ` return classes.filter(Boolean).join(' ')`,
232
- `}`,
233
- ``
234
- ].join("\n")
235
- );
236
- await writeFile(
237
- join(targetDir, "locales", "en.json"),
238
- JSON.stringify(
239
- {
240
- title: "Build APIs & UI, Zero Build Step",
241
- cta: "Try Alpine",
242
- docs: "Documentation",
243
- demo: "This is Alpine.js in action \u2014 click-toggled content, zero JavaScript written."
244
- },
245
- null,
246
- 2
247
- ) + "\n"
248
- );
249
- await writeFile(
250
- join(targetDir, "locales", "zh-CN.json"),
251
- JSON.stringify(
252
- {
253
- title: "\u96F6\u7F16\u8BD1\u6784\u5EFA API \u548C UI",
254
- cta: "\u4F53\u9A8C Alpine",
255
- docs: "\u6587\u6863",
256
- demo: "\u8FD9\u662F Alpine.js \u7684\u6F14\u793A\u2014\u2014\u70B9\u51FB\u5207\u6362\u5185\u5BB9\uFF0C\u4E0D\u9700\u8981\u5199 JavaScript\u3002"
257
- },
258
- null,
259
- 2
260
- ) + "\n"
261
- );
262
- await writePackageJson(targetDir, name, version, {
263
- dependencies: {
264
- weifuwu: `^${version}`
265
- },
266
- devDependencies: {}
267
- });
268
- await writeFile(
269
- join(targetDir, "tsconfig.json"),
270
- JSON.stringify(
271
- {
272
- compilerOptions: {
273
- target: "ESNext",
274
- module: "NodeNext",
275
- moduleResolution: "NodeNext",
276
- strict: true,
277
- skipLibCheck: true,
278
- noEmit: true,
279
- allowImportingTsExtensions: true,
280
- paths: {
281
- "@/*": ["./ui/*"]
282
- }
283
- },
284
- include: ["*.ts", "ui/**/*.ts"]
285
- },
286
- null,
287
- 2
288
- ) + "\n"
289
- );
290
- await writeCommonFiles(targetDir);
291
76
  await finishInit(targetDir, skipInstall);
292
77
  }
293
78
  async function writePackageJson(targetDir, name, version, extra) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "weifuwu",
3
3
  "type": "module",
4
- "version": "0.27.8",
4
+ "version": "0.27.10",
5
5
  "description": "Web-standard HTTP microframework for Node.js — (req, ctx) => Response",
6
6
  "exports": {
7
7
  ".": "./dist/index.js"