IGN.psm1

Import-Module "PSWriteColor"

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

function VerifyURL
{
    param(
        [parameter(Mandatory)]
        [string]$Url
    )
    $HTTP_Request = [System.Net.WebRequest]::Create($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

    return $HTTP_Status
}
function Resolve-FullPath 
{
    param(
        [parameter(Mandatory, Position = 0)]
        [string]$Path
    )

    if (-Not ([System.IO.Path]::IsPathRooted($Path))) {
        $Path = Join-Path (Get-Location) $Path    
    }
    return [IO.Path]::GetFullPath($Path)
}
function existsMkdir 
{
    param(
        [parameter(Mandatory)]
        [string]$Path
    )
    $exists = [System.IO.Directory]::Exists($Path)
    $drive = [System.IO.Path]::GetPathRoot($Path)
    if (-Not $exists) {
        # check if the drive exists then create the directory
        if (-Not ([System.IO.DriveInfo]::GetDrives() | Where-Object {$_.Name -eq $drive})) {
            Write-Host "`r`n`r`nThe drive $drive does not exist. Please enter a drive available on your machine.`r`n" -Fore Red
            Write-Host "Here is the list of available drives: `r`n"
            Write-Host "$(([System.IO.DriveInfo]::GetDrives()))`r`n" -Fore Cyan
            return
        } else {
            Write-Host "`r`n`r`nCreating directory $Path`r`n" -Fore Green
            [System.IO.Directory]::CreateDirectory($Path)
        }
    }
}
function Unzip
{
    param(
        [parameter(Mandatory, Position = 0)]
        [string]$Path, 
        [parameter(Mandatory, Position = 1)]
        [string]$OutFile)
    [System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $OutFile)
}
function Zip 
{
    param(
        [parameter(Mandatory, Position = 0)]
        [string]$Path, 
        [parameter(Mandatory, Position = 1)]
        [string]$OutFile)
    [System.IO.Compression.ZipFile]::CreateFromDirectory($Path, $OutFile)
}


function New-Gulp 
{
    param (
        [CmdletBinding()]
        [Parameter()]
        [string]$Path = "."
    )
    $final_path = Resolve-FullPath -Path $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;
    existsMkdir -Path $final_path
    Set-Location $final_path
    New-Item "dist/css" -ItemType Directory;
    New-Item "scss" -ItemType Directory;
    Write-Color -Text "`r`nGenerating ", "gulpfile.js" , "..." -C Gray, Green;
    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;
    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 DownloadURL {
    param(
    [parameter(Mandatory, Position = 0)]
    [string]$Url,
    [parameter(Mandatory, Position = 1)]
    [string]$Path,
    [parameter(Mandatory, Position = 2)]
    [string]$FileName
)

    $Path = Resolve-FullPath -Path $Path
    $HTTP_STATUS = VerifyURL -Url $Url

    if ($HTTP_STATUS -eq 200) {
        Write-Host "`r`n`r`nDownloading $Url to $Path..." -Fore Cyan
        existsMkdir -Path $Path
        Invoke-RestMethod -Uri $Url -OutFile $Path/$FileName
        Write-Host "`r`n`r`nDownload complete!`r`n" -Fore Green
    } else {
        Write-Host "`r`n`r`nCould not download $Url`r`n" -Fore Red
    }
}