feat(mod_format): Refactor formatting modules to new BaseFormatter and implement concrete analyze/apply logic
This commit is contained in:
@@ -1,8 +1,73 @@
|
||||
import { LegacyFormatter } from './legacy.formatter.js';
|
||||
import * as formatTsconfig from '../format.tsconfig.js';
|
||||
import { BaseFormatter } from '../classes.baseformatter.js';
|
||||
import type { IPlannedChange } from '../interfaces.format.js';
|
||||
import * as plugins from '../mod.plugins.js';
|
||||
import * as paths from '../../paths.js';
|
||||
import { logger, logVerbose } from '../../gitzone.logging.js';
|
||||
|
||||
export class TsconfigFormatter extends LegacyFormatter {
|
||||
constructor(context: any, project: any) {
|
||||
super(context, project, 'tsconfig', formatTsconfig);
|
||||
export class TsconfigFormatter extends BaseFormatter {
|
||||
get name(): string {
|
||||
return 'tsconfig';
|
||||
}
|
||||
|
||||
async analyze(): Promise<IPlannedChange[]> {
|
||||
const changes: IPlannedChange[] = [];
|
||||
const tsconfigPath = 'tsconfig.json';
|
||||
|
||||
// Check if file exists
|
||||
const exists = await plugins.smartfs.file(tsconfigPath).exists();
|
||||
if (!exists) {
|
||||
logVerbose('tsconfig.json does not exist, skipping');
|
||||
return changes;
|
||||
}
|
||||
|
||||
// Read current content
|
||||
const currentContent = (await plugins.smartfs
|
||||
.file(tsconfigPath)
|
||||
.encoding('utf8')
|
||||
.read()) as string;
|
||||
|
||||
// Parse and compute new content
|
||||
const tsconfigObject = JSON.parse(currentContent);
|
||||
tsconfigObject.compilerOptions = tsconfigObject.compilerOptions || {};
|
||||
tsconfigObject.compilerOptions.baseUrl = '.';
|
||||
tsconfigObject.compilerOptions.paths = {};
|
||||
|
||||
// Get module paths from tspublish
|
||||
try {
|
||||
const tsPublishMod = await import('@git.zone/tspublish');
|
||||
const tsPublishInstance = new tsPublishMod.TsPublish();
|
||||
const publishModules = await tsPublishInstance.getModuleSubDirs(paths.cwd);
|
||||
|
||||
for (const publishModule of Object.keys(publishModules)) {
|
||||
const publishConfig = publishModules[publishModule];
|
||||
tsconfigObject.compilerOptions.paths[`${publishConfig.name}`] = [
|
||||
`./${publishModule}/index.js`,
|
||||
];
|
||||
}
|
||||
} catch (error) {
|
||||
logVerbose(`Could not get tspublish modules: ${error.message}`);
|
||||
}
|
||||
|
||||
const newContent = JSON.stringify(tsconfigObject, null, 2);
|
||||
|
||||
// Only add change if content differs
|
||||
if (newContent !== currentContent) {
|
||||
changes.push({
|
||||
type: 'modify',
|
||||
path: tsconfigPath,
|
||||
module: this.name,
|
||||
description: 'Format tsconfig.json with path mappings',
|
||||
content: newContent,
|
||||
});
|
||||
}
|
||||
|
||||
return changes;
|
||||
}
|
||||
|
||||
async applyChange(change: IPlannedChange): Promise<void> {
|
||||
if (change.type !== 'modify' || !change.content) return;
|
||||
|
||||
await this.modifyFile(change.path, change.content);
|
||||
logger.log('info', 'Updated tsconfig.json');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user