create-miniprogram-scaffold 1.0.3 → 1.0.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/bin/cli.js +19 -7
- package/package.json +1 -1
- package/templates/server-python/.env.example +1 -0
- package/templates/server-python/Makefile +10 -0
- package/templates/server-python/README.md +29 -0
- package/templates/server-python/main.py +57 -0
- package/templates/server-python/requirements.txt +3 -0
package/bin/cli.js
CHANGED
|
@@ -5,9 +5,10 @@ const fs = require('fs-extra');
|
|
|
5
5
|
const path = require('path');
|
|
6
6
|
const chalk = require('chalk');
|
|
7
7
|
const program = new Command();
|
|
8
|
-
program.name('create-mini-scaffold').description('Create Taro + Go mini program projects').version('1.0.0')
|
|
8
|
+
program.name('create-mini-scaffold').description('Create Taro + Go/Python mini program projects').version('1.0.0')
|
|
9
9
|
.argument('[project-name]').option('-f, --framework <f>', 'Framework', 'React').option('-c, --css <css>', 'CSS preprocessor', 'Sass')
|
|
10
10
|
.option('-l, --layout <l>', 'Layout: header-tabbar | tabbar | fullscreen | all', 'header-tabbar')
|
|
11
|
+
.option('-s, --server <s>', 'Server: go | python', 'go')
|
|
11
12
|
.option('-p, --port <port>', 'Server port', '8080').option('-y, --yes', 'Skip prompts').option('--overwrite')
|
|
12
13
|
.action(async (name, opts) => {
|
|
13
14
|
try {
|
|
@@ -15,7 +16,7 @@ program.name('create-mini-scaffold').description('Create Taro + Go mini program
|
|
|
15
16
|
if (!/^[a-zA-Z0-9-_]+$/.test(name)) { console.error(chalk.red('Invalid name')); process.exit(1); }
|
|
16
17
|
const dir = path.join(process.cwd(), name);
|
|
17
18
|
if (await fs.pathExists(dir)) { if (!opts.overwrite) { if (opts.yes) { console.error(chalk.red('Exists. Use --overwrite.')); process.exit(1); } const { o } = await inquirer.prompt([{ type: 'confirm', name: 'o', message: 'Overwrite?', default: false }]); if (!o) { process.exit(0); } } await fs.remove(dir); }
|
|
18
|
-
const cfg = { framework: opts.framework, css: opts.css, layout: opts.layout, port: opts.port };
|
|
19
|
+
const cfg = { framework: opts.framework, css: opts.css, layout: opts.layout, server: opts.server, port: opts.port };
|
|
19
20
|
if (!opts.yes) { const a = await inquirer.prompt([
|
|
20
21
|
{ type: 'list', name: 'framework', message: 'Framework?', choices: ['React', 'Vue'] },
|
|
21
22
|
{ type: 'list', name: 'css', message: 'CSS?', choices: ['Sass', 'Less', 'Stylus', 'None'] },
|
|
@@ -24,8 +25,14 @@ program.name('create-mini-scaffold').description('Create Taro + Go mini program
|
|
|
24
25
|
{ name: 'tabbar - 原生导航栏 + 原生 TabBar', value: 'tabbar' },
|
|
25
26
|
{ name: 'fullscreen - 全屏沉浸式', value: 'fullscreen' },
|
|
26
27
|
{ name: 'all - 全部布局(推荐)', value: 'all' }
|
|
27
|
-
]},
|
|
28
|
+
]},
|
|
29
|
+
{ type: 'list', name: 'server', message: 'Backend?', choices: [
|
|
30
|
+
{ name: 'Go (Fiber)', value: 'go' },
|
|
31
|
+
{ name: 'Python (FastAPI)', value: 'python' }
|
|
32
|
+
]},
|
|
33
|
+
{ type: 'input', name: 'port', message: 'Port?', default: cfg.port } ]); Object.assign(cfg, a); }
|
|
28
34
|
if (!['header-tabbar','tabbar','fullscreen','all'].includes(cfg.layout)) { console.error(chalk.red('Invalid layout')); process.exit(1); }
|
|
35
|
+
if (!['go','python'].includes(cfg.server)) { console.error(chalk.red('Invalid server')); process.exit(1); }
|
|
29
36
|
console.log(chalk.blue('\nCreating...'));
|
|
30
37
|
const tDir = path.join(__dirname, '..', 'templates');
|
|
31
38
|
const mDir = path.join(dir, 'miniprogram');
|
|
@@ -57,13 +64,18 @@ program.name('create-mini-scaffold').description('Create Taro + Go mini program
|
|
|
57
64
|
const pj = await fs.readJson(path.join(mDir, 'package.json')); pj.name = name + '-miniprogram'; await fs.writeJson(path.join(mDir, 'package.json'), pj, { spaces: 2 });
|
|
58
65
|
const pc = await fs.readJson(path.join(mDir, 'project.config.json')); pc.projectname = name; await fs.writeJson(path.join(mDir, 'project.config.json'), pc, { spaces: 2 });
|
|
59
66
|
// Server
|
|
60
|
-
|
|
61
|
-
|
|
67
|
+
const serverDir = cfg.server === 'python' ? 'server-python' : 'server';
|
|
68
|
+
await fs.copy(path.join(tDir, serverDir), path.join(dir, 'server'));
|
|
69
|
+
if (cfg.server === 'go') {
|
|
70
|
+
let gm = await fs.readFile(path.join(dir, 'server/go.mod'), 'utf8'); await fs.writeFile(path.join(dir, 'server/go.mod'), gm.replace('module mini-scaffold-server', `module ${name}-server`));
|
|
71
|
+
}
|
|
62
72
|
const ep = path.join(dir, 'server/.env.example'); if (await fs.pathExists(ep)) { let e = await fs.readFile(ep, 'utf8'); e = e.replace('PORT=8080', `PORT=${cfg.port}`); await fs.writeFile(path.join(dir, 'server/.env'), e); await fs.writeFile(ep, e); }
|
|
63
73
|
// README
|
|
64
74
|
const nm = { 'header-tabbar': 'HeaderTabBarLayout', 'tabbar': 'TabBarLayout', 'fullscreen': 'FullscreenLayout', 'all': '全部布局' };
|
|
65
|
-
|
|
66
|
-
|
|
75
|
+
const sn = cfg.server === 'python' ? 'Python (FastAPI)' : 'Go (Fiber)';
|
|
76
|
+
const startCmd = cfg.server === 'python' ? 'pip install -r requirements.txt && python main.py' : 'go mod tidy && go run main.go';
|
|
77
|
+
await fs.writeFile(path.join(dir, 'README.md'), `# ${name}\n\nTaro + ${sn} 小程序。\n\nLayout: \`${cfg.layout}\` — ${nm[cfg.layout]}\n\n## Start\n\n\`\`\`bash\ncd miniprogram && npm install && npm run dev:weapp\ncd server && ${startCmd}\n\`\`\`\n`);
|
|
78
|
+
console.log(chalk.green('\n✓ Done!')); console.log(chalk.cyan(` Layout: ${cfg.layout} | Server: ${sn}`)); console.log(chalk.white(`\n cd ${name} && cd miniprogram && npm install && npm run dev:weapp`));
|
|
67
79
|
} catch (e) { console.error(chalk.red('Error:'), e); process.exit(1); }
|
|
68
80
|
});
|
|
69
81
|
program.parse();
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
PORT=8080
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Python FastAPI Server
|
|
2
|
+
|
|
3
|
+
## Quick Start
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install -r requirements.txt
|
|
7
|
+
python main.py
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Or with make:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
make install
|
|
14
|
+
make dev
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Server runs on http://localhost:8080
|
|
18
|
+
|
|
19
|
+
## API Endpoints
|
|
20
|
+
|
|
21
|
+
| Method | Path | Description |
|
|
22
|
+
|--------|------|-------------|
|
|
23
|
+
| GET | /api/health | Health check |
|
|
24
|
+
| GET | /api/hello | Hello World |
|
|
25
|
+
| POST | /api/hello | Hello with name |
|
|
26
|
+
|
|
27
|
+
## Docs
|
|
28
|
+
|
|
29
|
+
FastAPI 自动生成文档:http://localhost:8080/docs
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from fastapi import FastAPI
|
|
2
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
import os
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
|
|
8
|
+
load_dotenv()
|
|
9
|
+
|
|
10
|
+
app = FastAPI(title="Mini Scaffold API")
|
|
11
|
+
|
|
12
|
+
app.add_middleware(
|
|
13
|
+
CORSMiddleware,
|
|
14
|
+
allow_origins=["*"],
|
|
15
|
+
allow_methods=["*"],
|
|
16
|
+
allow_headers=["*"],
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@app.get("/api/health")
|
|
21
|
+
def health():
|
|
22
|
+
return {"code": 0, "message": "OK", "data": {"status": "healthy"}}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@app.get("/api/hello")
|
|
26
|
+
def hello():
|
|
27
|
+
return {
|
|
28
|
+
"code": 0,
|
|
29
|
+
"message": "success",
|
|
30
|
+
"data": {
|
|
31
|
+
"greeting": "Hello, World! 来自 Python 后端",
|
|
32
|
+
"timestamp": datetime.now().isoformat(),
|
|
33
|
+
"version": "1.0.0",
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class HelloRequest(BaseModel):
|
|
39
|
+
name: str = "World"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@app.post("/api/hello")
|
|
43
|
+
def hello_name(req: HelloRequest):
|
|
44
|
+
return {
|
|
45
|
+
"code": 0,
|
|
46
|
+
"message": "success",
|
|
47
|
+
"data": {
|
|
48
|
+
"greeting": f"Hello, {req.name}! 来自 Python 后端",
|
|
49
|
+
"timestamp": datetime.now().isoformat(),
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if __name__ == "__main__":
|
|
55
|
+
import uvicorn
|
|
56
|
+
port = int(os.getenv("PORT", "8080"))
|
|
57
|
+
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)
|