@fivetu53/soul-chat 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/api.js +262 -0
- package/bin/auth.js +363 -0
- package/bin/config.js +110 -0
- package/bin/index.js +1076 -0
- package/package.json +36 -0
package/bin/config.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
|
|
5
|
+
// Config path: ~/.config/soul-chat/config.json
|
|
6
|
+
const CONFIG_DIR = path.join(os.homedir(), '.config', 'soul-chat');
|
|
7
|
+
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
8
|
+
const AUTH_FILE = path.join(CONFIG_DIR, 'auth.json');
|
|
9
|
+
|
|
10
|
+
// Default config
|
|
11
|
+
const defaultConfig = {
|
|
12
|
+
theme: 'default',
|
|
13
|
+
language: 'zh',
|
|
14
|
+
username: 'User',
|
|
15
|
+
lastVisit: null,
|
|
16
|
+
apiUrl: 'https://soul-chat.jdctools.com.cn', // 生产环境默认地址
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Ensure config directory exists
|
|
20
|
+
function ensureConfigDir() {
|
|
21
|
+
if (!fs.existsSync(CONFIG_DIR)) {
|
|
22
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Load config
|
|
27
|
+
export function loadConfig() {
|
|
28
|
+
try {
|
|
29
|
+
ensureConfigDir();
|
|
30
|
+
if (fs.existsSync(CONFIG_FILE)) {
|
|
31
|
+
const data = fs.readFileSync(CONFIG_FILE, 'utf-8');
|
|
32
|
+
return { ...defaultConfig, ...JSON.parse(data) };
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
console.error('Failed to load config:', err.message);
|
|
36
|
+
}
|
|
37
|
+
return { ...defaultConfig };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Save config
|
|
41
|
+
export function saveConfig(config) {
|
|
42
|
+
try {
|
|
43
|
+
ensureConfigDir();
|
|
44
|
+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
|
|
45
|
+
return true;
|
|
46
|
+
} catch (err) {
|
|
47
|
+
console.error('Failed to save config:', err.message);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Get single config item
|
|
53
|
+
export function getConfig(key) {
|
|
54
|
+
const config = loadConfig();
|
|
55
|
+
return config[key];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Set single config item
|
|
59
|
+
export function setConfig(key, value) {
|
|
60
|
+
const config = loadConfig();
|
|
61
|
+
config[key] = value;
|
|
62
|
+
return saveConfig(config);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Get config file path
|
|
66
|
+
export function getConfigPath() {
|
|
67
|
+
return CONFIG_FILE;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Get config directory
|
|
71
|
+
export function getConfigDir() {
|
|
72
|
+
return CONFIG_DIR;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Auth token management
|
|
76
|
+
export function loadAuth() {
|
|
77
|
+
try {
|
|
78
|
+
ensureConfigDir();
|
|
79
|
+
if (fs.existsSync(AUTH_FILE)) {
|
|
80
|
+
const data = fs.readFileSync(AUTH_FILE, 'utf-8');
|
|
81
|
+
return JSON.parse(data);
|
|
82
|
+
}
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.error('Failed to load auth:', err.message);
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function saveAuth(auth) {
|
|
90
|
+
try {
|
|
91
|
+
ensureConfigDir();
|
|
92
|
+
fs.writeFileSync(AUTH_FILE, JSON.stringify(auth, null, 2));
|
|
93
|
+
return true;
|
|
94
|
+
} catch (err) {
|
|
95
|
+
console.error('Failed to save auth:', err.message);
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function clearAuth() {
|
|
101
|
+
try {
|
|
102
|
+
if (fs.existsSync(AUTH_FILE)) {
|
|
103
|
+
fs.unlinkSync(AUTH_FILE);
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
} catch (err) {
|
|
107
|
+
console.error('Failed to clear auth:', err.message);
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|