import { Rule, SchematicContext, Tree, chain, mergeWith, apply, move, template, url, MergeStrategy } from '@angular-devkit/schematics'; import { strings } from '@angular-devkit/core'; import { Schema } from './schema'; export function ngAddComponent(options: Schema): Rule { return (_tree: Tree, context: SchematicContext) => { const validComponents = [ 'button', 'accordion', 'alert', 'badge', 'card', 'dialog', 'input', 'tabs', 'tooltip', 'progress' ]; const componentName = options.name || options.component; if (!componentName || !validComponents.includes(componentName)) { throw new Error( `Invalid component name. Choose from: ${validComponents.join(', ')}` ); } const destinationPath = options.path || 'src/app/components'; const templateSource = apply( url(`./files/${componentName}`), [ template({ ...strings, ...options, name: componentName }), move(`${destinationPath}/${componentName}`) // put inside folder ] ); return chain([ mergeWith(templateSource, MergeStrategy.Overwrite), (tree: Tree) => { context.logger.info( `✅ Added ${componentName} component to ${destinationPath}/${componentName}` ); return tree; } ]); }; }