create-mabjs 1.0.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/bin/create-mab.js +58 -0
- package/lib/scaffold.js +33 -0
- package/lib/templates.js +110 -0
- package/package.json +13 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { execSync } = require('child_process');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const scaffold = require('../lib/scaffold');
|
|
7
|
+
|
|
8
|
+
const args = process.argv.slice(2);
|
|
9
|
+
|
|
10
|
+
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
|
|
11
|
+
console.log(`
|
|
12
|
+
Usage: create-mabjs <project-name> [options]
|
|
13
|
+
|
|
14
|
+
Options:
|
|
15
|
+
--skip-install Skip running npm install after scaffolding
|
|
16
|
+
--help, -h Show this help message
|
|
17
|
+
`);
|
|
18
|
+
process.exit(0);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const projectName = args[0];
|
|
22
|
+
const skipInstall = args.includes('--skip-install');
|
|
23
|
+
const targetDir = path.resolve(process.cwd(), projectName);
|
|
24
|
+
|
|
25
|
+
// 检查目录是否已存在
|
|
26
|
+
if (fs.existsSync(targetDir)) {
|
|
27
|
+
console.error(`Error: Directory "${projectName}" already exists.`);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 创建项目骨架
|
|
32
|
+
console.log(`Creating a new mabjs project in ${targetDir}...`);
|
|
33
|
+
scaffold.createProject(targetDir);
|
|
34
|
+
|
|
35
|
+
// 安装依赖
|
|
36
|
+
if (!skipInstall) {
|
|
37
|
+
console.log('Installing dependencies...');
|
|
38
|
+
try {
|
|
39
|
+
execSync('npm install', { cwd: targetDir, stdio: 'inherit' });
|
|
40
|
+
console.log('Dependencies installed successfully.');
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.error('Failed to install dependencies. Please run `npm install` manually.');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
console.log(`
|
|
47
|
+
✨ Project created successfully!
|
|
48
|
+
|
|
49
|
+
cd ${projectName}
|
|
50
|
+
npm run dev
|
|
51
|
+
|
|
52
|
+
To start development server:
|
|
53
|
+
npm run dev
|
|
54
|
+
|
|
55
|
+
Or if you prefer using the global command:
|
|
56
|
+
npm install -g mab-cli
|
|
57
|
+
mab dev
|
|
58
|
+
`);
|
package/lib/scaffold.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const templates = require('./templates');
|
|
4
|
+
|
|
5
|
+
function createProject(targetDir) {
|
|
6
|
+
|
|
7
|
+
// 创建目录
|
|
8
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
9
|
+
fs.mkdirSync(path.join(targetDir, 'routes'), { recursive: true });
|
|
10
|
+
fs.mkdirSync(path.join(targetDir, 'cloudfunctions'), { recursive: true });
|
|
11
|
+
fs.mkdirSync(path.join(targetDir, 'uploads'), { recursive: true });
|
|
12
|
+
fs.mkdirSync(path.join(targetDir, 'logs'), { recursive: true });
|
|
13
|
+
const projectName = path.basename(targetDir);
|
|
14
|
+
const pkgContent = templates.packageJson.replace('PROJECT_NAME_PLACEHOLDER', projectName);
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
// 生成文件
|
|
18
|
+
writeFile(targetDir, 'package.json', pkgContent);
|
|
19
|
+
writeFile(targetDir, '.env.example', templates.envExample);
|
|
20
|
+
writeFile(targetDir, 'mab.config.js', templates.mabConfig);
|
|
21
|
+
writeFile(targetDir, '.gitignore', templates.gitignore);
|
|
22
|
+
writeFile(targetDir, 'app.js', templates.appJs);
|
|
23
|
+
writeFile(targetDir, 'routes/hello.js', templates.helloRoute);
|
|
24
|
+
writeFile(targetDir, 'cloudfunctions/.gitkeep', '');
|
|
25
|
+
writeFile(targetDir, 'uploads/.gitkeep', '');
|
|
26
|
+
writeFile(targetDir, 'logs/.gitkeep', '');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function writeFile(baseDir, filename, content) {
|
|
30
|
+
fs.writeFileSync(path.join(baseDir, filename), content.trim() + '\n');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = { createProject };
|
package/lib/templates.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// package.json 模板,依赖 mab-core
|
|
2
|
+
const packageJson = `
|
|
3
|
+
{
|
|
4
|
+
"name": "PROJECT_NAME_PLACEHOLDER",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"private": true,
|
|
7
|
+
"description": "A mabjs backend project",
|
|
8
|
+
"main": "app.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node app.js",
|
|
11
|
+
"dev": "mab dev"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"mab-core": "^1.0.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"mab-cli": "^1.0.0"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
`.trim();
|
|
21
|
+
|
|
22
|
+
// .env.example
|
|
23
|
+
const envExample = `
|
|
24
|
+
# 微信小程序配置
|
|
25
|
+
WX_APPID=your_wechat_appid
|
|
26
|
+
WX_SECRET=your_wechat_secret
|
|
27
|
+
|
|
28
|
+
# JWT 密钥(生产环境务必更换)
|
|
29
|
+
JWT_SECRET=change_me_to_a_random_string_at_least_32_chars
|
|
30
|
+
JWT_EXPIRES_IN=7d
|
|
31
|
+
|
|
32
|
+
# 服务端口
|
|
33
|
+
PORT=3000
|
|
34
|
+
`.trim();
|
|
35
|
+
|
|
36
|
+
// mab.config.js 完整配置模板
|
|
37
|
+
const mabConfig = `
|
|
38
|
+
require('dotenv').config();
|
|
39
|
+
module.exports = {
|
|
40
|
+
port: process.env.PORT || 3000,
|
|
41
|
+
jwtSecret: process.env.JWT_SECRET,
|
|
42
|
+
jwtExpiresIn: process.env.JWT_EXPIRES_IN || '7d',
|
|
43
|
+
wxAppId: process.env.WX_APPID,
|
|
44
|
+
wxSecret: process.env.WX_SECRET,
|
|
45
|
+
database: {
|
|
46
|
+
type: 'sqlite',
|
|
47
|
+
path: './data.db',
|
|
48
|
+
},
|
|
49
|
+
storage: {
|
|
50
|
+
provider: 'local',
|
|
51
|
+
localDir: 'uploads',
|
|
52
|
+
maxFileSize: 5 * 1024 * 1024, // 5MB
|
|
53
|
+
allowedTypes: /\.(jpg|jpeg|png|gif|bmp|pdf|docx|xlsx)\$/i,
|
|
54
|
+
},
|
|
55
|
+
plugins: [],
|
|
56
|
+
functionsDir: 'cloudfunctions',
|
|
57
|
+
cors: { origin: '*' },
|
|
58
|
+
helmet: true,
|
|
59
|
+
rateLimit: {
|
|
60
|
+
windowMs: 15 * 60 * 1000,
|
|
61
|
+
max: 1000,
|
|
62
|
+
},
|
|
63
|
+
bodyLimit: '1mb',
|
|
64
|
+
logging: {
|
|
65
|
+
accessLog: true,
|
|
66
|
+
logDir: 'logs',
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
`.trim();
|
|
70
|
+
|
|
71
|
+
const gitignore = `
|
|
72
|
+
node_modules/
|
|
73
|
+
.env
|
|
74
|
+
*.db
|
|
75
|
+
logs/
|
|
76
|
+
uploads/*
|
|
77
|
+
!uploads/.gitkeep
|
|
78
|
+
`.trim();
|
|
79
|
+
|
|
80
|
+
// 入口 app.js
|
|
81
|
+
const appJs = `
|
|
82
|
+
const { createServer } = require('mab-core');
|
|
83
|
+
const app = createServer(require('./mab.config.js'));
|
|
84
|
+
const port = process.env.PORT || 3000;
|
|
85
|
+
|
|
86
|
+
app.listen(port, () => {
|
|
87
|
+
console.log(\`Mab server running on http://localhost:\${port}\`);
|
|
88
|
+
});
|
|
89
|
+
`.trim();
|
|
90
|
+
|
|
91
|
+
// 示例路由
|
|
92
|
+
const helloRoute = `
|
|
93
|
+
const express = require('express');
|
|
94
|
+
const router = express.Router();
|
|
95
|
+
|
|
96
|
+
router.get('/', (req, res) => {
|
|
97
|
+
res.json({ message: 'Hello from mabjs!' });
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
module.exports = router;
|
|
101
|
+
`.trim();
|
|
102
|
+
|
|
103
|
+
module.exports = {
|
|
104
|
+
packageJson,
|
|
105
|
+
envExample,
|
|
106
|
+
mabConfig,
|
|
107
|
+
gitignore,
|
|
108
|
+
appJs,
|
|
109
|
+
helloRoute,
|
|
110
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-mabjs",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Scaffold a new mabjs project for WeChat Mini Program backend",
|
|
5
|
+
"bin": {
|
|
6
|
+
"create-mab": "./bin/create-mab.js"
|
|
7
|
+
},
|
|
8
|
+
"main": "./bin/create-mab.js",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=16.0.0"
|
|
12
|
+
}
|
|
13
|
+
}
|