refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
129 lines • 4.33 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.UsageTracker = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
class UsageTracker {
static logUsage(command, args) {
const entry = this.createUsageEntry(command, args);
this.writeLogEntry(entry);
}
static getUsageLog() {
try {
return this.readLogFile();
}
catch {
return [];
}
}
static getUsageCounts() {
const entries = this.getUsageLog();
const counts = {};
for (const entry of entries) {
counts[entry.command] = (counts[entry.command] || 0) + 1;
}
return counts;
}
static clearUsageLog() {
try {
this.removeLogFile();
}
catch (error) {
// eslint-disable-next-line no-console
console.warn('Failed to clear usage log:', error);
}
}
static createUsageEntry(command, args) {
return {
command,
timestamp: new Date().toISOString(),
args: this.filterPrivateArgs(args)
};
}
static writeLogEntry(entry) {
try {
const logLine = JSON.stringify(entry) + '\n';
fs.appendFileSync(this.LOG_FILE, logLine);
}
catch (error) {
// eslint-disable-next-line no-console
console.warn('Failed to write usage log:', error);
}
}
static readLogFile() {
if (!fs.existsSync(this.LOG_FILE)) {
return [];
}
const content = fs.readFileSync(this.LOG_FILE, 'utf8');
return this.parseLogContent(content);
}
static parseLogContent(content) {
const lines = this.getNonEmptyLines(content);
const entries = this.parseLogLines(lines);
return this.filterValidEntries(entries);
}
static getNonEmptyLines(content) {
return content.split('\n').filter(line => line.trim());
}
static parseLogLines(lines) {
return lines.map(line => this.parseSingleLine(line));
}
static parseSingleLine(line) {
try {
return JSON.parse(line);
}
catch (error) {
// eslint-disable-next-line no-console
console.warn('Failed to parse usage log line:', line, error);
return null;
}
}
static filterValidEntries(entries) {
return entries.filter((entry) => entry !== null);
}
static removeLogFile() {
if (fs.existsSync(this.LOG_FILE)) {
fs.unlinkSync(this.LOG_FILE);
}
}
static filterPrivateArgs(args) {
return args.filter(arg => !arg.includes('/') && !path.isAbsolute(arg));
}
}
exports.UsageTracker = UsageTracker;
UsageTracker.LOG_FILE = path.join(os.homedir(), '.refakts-usage.jsonl');
//# sourceMappingURL=usage-tracker.js.map