loccon
Version:
A simple local context storage and management tool with CLI and web interfaces. Store, search, and organize code snippets, notes, and development contexts with sharded JSON storage.
97 lines • 3.66 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.IndexManager = void 0;
const path = __importStar(require("path"));
const fs = __importStar(require("fs-extra"));
class IndexManager {
constructor(storagePath) {
this.storagePath = storagePath;
}
async getIndex() {
const indexPath = path.join(this.storagePath, 'index.json');
return await fs.readJson(indexPath);
}
async saveIndex(index) {
const indexPath = path.join(this.storagePath, 'index.json');
index.lastUpdated = new Date().toISOString();
await fs.writeJson(indexPath, index, { spaces: 2 });
}
async getShardForTag(tag) {
const index = await this.getIndex();
return index.tagToShard[tag] || null;
}
async setTagShard(tag, shardId) {
const index = await this.getIndex();
index.tagToShard[tag] = shardId;
await this.saveIndex(index);
}
async removeTag(tag) {
const index = await this.getIndex();
delete index.tagToShard[tag];
await this.saveIndex(index);
}
async getAllTags() {
const index = await this.getIndex();
return Object.keys(index.tagToShard);
}
async tagExists(tag) {
const index = await this.getIndex();
return tag in index.tagToShard;
}
async rebuildIndex() {
// Rebuild index by scanning all shards
const shardsDir = path.join(this.storagePath, 'shards');
const shardFiles = await fs.readdir(shardsDir);
const newIndex = {
tagToShard: {},
lastUpdated: new Date().toISOString()
};
for (const shardFile of shardFiles) {
if (shardFile.endsWith('.json')) {
const shardPath = path.join(shardsDir, shardFile);
const shardData = await fs.readJson(shardPath);
const shardId = path.basename(shardFile, '.json');
// Add all tags from this shard to the index
for (const tag of Object.keys(shardData.contexts)) {
newIndex.tagToShard[tag] = shardId;
}
}
}
await this.saveIndex(newIndex);
}
}
exports.IndexManager = IndexManager;
//# sourceMappingURL=indexManager.js.map