locker-mcp
Version:
MCP server for file locking and access control for AI code tools
137 lines • 5.23 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.MetadataManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class MetadataManager {
constructor(projectRoot = process.cwd()) {
this.metadataDir = path.join(projectRoot, '.reporepo', 'locker');
this.metadataFile = path.join(this.metadataDir, 'locker.json');
}
ensureMetadataDir() {
if (!fs.existsSync(this.metadataDir)) {
fs.mkdirSync(this.metadataDir, { recursive: true });
}
}
loadMetadata() {
this.ensureMetadataDir();
if (!fs.existsSync(this.metadataFile)) {
return { entries: [] };
}
try {
const content = fs.readFileSync(this.metadataFile, 'utf-8');
const metadata = JSON.parse(content);
return metadata;
}
catch (error) {
throw new Error(`Failed to parse metadata file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
saveMetadata(metadata) {
this.ensureMetadataDir();
try {
const content = JSON.stringify(metadata, null, 2);
fs.writeFileSync(this.metadataFile, content, 'utf-8');
}
catch (error) {
throw new Error(`Failed to save metadata file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
getEntry(filePath) {
const metadata = this.loadMetadata();
const normalizedPath = path.normalize(filePath);
return metadata.entries.find(entry => entry.filePath === normalizedPath) || null;
}
createEntry(filePath, id, context, state) {
const metadata = this.loadMetadata();
const normalizedPath = path.normalize(filePath);
const existingIndex = metadata.entries.findIndex(entry => entry.filePath === normalizedPath);
if (existingIndex !== -1) {
throw new Error(`Entry already exists for file: ${filePath}`);
}
const newEntry = {
filePath: normalizedPath,
id,
state,
context,
history: [{
state,
timestamp: new Date().toISOString()
}]
};
metadata.entries.push(newEntry);
this.saveMetadata(metadata);
return newEntry;
}
updateEntry(filePath, updates) {
const metadata = this.loadMetadata();
const normalizedPath = path.normalize(filePath);
const entryIndex = metadata.entries.findIndex(entry => entry.filePath === normalizedPath);
if (entryIndex === -1) {
throw new Error(`No entry found for file: ${filePath}`);
}
const entry = metadata.entries[entryIndex];
if (updates.state !== undefined && updates.state !== entry.state) {
entry.state = updates.state;
entry.history.push({
state: updates.state,
timestamp: new Date().toISOString()
});
}
if (updates.context !== undefined) {
entry.context = updates.context;
}
this.saveMetadata(metadata);
return entry;
}
removeEntry(filePath) {
const metadata = this.loadMetadata();
const normalizedPath = path.normalize(filePath);
const initialLength = metadata.entries.length;
metadata.entries = metadata.entries.filter(entry => entry.filePath !== normalizedPath);
if (metadata.entries.length < initialLength) {
this.saveMetadata(metadata);
return true;
}
return false;
}
getAllEntries() {
const metadata = this.loadMetadata();
return [...metadata.entries];
}
}
exports.MetadataManager = MetadataManager;
//# sourceMappingURL=metadata.js.map