-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build.ps1
55 lines (50 loc) · 2.13 KB
/
Build.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
54
55
#Requires -Version 6
$InformationPreference = 'Continue'
$WarningPreference = 'Continue'
$ErrorActionPreference = 'Stop'
$baseDir = (Resolve-Path .).Path
$srcDir = [System.IO.Path]::Join($baseDir, 'src')
if (!(Test-Path $srcDir)) {
throw [System.IO.DirectoryNotFoundException]::new("Source directory not found.")
}
$targetCss = [System.IO.Path]::Join($srcDir, 'common.css')
$targetJs = [System.IO.Path]::Join($srcDir, 'nptu-redux.js')
$cssConfig = [System.IO.Path]::Join($srcDir, 'config_css.json')
$outputCss = [System.IO.Path]::Join($baseDir, 'common.min.css')
function Start-Build {
begin {
$cssPurgeExecutable = Get-Command "css-purge" -ErrorAction SilentlyContinue
if ($null -eq $cssPurgeExecutable) {
Write-Warning "css-purge not found; attempting to install via npm..."
$npmExecutable = Get-Command "npm" -ErrorAction SilentlyContinue
if ($null -eq $npmExecutable) {
throw [System.IO.FileNotFoundException]::new("npm not found. Please install npm before building.")
}
Start-Process $npmExecutable.Source -ArgumentList "install css-purge -g"
$cssPurgeExecutable = Get-Command "css-purge"
}
$argBuilder = [System.Text.StringBuilder]::new("-i $targetCss -o $outputCss")
if (Test-Path $cssConfig) {
$argBuilder.AppendJoin($cssConfig) > $null
}
$args = $argBuilder.ToString()
}
process {
Write-Information "Minifying CSS with args '$args'..."
Invoke-Expression "$cssPurgeExecutable $args"
$minifiedCss = Get-Content $outputCss
$newJsName = [System.IO.Path]::GetFileNameWithoutExtension($targetJs) + ".user" + [System.IO.Path]::GetExtension($targetJs)
$outputFile = [System.IO.Path]::Join($baseDir, $newJsName)
Copy-Item -LiteralPath $targetJs -Destination $outputFile
Write-Information "Injecting 'injectCustomCss' to $targetJs..."
@"
function injectCustomCss(head){
head.appendChild(make({el: 'style', html: ``$minifiedCss``}))
}
"@ | Out-File $outputFile -Append
}
end {
Write-Information "Finished."
}
}
Start-Build