-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch-sourcemap.ps1
More file actions
90 lines (76 loc) · 3.21 KB
/
watch-sourcemap.ps1
File metadata and controls
90 lines (76 loc) · 3.21 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
param(
[int]$DebounceMs = 300
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Invoke-RefreshSourcemap {
Write-Host ("[{0}] Regenerating sourcemap.json..." -f (Get-Date).ToString("HH:mm:ss"))
& rojo sourcemap default.project.json --output sourcemap.json
Write-Host ("[{0}] sourcemap.json updated." -f (Get-Date).ToString("HH:mm:ss"))
}
if (-not (Test-Path -LiteralPath "default.project.json")) {
throw "default.project.json not found in repo root."
}
$srcWatcher = New-Object System.IO.FileSystemWatcher
$srcWatcher.Path = (Resolve-Path -LiteralPath "src").Path
$srcWatcher.Filter = "*"
$srcWatcher.IncludeSubdirectories = $true
$srcWatcher.NotifyFilter = [System.IO.NotifyFilters]::FileName -bor `
[System.IO.NotifyFilters]::DirectoryName
$srcWatcher.EnableRaisingEvents = $true
$rootWatcher = New-Object System.IO.FileSystemWatcher
$rootWatcher.Path = (Resolve-Path -LiteralPath ".").Path
$rootWatcher.Filter = "*.project.json"
$rootWatcher.IncludeSubdirectories = $false
$rootWatcher.NotifyFilter = [System.IO.NotifyFilters]::FileName
$rootWatcher.EnableRaisingEvents = $true
$subscriptions = @()
$subscriptions += Register-ObjectEvent -InputObject $srcWatcher -EventName Created -SourceIdentifier "sourcemapwatch.src.created"
$subscriptions += Register-ObjectEvent -InputObject $srcWatcher -EventName Deleted -SourceIdentifier "sourcemapwatch.src.deleted"
$subscriptions += Register-ObjectEvent -InputObject $srcWatcher -EventName Renamed -SourceIdentifier "sourcemapwatch.src.renamed"
$subscriptions += Register-ObjectEvent -InputObject $rootWatcher -EventName Created -SourceIdentifier "sourcemapwatch.root.created"
$subscriptions += Register-ObjectEvent -InputObject $rootWatcher -EventName Deleted -SourceIdentifier "sourcemapwatch.root.deleted"
$subscriptions += Register-ObjectEvent -InputObject $rootWatcher -EventName Renamed -SourceIdentifier "sourcemapwatch.root.renamed"
Write-Host "Watching for new files (src/** and *.project.json). Press Ctrl+C to stop."
$lastEventAt = $null
$lastEvent = $null
try {
while ($true) {
$receivedEvent = Wait-Event -Timeout 0.1
if ($receivedEvent) {
$lastEventAt = Get-Date
$lastEvent = $receivedEvent
Remove-Event -EventIdentifier $receivedEvent.EventIdentifier
continue
}
if (-not $lastEventAt) {
continue
}
$elapsedMs = (New-TimeSpan -Start $lastEventAt -End (Get-Date)).TotalMilliseconds
if ($elapsedMs -lt $DebounceMs) {
continue
}
$lastEventAt = $null
if ($lastEvent) {
$sourceEventArgs = $lastEvent.SourceEventArgs
$changeType = $sourceEventArgs.ChangeType
if ($changeType -ne "Created" -and $changeType -ne "Renamed") {
continue
}
$fullPath = $sourceEventArgs.FullPath
if ($changeType -eq "Renamed" -and $sourceEventArgs.OldFullPath) {
Write-Host ("[{0}] Change detected: {1} {2} -> {3}" -f (Get-Date).ToString("HH:mm:ss"), $changeType, $sourceEventArgs.OldFullPath, $fullPath)
}
else {
Write-Host ("[{0}] Change detected: {1} {2}" -f (Get-Date).ToString("HH:mm:ss"), $changeType, $fullPath)
}
}
$lastEvent = $null
Invoke-RefreshSourcemap
}
}
finally {
foreach ($sub in $subscriptions) {
try { Unregister-Event -SourceIdentifier $sub.SourceIdentifier -ErrorAction SilentlyContinue } catch { }
}
}