flashbacker
Version:
Claude Code state management with session continuity and AI personas
139 lines • 5.6 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClaudeSettingsPath = getClaudeSettingsPath;
exports.getFlashbackConfig = getFlashbackConfig;
exports.createDefaultConfig = createDefaultConfig;
exports.updateConfig = updateConfig;
exports.detectProjectDirectory = detectProjectDirectory;
exports.detectCurrentProjectDirectory = detectCurrentProjectDirectory;
exports.getProjectDirectory = getProjectDirectory;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
function getClaudeSettingsPath() {
return path_1.default.join(os_1.default.homedir(), '.claude', 'settings.json');
}
async function getFlashbackConfig(projectDir) {
const configPath = path_1.default.join(projectDir, '.claude', 'flashback', 'config', 'flashback.json');
if (!await fs_extra_1.default.pathExists(configPath)) {
return null;
}
try {
return await fs_extra_1.default.readJson(configPath);
}
catch {
return null;
}
}
async function createDefaultConfig(projectDir) {
const configDir = path_1.default.join(projectDir, '.claude');
await fs_extra_1.default.ensureDir(configDir);
const config = {
claude_settings_path: getClaudeSettingsPath(),
state_directory: path_1.default.join(projectDir, '.claude', 'flashback', 'state'),
archive_after_days: 30,
default_persona: undefined,
auto_archive: true,
};
const configPath = path_1.default.join(configDir, 'config', 'flashback.json');
await fs_extra_1.default.writeJson(configPath, config, { spaces: 2 });
return config;
}
async function updateConfig(projectDir, updates) {
const current = await getFlashbackConfig(projectDir) || await createDefaultConfig(projectDir);
const updated = { ...current, ...updates };
const configPath = path_1.default.join(projectDir, '.claude', 'flashback', 'config', 'flashback.json');
await fs_extra_1.default.writeJson(configPath, updated, { spaces: 2 });
}
/**
* Automatically detect the project directory by looking for .claude/flashback/config/flashback.json
* starting from the current directory and walking up the directory tree.
*
* @param startDir - Directory to start search from (defaults to process.cwd())
* @returns Promise<string | null> - Project directory path or null if not found
*/
async function detectProjectDirectory(startDir) {
const start = startDir || process.cwd();
let currentDir = path_1.default.resolve(start);
// Walk up the directory tree
// eslint-disable-next-line no-constant-condition
while (true) {
const configPath = path_1.default.join(currentDir, '.claude', 'flashback', 'config', 'flashback.json');
if (await fs_extra_1.default.pathExists(configPath)) {
return currentDir;
}
const parentDir = path_1.default.dirname(currentDir);
// If we've reached the root directory, stop searching
if (parentDir === currentDir) {
break;
}
currentDir = parentDir;
}
return null;
}
/**
* Detect any project directory by looking for common project indicators
* starting from the current directory and walking up the directory tree.
*
* @param startDir - Directory to start search from (defaults to process.cwd())
* @returns Promise<string> - Project directory path (defaults to current working directory)
*/
async function detectCurrentProjectDirectory(startDir) {
const start = startDir || process.cwd();
let currentDir = path_1.default.resolve(start);
// Common project root indicators
const projectIndicators = [
'package.json',
'.git',
'pyproject.toml',
'Cargo.toml',
'composer.json',
'pom.xml',
'build.gradle',
'go.mod',
'requirements.txt',
'Makefile',
'tsconfig.json',
];
// Walk up the directory tree
// eslint-disable-next-line no-constant-condition
while (true) {
// Check for any project indicators
for (const indicator of projectIndicators) {
const indicatorPath = path_1.default.join(currentDir, indicator);
if (await fs_extra_1.default.pathExists(indicatorPath)) {
return currentDir;
}
}
const parentDir = path_1.default.dirname(currentDir);
// If we've reached the root directory, return current working directory
if (parentDir === currentDir) {
return process.cwd();
}
currentDir = parentDir;
}
}
/**
* Get project directory, using automatic detection if not provided
*
* @param explicitDir - Explicitly provided project directory
* @returns Promise<string> - Project directory path
* @throws Error if no project directory found
*/
async function getProjectDirectory(explicitDir) {
if (explicitDir) {
return path_1.default.resolve(explicitDir);
}
const detected = await detectProjectDirectory();
if (!detected) {
throw new Error('No Flashback project found. Either:\n' +
'1. Run this command from within a Flashback-initialized project directory\n' +
'2. Specify the project directory with --project <dir>\n' +
'3. Initialize Flashback in the current directory with: flashback init');
}
return detected;
}
//# sourceMappingURL=config.js.map
;