This repository was archived by the owner on Feb 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathlogs.ts
More file actions
185 lines (162 loc) · 4.48 KB
/
logs.ts
File metadata and controls
185 lines (162 loc) · 4.48 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import lm from '@synonymdev/react-native-ldk';
import { Result, err, ok } from '@synonymdev/result';
import RNFS, { copyFile, exists, mkdir, unlink } from 'react-native-fs';
import { zip } from 'react-native-zip-archive';
/**
* Zips up the newest LDK logs and returns base64 of zip file
* @param {number} limit
* @param {boolean} allAccounts
*/
export const zipLogs = async ({
limit = 10,
includeJson = false,
includeBinaries = false,
allAccounts = false,
}: {
limit?: number;
includeJson?: boolean;
includeBinaries?: boolean;
allAccounts?: boolean;
} = {}): Promise<Result<string>> => {
const time = new Date().getTime();
const logFilePrefix = 'bitkit_ldk_logs';
const ldkPath = `${RNFS.DocumentDirectoryPath}/ldk`;
const tempPath = `${RNFS.DocumentDirectoryPath}/bitkit_temp`;
const zipFileName = `${logFilePrefix}_${time}`;
const zipPath = `${tempPath}/${zipFileName}.zip`;
try {
// Create temporary folder
await unlinkIfExists(tempPath);
await mkdir(`${tempPath}/${zipFileName}`);
const accounts = await listLogs({
path: ldkPath,
limit,
includeJson,
includeBinaries,
accountName: allAccounts ? undefined : lm.account.name,
});
// Copy files to temporary folder to be zipped
for (const account of accounts) {
// Make a subfolder for each account
const accountFolder = `${tempPath}/${zipFileName}/${account.id}`;
await mkdir(accountFolder);
// Copy each log file to the account folder
for (const filePath of account.files) {
const fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
await copyFile(filePath, `${accountFolder}/${fileName}`);
}
}
// Zip up files
const result = await zip(tempPath, zipPath);
return ok(result);
} catch (error) {
return err(error);
}
};
/**
* Lists .log files for all LDK accounts sorted by newest first
* @param {string} path
* @param {number} limit
* @param {string} [accountName]
* @param {boolean} [includeJson]
*/
const listLogs = async ({
path,
limit,
accountName,
includeJson = false,
includeBinaries = false,
}: {
path: string;
limit: number;
accountName?: string;
includeJson?: boolean;
includeBinaries?: boolean;
}): Promise<{ id: string; files: string[] }[]> => {
const ldkPathItems = await RNFS.readDir(path);
const filter = accountName ?? 'ldkaccount';
const accounts = ldkPathItems.filter((item) => item.path.includes(filter));
const promises = accounts.map(async (account) => {
const files = await listLogsForAccount(`${account.path}/logs`, limit);
if (includeJson) {
const jsonFiles = await listFilesForAccount({
path: account.path,
filter: ['.json'],
});
files.push(...jsonFiles);
}
if (includeBinaries) {
// Include .bin files from account root
const binFiles = await listFilesForAccount({
path: account.path,
filter: ['.bin'],
});
files.push(...binFiles);
// Include .bin files from channels folder
const channelsBinFiles = await listFilesForAccount({
path: `${account.path}/channels`,
filter: ['.bin'],
});
files.push(...channelsBinFiles);
}
const filePaths = files.map((f) => f.path);
return { id: account.name, files: filePaths };
});
return Promise.all(promises);
};
/**
* Lists .log files for an LDK account sorted by newest first
* @param {string} path
* @param {number} limit
* @returns {Promise<RNFS.ReadDirItem[]>}
*/
const listLogsForAccount = async (
path: string,
limit: number,
): Promise<RNFS.ReadDirItem[]> => {
const files = await listFilesForAccount({ path, filter: ['.log'] });
// Sort by newest
files.sort((a, b) => {
const aTime = (a.mtime ?? new Date()).getTime();
const bTime = (b.mtime ?? new Date()).getTime();
return bTime - aTime;
});
// Limit number of log files
return files.slice(0, limit);
};
/**
* Lists files in a given directory
* @param {string} path
* @param {string[]} [filter]
* @returns {Promise<RNFS.ReadDirItem[]>}
*/
const listFilesForAccount = async ({
path,
filter,
}: {
path: string;
filter?: string[];
}): Promise<RNFS.ReadDirItem[]> => {
if (!(await exists(path))) {
return [];
}
let files = await RNFS.readDir(path);
// Filter files
if (filter) {
const regex = new RegExp(filter.join('|'));
files = files.filter((f) => f.isFile() && f.size > 0 && regex.test(f.name));
}
return files;
};
/**
* Deletes a file or dir and ignores any errors
* @param path
* @returns {Promise<void>}
*/
const unlinkIfExists = async (path: string): Promise<void> => {
try {
if (await exists(path)) {
await unlink(path);
}
} catch (_e) {}
};