create-forkly 0.3.3 → 0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-forkly",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "description": "CLI to scaffold a new Forkly project",
5
5
  "type": "module",
6
6
  "bin": {
package/template/app.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  /** Forkly 应用入口 — 启动 App 并注册钩子与定时任务 */
2
- import { Forkly } from 'forkly';
2
+ import { Forkly, loadConfig } from 'forkly';
3
+ import { resolve, dirname } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
3
5
 
4
- const app = new Forkly({
5
- port: 3000,
6
- workers: 4,
7
- });
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+
8
+ const app = new Forkly(loadConfig(undefined, resolve(__dirname, 'config')));
8
9
 
9
10
  // 注册全局日志回调
10
11
  app.onLog((level, message) => {
@@ -28,6 +29,4 @@ app.schedule('0 3 * * *', async () => {
28
29
  });
29
30
 
30
31
  // 启动应用
31
- app.start().then(() => {
32
- console.log('Forkly app started');
33
- });
32
+ app.start();
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "port": 3000,
3
3
  "workers": 4,
4
+ "http2": false,
4
5
  "session": { "secret": "your-secret-key-change-me", "maxAge": 86400000 },
5
6
  "errorFormat": "json",
6
7
  "logger": { "level": "info" },
@@ -1,11 +1,12 @@
1
- /** Hello 路由 — GET /hello?name=world,含 dataType 校验 */
2
- import { Router } from 'forkly';
1
+ /** Hello 路由 — GET /hello?name=world,POST setRouter 输入校验 */
2
+ import { setRouter, string } from 'forkly';
3
3
 
4
- export class HelloRouter extends Router {
4
+ const Base = setRouter({
5
+ post: { body: { name: string } },
6
+ });
7
+
8
+ export class HelloRouter extends Base {
5
9
  static channel = 'async' as const;
6
- static dataType = {
7
- query: { name: 'string' },
8
- } as const;
9
10
 
10
11
  async get() {
11
12
  const name = this.request.query.name ?? 'World';
@@ -13,7 +14,7 @@ export class HelloRouter extends Router {
13
14
  }
14
15
 
15
16
  async post() {
16
- const { name } = this.request.body as { name?: string } ?? {};
17
- return { message: `Hello, ${name ?? 'World'}! (POST)` };
17
+ const { name } = this.request.body as { name: string };
18
+ return { message: `Hello, ${name}!` };
18
19
  }
19
20
  }
package/dist/index.js DELETED
@@ -1,73 +0,0 @@
1
- #!/usr/bin/env node
2
- /** create-forkly — Forkly 项目脚手架 CLI */
3
- import { cac } from 'cac';
4
- import * as p from '@clack/prompts';
5
- import { mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';
6
- import { resolve, dirname } from 'node:path';
7
- import { fileURLToPath } from 'node:url';
8
- import { execSync } from 'node:child_process';
9
- const __dirname = dirname(fileURLToPath(import.meta.url));
10
- const templateDir = resolve(__dirname, '../template');
11
- function copyDir(src, dest, replaces) {
12
- mkdirSync(dest, { recursive: true });
13
- for (const entry of readdirSync(src)) {
14
- const srcPath = resolve(src, entry);
15
- const destPath = resolve(dest, entry);
16
- if (statSync(srcPath).isDirectory()) {
17
- copyDir(srcPath, destPath, replaces);
18
- }
19
- else {
20
- let content = readFileSync(srcPath, 'utf-8');
21
- for (const [k, v] of Object.entries(replaces)) {
22
- content = content.replace(new RegExp(k, 'g'), v);
23
- }
24
- writeFileSync(destPath, content);
25
- }
26
- }
27
- }
28
- const cli = cac('create-forkly');
29
- cli.command('[dir]', 'Create a new Forkly project')
30
- .action(async (dir = '.') => {
31
- p.intro('Forkly Project Scaffolder');
32
- const projectName = await p.text({
33
- message: 'Project name:',
34
- placeholder: 'my-forkly-app',
35
- defaultValue: dir === '.' ? 'my-forkly-app' : dir,
36
- });
37
- if (p.isCancel(projectName)) {
38
- p.cancel('Cancelled');
39
- process.exit(0);
40
- }
41
- const useTypeScript = await p.confirm({
42
- message: 'Use TypeScript?',
43
- initialValue: true,
44
- });
45
- if (p.isCancel(useTypeScript)) {
46
- p.cancel('Cancelled');
47
- process.exit(0);
48
- }
49
- const targetDir = resolve(process.cwd(), dir === '.' ? projectName : dir);
50
- const replaces = { 'forkly-app': projectName };
51
- p.log.step('Copying template files...');
52
- copyDir(templateDir, targetDir, replaces);
53
- // 更新 package.json 中的项目名
54
- const pkgPath = resolve(targetDir, 'package.json');
55
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
56
- pkg.name = projectName;
57
- writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
58
- if (!useTypeScript) {
59
- // 如果不用 TS,移除 tsconfig,把 .ts 文件改为 .js
60
- p.log.step('Setting up JavaScript project...');
61
- }
62
- p.log.step('Installing dependencies...');
63
- try {
64
- execSync('npm install', { cwd: targetDir, stdio: 'inherit' });
65
- }
66
- catch {
67
- p.log.warn('npm install failed. You can run it manually.');
68
- }
69
- p.outro(`Done! Run:\n cd ${projectName}\n npm run dev`);
70
- });
71
- cli.help();
72
- cli.version('0.3.0');
73
- cli.parse();
@@ -1,6 +0,0 @@
1
- {
2
- "port": 8080,
3
- "session": { "secret": "production-secret-change-me" },
4
- "errorFormat": "json",
5
- "logger": { "level": "warn" }
6
- }