forked from lencx/ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
98 lines (83 loc) · 2.72 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { readTextFile, writeTextFile, exists, createDir } from '@tauri-apps/api/fs';
import { homeDir, join, dirname } from '@tauri-apps/api/path';
import dayjs from 'dayjs';
export const APP_CONF_JSON = 'chat.conf.json';
export const CHAT_MODEL_JSON = 'chat.model.json';
export const CHAT_MODEL_CMD_JSON = 'chat.model.cmd.json';
export const CHAT_DOWNLOAD_JSON = 'chat.download.json';
export const CHAT_AWESOME_JSON = 'chat.awesome.json';
export const CHAT_NOTES_JSON = 'chat.notes.json';
export const CHAT_PROMPTS_CSV = 'chat.prompts.csv';
export const GITHUB_PROMPTS_CSV_URL =
'https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv';
export const GITHUB_LOG_URL = 'https://raw.githubusercontent.com/lencx/ChatGPT/main/UPDATE_LOG.md';
export const DISABLE_AUTO_COMPLETE = {
autoCapitalize: 'off',
autoComplete: 'off',
spellCheck: false,
};
export const chatRoot = async () => {
return join(await homeDir(), '.chatgpt');
};
export const chatModelPath = async (): Promise<string> => {
return join(await chatRoot(), CHAT_MODEL_JSON);
};
export const chatPromptsPath = async (): Promise<string> => {
return join(await chatRoot(), CHAT_PROMPTS_CSV);
};
type readJSONOpts = { defaultVal?: Record<string, any>; isRoot?: boolean; isList?: boolean };
export const readJSON = async (path: string, opts: readJSONOpts = {}) => {
const { defaultVal = {}, isRoot = false, isList = false } = opts;
const root = await chatRoot();
let file = path;
if (!isRoot) {
file = await join(root, path);
}
if (!(await exists(file))) {
if ((await dirname(file)) !== root) {
await createDir(await dirname(file), { recursive: true });
}
await writeTextFile(
file,
isList
? '[]'
: JSON.stringify(
{
name: 'ChatGPT',
link: 'https://github.com/lencx/ChatGPT',
...defaultVal,
},
null,
2,
),
);
}
try {
return JSON.parse(await readTextFile(file));
} catch (e) {
return {};
}
};
type writeJSONOpts = { dir?: string; isRoot?: boolean };
export const writeJSON = async (
path: string,
data: Record<string, any>,
opts: writeJSONOpts = {},
) => {
const { isRoot = false } = opts;
const root = await chatRoot();
let file = path;
if (!isRoot) {
file = await join(root, path);
}
if (isRoot && !(await exists(await dirname(file)))) {
await createDir(await dirname(file), { recursive: true });
}
await writeTextFile(file, JSON.stringify(data, null, 2));
};
export const fmtDate = (date: any) => dayjs(date).format('YYYY-MM-DD HH:mm:ss');
export const genCmd = (act: string) =>
act
.replace(/\s+|\/+/g, '_')
.replace(/[^\d\w]/g, '')
.toLocaleLowerCase();