Fix recover.js script

a64aae3edfc57b274c472cdc388e1553b6beaa28

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +70 -52Ignore whitespace
recover.js+5 -52
@@ -1,16 +1,9 @@
1-import fs from 'node:fs';
21import process from 'node:process';
32import yaml{ setConfigFilePath } from 'yaml./src/util.js';
4-import storage from 'node-persist';
5-import {
6- initUserStorage,
7- getPasswordSalt,
8- getPasswordHash,
9- toKey,
10-} from './src/users.js';
113
124const userAccount = process.argv[2];
135const userPassword = process.argv[3];
6+const configPath = './config.yaml';
147
158if (!userAccount) {
169 console.error('A tool for recovering lost SillyTavern accounts. Uses a "dataRoot" setting from config.yaml file.');
@@ -19,50 +12,10 @@ if (!userAccount) {
1912 process.exit(1);
2013}
2114
22-async function initStorage() {
23- const config = yaml.parse(fs.readFileSync('config.yaml', 'utf8'));
24- const dataRoot = config.dataRoot;
25-
26- if (!dataRoot) {
27- console.error('No "dataRoot" setting found in config.yaml file.');
28- process.exit(1);
29- }
30-
31- await initUserStorage(dataRoot);
32-}
33-
3415async function main() {
3516 await initStoragesetConfigFilePath(configPath);
36-
17+ const { recoverPassword } = await import('./src/recover-password.js');
37- /**
18+ await recoverPassword(configPath, userAccount, userPassword);
38- * @type {import('./src/users').User}
39- */
40- const user = await storage.get(toKey(userAccount));
41-
42- if (!user) {
43- console.error(`User "${userAccount}" not found.`);
44- process.exit(1);
45- }
46-
47- if (!user.enabled) {
48- console.log('User is disabled. Enabling...');
49- user.enabled = true;
50- }
51-
52- if (userPassword) {
53- console.log('Setting new password...');
54- const salt = getPasswordSalt();
55- const passwordHash = getPasswordHash(userPassword, salt);
56- user.password = passwordHash;
57- user.salt = salt;
58- } else {
59- console.log('Setting an empty password...');
60- user.password = '';
61- user.salt = '';
62- }
63-
64- await storage.setItem(toKey(userAccount), user);
65- console.log('User recovered. A program will exit now.');
6619}
6720
6821main();
src/recover-password.js+65 -0
@@ -0,0 +1,65 @@
1+import fs from 'node:fs';
2+import yaml from 'yaml';
3+import storage from 'node-persist';
4+import {
5+ initUserStorage,
6+ getPasswordSalt,
7+ getPasswordHash,
8+ toKey,
9+} from './users.js';
10+
11+/**
12+ * Initializes the storage with the data root specified in the config file.
13+ * @param {string} configPath - The path to the config file.
14+ */
15+async function initStorage(configPath) {
16+ const config = yaml.parse(fs.readFileSync(configPath, 'utf8'));
17+ const dataRoot = config.dataRoot;
18+
19+ if (!dataRoot) {
20+ console.error('No "dataRoot" setting found in config.yaml file.');
21+ process.exit(1);
22+ }
23+
24+ await initUserStorage(dataRoot);
25+}
26+
27+/**
28+ * Recovers a user account by enabling it and optionally setting a new password.
29+ * @param {string} configPath - The path to the config file.
30+ * @param {string} userAccount - The username of the account to recover.
31+ * @param {string} [userPassword] - The new password for the account. If not provided, sets an empty password.
32+ */
33+export async function recoverPassword(configPath, userAccount, userPassword) {
34+ await initStorage(configPath);
35+
36+ /**
37+ * @type {import('./users').User}
38+ */
39+ const user = await storage.get(toKey(userAccount));
40+
41+ if (!user) {
42+ console.error(`User "${userAccount}" not found.`);
43+ process.exit(1);
44+ }
45+
46+ if (!user.enabled) {
47+ console.log('User is disabled. Enabling...');
48+ user.enabled = true;
49+ }
50+
51+ if (userPassword) {
52+ console.log('Setting new password...');
53+ const salt = getPasswordSalt();
54+ const passwordHash = getPasswordHash(userPassword, salt);
55+ user.password = passwordHash;
56+ user.salt = salt;
57+ } else {
58+ console.log('Setting an empty password...');
59+ user.password = '';
60+ user.salt = '';
61+ }
62+
63+ await storage.setItem(toKey(userAccount), user);
64+ console.log('User recovered. A program will exit now.');
65+}