39 lines
2.5 KiB
TypeScript
39 lines
2.5 KiB
TypeScript
import { join } from 'path';
|
|
import * as fs from 'fs-extra';
|
|
import {
|
|
loadManifest,
|
|
writeManifest,
|
|
findManifestRoot,
|
|
} from '../manifest/manifest-store';
|
|
import { Command } from 'commander';
|
|
import { logger } from '../lib/logger';
|
|
import { scanProject } from '../manifest/manifest-scanner';
|
|
|
|
export function registerProjectCommand(program: Command): void {
|
|
const project = program.command('project').description('Inspect and initialize xFramework projects');
|
|
project.command('init').description('Create xFramework.json by scanning the current project').option('--force', 'Overwrite existing manifest').action(async (options) => {
|
|
const root = process.cwd(); const file = join(root, 'xFramework.json');
|
|
const hasProjectFiles = await fs.pathExists(join(root, 'package.json')) || await fs.pathExists(join(root, 'angular.json'));
|
|
if (!hasProjectFiles) throw new Error('The current directory is not an xFramework project root. package.json or angular.json is required.');
|
|
if (await fs.pathExists(file) && !options.force) throw new Error('xFramework.json already exists. Use --force to overwrite it.');
|
|
const pkg = await fs.readJson(join(root, 'package.json')).catch(() => ({}));
|
|
const manifest = scanProject(root, pkg.name || root.split(/[\\/]/).pop() || 'xFramework-project');
|
|
await writeManifest(root, manifest); logger.success(`Manifest created: ${file}`);
|
|
});
|
|
project.command('inspect').description('Show the project manifest summary').action(async () => {
|
|
const root = await requiredRoot(); const manifest = await loadManifest(root);
|
|
console.log(`Project: ${manifest.project.name}`); console.log(`Manifest: ${join(root, 'xFramework.json')}`);
|
|
Object.keys(manifest.components).forEach(key => console.log(`${key}: ${manifest.components[key].length}`));
|
|
});
|
|
project.command('validate').description('Validate xFramework.json and registered files').action(async () => {
|
|
const root = await requiredRoot(); const manifest = await loadManifest(root); const missing: string[] = [];
|
|
Object.values(manifest.components).flat().forEach(component => component.files.forEach(file => { if (!fs.pathExistsSync(join(root, file.path))) missing.push(file.path); }));
|
|
if (missing.length) throw new Error(`Manifest references missing files:\n${missing.join('\n')}`);
|
|
logger.success('xFramework project is valid');
|
|
});
|
|
}
|
|
|
|
async function requiredRoot(): Promise<string> {
|
|
const root = await findManifestRoot(process.cwd()); if (!root) throw new Error('Not an xFramework project. Required file xFramework.json was not found.'); return root;
|
|
}
|