#!/usr/bin/env node
/**
* Script to update Keet identity files to be real identities
* This script is specifically designed to work with the real Keet implementation
* It updates both:
* 1. The main identity file (storage/identity.json)
* 2. The database identity file (storage/db/identity.json)
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { parseArgs } from 'node:util';
// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Configure logging
function log(level, message) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] [${level}] ${message}`);
}
// Parse command-line arguments
function parseArguments() {
const options = {
storage: { type: 'string' },
name: { type: 'string' },
verbose: { type: 'boolean', default: false }
};
const { values } = parseArgs({ options });
return values;
}
// Main function
async function main() {
try {
// Parse command-line arguments
const args = parseArguments();
// Create a storage directory if it doesn't exist
const storagePath = args.storage || path.join(__dirname, '..', 'test-keet-storage');
if (!fs.existsSync(storagePath)) {
fs.mkdirSync(storagePath, { recursive: true });
}
// Create db directory if it doesn't exist
const dbPath = path.join(storagePath, 'db');
if (!fs.existsSync(dbPath)) {
fs.mkdirSync(dbPath, { recursive: true });
}
// Path to the main identity file
const mainIdentityPath = path.join(storagePath, 'identity.json');
// Path to the database identity file
const dbIdentityPath = path.join(dbPath, 'identity.json');
// Check if the main identity file exists
let mainIdentity;
if (fs.existsSync(mainIdentityPath)) {
log('INFO', `Reading main identity from: ${mainIdentityPath}`);
mainIdentity = JSON.parse(fs.readFileSync(mainIdentityPath, 'utf8'));
log('INFO', `Current main identity: ${JSON.stringify(mainIdentity)}`);
} else {
// Create a new identity if it doesn't exist
log('INFO', `Main identity file not found, creating a new one at: ${mainIdentityPath}`);
const deviceId = `device-${Date.now()}`;
mainIdentity = {
anonymous: false,
memberId: deviceId,
timestamp: Date.now(),
displayName: args.name || 'KeetBot',
avatar: null,
devices: [{
deviceId: deviceId,
isMobile: false,
name: null,
confirmed: true
}],
local: {
deviceId: deviceId
}
};
}
// Force anonymous to false
mainIdentity.anonymous = false;
// Update the display name if provided
if (args.name) {
mainIdentity.displayName = args.name;
}
// Make sure the display name is set
if (!mainIdentity.displayName) {
mainIdentity.displayName = 'KeetBot';
}
// Make sure the devices array exists and has at least one device
if (!mainIdentity.devices || !Array.isArray(mainIdentity.devices) || mainIdentity.devices.length === 0) {
mainIdentity.devices = [{
deviceId: mainIdentity.memberId || `device-${Date.now()}`,
isMobile: false,
name: null,
confirmed: true
}];
}
// Make sure the local deviceId is set and matches a device in the devices array
if (!mainIdentity.local || !mainIdentity.local.deviceId) {
mainIdentity.local = {
deviceId: mainIdentity.devices[0].deviceId
};
}
// Make sure the memberId is set
if (!mainIdentity.memberId) {
mainIdentity.memberId = mainIdentity.devices[0].deviceId;
}
// Write the updated main identity back to the file
log('INFO', `Writing updated main identity to: ${mainIdentityPath}`);
fs.writeFileSync(mainIdentityPath, JSON.stringify(mainIdentity, null, 2));
log('INFO', `Updated main identity: ${JSON.stringify(mainIdentity)}`);
// Create or update the database identity file
const updatedDbIdentity = {
id: mainIdentity.memberId,
name: mainIdentity.displayName,
anonymous: false
};
// Write the updated database identity back to the file
log('INFO', `Writing updated database identity to: ${dbIdentityPath}`);
fs.writeFileSync(dbIdentityPath, JSON.stringify(updatedDbIdentity, null, 2));
log('INFO', `Updated database identity: ${JSON.stringify(updatedDbIdentity)}`);
// Create a backup of the identity files in the parent directory
const backupDir = path.join(storagePath, 'backup');
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir, { recursive: true });
}
const backupMainIdentityPath = path.join(backupDir, 'identity.json');
const backupDbIdentityPath = path.join(backupDir, 'db_identity.json');
fs.writeFileSync(backupMainIdentityPath, JSON.stringify(mainIdentity, null, 2));
fs.writeFileSync(backupDbIdentityPath, JSON.stringify(updatedDbIdentity, null, 2));
log('INFO', `Backup identity files created in: ${backupDir}`);
log('INFO', 'Identity updated successfully!');
return 0;
} catch (error) {
log('ERROR', `Error updating identity: ${error.message}`);
log('ERROR', error.stack);
return 1;
}
}
// Run the main function if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
try {
const exitCode = await main();
process.exit(exitCode);
} catch (error) {
log('ERROR', `Unhandled error: ${error.message}`);
log('ERROR', error.stack);
process.exit(1);
}
}