Private/New-FolderStructure.ps1
Function New-FolderStructure { <# .SYNOPSIS Creates a basic folder structure with a root folder, including 'Tests', 'en-US', 'Private', and 'Public' subfolders. .DESCRIPTION This function automates the creation of a standard folder structure for organising project files. It includes a root folder (defined by the function's parameters), a 'Tests' subfolder, and a module subfolder with further 'en-US', 'Private', and 'Public' subfolders within it. The function will indicate if folders or subfolders already exist. .PARAMETER Path Required. Specifies the absolute path where the root folder will be created. .PARAMETER Name Required. The name of the root folder and the module subfolder. .EXAMPLE New-FolderStructure -Path C:\MyProjects -Name NewModule This command creates a folder named "NewModule" at the location "C:\MyProjects" and includes the following subfolders: * Tests * NewModule * en-US * Private * Public .NOTES Author: owen.heaume Version: 1.0.0 - Initial release #> [cmdletbinding()] Param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Name ) Begin { Write-Host "Creating folder structure" -ForegroundColor DarkCyan try { $folderPath = Join-Path -Path $Path -ChildPath $name -ea Stop } catch { throw "Error joining path: $_" } } Process { # Create the root folder if (-not (Test-Path $folderPath)) { Write-Host "Creating folder: $folderPath" -ForegroundColor DarkGray try { New-Item -Path $folderPath -ItemType Directory -ea stop | Out-Null Write-Host "Folder created successfully" -ForegroundColor DarkGreen } catch { throw "Error creating folder: $_" } } else { Write-Host "$folderPath already exists" -ForegroundColor DarkYellow } # Create the Tests subfolder if (-not (Test-Path (Join-Path -Path $folderPath -ChildPath 'Tests'))) { Write-Host "Creating subfolder: $(Join-Path -Path $folderPath -ChildPath 'Tests')" -ForegroundColor DarkGray try { New-Item -Path (Join-Path -Path $folderPath -ChildPath 'Tests') -ItemType Directory -ea stop | Out-Null Write-Host "Subfolder created successfully" -ForegroundColor DarkGreen } catch { throw "Error creating subfolder: $_" } } else { Write-Host "$(Join-Path -Path $folderPath -ChildPath 'Tests') already exists" -ForegroundColor DarkYellow } # Create the module subfolder if (-not (Test-Path (Join-Path -Path $folderPath -ChildPath $name))) { Write-Host "Creating subfolder: $(Join-Path -Path $folderPath -ChildPath $name)" -ForegroundColor DarkGray try { New-Item -Path (Join-Path -Path $folderPath -ChildPath $name) -ItemType Directory -ea stop | Out-Null Write-Host "Subfolder created successfully" -ForegroundColor DarkGreen } catch { throw "Error creating subfolder: $_" } } else { Write-Host "$(Join-Path -Path $folderPath -ChildPath $name) already exists" -ForegroundColor DarkYellow } $moduleFolder = (Join-Path -Path $folderPath -ChildPath $name) # Create the en-US subfolder if (-not (Test-Path (Join-Path -Path $moduleFolder -ChildPath 'en-US'))) { Write-Host "Creating subfolder: $(Join-Path -Path $moduleFolder -ChildPath 'en-US')" -ForegroundColor DarkGray try { New-Item -Path (Join-Path -Path $moduleFolder -ChildPath 'en-US') -ItemType Directory -ea stop | Out-Null Write-Host "Subfolder created successfully" -ForegroundColor DarkGreen } catch { throw "Error creating subfolder: $_" } } else { Write-Host "$(Join-Path -Path $moduleFolder -ChildPath 'en-US') already exists" -ForegroundColor DarkYellow } # Create the Private subfolder if (-not (Test-Path (Join-Path -Path $moduleFolder -ChildPath 'Private'))) { Write-Host "Creating subfolder: $(Join-Path -Path $moduleFolder -ChildPath 'Private')" -ForegroundColor DarkGray try { New-Item -Path (Join-Path -Path $moduleFolder -ChildPath 'Private') -ItemType Directory -ea stop | Out-Null Write-Host "Subfolder created successfully" -ForegroundColor DarkGreen } catch { throw "Error creating subfolder: $_" } } else { Write-Host "$(Join-Path -Path $moduleFolder -ChildPath 'Private') already exists" -ForegroundColor DarkYellow } # Create the Public subfolder if (-not (Test-Path (Join-Path -Path $moduleFolder -ChildPath 'Public'))) { Write-Host "Creating subfolder: $(Join-Path -Path $moduleFolder -ChildPath 'Public')" -ForegroundColor DarkGray try { New-Item -Path (Join-Path -Path $moduleFolder -ChildPath 'Public') -ItemType Directory -ea stop | Out-Null Write-Host "Subfolder created successfully" -ForegroundColor DarkGreen } catch { throw "Error creating subfolder: $_" } } else { Write-Host "$(Join-Path -Path $moduleFolder -ChildPath 'Public') already exists" -ForegroundColor DarkYellow } [pscustomobject]@{ folderPath = $folderPath tests = (Join-Path -Path $folderPath -ChildPath 'Tests') module = (Join-Path -Path $folderPath -ChildPath $name) enUS = (Join-Path -Path $moduleFolder -ChildPath 'en-US') private = (Join-Path -Path $moduleFolder -ChildPath 'Private') public = (Join-Path -Path $moduleFolder -ChildPath 'Public') } } End {} } |