protected function generateModule()
    {
        $location = $this->container['location'];
        $root     = module_path(null, '', $location);
        $manifest = config("modules.locations.$location.manifest") ?: 'module.json';
        $provider = config("modules.locations.$location.provider") ?: 'ModuleServiceProvider';

        if (!$this->files->isDirectory($root)) {
            $this->files->makeDirectory($root);
        }

        $mapping   = config("modules.locations.$location.mapping");
        $directory = module_path(null, $this->container['basename'], $location);
        $source    = __DIR__ . '/../../../resources/stubs/module';

        $this->files->makeDirectory($directory);

        $sourceFiles = $this->files->allFiles($source, true);

        if (!empty($mapping)) {
            $search = array_keys($mapping);
            $replace = array_values($mapping);
        }

        foreach ($sourceFiles as $file) {
            $contents = $this->replacePlaceholders($file->getContents());
            $subPath = $file->getRelativePathname();

            if (!empty($mapping)) {
                $subPath = str_replace($search, $replace, $subPath);
            }

            $filePath = $directory . '/' . $subPath;
            
            // if the file is module.json, replace it with the custom manifest file name
            if ($file->getFilename() === 'module.json' && $manifest) {
                $filePath = str_replace('module.json', $manifest, $filePath);
            }
            
            // if the file is ModuleServiceProvider.php, replace it with the custom provider file name
            if ($file->getFilename() === 'ModuleServiceProvider.php') {
                $filePath = str_replace('ModuleServiceProvider', $provider, $filePath);
            }
            
            $dir = dirname($filePath);
            
            if (! $this->files->isDirectory($dir)) {
                $this->files->makeDirectory($dir, 0755, true);
            }

            $this->files->put($filePath, $contents);
        }
    }