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 PackageJsonFormatter extends BaseFormatter { get name(): string { return 'packagejson'; } async analyze(): Promise { const changes: IPlannedChange[] = []; const packageJsonPath = 'package.json'; // Check if file exists const exists = await plugins.smartfs.file(packageJsonPath).exists(); if (!exists) { logVerbose('package.json does not exist, skipping'); return changes; } // Read current content const currentContent = (await plugins.smartfs .file(packageJsonPath) .encoding('utf8') .read()) as string; // Parse and compute new content const packageJson = JSON.parse(currentContent); // Get gitzone config from smartconfig const smartconfigInstance = new plugins.smartconfig.Smartconfig(paths.cwd); const gitzoneData: any = smartconfigInstance.dataFor('@git.zone/cli', {}); // Set metadata from gitzone config if (gitzoneData.module) { packageJson.repository = { type: 'git', url: `https://${gitzoneData.module.githost}/${gitzoneData.module.gitscope}/${gitzoneData.module.gitrepo}.git`, }; packageJson.bugs = { url: `https://${gitzoneData.module.githost}/${gitzoneData.module.gitscope}/${gitzoneData.module.gitrepo}/issues`, }; packageJson.homepage = `https://${gitzoneData.module.githost}/${gitzoneData.module.gitscope}/${gitzoneData.module.gitrepo}#readme`; } // Ensure module type if (!packageJson.type) { packageJson.type = 'module'; } // Ensure private field exists if (packageJson.private === undefined) { packageJson.private = true; } // Ensure license field exists if (!packageJson.license) { packageJson.license = 'UNLICENSED'; } // Ensure scripts object exists if (!packageJson.scripts) { packageJson.scripts = {}; } // Ensure build script exists if (!packageJson.scripts.build) { packageJson.scripts.build = `echo "Not needed for now"`; } // Set files array packageJson.files = [ 'ts/**/*', 'ts_web/**/*', 'dist/**/*', 'dist_*/**/*', 'dist_ts/**/*', 'dist_ts_web/**/*', 'assets/**/*', 'cli.js', '.smartconfig.json', 'readme.md', ]; // Set pnpm overrides from assets try { const overridesContent = (await plugins.smartfs .file(plugins.path.join(paths.assetsDir, 'overrides.json')) .encoding('utf8') .read()) as string; const overrides = JSON.parse(overridesContent); packageJson.pnpm = packageJson.pnpm || {}; packageJson.pnpm.overrides = overrides; } catch (error) { logVerbose(`Could not read overrides.json: ${error.message}`); } const newContent = JSON.stringify(packageJson, null, 2); // Only add change if content differs if (newContent !== currentContent) { changes.push({ type: 'modify', path: packageJsonPath, module: this.name, description: 'Format package.json', content: newContent, }); } return changes; } async applyChange(change: IPlannedChange): Promise { if (change.type !== 'modify' || !change.content) return; await this.modifyFile(change.path, change.content); logger.log('info', 'Updated package.json'); } }