#!/usr/bin/env node
/**
* Script to update a single Keet identity to be a real identity
* This updates both:
* 1. The main identity file (storage/identity.json)
* 2. The database identity file (storage/db/identity.json)
*
* This script focuses on a single device only, not multi-device setups.
*/
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
}
};
}
// Update the main identity
const updatedMainIdentity = {
...mainIdentity,
anonymous: false // Set to false for a real identity
};
// Update the display name if provided
if (args.name) {
updatedMainIdentity.displayName = args.name;
}
// Write the updated main identity back to the file
log('INFO', `Writing updated main identity to: ${mainIdentityPath}`);
fs.writeFileSync(mainIdentityPath, JSON.stringify(updatedMainIdentity));
log('INFO', `Updated main identity: ${JSON.stringify(updatedMainIdentity)}`);
// Create or update the database identity file
const updatedDbIdentity = {
id: updatedMainIdentity.memberId,
name: updatedMainIdentity.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));
log('INFO', `Updated database identity: ${JSON.stringify(updatedDbIdentity)}`);
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);
}
}