@iyulab/oops
Version:
Core SDK for Oops - Safe text file editing with automatic backup
158 lines • 5.96 kB
JavaScript
;
/**
* Workspace management for Oops
*/
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.WorkspaceManager = void 0;
const path = __importStar(require("path"));
const file_system_1 = require("./file-system");
const errors_1 = require("./errors");
class WorkspaceManager {
workspacePath;
constructor(workspacePath) {
this.workspacePath = workspacePath || this.resolveWorkspacePath();
}
resolveWorkspacePath() {
// Check environment variable first (for testing and explicit overrides)
if (process.env.OOPS_WORKSPACE) {
return process.env.OOPS_WORKSPACE;
}
// Default: use .oops in current directory
return path.join(process.cwd(), '.oops');
}
getWorkspacePath() {
return this.workspacePath;
}
async init() {
// Create workspace directory structure
await file_system_1.FileSystem.mkdir(this.workspacePath);
await file_system_1.FileSystem.mkdir(path.join(this.workspacePath, 'files'));
// Create initial config
const configPath = path.join(this.workspacePath, 'config.json');
const config = {
version: '0.1.0',
createdAt: new Date().toISOString(),
type: 'local',
};
await file_system_1.FileSystem.writeFile(configPath, JSON.stringify(config, null, 2));
// Create initial state
const statePath = path.join(this.workspacePath, 'state.json');
const state = {
trackedFiles: [],
lastModified: new Date().toISOString(),
};
await file_system_1.FileSystem.writeFile(statePath, JSON.stringify(state, null, 2));
}
async exists() {
return await file_system_1.FileSystem.exists(this.workspacePath);
}
async isHealthy() {
if (!(await this.exists())) {
return false;
}
// Check for required files
const requiredFiles = ['config.json', 'state.json'];
for (const file of requiredFiles) {
const filePath = path.join(this.workspacePath, file);
if (!(await file_system_1.FileSystem.exists(filePath))) {
return false;
}
}
return true;
}
async getInfo() {
if (!(await this.exists())) {
throw new errors_1.WorkspaceNotFoundError(this.workspacePath);
}
if (!(await this.isHealthy())) {
throw new errors_1.WorkspaceCorruptedError(this.workspacePath);
}
// Read state to get tracked files
const statePath = path.join(this.workspacePath, 'state.json');
let trackedFiles = [];
let createdAt = new Date();
try {
const stateContent = await file_system_1.FileSystem.readFile(statePath);
const state = JSON.parse(stateContent);
trackedFiles = state.trackedFiles || [];
}
catch {
// If state can't be read, return empty tracked files
}
// Read config for creation date
const configPath = path.join(this.workspacePath, 'config.json');
try {
const configContent = await file_system_1.FileSystem.readFile(configPath);
const config = JSON.parse(configContent);
if (config.createdAt) {
createdAt = new Date(config.createdAt);
}
}
catch {
// If config can't be read, use current date
}
return {
path: this.workspacePath,
type: 'local',
exists: true,
isHealthy: true,
trackedFiles: trackedFiles,
createdAt: createdAt,
};
}
async clean() {
if (await this.exists()) {
// Remove all files in the workspace
const filesDir = path.join(this.workspacePath, 'files');
if (await file_system_1.FileSystem.exists(filesDir)) {
await file_system_1.FileSystem.remove(filesDir);
await file_system_1.FileSystem.mkdir(filesDir);
}
// Reset state
const statePath = path.join(this.workspacePath, 'state.json');
const state = {
trackedFiles: [],
lastModified: new Date().toISOString(),
};
await file_system_1.FileSystem.writeFile(statePath, JSON.stringify(state, null, 2));
}
}
async repair() {
// TODO: Implement workspace repair
}
}
exports.WorkspaceManager = WorkspaceManager;
//# sourceMappingURL=workspace.js.map