@myassis/gateway 1.0.13 → 1.0.15
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/dist/cli.js +2 -3
- package/dist/config/index.js +16 -8
- package/dist/services/session/SessionStore.js +18 -10
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// ─── CLI 快速入口(在导入任何模块之前处理参数)─────────────────
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import fs from 'fs';
|
|
3
5
|
const args = process.argv.slice(2);
|
|
4
6
|
// 快速响应命令(无需加载任何模块)
|
|
5
7
|
if (args.includes('--version') || args.includes('-v')) {
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const path = require('path');
|
|
8
8
|
try {
|
|
9
9
|
const pkgPath = path.join(__dirname, '..', 'package.json');
|
|
10
10
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
@@ -42,4 +42,3 @@ CORS 管理命令:
|
|
|
42
42
|
}
|
|
43
43
|
// 需要加载模块的命令,导入主程序
|
|
44
44
|
import('./main.js');
|
|
45
|
-
export {};
|
package/dist/config/index.js
CHANGED
|
@@ -3,19 +3,27 @@ const logger = getLogger('GatewayConfig');
|
|
|
3
3
|
import { config as dotenvConfig } from 'dotenv';
|
|
4
4
|
import * as path from 'path';
|
|
5
5
|
import * as fs from 'fs';
|
|
6
|
-
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
/**
|
|
10
|
+
* 获取包安装根目录(npm install -g 后的包目录)
|
|
11
|
+
* dist/config/index.js → 上三级 = 包根目录
|
|
12
|
+
*/
|
|
13
|
+
function getPackageRootDir() {
|
|
14
|
+
return path.resolve(__dirname, '..', '..', '..');
|
|
15
|
+
}
|
|
7
16
|
const getEnvPath = () => {
|
|
8
|
-
//
|
|
9
|
-
const exeDir = path.dirname(process.execPath);
|
|
10
|
-
const exeEnvPath = path.join(exeDir, '.env');
|
|
11
|
-
if (fs.existsSync(exeEnvPath)) {
|
|
12
|
-
return exeEnvPath;
|
|
13
|
-
}
|
|
14
|
-
// 开发环境:从 CWD 读取
|
|
17
|
+
// 1. 用户工作目录(运行时配置优先)
|
|
15
18
|
const cwdEnvPath = path.resolve(process.cwd(), '.env');
|
|
16
19
|
if (fs.existsSync(cwdEnvPath)) {
|
|
17
20
|
return cwdEnvPath;
|
|
18
21
|
}
|
|
22
|
+
// 2. 包安装目录(随包发布的默认 .env)
|
|
23
|
+
const pkgEnvPath = path.join(getPackageRootDir(), 'dist', '.env');
|
|
24
|
+
if (fs.existsSync(pkgEnvPath)) {
|
|
25
|
+
return pkgEnvPath;
|
|
26
|
+
}
|
|
19
27
|
return undefined;
|
|
20
28
|
};
|
|
21
29
|
const envPath = getEnvPath();
|
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
import Database from 'better-sqlite3';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import fs from 'fs';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
4
5
|
import { getLogger } from '@myassis/shared';
|
|
5
6
|
import { MigrationManager } from './MigrationManager.js';
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
6
9
|
const logger = getLogger('SessionStore');
|
|
10
|
+
/**
|
|
11
|
+
* 获取包安装根目录(npm install -g 后的包目录)
|
|
12
|
+
* ESM 下用 import.meta.url 定位,比 process.execPath 可靠
|
|
13
|
+
*/
|
|
14
|
+
function getPackageRootDir() {
|
|
15
|
+
// dist/SessionStore.js → 包根目录
|
|
16
|
+
return path.resolve(__dirname, '..', '..', '..');
|
|
17
|
+
}
|
|
7
18
|
export function getDataDir() {
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
if (fs.existsSync(dataDir)) {
|
|
11
|
-
return dataDir;
|
|
12
|
-
}
|
|
13
|
-
const defaultDataDir = path.join(process.cwd(), 'data');
|
|
19
|
+
// 优先使用用户工作目录下的 data(运行时数据应存放在用户空间)
|
|
20
|
+
const defaultDataDir = path.join(getPackageRootDir(), 'data');
|
|
14
21
|
if (!fs.existsSync(defaultDataDir)) {
|
|
15
22
|
fs.mkdirSync(defaultDataDir, { recursive: true });
|
|
16
23
|
}
|
|
@@ -21,11 +28,12 @@ export function getDbPath() {
|
|
|
21
28
|
return path.join(dataDir, 'sessions.db');
|
|
22
29
|
}
|
|
23
30
|
export function getMigrationsDir() {
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
if (fs.existsSync(
|
|
27
|
-
return
|
|
31
|
+
// migrations 随包发布,从包安装目录读取
|
|
32
|
+
const pkgMigrations = path.join(getPackageRootDir(), 'migrations');
|
|
33
|
+
if (fs.existsSync(pkgMigrations)) {
|
|
34
|
+
return pkgMigrations;
|
|
28
35
|
}
|
|
36
|
+
// 回退到 CWD(开发环境)
|
|
29
37
|
return path.join(process.cwd(), 'migrations');
|
|
30
38
|
}
|
|
31
39
|
/**
|