fix(mod_format): move smartconfig file renaming into the formatter orchestrator

This commit is contained in:
2026-03-24 19:59:13 +00:00
parent b257c82bd6
commit bf858c8650
4 changed files with 48 additions and 60 deletions

View File

@@ -16,6 +16,27 @@ import { PrettierFormatter } from './formatters/prettier.formatter.js';
import { ReadmeFormatter } from './formatters/readme.formatter.js';
import { CopyFormatter } from './formatters/copy.formatter.js';
/**
* Rename npmextra.json or smartconfig.json to .smartconfig.json
* before any formatter tries to read config.
*/
async function migrateConfigFile(): Promise<void> {
const target = '.smartconfig.json';
const targetExists = await plugins.smartfs.file(target).exists();
if (targetExists) return;
for (const oldName of ['smartconfig.json', 'npmextra.json']) {
const exists = await plugins.smartfs.file(oldName).exists();
if (exists) {
const content = await plugins.smartfs.file(oldName).encoding('utf8').read() as string;
await plugins.smartfs.file(`./${target}`).encoding('utf8').write(content);
await plugins.smartfs.file(oldName).delete();
logger.log('info', `Migrated ${oldName} to ${target}`);
return;
}
}
}
// Shared formatter class map used by both run() and runFormatter()
const formatterMap: Record<string, new (ctx: FormatContext, proj: Project) => BaseFormatter> = {
cleanup: CleanupFormatter,
@@ -53,6 +74,9 @@ export let run = async (
const shouldWrite = options.write ?? (options.dryRun === false);
// Migrate config file before anything reads it
await migrateConfigFile();
const project = await Project.fromCwd({ requireProjectType: false });
const context = new FormatContext();
const planner = new FormatPlanner();