update to smartconfig

This commit is contained in:
2026-03-24 16:10:51 +00:00
parent eda67395fe
commit d0d922e53b
41 changed files with 425 additions and 2091 deletions

View File

@@ -2,11 +2,12 @@ import * as plugins from './mod.plugins.js';
import { FormatContext } from './classes.formatcontext.js';
import type { IPlannedChange, ICheckResult } from './interfaces.format.js';
import { Project } from '../classes.project.js';
import { FormatStats } from './classes.formatstats.js';
export abstract class BaseFormatter {
protected context: FormatContext;
protected project: Project;
protected stats: any; // Will be FormatStats from context
protected stats: FormatStats;
constructor(context: FormatContext, project: Project) {
this.context = context;
@@ -36,9 +37,6 @@ export abstract class BaseFormatter {
}
await this.postExecute();
} catch (error) {
// Don't rollback here - let the FormatPlanner handle it
throw error;
} finally {
this.stats.endModule(this.name, startTime);
}
@@ -53,13 +51,10 @@ export abstract class BaseFormatter {
}
protected async modifyFile(filepath: string, content: string): Promise<void> {
// Validate filepath before writing
if (!filepath || filepath.trim() === '') {
throw new Error(`Invalid empty filepath in modifyFile`);
}
// Ensure we have a proper path with directory component
// If the path has no directory component (e.g., "package.json"), prepend "./"
let normalizedPath = filepath;
if (!plugins.path.parse(filepath).dir) {
normalizedPath = './' + filepath;
@@ -69,44 +64,46 @@ export abstract class BaseFormatter {
}
protected async createFile(filepath: string, content: string): Promise<void> {
await plugins.smartfs.file(filepath).encoding('utf8').write(content);
let normalizedPath = filepath;
if (!plugins.path.parse(filepath).dir) {
normalizedPath = './' + filepath;
}
// Ensure parent directory exists
const dir = plugins.path.dirname(normalizedPath);
if (dir && dir !== '.') {
await plugins.smartfs.directory(dir).recursive().create();
}
await plugins.smartfs.file(normalizedPath).encoding('utf8').write(content);
}
protected async deleteFile(filepath: string): Promise<void> {
await plugins.smartfs.file(filepath).delete();
}
protected async shouldProcessFile(filepath: string): Promise<boolean> {
return true;
}
/**
* Check for diffs without applying changes
* Returns information about what would change
*/
async check(): Promise<ICheckResult> {
const changes = await this.analyze();
const diffs: ICheckResult['diffs'] = [];
for (const change of changes) {
// Skip generic changes that don't have actual content
if (change.path === '<various files>') {
continue;
}
if (change.type === 'modify' || change.type === 'create') {
// Read current content if file exists
let currentContent: string | undefined;
try {
currentContent = await plugins.smartfs.file(change.path).encoding('utf8').read() as string;
} catch {
// File doesn't exist yet
currentContent = undefined;
}
const newContent = change.content;
// Check if there's an actual diff
if (currentContent !== newContent && newContent !== undefined) {
diffs.push({
path: change.path,
@@ -116,7 +113,6 @@ export abstract class BaseFormatter {
});
}
} else if (change.type === 'delete') {
// Check if file exists before marking for deletion
try {
const currentContent = await plugins.smartfs.file(change.path).encoding('utf8').read() as string;
diffs.push({
@@ -137,9 +133,6 @@ export abstract class BaseFormatter {
};
}
/**
* Display a single diff using smartdiff
*/
displayDiff(diff: ICheckResult['diffs'][0]): void {
console.log(`\n--- ${diff.path}`);
if (diff.before && diff.after) {
@@ -150,7 +143,6 @@ export abstract class BaseFormatter {
}));
} else if (diff.after && !diff.before) {
console.log(' (new file)');
// Show first few lines of new content
const lines = diff.after.split('\n').slice(0, 10);
lines.forEach(line => console.log(` + ${line}`));
if (diff.after.split('\n').length > 10) {
@@ -161,9 +153,6 @@ export abstract class BaseFormatter {
}
}
/**
* Display all diffs from a check result
*/
displayAllDiffs(result: ICheckResult): void {
if (!result.hasDiff) {
console.log(' No changes detected');