liufumingren bd2363bf00 chore: 初始化仓库(backend / frontend / extension 三块)
- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权;
  已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF),
  开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。
  单测 91 个通过(不需要本机数据库)。
- frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 /
  首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。
- extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。
- 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、
  .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
2026-10-17 08:50:32 +08:00

55 lines
1.7 KiB
JavaScript

'use strict';
/**
* 执行 sql/schema.sql 建表(本机没装 mysql 客户端时用它)
*
* 用法:
* npm run db:init # 执行 sql/schema.sql
* node scripts/init-db.js sql/xxx.sql # 执行指定 SQL 文件
*
* 连接信息来源:环境变量 > config/config.local.js > 内置默认值
*/
const fs = require('fs');
const path = require('path');
const mysql = require('mysql2/promise');
// 数据库连接信息统一在 scripts/db-config.js 里解析
// (环境变量 > config/config.local.js > 默认值)
const resolveDbConfig = require('./db-config');
/** 去掉 -- 行注释,再按分号切分成可逐条执行的语句 */
function splitStatements(sql) {
return sql
.split(/\r?\n/)
.filter((line) => !line.trim().startsWith('--'))
.join('\n')
.split(';')
.map((stmt) => stmt.trim())
.filter(Boolean);
}
async function main() {
const file = process.argv[2] || path.join(__dirname, '..', 'sql', 'schema.sql');
const config = resolveDbConfig();
const statements = splitStatements(fs.readFileSync(file, 'utf8'));
console.log(`[db:init] 连接 ${config.host}:${config.port}/${config.database} as ${config.user}`);
const conn = await mysql.createConnection(config);
try {
for (const stmt of statements) {
console.log(`[db:init] 执行: ${stmt.split('\n')[0].slice(0, 70)} ...`);
await conn.query(stmt);
}
const [tables] = await conn.query('SHOW TABLES');
console.log('[db:init] 完成,当前表: %s', tables.map((t) => Object.values(t)[0]).join(', ') || '(空)');
} finally {
await conn.end();
}
}
main().catch((err) => {
console.error(`[db:init] 失败: ${err.code || ''} ${err.message}`);
process.exit(1);
});