can-algorithm
Version:
Cortex Algorithm Numeral - Intelligent development automation tool
176 lines (155 loc) • 6.2 kB
JavaScript
import { promises as fs } from 'fs';
import path from 'path';
import inquirer from 'inquirer';
import { encrypt, decrypt } from '../lib/encryption.js';
import { activateLicense } from './license.js';
import { setupProtection } from './protection.js';
import chalk from 'chalk';
import crypto from 'crypto';
export async function generateConfig() {
console.log(chalk.cyan('Can Algorithm Configuration'));
console.log(chalk.gray('This will create your config-can.cfg file\n'));
const answers = await inquirer.prompt([
{
type: 'input',
name: 'license_key',
message: 'Enter your license key (CAN-XXXX-XXXX-XXXX):',
validate: (input) => {
if (/^CAN-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}$/.test(input)) {
return true;
}
return 'Invalid license format. Must be CAN-XXXX-XXXX-XXXX';
}
},
{
type: 'password',
name: 'password',
message: 'Create protection password (12 characters):',
validate: (input) => {
if (input.length === 12) {
return true;
}
return 'Password must be exactly 12 characters';
}
},
{
type: 'list',
name: 'language',
message: 'Select your preferred language:',
choices: ['English', 'Français', 'Deutsch', 'Русский', 'Türkçe'],
default: 'English'
},
{
type: 'number',
name: 'scan_depth',
message: 'Maximum scan depth for project analysis:',
default: 10
},
{
type: 'input',
name: 'exclude_patterns',
message: 'Folders to exclude (comma-separated):',
default: 'node_modules,dist,.git,vendor,cache'
},
{
type: 'confirm',
name: 'enable_custom_commands',
message: 'Enable custom commands?',
default: true
}
]);
try {
const activation = await activateLicense(answers.license_key, answers.password);
if (!activation.success) {
console.error(chalk.red('License activation failed'));
return;
}
const config = {
authentication: {
license_key: answers.license_key,
user_id: activation.user_id,
password_hash: crypto.createHash('sha256').update(answers.password).digest('hex')
},
backend: {
cortex_endpoint: 'http://localhost:3001/cortex',
esp_endpoint: 'http://localhost:3002/esp',
timeout: 30000
},
project: {
scan_depth: answers.scan_depth,
exclude_patterns: answers.exclude_patterns.split(',').map(p => p.trim()),
include_extensions: ['.js', '.php', '.html', '.css', '.py', '.java', '.cpp']
},
preferences: {
language: answers.language.toLowerCase(),
notifications: true,
auto_update: true
},
commands: {
custom_commands_enabled: answers.enable_custom_commands,
custom_commands: {}
},
security: {
encryption_level: 256,
secure_mode: true,
data_retention: false
}
};
const canDir = path.join(process.cwd(), '.can');
await fs.mkdir(canDir, { recursive: true, mode: 0o700 });
const encryptedConfig = encrypt(JSON.stringify(config, null, 2));
await fs.writeFile(
path.join(canDir, '.config.encrypted'),
encryptedConfig,
{ mode: 0o600 }
);
const readableConfig = generateReadableConfig(config);
await fs.writeFile('config-can.cfg', readableConfig);
await setupProtection();
console.log(chalk.green('\n✓ Configuration saved successfully'));
console.log(chalk.green('✓ Protection system activated'));
console.log(chalk.yellow('\nYou can now run: npm install can-algorithm'));
} catch (error) {
console.error(chalk.red('Configuration failed:'), error.message);
}
}
function generateReadableConfig(config) {
return `[authentication]
license_key=${config.authentication.license_key}
user_id=${config.authentication.user_id}
[backend]
cortex_endpoint=${config.backend.cortex_endpoint}
esp_endpoint=${config.backend.esp_endpoint}
timeout=${config.backend.timeout}
[project]
scan_depth=${config.project.scan_depth}
exclude_patterns=${config.project.exclude_patterns.join(',')}
include_extensions=${config.project.include_extensions.join(',')}
[preferences]
language=${config.preferences.language}
notifications=${config.preferences.notifications}
auto_update=${config.preferences.auto_update}
[commands]
custom_commands_enabled=${config.commands.custom_commands_enabled}
[security]
encryption_level=${config.security.encryption_level}
secure_mode=${config.security.secure_mode}
data_retention=${config.security.data_retention}
# This file is for reference only.
# Actual configuration is stored encrypted in .can/.config.encrypted`;
}
export async function loadConfig() {
const configPath = path.join(process.cwd(), '.can', '.config.encrypted');
const encryptedData = await fs.readFile(configPath, 'utf8');
return JSON.parse(decrypt(encryptedData));
}
export async function updateConfig(updates) {
const config = await loadConfig();
const updatedConfig = { ...config, ...updates };
const encryptedConfig = encrypt(JSON.stringify(updatedConfig, null, 2));
await fs.writeFile(
path.join(process.cwd(), '.can', '.config.encrypted'),
encryptedConfig,
{ mode: 0o600 }
);
}