74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
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 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');
|
|
}
|
|
}
|