from django.core.management.base import BaseCommand, CommandError from django.conf import settings import os class Command(BaseCommand): help = 'Creates a new component' def add_arguments(self, parser): parser.add_argument('name', type=str, help='The name of the component to create') def handle(self, *args, **kwargs): name = kwargs['name'] base_dir = settings.BASE_DIR components_path = os.path.join(base_dir, f'components/{name}') if os.path.exists(components_path): # If component directory already exists raise CommandError(f'The component "{name}" already exists at {components_path}.') os.makedirs(components_path) with open(components_path + '/index.js', 'w') as f: script_content = f""" window.addEventListener('load', (event) => {{ console.log("{name} component is fully loaded"); }}); """ f.write(script_content.strip()) with open(components_path + '/style.css', 'w') as f: style_content = f""" .component-{name} {{ background: red; }} """ f.write(style_content.strip()) with open(components_path + '/template.html', 'w') as f: template_content = f"""