chromancer
Version:
A powerful command-line interface for automating Chrome browser using Playwright. Perfect for web scraping, automation, testing, and browser workflows.
163 lines (162 loc) • 6.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowStorage = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("node:fs/promises"));
const path = tslib_1.__importStar(require("node:path"));
const os = tslib_1.__importStar(require("node:os"));
const uuid_1 = require("uuid");
class WorkflowStorage {
storageDir;
indexFile;
constructor(baseDir) {
this.storageDir = baseDir || path.join(os.homedir(), '.chromancer', 'workflows');
this.indexFile = path.join(this.storageDir, 'index.json');
}
async init() {
await fs.mkdir(this.storageDir, { recursive: true });
// Create index file if it doesn't exist
try {
await fs.access(this.indexFile);
}
catch {
await fs.writeFile(this.indexFile, JSON.stringify([], null, 2));
}
}
async save(name, prompt, yaml, description, tags) {
await this.init();
const workflow = {
id: (0, uuid_1.v4)(),
name,
description,
prompt,
yaml,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
tags,
executions: 0,
versions: [{
version: 1,
yaml,
prompt,
createdAt: new Date().toISOString(),
reason: 'Initial version'
}]
};
// Save workflow file
const workflowFile = path.join(this.storageDir, `${workflow.id}.json`);
await fs.writeFile(workflowFile, JSON.stringify(workflow, null, 2));
// Update index
const index = await this.loadIndex();
index.push({
id: workflow.id,
name: workflow.name,
description: workflow.description,
tags: workflow.tags,
createdAt: workflow.createdAt,
updatedAt: workflow.updatedAt,
executions: workflow.executions
});
await fs.writeFile(this.indexFile, JSON.stringify(index, null, 2));
return workflow;
}
async update(id, updates) {
const workflow = await this.load(id);
if (updates.yaml && updates.yaml !== workflow.yaml) {
// Add new version
const newVersion = {
version: (workflow.versions?.length || 0) + 1,
yaml: updates.yaml,
prompt: updates.prompt || workflow.prompt,
createdAt: new Date().toISOString(),
reason: updates.reason || 'Manual update'
};
workflow.versions = workflow.versions || [];
workflow.versions.push(newVersion);
workflow.yaml = updates.yaml;
}
if (updates.prompt)
workflow.prompt = updates.prompt;
if (updates.name)
workflow.name = updates.name;
if (updates.description !== undefined)
workflow.description = updates.description;
if (updates.tags)
workflow.tags = updates.tags;
workflow.updatedAt = new Date().toISOString();
// Save updated workflow
const workflowFile = path.join(this.storageDir, `${id}.json`);
await fs.writeFile(workflowFile, JSON.stringify(workflow, null, 2));
// Update index
const index = await this.loadIndex();
const indexItem = index.find(item => item.id === id);
if (indexItem) {
if (updates.name)
indexItem.name = updates.name;
if (updates.description !== undefined)
indexItem.description = updates.description;
if (updates.tags)
indexItem.tags = updates.tags;
indexItem.updatedAt = workflow.updatedAt;
await fs.writeFile(this.indexFile, JSON.stringify(index, null, 2));
}
return workflow;
}
async load(idOrName) {
await this.init();
// Try to find by ID first
let workflowFile = path.join(this.storageDir, `${idOrName}.json`);
try {
const content = await fs.readFile(workflowFile, 'utf-8');
return JSON.parse(content);
}
catch {
// Try to find by name
const index = await this.loadIndex();
const item = index.find(w => w.name.toLowerCase() === idOrName.toLowerCase());
if (!item) {
throw new Error(`Workflow not found: ${idOrName}`);
}
workflowFile = path.join(this.storageDir, `${item.id}.json`);
const content = await fs.readFile(workflowFile, 'utf-8');
return JSON.parse(content);
}
}
async list(filter) {
await this.init();
const index = await this.loadIndex();
let workflows = index;
if (filter?.tags && filter.tags.length > 0) {
workflows = workflows.filter(w => w.tags && filter.tags.some(tag => w.tags.includes(tag)));
}
return workflows;
}
async delete(idOrName) {
await this.init();
const workflow = await this.load(idOrName);
// Delete workflow file
const workflowFile = path.join(this.storageDir, `${workflow.id}.json`);
await fs.unlink(workflowFile);
// Update index
const index = await this.loadIndex();
const filtered = index.filter(item => item.id !== workflow.id);
await fs.writeFile(this.indexFile, JSON.stringify(filtered, null, 2));
}
async recordExecution(id) {
const workflow = await this.load(id);
workflow.executions = (workflow.executions || 0) + 1;
workflow.lastExecuted = new Date().toISOString();
const workflowFile = path.join(this.storageDir, `${id}.json`);
await fs.writeFile(workflowFile, JSON.stringify(workflow, null, 2));
}
async loadIndex() {
try {
const content = await fs.readFile(this.indexFile, 'utf-8');
return JSON.parse(content);
}
catch {
return [];
}
}
}
exports.WorkflowStorage = WorkflowStorage;