File size: 5,452 Bytes
8a8c5bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<#
.SYNOPSIS
    Sync upstream, build the static site, commit, and push to Hugging Face.

.DESCRIPTION
    The one command to run after updating DocEditorPlayground:

        .\scripts\deploy.ps1

    It re-mirrors app/ from the local upstream checkout, builds the static export
    into the repository root, commits with the upstream version and commit in the
    message, and pushes to the Hugging Face Space remote.

    The Space serves files directly with no build step of its own, so what you
    push is exactly what visitors get.

.PARAMETER UpstreamPath
    Local DocEditorPlayground checkout. Defaults to C:\dev\DocEditorPlayground.

.PARAMETER Message
    Commit message. Defaults to "chore: sync doceditorplayground v<version> @ <sha>".

.PARAMETER Remote
    Git remote to push to. Defaults to 'hf' (the Hugging Face Space).

.PARAMETER AlsoPushOrigin
    Additionally push to 'origin' (the GitHub mirror of this deployment repo).

.PARAMETER SkipSync
    Do not re-mirror app/; build and deploy what is already there.

.PARAMETER SkipBuild
    Do not rebuild; commit and push the static output already at the repo root.

.PARAMETER AllowDirty
    Pass through to sync-upstream.ps1: allow a dirty upstream working tree.

.PARAMETER Clean
    Pass through to build-static.ps1: discard .build first, including node_modules.

.PARAMETER DryRun
    Sync and build, show what would be committed, but do not commit or push.

.EXAMPLE
    .\scripts\deploy.ps1
.EXAMPLE
    .\scripts\deploy.ps1 -Message "feat: add markdown export" -AlsoPushOrigin
#>
[CmdletBinding()]
param(
    [string] $UpstreamPath = 'C:\dev\DocEditorPlayground',
    [string] $Message,
    [string] $Remote = 'hf',
    [switch] $AlsoPushOrigin,
    [switch] $SkipSync,
    [switch] $SkipBuild,
    [switch] $AllowDirty,
    [switch] $Clean,
    [switch] $DryRun
)

$ErrorActionPreference = 'Stop'

$repoRoot = Split-Path -Parent $PSScriptRoot

if (-not $SkipSync) {
    $syncArgs = @{ UpstreamPath = $UpstreamPath }
    if ($AllowDirty) { $syncArgs['AllowDirty'] = $true }
    & (Join-Path $PSScriptRoot 'sync-upstream.ps1') @syncArgs
    Write-Host ''
}

if (-not $SkipBuild) {
    $buildArgs = @{}
    if ($Clean) { $buildArgs['Clean'] = $true }
    & (Join-Path $PSScriptRoot 'build-static.ps1') @buildArgs
    Write-Host ''
}

Push-Location $repoRoot
try {
    # Verify the target remote exists before doing anything that needs undoing.
    $remotes = & git remote
    if ($remotes -notcontains $Remote) {
        $missingRemoteMessage = @"
Git remote '$Remote' is not configured.

Create the Space at https://huggingface.co/new-space (choose the Static SDK,
Blank template), then wire it up:

    git remote add $Remote https://huggingface.co/spaces/<user>/<space-name>

See DEPLOY.md for the full first-time setup.
"@
        # A dry run only reports, so a missing remote is a warning there and a
        # hard stop for a real deploy.
        if ($DryRun) { Write-Warning $missingRemoteMessage } else { throw $missingRemoteMessage }
    }

    $changes = & git status --porcelain
    if ([string]::IsNullOrWhiteSpace(($changes -join ''))) {
        Write-Host 'Nothing to deploy - working tree is clean.' -ForegroundColor Yellow
        return
    }

    if (-not $Message) {
        $upstreamJsonPath = Join-Path $repoRoot 'UPSTREAM.json'
        if (Test-Path -LiteralPath $upstreamJsonPath) {
            $u = Get-Content -LiteralPath $upstreamJsonPath -Raw | ConvertFrom-Json
            $short = if ($u.commit.Length -ge 7) { $u.commit.Substring(0, 7) } else { $u.commit }
            $Message = "chore: sync doceditorplayground v$($u.appVersion) @ $short"
        }
        else {
            $Message = 'chore: sync doceditorplayground'
        }
    }

    if ($DryRun) {
        Write-Host '--- dry run, nothing committed or pushed ---' -ForegroundColor Yellow
        Write-Host "Would commit: $Message"
        Write-Host "Would push to: $Remote$(if ($AlsoPushOrigin) { ' and origin' })"
        Write-Host ''
        # Summarize rather than list: a rebuild touches hundreds of hashed chunks.
        $changed = (& git status --porcelain).Count
        Write-Host "$changed path(s) changed. Summary by top-level entry:"
        & git status --porcelain |
            ForEach-Object { ($_.Substring(3) -split '[/\\]')[0].Trim('"') } |
            Group-Object |
            Sort-Object -Property Count -Descending |
            ForEach-Object { Write-Host ("  {0,5}  {1}" -f $_.Count, $_.Name) }
        return
    }

    & git add -A
    if ($LASTEXITCODE -ne 0) { throw 'git add failed' }

    & git commit -m $Message
    if ($LASTEXITCODE -ne 0) { throw 'git commit failed' }

    $branch = (& git rev-parse --abbrev-ref HEAD).Trim()

    Write-Host ''
    Write-Host "Pushing $branch -> $Remote" -ForegroundColor Cyan
    & git push $Remote $branch
    if ($LASTEXITCODE -ne 0) {
        throw "Push to '$Remote' failed. If the Space has commits this repo lacks, run: git pull --rebase $Remote $branch"
    }

    if ($AlsoPushOrigin) {
        Write-Host "Pushing $branch -> origin" -ForegroundColor Cyan
        & git push origin $branch
        if ($LASTEXITCODE -ne 0) { throw 'Push to origin failed.' }
    }

    Write-Host ''
    Write-Host 'Deployed. A Static Space serves files directly, so the update is live' -ForegroundColor Green
    Write-Host 'as soon as the push lands - no build wait.' -ForegroundColor Green
}
finally { Pop-Location }