IGN.psm1

Import-Module "PSWriteColor"
Import-Module "7Zip4Powershell"

$assets = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot "assets\"))
$relative_path = Get-Location

function New-Gulp {
    param (
        [CmdletBinding()]
        [Parameter()]
        [string]$Path = "."
    )
    $final_path = [System.IO.Path]::GetFullPath((Join-Path -Path $relative_path -ChildPath $Path))
    $exists = [System.IO.Directory]::Exists($final_path)
    $gulpfile = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot 'assets\gulpfile.txt'))
    
    Write-Host "`r`n`r`nThanks for using @IGN/create-gulp! Scaffolding gulp project into $final_path in 10 seconds..."
    Start-Sleep -Seconds 10;
    if (-Not $exists) {
        Write-Host "`r`nGenerating directory $final_path..."
        New-item $final_path -ItemType Directory
    }
    Start-Sleep -Seconds 2
    Set-Location $final_path
    Start-Sleep -Seconds .1
    New-Item "dist/css" -ItemType Directory;
    New-Item "scss" -ItemType Directory;
    Write-Color -Text "`r`nGenerating ", "gulpfile.js" , "..." -C Gray, Green;
    Start-Sleep -Seconds 1;
    Copy-Item -Path $gulpfile -Destination "$final_path\gulpfile.js";
    "// Start typing some scss ;)`r`n@debug 'Hello World from scss/index.scss!'`r`n" | Out-File -FilePath "scss/index.scss"; 
    Write-Color "Initializing ", "package.json ", "using: ", "npm ", "init ", "-", "y..." -C Gray, Green, Gray, Red, Cyan, DarkGreen
    npm init -y;
    Start-Sleep -Seconds 0.5;
    Write-Host "Installing dependencies, could take up to a minute...`r`n";
    npm i gulp gulp-csso gulp-sass sass gulp-csso gulp-rename gulp-sourcemaps --silent -D;
    Set-Location $relative_path;
    Write-Color "`r`To get started run:`r`n", "
    Set-Location "
, "$Path", "
    gulp`r`n`r`n"
,
    "And start editing $Path\scss\", "index.scss`r`n" -C Gray, Cyan, Gray, Cyan, Gray, Magenta;
}
function New-Git {
    param (
        [Parameter()]
        [string]$Path = ".",
        [Parameter(Mandatory)]
        [string]$Repo,
        [Parameter(Mandatory)]
        [string]$Branch
    )
    $download_url = "https://github.com/$Repo/archive/refs/heads/$Branch.zip"
    $final_path = [System.IO.Path]::GetFullPath((Join-Path -Path $relative_path -ChildPath $Path))
    $temp_name = "temp_git-$($Repo.Split("/")[1])-$(Get-Random)@$Branch.zip"
    $exists = [System.IO.Directory]::Exists($final_path)
    
    $HTTP_Request = [System.Net.WebRequest]::Create($download_url)

    # We then get a response from the site.
    $HTTP_Response = $HTTP_Request.GetResponse()

    # We then get the HTTP code as an integer.
    $HTTP_Status = [int]$HTTP_Response.StatusCode

    If ($HTTP_Status -eq 200) {
        Write-Color -Text "`r`n`r`nThanks for using ", "@IGN/create-git", "! Scaffolding cloned git repository into $final_path in 10 seconds..." -C Gray, Cyan
        Start-Sleep -Seconds 10;
        if (-Not $exists) {
            Write-Host "`r`nGenerating directory $final_path..."
            New-item $final_path -ItemType Directory
        }
        Start-Sleep -Seconds 2
        Set-Location $final_path
        Start-Sleep -Seconds .1
        Write-Host "`r`nDownloading files from repository: https://github.com/$Repo..." -ForegroundColor Cyan
        Invoke-RestMethod -Uri $download_url -OutFile "$assets\$temp_name"
        Start-Sleep -Seconds 2
        Write-Host "`r`nExtracting files into $final_path..."
        Expand-7Zip -ArchiveFileName "$assets\$temp_name" -TargetPath $final_path
        Write-Host "`r`n`r`nLast steps..."
        Write-Host "`r`nRemoving temporary files..."
        Remove-Item "$assets\$temp_name" -Force -Recurse -ErrorAction SilentlyContinue || Out-Null
        Write-Host "`r`n`Done!" -ForegroundColor Cyan
    }
    Else {
        Write-Host "`r`nCould not reach the repository, please check the entered branch (example: master) and repo (example: Mojang\Minecraft).`r`n" -ForegroundColor Red
    }
    ($null -ne $HTTP_Response) ? $HTTP_Response.Close() : $()             

}