forked from dustinmoris/DustedCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinifyCss.ps1
53 lines (44 loc) · 1.29 KB
/
MinifyCss.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[CmdletBinding()]
param
(
[Parameter(Position = 0, Mandatory = $true)]
[string] $SolutionDir,
[Parameter(Position = 1, Mandatory = $false)]
[string[]] $ExcludeFiles
)
Function Compress-CssFile
{
[CmdletBinding()]
param
(
[string] $CssFilePath
)
$cssFile = Get-Item -Path $CssFilePath
$content = [System.IO.File]::ReadAllText($cssFile.FullName)
$body = @{input = $content}
$response = Invoke-WebRequest -Uri "http://cssminifier.com/raw" -Method Post -Body $body
if ($response.StatusCode -ne 200)
{
throw ("Could not compress CSS file $CssFilePath" +
[System.Environment]::NewLine +
[System.Environment]::NewLine +
"Response:" +
[System.Environment]::NewLine +
$response.RawContent)
}
$compressedContent = $response.Content
$newFilePath = $CssFilePath.Replace(".css", ".min.css")
Set-Content -Path $newFilePath -Value $compressedContent -Force
}
# -----------------
# BEGIN:
$ErrorActionPreference = "Stop"
Write-Output "-----"
Write-Output "Compressing CSS Files"
Write-Output "-----"
Get-ChildItem $SolutionDir -Recurse -Include *.css -Exclude *.min.css | % {
if (!$ExcludeFiles.Contains($_.Name)) {
Write-Output "Compressing $_"
Compress-CssFile -CssFilePath $_
}
}