From a64aae3edfc57b274c472cdc388e1553b6beaa28 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 17 Jul 2025 13:49:15 +0000 Subject: [PATCH] Fix recover.js script --- recover.js | 57 ++++-------------------------------- src/recover-password.js | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 52 deletions(-) create mode 100644 src/recover-password.js diff --git a/recover.js b/recover.js index a017bf9a3..e5ad124a7 100644 --- a/recover.js +++ b/recover.js @@ -1,16 +1,9 @@ -import fs from 'node:fs'; import process from 'node:process'; -import yaml from 'yaml'; -import storage from 'node-persist'; -import { - initUserStorage, - getPasswordSalt, - getPasswordHash, - toKey, -} from './src/users.js'; +import { setConfigFilePath } from './src/util.js'; const userAccount = process.argv[2]; const userPassword = process.argv[3]; +const configPath = './config.yaml'; if (!userAccount) { console.error('A tool for recovering lost SillyTavern accounts. Uses a "dataRoot" setting from config.yaml file.'); @@ -19,50 +12,10 @@ if (!userAccount) { process.exit(1); } -async function initStorage() { - const config = yaml.parse(fs.readFileSync('config.yaml', 'utf8')); - const dataRoot = config.dataRoot; - - if (!dataRoot) { - console.error('No "dataRoot" setting found in config.yaml file.'); - process.exit(1); - } - - await initUserStorage(dataRoot); -} - async function main() { - await initStorage(); - - /** - * @type {import('./src/users').User} - */ - const user = await storage.get(toKey(userAccount)); - - if (!user) { - console.error(`User "${userAccount}" not found.`); - process.exit(1); - } - - if (!user.enabled) { - console.log('User is disabled. Enabling...'); - user.enabled = true; - } - - if (userPassword) { - console.log('Setting new password...'); - const salt = getPasswordSalt(); - const passwordHash = getPasswordHash(userPassword, salt); - user.password = passwordHash; - user.salt = salt; - } else { - console.log('Setting an empty password...'); - user.password = ''; - user.salt = ''; - } - - await storage.setItem(toKey(userAccount), user); - console.log('User recovered. A program will exit now.'); + setConfigFilePath(configPath); + const { recoverPassword } = await import('./src/recover-password.js'); + await recoverPassword(configPath, userAccount, userPassword); } main(); diff --git a/src/recover-password.js b/src/recover-password.js new file mode 100644 index 000000000..8945e60df --- /dev/null +++ b/src/recover-password.js @@ -0,0 +1,65 @@ +import fs from 'node:fs'; +import yaml from 'yaml'; +import storage from 'node-persist'; +import { + initUserStorage, + getPasswordSalt, + getPasswordHash, + toKey, +} from './users.js'; + +/** + * Initializes the storage with the data root specified in the config file. + * @param {string} configPath - The path to the config file. + */ +async function initStorage(configPath) { + const config = yaml.parse(fs.readFileSync(configPath, 'utf8')); + const dataRoot = config.dataRoot; + + if (!dataRoot) { + console.error('No "dataRoot" setting found in config.yaml file.'); + process.exit(1); + } + + await initUserStorage(dataRoot); +} + +/** + * Recovers a user account by enabling it and optionally setting a new password. + * @param {string} configPath - The path to the config file. + * @param {string} userAccount - The username of the account to recover. + * @param {string} [userPassword] - The new password for the account. If not provided, sets an empty password. + */ +export async function recoverPassword(configPath, userAccount, userPassword) { + await initStorage(configPath); + + /** + * @type {import('./users').User} + */ + const user = await storage.get(toKey(userAccount)); + + if (!user) { + console.error(`User "${userAccount}" not found.`); + process.exit(1); + } + + if (!user.enabled) { + console.log('User is disabled. Enabling...'); + user.enabled = true; + } + + if (userPassword) { + console.log('Setting new password...'); + const salt = getPasswordSalt(); + const passwordHash = getPasswordHash(userPassword, salt); + user.password = passwordHash; + user.salt = salt; + } else { + console.log('Setting an empty password...'); + user.password = ''; + user.salt = ''; + } + + await storage.setItem(toKey(userAccount), user); + console.log('User recovered. A program will exit now.'); +}