65 lines
2.1 KiBLFS
JavaScript
65 lines
2.1 KiBLFS
JavaScript
#!/usr/bin/env node
|
|
|
|
import { readFileSync, writeFileSync, existsSync, copyFileSync } from 'fs';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname, join } from 'path';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
|
|
const TOKEN_PATH = join(__dirname, '../../../secrets/gmail/tokens.json');
|
|
const BACKUP_PATH = join(__dirname, '../../../secrets/gmail/tokens.json.backup');
|
|
|
|
async function migrateTokens() {
|
|
console.log('\n🔄 Gmail Token Migration\n');
|
|
|
|
if (!existsSync(TOKEN_PATH)) {
|
|
console.log('❌ No token.json file found. Nothing to migrate.');
|
|
console.log(' Run: npm run setup -- --account <account-id>');
|
|
return;
|
|
}
|
|
|
|
// Read current token
|
|
const content = readFileSync(TOKEN_PATH, 'utf8');
|
|
const data = JSON.parse(content);
|
|
|
|
// Check if already in new format
|
|
if (data.accounts && data.defaultAccount) {
|
|
console.log('✅ Token file is already in multi-account format.');
|
|
console.log(` Default account: ${data.defaultAccount}`);
|
|
console.log(` Accounts: ${Object.keys(data.accounts).join(', ')}`);
|
|
return;
|
|
}
|
|
|
|
// Backup old token
|
|
console.log('📦 Creating backup: token.json.backup');
|
|
copyFileSync(TOKEN_PATH, BACKUP_PATH);
|
|
|
|
// Migrate to new format
|
|
const migratedConfig = {
|
|
defaultAccount: 'default',
|
|
accounts: {
|
|
default: {
|
|
...data,
|
|
email: data.email || 'unknown@gmail.com'
|
|
}
|
|
}
|
|
};
|
|
|
|
// Save migrated config
|
|
writeFileSync(TOKEN_PATH, JSON.stringify(migratedConfig, null, 2), { mode: 0o600 });
|
|
|
|
console.log('✅ Migration complete!');
|
|
console.log(` Default account: ${migratedConfig.defaultAccount}`);
|
|
console.log(' Your existing authentication is preserved.\n');
|
|
console.log('💡 Next steps:');
|
|
console.log(' - Add second account: npm run setup -- --account work');
|
|
console.log(' - List accounts: node scripts/manage-accounts.js --list');
|
|
console.log(' - Set default: node scripts/manage-accounts.js --set-default <account-id>\n');
|
|
}
|
|
|
|
migrateTokens().catch(error => {
|
|
console.error('❌ Migration failed:', error.message);
|
|
process.exit(1);
|
|
});
|