shared-hosting-sync 0.1.1 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/README.md +20 -6
- package/package.json +2 -2
- package/src/arguments.js +1 -1
- package/src/cli.js +562 -511
- package/src/core/deployment.js +2 -0
- package/src/core/fileFingerprint.js +9 -0
- package/src/core/migrations/003_sync_state.sql +18 -0
- package/src/core/syncState.js +65 -0
- package/src/server/deployments.js +29 -6
- package/src/server/index.js +2 -1
- package/src/server/routes.js +1 -1
- package/src/ui/deployments.js +15 -7
- package/src/ui/index.html +26 -3
- package/src/ui/logo.svg +7 -0
- package/src/ui/style.css +20 -2
- package/src/ui/ui.js +309 -289
- package/src/uploader.js +37 -9
package/src/ui/ui.js
CHANGED
|
@@ -1,289 +1,309 @@
|
|
|
1
|
-
import { DeploymentPanel } from './deployments.js'
|
|
2
|
-
|
|
3
|
-
const $ = (selector) => document.querySelector(selector)
|
|
4
|
-
const $$ = (selector) => [...document.querySelectorAll(selector)]
|
|
5
|
-
const esc = (value) => String(value ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]))
|
|
6
|
-
const state = { projects: [], current: null, draft: null, step: 0, busy: false, local: null, remote: null, verifiedPath: null, fileOverrides: false }
|
|
7
|
-
let session
|
|
8
|
-
let renderedStep = 0
|
|
9
|
-
const deploymentPanel = new DeploymentPanel({ api, run: act, isBusy: () => state.busy, message })
|
|
10
|
-
|
|
11
|
-
async function api(method, url, body) {
|
|
12
|
-
if (!session) {
|
|
13
|
-
const response = await fetch('/api/session')
|
|
14
|
-
const data = await response.json()
|
|
15
|
-
if (!response.ok || !data.token) throw new Error(data.error || 'Could not start a local session')
|
|
16
|
-
session = data.token
|
|
17
|
-
}
|
|
18
|
-
const response = await fetch(url, {
|
|
19
|
-
method, headers: { 'content-type': 'application/json', 'x-devsync-session': session },
|
|
20
|
-
...(body === undefined ? {} : { body: JSON.stringify(body) })
|
|
21
|
-
})
|
|
22
|
-
const data = await response.json()
|
|
23
|
-
if (!response.ok) throw new Error(data.error || 'Could not complete that operation')
|
|
24
|
-
return data
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function message(text, kind = 'info') {
|
|
28
|
-
const box = $('#message')
|
|
29
|
-
if (!box) return
|
|
30
|
-
box.textContent = text
|
|
31
|
-
box.className = `notice ${kind}`
|
|
32
|
-
box.setAttribute('role', kind === 'error' ? 'alert' : 'status')
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function setBusy(busy) {
|
|
36
|
-
$('#pane').setAttribute('aria-busy', String(busy))
|
|
37
|
-
$$('button,input,select').forEach((element) => {
|
|
38
|
-
if (busy) {
|
|
39
|
-
if (element.dataset.beforeBusy === undefined) element.dataset.beforeBusy = String(element.disabled)
|
|
40
|
-
element.disabled = true
|
|
41
|
-
} else if (element.dataset.beforeBusy !== undefined) {
|
|
42
|
-
element.disabled = element.dataset.beforeBusy === 'true'
|
|
43
|
-
delete element.dataset.beforeBusy
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async function act(action, text = '') {
|
|
49
|
-
if (state.busy) return
|
|
50
|
-
state.busy = true
|
|
51
|
-
setBusy(true)
|
|
52
|
-
if (text) message(text)
|
|
53
|
-
try { await action() }
|
|
54
|
-
catch (error) { message(error.message, 'error') }
|
|
55
|
-
finally { state.busy = false; setBusy(false) }
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async function refreshProjects() {
|
|
59
|
-
state.projects = (await api('GET', '/api/projects')).projects
|
|
60
|
-
$('#projectCount').textContent = state.projects.length
|
|
61
|
-
$('#projectList').innerHTML = state.projects.map((project) => `<button class="project ${state.current?.id === project.id ? 'active' : ''}" data-project="${project.id}"><strong>${esc(project.label)}</strong><small title="${esc(project.localPath)}">${esc(project.localPath)}</small></button>`).join('')
|
|
62
|
-
$$('[data-project]').forEach((button) => { button.onclick = () => act(async () => { await discardDraft(); await openProject(Number(button.dataset.project)) }) })
|
|
63
|
-
setBusy(state.busy)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async function discardDraft() {
|
|
67
|
-
deploymentPanel.close()
|
|
68
|
-
if (state.draft) {
|
|
69
|
-
try { await api('DELETE', `/api/onboarding/${state.draft.id}`) } catch {}
|
|
70
|
-
}
|
|
71
|
-
state.draft = null
|
|
72
|
-
state.step = 0
|
|
73
|
-
state.local = null
|
|
74
|
-
state.remote = null
|
|
75
|
-
state.verifiedPath = null
|
|
76
|
-
state.fileOverrides = false
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function welcome() {
|
|
80
|
-
deploymentPanel.close()
|
|
81
|
-
$('#pane').innerHTML = `<div id="message" class="notice"></div><div class="welcome"><p class="eyebrow">FROM YOUR FOLDER TO YOUR HOSTING</p><h1>A simpler way to get your project online.</h1><p class="sub">Choose a local folder, connect your hosting account, and pick where your files belong.</p><button class="primary" id="welcomeAdd">${state.projects.length ? 'Add another project' : 'Add your first project'}</button><div class="cards"><article class="card"><div class="number">01 / LOCAL</div><h3>Start with your files</h3><p>Choose your project or build folder. We’ll pick up hosting settings from its .env.</p></article><article class="card"><div class="number">02 / CONNECT</div><h3>Bring your hosting</h3><p>Use FTP, FTPS, or SFTP details from your hosting provider.</p></article><article class="card"><div class="number">03 / DESTINATION</div><h3>Choose the right folder</h3><p>Browse your server folders and save the destination for this project.</p></article></div></div>`
|
|
82
|
-
$('#welcomeAdd').onclick = () => act(startNew)
|
|
83
|
-
setBusy(state.busy)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async function startNew() {
|
|
87
|
-
await discardDraft()
|
|
88
|
-
state.current = null
|
|
89
|
-
state.step = 1
|
|
90
|
-
await refreshProjects()
|
|
91
|
-
renderWizard()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function source(key) {
|
|
95
|
-
const value = state.draft?.sources[key]
|
|
96
|
-
const supplied = state.draft?.secrets[key] || state.draft?.values[key] !== '' && state.draft?.values[key] !== undefined
|
|
97
|
-
if (value === 'default' && !supplied && key !== 'DEVSYNC_PORT') {
|
|
98
|
-
return ['DEVSYNC_HOST', 'DEVSYNC_USERNAME', 'DEVSYNC_TRANSPORT'].includes(key) ? '<span class="source">Needed</span>' : ''
|
|
99
|
-
}
|
|
100
|
-
const names = { default: 'Automatic', ui: 'Entered here', override: 'Saved', secret: 'Saved' }
|
|
101
|
-
return value ? `<span class="source">${esc(names[value] || value)}</span>` : ''
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function input(key, label, { secret = false, type = 'text', optional = false } = {}) {
|
|
105
|
-
const draft = state.draft
|
|
106
|
-
const automaticPort = key === 'DEVSYNC_PORT' && draft.sources[key] === 'default'
|
|
107
|
-
const value = secret || automaticPort ? '' : draft.values[key]
|
|
108
|
-
const present = draft.secrets[key]
|
|
109
|
-
const placeholder = present ? 'Already supplied — leave blank to keep' : automaticPort ? `Automatic (${draft.values.DEVSYNC_TRANSPORT === 'sftp' ? 22 : 21})` : ''
|
|
110
|
-
return `<div class="field"><label for="${key}">${esc(label)}${optional ? ' <span class="muted">(optional)</span>' : ''}${source(key)}</label><input id="${key}" name="${key}" type="${secret ? 'password' : type}" value="${esc(value)}" placeholder="${esc(placeholder)}" autocomplete="off">${present ? `<label class="check"><input type="checkbox" data-clear="${key}">Remove supplied ${key === 'DEVSYNC_PASSWORD' ? 'password' : 'key'}</label>` : ''}</div>`
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function renderWizard() {
|
|
114
|
-
const draft = state.draft
|
|
115
|
-
const steps = ['Local folder', 'Hosting', 'Destination', 'Review']
|
|
116
|
-
const step = state.step
|
|
117
|
-
const changedStep = renderedStep !== step
|
|
118
|
-
renderedStep = step
|
|
119
|
-
let content = ''
|
|
120
|
-
if (step === 1) {
|
|
121
|
-
content = `<h2>Where are your project files?</h2><p>Choose the folder you want to deploy. For a built frontend, choose its dist or build folder.</p><form id="localForm"><div class="field"><label for="localPath">Local project folder</label><div class="path-row"><input id="localPath" value="${esc(draft?.localPath || '')}" placeholder="Absolute path to a project folder"><button class="secondary" type="button" id="openLocal">Browse folders</button></div><span class="help">Only hosting settings named DEVSYNC_* are read from this folder’s .env.</span></div><div id="localPicker"></div><div class="actions"><button class="secondary" type="button" id="cancelSetup">Cancel</button><span class="spacer"></span><button class="primary" type="submit">Continue to hosting</button></div></form>`
|
|
122
|
-
} else if (step === 2) {
|
|
123
|
-
const transport = draft.values.DEVSYNC_TRANSPORT
|
|
124
|
-
const loaded = Object.entries(draft.sources).filter(([, value]) => value === '.env' || value === '.devsync.env').length
|
|
125
|
-
content = `<h2>Connect your hosting account</h2><p>${loaded ? `Loaded ${loaded} hosting settings from this folder’s environment files. Fill in anything missing.` : 'Enter the connection details from your hosting provider.'} Imported passwords stay on this computer and are not sent to the browser.</p><form id="hostingForm" autocomplete="off"><div class="form-grid"><div class="field"><label for="DEVSYNC_TRANSPORT">Protocol${source('DEVSYNC_TRANSPORT')}</label><select id="DEVSYNC_TRANSPORT" name="DEVSYNC_TRANSPORT"><option value="">Choose a protocol</option>${['ftps', 'sftp', 'ftp'].map((value) => `<option value="${value}" ${transport === value ? 'selected' : ''}>${value === 'ftps' ? 'FTPS — encrypted FTP' : value === 'sftp' ? 'SFTP — SSH file transfer' : 'FTP — unencrypted'}</option>`).join('')}</select></div>${input('DEVSYNC_HOST', 'Host')}${input('DEVSYNC_PORT', 'Port', { type: 'number', optional: true })}${input('DEVSYNC_USERNAME', 'Username')}${input('DEVSYNC_PASSWORD', 'Password / key passphrase', { secret: true })}</div><details><summary>SSH key and advanced settings</summary><div class="form-grid">${input('DEVSYNC_PRIVATE_KEY', 'SSH private key path', { secret: true, optional: true })}<div class="field"><label for="DEVSYNC_DRY_RUN">Default operation</label><select id="DEVSYNC_DRY_RUN" name="DEVSYNC_DRY_RUN"><option value="false" ${String(draft.values.DEVSYNC_DRY_RUN) === 'false' ? 'selected' : ''}>Allow uploads when requested</option><option value="true" ${String(draft.values.DEVSYNC_DRY_RUN) === 'true' ? 'selected' : ''}>Preview only</option></select></div>${input('DEVSYNC_DEBOUNCE_MS', 'Watch delay (milliseconds)', { type: 'number', optional: true })}</div><p class="help">An SSH key is optional and applies to SFTP only. With a key, the password field is an optional passphrase. Local deletions are never mirrored.</p></details><div class="actions"><button class="secondary" type="button" id="backSetup">Back</button><button class="secondary" type="button" id="cancelSetup">Cancel</button><span class="spacer"></span><button class="primary" type="submit">Test connection & continue</button></div></form>`
|
|
126
|
-
} else if (step === 3) {
|
|
127
|
-
const selected = draft.values.DEVSYNC_REMOTE_PATH
|
|
128
|
-
content = `<h2>Choose the folder on your server</h2><p>Open a folder, then select “Use server folder”. Your local files will be placed inside this destination.</p><div class="notice success">Connection verified with ${esc(draft.values.DEVSYNC_HOST)}.</div><div class="selection"><small>Selected destination ${source('DEVSYNC_REMOTE_PATH')}</small><strong id="selectedRemote">${esc(selected || 'Choose a server folder below')}</strong></div><div id="remotePicker"></div><div class="actions"><button class="secondary" type="button" id="backSetup">Back</button><button class="secondary" type="button" id="cancelSetup">Cancel</button><span class="spacer"></span><button class="primary" id="reviewSetup" ${selected && state.verifiedPath === selected ? '' : 'disabled'}>Continue to review</button></div>`
|
|
129
|
-
} else {
|
|
130
|
-
const values = draft.values
|
|
131
|
-
content = `<h2>Ready to save your project</h2><p>Check the source and destination. Saving will not upload or delete server files.</p><div class="field"><label for="projectName">Project name</label><input id="projectName" value="${esc(draft.label)}" maxlength="80"></div><dl class="review"><dt>Local folder</dt><dd>${esc(draft.localPath)}</dd><dt>Hosting</dt><dd>${esc(values.DEVSYNC_HOST)}:${esc(values.DEVSYNC_PORT)} · ${esc(values.DEVSYNC_TRANSPORT?.toUpperCase())}</dd><dt>Username</dt><dd>${esc(values.DEVSYNC_USERNAME)}</dd><dt>Credentials</dt><dd>${draft.secrets.DEVSYNC_PASSWORD ? 'Password supplied' : 'SSH key supplied'} · stored encrypted</dd><dt>Server folder</dt><dd>${esc(values.DEVSYNC_REMOTE_PATH)}</dd></dl>${state.fileOverrides ? '<div class="notice warning">You changed settings loaded from an environment file. The file stays unchanged and will take priority on future runs. Update that file to keep those changes.</div>' : ''}<p class="snapshot-note">The CLI and dashboard will share this saved project. Your application’s .env is never rewritten or uploaded.</p><div class="actions"><button class="secondary" id="backSetup">Back</button><button class="secondary" id="cancelSetup">Cancel</button><span class="spacer"></span><button class="primary" id="saveSetup">Save project</button></div>`
|
|
132
|
-
}
|
|
133
|
-
$('#pane').innerHTML = `<p class="eyebrow">${draft?.projectId ? 'PROJECT SETTINGS' : 'ADD A PROJECT'}</p><h1>A home for your next deployment.</h1><ol class="steps">${steps.map((name, i) => `<li class="${step === i + 1 ? 'active' : step > i + 1 ? 'done' : ''}" ${step === i + 1 ? 'aria-current="step"' : ''}><span class="step-number">0${i + 1}</span>${name}</li>`).join('')}</ol><div id="message" class="notice"></div><section class="setup-card">${content}</section>`
|
|
134
|
-
$('#cancelSetup').onclick = () => act(async () => { const current = state.current; await discardDraft(); if (current) await openProject(current.id); else welcome() })
|
|
135
|
-
if ($('#backSetup')) $('#backSetup').onclick = () => act(async () => { if (step === 2) await pushHosting(); state.step--; renderWizard() })
|
|
136
|
-
if (step === 1) {
|
|
137
|
-
$('#openLocal').onclick = () => act(() => loadLocal($('#localPath').value || undefined))
|
|
138
|
-
$('#localForm').onsubmit = (event) => { event.preventDefault(); act(async () => {
|
|
139
|
-
const localPath = $('#localPath').value.trim()
|
|
140
|
-
if (!localPath) throw new Error('Choose a local project folder first')
|
|
141
|
-
if (!draft || localPath !== draft.localPath) {
|
|
142
|
-
const loaded = await api('POST', '/api/onboarding', { localPath, ...(draft?.projectId ? { projectId: draft.projectId } : {}) })
|
|
143
|
-
if (draft) try { await api('DELETE', `/api/onboarding/${draft.id}`) } catch {}
|
|
144
|
-
state.draft = loaded
|
|
145
|
-
state.fileOverrides = false
|
|
146
|
-
state.remote = null
|
|
147
|
-
state.verifiedPath = null
|
|
148
|
-
}
|
|
149
|
-
state.step = 2
|
|
150
|
-
renderWizard()
|
|
151
|
-
}, 'Reading hosting settings from the selected folder…') }
|
|
152
|
-
if (state.local) renderLocal()
|
|
153
|
-
}
|
|
154
|
-
if (step === 2) {
|
|
155
|
-
$$('[name],[data-clear]').forEach((element) => element.addEventListener('input', () => { element.dataset.dirty = 'true' }))
|
|
156
|
-
$('#DEVSYNC_TRANSPORT').addEventListener('change', () => {
|
|
157
|
-
if (!$('#DEVSYNC_PORT').value) $('#DEVSYNC_PORT').placeholder = `Automatic (${$('#DEVSYNC_TRANSPORT').value === 'sftp' ? 22 : 21})`
|
|
158
|
-
})
|
|
159
|
-
$('#hostingForm').onsubmit = (event) => { event.preventDefault(); act(async () => {
|
|
160
|
-
await pushHosting()
|
|
161
|
-
state.draft = await api('POST', `/api/onboarding/${state.draft.id}/test`, {})
|
|
162
|
-
state.step = 3
|
|
163
|
-
state.remote = null
|
|
164
|
-
state.verifiedPath = null
|
|
165
|
-
renderWizard()
|
|
166
|
-
await loadRemote(state.draft.values.DEVSYNC_REMOTE_PATH || '/')
|
|
167
|
-
}, 'Testing your hosting connection…') }
|
|
168
|
-
}
|
|
169
|
-
if (step === 3) {
|
|
170
|
-
if (state.remote) renderRemote()
|
|
171
|
-
else renderRemoteAddress()
|
|
172
|
-
$('#reviewSetup').onclick = () => { state.step = 4; renderWizard() }
|
|
173
|
-
}
|
|
174
|
-
if (step === 4) $('#saveSetup').onclick = () => act(async () => {
|
|
175
|
-
const label = $('#projectName').value.trim()
|
|
176
|
-
const saved = await api('POST', `/api/onboarding/${state.draft.id}/save`, { label })
|
|
177
|
-
const overridden = state.fileOverrides
|
|
178
|
-
await discardDraft()
|
|
179
|
-
await refreshProjects()
|
|
180
|
-
await openProject(saved.id)
|
|
181
|
-
message(overridden ? 'Project saved. Its environment file still takes priority over the settings snapshot.' : 'Project saved. Your source and server destination are ready.', overridden ? 'warning' : 'success')
|
|
182
|
-
}, 'Checking the destination and saving your project…')
|
|
183
|
-
setBusy(state.busy)
|
|
184
|
-
if (changedStep) {
|
|
185
|
-
const heading = $('#pane h1')
|
|
186
|
-
heading.tabIndex = -1
|
|
187
|
-
heading.focus({ preventScroll: true })
|
|
188
|
-
window.scrollTo(0, 0)
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
async function pushHosting() {
|
|
193
|
-
const entries = []
|
|
194
|
-
$$('[name][data-dirty="true"]').forEach((element) => {
|
|
195
|
-
if (element.type === 'password' && !element.value) return
|
|
196
|
-
entries.push({ key: element.name, value: element.value })
|
|
197
|
-
})
|
|
198
|
-
$$('[data-clear]:checked').forEach((element) => entries.push({ key: element.dataset.clear, value: null }))
|
|
199
|
-
if (!entries.length) return
|
|
200
|
-
noteFileOverrides(entries)
|
|
201
|
-
state.draft = await api('PUT', `/api/onboarding/${state.draft.id}`, { entries })
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function noteFileOverrides(entries) {
|
|
205
|
-
if (entries.some((entry) => ['.env', '.devsync.env'].includes(state.draft.sources[entry.key]))) state.fileOverrides = true
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
async function loadLocal(path) {
|
|
209
|
-
state.local = await api('GET', '/api/local' + (path ? `?path=${encodeURIComponent(path)}` : ''))
|
|
210
|
-
renderLocal()
|
|
211
|
-
}
|
|
212
|
-
function renderLocal() {
|
|
213
|
-
const current = state.local
|
|
214
|
-
$('#localPicker').innerHTML = `<div class="picker"><label class="help" for="localAddress">Browse local folders</label><div class="path-row"><input id="localAddress" value="${esc(current.path)}" aria-label="Local browser path"><button class="secondary" type="button" id="goLocal">Open local path</button></div><div class="picker-toolbar"><button class="secondary" type="button" data-local="${esc(current.home)}">Home</button>${current.parent ? `<button class="secondary" type="button" data-local="${esc(current.parent)}">Up one local folder</button>` : ''}${current.roots.map((root) => `<button class="secondary" type="button" data-local="${esc(root)}">${esc(root)}</button>`).join('')}</div><div class="folder-list">${current.entries.map((entry) => `<button class="folder" type="button" data-local="${esc(entry.path)}">${esc(entry.name)}</button>`).join('') || '<div class="empty">No subfolders here.</div>'}</div><button class="primary" type="button" id="useLocal">Use local folder</button></div>`
|
|
215
|
-
$$('[data-local]').forEach((button) => { button.onclick = () => act(() => loadLocal(button.dataset.local)) })
|
|
216
|
-
$('#goLocal').onclick = () => act(() => loadLocal($('#localAddress').value.trim()))
|
|
217
|
-
$('#localAddress').onkeydown = (event) => { if (event.key === 'Enter') { event.preventDefault(); $('#goLocal').click() } }
|
|
218
|
-
$('#useLocal').onclick = () => { $('#localPath').value = current.path; $('#localPicker').innerHTML = '' }
|
|
219
|
-
setBusy(state.busy)
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function renderRemoteAddress() {
|
|
223
|
-
$('#remotePicker').innerHTML = '<div class="picker"><label class="help" for="remoteAddress">Open an existing server folder</label><div class="path-row"><input id="remoteAddress" value="/" aria-label="Server browser path"><button class="secondary" id="goRemote">Open server path</button></div><div id="remoteContents"></div></div>'
|
|
224
|
-
$('#goRemote').onclick = () => act(() => loadRemote($('#remoteAddress').value.trim()))
|
|
225
|
-
$('#remoteAddress').onkeydown = (event) => { if (event.key === 'Enter') { event.preventDefault(); $('#goRemote').click() } }
|
|
226
|
-
}
|
|
227
|
-
async function loadRemote(path) {
|
|
228
|
-
state.remote = await api('POST', `/api/onboarding/${state.draft.id}/browse`, { path })
|
|
229
|
-
if (state.remote.path === state.draft.values.DEVSYNC_REMOTE_PATH) state.verifiedPath = state.remote.path
|
|
230
|
-
renderWizard()
|
|
231
|
-
}
|
|
232
|
-
function renderRemote() {
|
|
233
|
-
const current = state.remote
|
|
234
|
-
renderRemoteAddress()
|
|
235
|
-
$('#remoteAddress').value = current.path
|
|
236
|
-
$('#remoteContents').innerHTML = `<div class="picker-toolbar"><button class="secondary" data-remote="/">Account root</button>${current.parent ? `<button class="secondary" data-remote="${esc(current.parent)}">Up one server folder</button>` : ''}</div><div class="folder-list">${current.entries.map((entry) => `<button class="folder" data-remote="${esc(entry.path)}">${esc(entry.name)}</button>`).join('') || '<div class="empty">No subfolders here. You can still use this folder.</div>'}</div><button class="primary" id="useRemote">Use server folder</button>`
|
|
237
|
-
$$('[data-remote]').forEach((button) => { button.onclick = () => act(() => loadRemote(button.dataset.remote)) })
|
|
238
|
-
$('#useRemote').onclick = () => act(async () => {
|
|
239
|
-
const entries = [{ key: 'DEVSYNC_REMOTE_PATH', value: current.path }]
|
|
240
|
-
noteFileOverrides(entries)
|
|
241
|
-
state.draft = await api('PUT', `/api/onboarding/${state.draft.id}`, { entries })
|
|
242
|
-
state.verifiedPath = current.path
|
|
243
|
-
renderWizard()
|
|
244
|
-
message(`Destination selected: ${current.path}`, 'success')
|
|
245
|
-
})
|
|
246
|
-
setBusy(state.busy)
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
async function openProject(id) {
|
|
250
|
-
deploymentPanel.close()
|
|
251
|
-
const data = await api('GET', `/api/projects/${id}/env`)
|
|
252
|
-
state.current = { id, ...data }
|
|
253
|
-
state.step = 0
|
|
254
|
-
const config = data.configuration
|
|
255
|
-
const values = config.values
|
|
256
|
-
$('#pane').innerHTML = `<p class="eyebrow">SAVED PROJECT</p><div class="summary-head"><div><h1>${esc(data.label)}</h1><p class="sub">${esc(data.localPath)}</p></div><span class="ready">● ${Object.keys(config.errors).length ? 'Setup needed' : 'Project configured'}</span></div><div id="message" class="notice"></div><section class="summary-card"><h2>Source → destination</h2><dl class="review"><dt>Local source</dt><dd>${esc(data.localPath)}</dd><dt>Server folder</dt><dd>${esc(values.DEVSYNC_REMOTE_PATH || 'Not configured')}</dd><dt>Connection</dt><dd>${esc(values.DEVSYNC_TRANSPORT?.toUpperCase())} · ${esc(values.DEVSYNC_HOST || 'Not configured')}:${esc(values.DEVSYNC_PORT)}</dd><dt>Username</dt><dd>${esc(values.DEVSYNC_USERNAME || 'Not configured')}</dd><dt>Credentials</dt><dd>${config.secrets.DEVSYNC_PASSWORD || config.secrets.DEVSYNC_PRIVATE_KEY ? 'Supplied · never shown in this browser' : 'Not configured'}</dd></dl><div class="actions"><button class="primary" id="editProject">Edit setup</button><button class="secondary" id="testProject">Test connection</button><span class="spacer"></span><button class="danger" id="deleteProject">Remove project</button></div></section>`
|
|
257
|
-
if (Object.keys(config.errors).length) message(Object.values(config.errors).join('; '), 'warning')
|
|
258
|
-
$('#editProject').onclick = () => act(async () => { state.draft = await api('POST', '/api/onboarding', { projectId: id }); deploymentPanel.close(); state.fileOverrides = false; state.local = null; state.remote = null; state.verifiedPath = null; state.step = 1; renderWizard() })
|
|
259
|
-
$('#testProject').onclick = () => act(async () => { await api('POST', '/api/test', { projectId: id }); message('Connection test passed.', 'success') }, 'Testing your hosting connection…')
|
|
260
|
-
$('#deleteProject').onclick = () => act(async () => {
|
|
261
|
-
if (!confirm(`Remove "${data.label}" from devsync? Local and server files stay in place.`)) return
|
|
262
|
-
await api('DELETE', `/api/projects/${id}`)
|
|
263
|
-
state.current = null
|
|
264
|
-
await refreshProjects()
|
|
265
|
-
welcome()
|
|
266
|
-
})
|
|
267
|
-
await refreshProjects()
|
|
268
|
-
const deploymentElement = document.createElement('section')
|
|
269
|
-
deploymentElement.id = 'deploymentPanel'
|
|
270
|
-
deploymentElement.className = 'summary-card operation-card'
|
|
271
|
-
$('#pane').appendChild(deploymentElement)
|
|
272
|
-
await deploymentPanel.open(id, config)
|
|
273
|
-
window.scrollTo(0, 0)
|
|
274
|
-
setBusy(state.busy)
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
1
|
+
import { DeploymentPanel } from './deployments.js'
|
|
2
|
+
|
|
3
|
+
const $ = (selector) => document.querySelector(selector)
|
|
4
|
+
const $$ = (selector) => [...document.querySelectorAll(selector)]
|
|
5
|
+
const esc = (value) => String(value ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]))
|
|
6
|
+
const state = { projects: [], current: null, draft: null, step: 0, busy: false, local: null, remote: null, verifiedPath: null, fileOverrides: false }
|
|
7
|
+
let session
|
|
8
|
+
let renderedStep = 0
|
|
9
|
+
const deploymentPanel = new DeploymentPanel({ api, run: act, isBusy: () => state.busy, message })
|
|
10
|
+
|
|
11
|
+
async function api(method, url, body) {
|
|
12
|
+
if (!session) {
|
|
13
|
+
const response = await fetch('/api/session')
|
|
14
|
+
const data = await response.json()
|
|
15
|
+
if (!response.ok || !data.token) throw new Error(data.error || 'Could not start a local session')
|
|
16
|
+
session = data.token
|
|
17
|
+
}
|
|
18
|
+
const response = await fetch(url, {
|
|
19
|
+
method, headers: { 'content-type': 'application/json', 'x-devsync-session': session },
|
|
20
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) })
|
|
21
|
+
})
|
|
22
|
+
const data = await response.json()
|
|
23
|
+
if (!response.ok) throw new Error(data.error || 'Could not complete that operation')
|
|
24
|
+
return data
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function message(text, kind = 'info') {
|
|
28
|
+
const box = $('#message')
|
|
29
|
+
if (!box) return
|
|
30
|
+
box.textContent = text
|
|
31
|
+
box.className = `notice ${kind}`
|
|
32
|
+
box.setAttribute('role', kind === 'error' ? 'alert' : 'status')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function setBusy(busy) {
|
|
36
|
+
$('#pane').setAttribute('aria-busy', String(busy))
|
|
37
|
+
$$('button,input,select').forEach((element) => {
|
|
38
|
+
if (busy) {
|
|
39
|
+
if (element.dataset.beforeBusy === undefined) element.dataset.beforeBusy = String(element.disabled)
|
|
40
|
+
element.disabled = true
|
|
41
|
+
} else if (element.dataset.beforeBusy !== undefined) {
|
|
42
|
+
element.disabled = element.dataset.beforeBusy === 'true'
|
|
43
|
+
delete element.dataset.beforeBusy
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function act(action, text = '') {
|
|
49
|
+
if (state.busy) return
|
|
50
|
+
state.busy = true
|
|
51
|
+
setBusy(true)
|
|
52
|
+
if (text) message(text)
|
|
53
|
+
try { await action() }
|
|
54
|
+
catch (error) { message(error.message, 'error') }
|
|
55
|
+
finally { state.busy = false; setBusy(false) }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function refreshProjects() {
|
|
59
|
+
state.projects = (await api('GET', '/api/projects')).projects
|
|
60
|
+
$('#projectCount').textContent = state.projects.length
|
|
61
|
+
$('#projectList').innerHTML = state.projects.map((project) => `<button class="project ${state.current?.id === project.id ? 'active' : ''}" data-project="${project.id}"><strong>${esc(project.label)}</strong><small title="${esc(project.localPath)}">${esc(project.localPath)}</small></button>`).join('')
|
|
62
|
+
$$('[data-project]').forEach((button) => { button.onclick = () => act(async () => { await discardDraft(); await openProject(Number(button.dataset.project)) }) })
|
|
63
|
+
setBusy(state.busy)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function discardDraft() {
|
|
67
|
+
deploymentPanel.close()
|
|
68
|
+
if (state.draft) {
|
|
69
|
+
try { await api('DELETE', `/api/onboarding/${state.draft.id}`) } catch {}
|
|
70
|
+
}
|
|
71
|
+
state.draft = null
|
|
72
|
+
state.step = 0
|
|
73
|
+
state.local = null
|
|
74
|
+
state.remote = null
|
|
75
|
+
state.verifiedPath = null
|
|
76
|
+
state.fileOverrides = false
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function welcome() {
|
|
80
|
+
deploymentPanel.close()
|
|
81
|
+
$('#pane').innerHTML = `<div id="message" class="notice"></div><div class="welcome"><p class="eyebrow">FROM YOUR FOLDER TO YOUR HOSTING</p><h1>A simpler way to get your project online.</h1><p class="sub">Choose a local folder, connect your hosting account, and pick where your files belong.</p><button class="primary" id="welcomeAdd">${state.projects.length ? 'Add another project' : 'Add your first project'}</button><div class="cards"><article class="card"><div class="number">01 / LOCAL</div><h3>Start with your files</h3><p>Choose your project or build folder. We’ll pick up hosting settings from its .env.</p></article><article class="card"><div class="number">02 / CONNECT</div><h3>Bring your hosting</h3><p>Use FTP, FTPS, or SFTP details from your hosting provider.</p></article><article class="card"><div class="number">03 / DESTINATION</div><h3>Choose the right folder</h3><p>Browse your server folders and save the destination for this project.</p></article></div></div>`
|
|
82
|
+
$('#welcomeAdd').onclick = () => act(startNew)
|
|
83
|
+
setBusy(state.busy)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function startNew() {
|
|
87
|
+
await discardDraft()
|
|
88
|
+
state.current = null
|
|
89
|
+
state.step = 1
|
|
90
|
+
await refreshProjects()
|
|
91
|
+
renderWizard()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function source(key) {
|
|
95
|
+
const value = state.draft?.sources[key]
|
|
96
|
+
const supplied = state.draft?.secrets[key] || state.draft?.values[key] !== '' && state.draft?.values[key] !== undefined
|
|
97
|
+
if (value === 'default' && !supplied && key !== 'DEVSYNC_PORT') {
|
|
98
|
+
return ['DEVSYNC_HOST', 'DEVSYNC_USERNAME', 'DEVSYNC_TRANSPORT'].includes(key) ? '<span class="source">Needed</span>' : ''
|
|
99
|
+
}
|
|
100
|
+
const names = { default: 'Automatic', ui: 'Entered here', override: 'Saved', secret: 'Saved' }
|
|
101
|
+
return value ? `<span class="source">${esc(names[value] || value)}</span>` : ''
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function input(key, label, { secret = false, type = 'text', optional = false } = {}) {
|
|
105
|
+
const draft = state.draft
|
|
106
|
+
const automaticPort = key === 'DEVSYNC_PORT' && draft.sources[key] === 'default'
|
|
107
|
+
const value = secret || automaticPort ? '' : draft.values[key]
|
|
108
|
+
const present = draft.secrets[key]
|
|
109
|
+
const placeholder = present ? 'Already supplied — leave blank to keep' : automaticPort ? `Automatic (${draft.values.DEVSYNC_TRANSPORT === 'sftp' ? 22 : 21})` : ''
|
|
110
|
+
return `<div class="field"><label for="${key}">${esc(label)}${optional ? ' <span class="muted">(optional)</span>' : ''}${source(key)}</label><input id="${key}" name="${key}" type="${secret ? 'password' : type}" value="${esc(value)}" placeholder="${esc(placeholder)}" autocomplete="off">${present ? `<label class="check"><input type="checkbox" data-clear="${key}">Remove supplied ${key === 'DEVSYNC_PASSWORD' ? 'password' : 'key'}</label>` : ''}</div>`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function renderWizard() {
|
|
114
|
+
const draft = state.draft
|
|
115
|
+
const steps = ['Local folder', 'Hosting', 'Destination', 'Review']
|
|
116
|
+
const step = state.step
|
|
117
|
+
const changedStep = renderedStep !== step
|
|
118
|
+
renderedStep = step
|
|
119
|
+
let content = ''
|
|
120
|
+
if (step === 1) {
|
|
121
|
+
content = `<h2>Where are your project files?</h2><p>Choose the folder you want to deploy. For a built frontend, choose its dist or build folder.</p><form id="localForm"><div class="field"><label for="localPath">Local project folder</label><div class="path-row"><input id="localPath" value="${esc(draft?.localPath || '')}" placeholder="Absolute path to a project folder"><button class="secondary" type="button" id="openLocal">Browse folders</button></div><span class="help">Only hosting settings named DEVSYNC_* are read from this folder’s .env.</span></div><div id="localPicker"></div><div class="actions"><button class="secondary" type="button" id="cancelSetup">Cancel</button><span class="spacer"></span><button class="primary" type="submit">Continue to hosting</button></div></form>`
|
|
122
|
+
} else if (step === 2) {
|
|
123
|
+
const transport = draft.values.DEVSYNC_TRANSPORT
|
|
124
|
+
const loaded = Object.entries(draft.sources).filter(([, value]) => value === '.env' || value === '.devsync.env').length
|
|
125
|
+
content = `<h2>Connect your hosting account</h2><p>${loaded ? `Loaded ${loaded} hosting settings from this folder’s environment files. Fill in anything missing.` : 'Enter the connection details from your hosting provider.'} Imported passwords stay on this computer and are not sent to the browser.</p><form id="hostingForm" autocomplete="off"><div class="form-grid"><div class="field"><label for="DEVSYNC_TRANSPORT">Protocol${source('DEVSYNC_TRANSPORT')}</label><select id="DEVSYNC_TRANSPORT" name="DEVSYNC_TRANSPORT"><option value="">Choose a protocol</option>${['ftps', 'sftp', 'ftp'].map((value) => `<option value="${value}" ${transport === value ? 'selected' : ''}>${value === 'ftps' ? 'FTPS — encrypted FTP' : value === 'sftp' ? 'SFTP — SSH file transfer' : 'FTP — unencrypted'}</option>`).join('')}</select></div>${input('DEVSYNC_HOST', 'Host')}${input('DEVSYNC_PORT', 'Port', { type: 'number', optional: true })}${input('DEVSYNC_USERNAME', 'Username')}${input('DEVSYNC_PASSWORD', 'Password / key passphrase', { secret: true })}</div><details><summary>SSH key and advanced settings</summary><div class="form-grid">${input('DEVSYNC_PRIVATE_KEY', 'SSH private key path', { secret: true, optional: true })}<div class="field"><label for="DEVSYNC_DRY_RUN">Default operation</label><select id="DEVSYNC_DRY_RUN" name="DEVSYNC_DRY_RUN"><option value="false" ${String(draft.values.DEVSYNC_DRY_RUN) === 'false' ? 'selected' : ''}>Allow uploads when requested</option><option value="true" ${String(draft.values.DEVSYNC_DRY_RUN) === 'true' ? 'selected' : ''}>Preview only</option></select></div>${input('DEVSYNC_DEBOUNCE_MS', 'Watch delay (milliseconds)', { type: 'number', optional: true })}</div><p class="help">An SSH key is optional and applies to SFTP only. With a key, the password field is an optional passphrase. Local deletions are never mirrored.</p></details><div class="actions"><button class="secondary" type="button" id="backSetup">Back</button><button class="secondary" type="button" id="cancelSetup">Cancel</button><span class="spacer"></span><button class="primary" type="submit">Test connection & continue</button></div></form>`
|
|
126
|
+
} else if (step === 3) {
|
|
127
|
+
const selected = draft.values.DEVSYNC_REMOTE_PATH
|
|
128
|
+
content = `<h2>Choose the folder on your server</h2><p>Open a folder, then select “Use server folder”. Your local files will be placed inside this destination.</p><div class="notice success">Connection verified with ${esc(draft.values.DEVSYNC_HOST)}.</div><div class="selection"><small>Selected destination ${source('DEVSYNC_REMOTE_PATH')}</small><strong id="selectedRemote">${esc(selected || 'Choose a server folder below')}</strong></div><div id="remotePicker"></div><div class="actions"><button class="secondary" type="button" id="backSetup">Back</button><button class="secondary" type="button" id="cancelSetup">Cancel</button><span class="spacer"></span><button class="primary" id="reviewSetup" ${selected && state.verifiedPath === selected ? '' : 'disabled'}>Continue to review</button></div>`
|
|
129
|
+
} else {
|
|
130
|
+
const values = draft.values
|
|
131
|
+
content = `<h2>Ready to save your project</h2><p>Check the source and destination. Saving will not upload or delete server files.</p><div class="field"><label for="projectName">Project name</label><input id="projectName" value="${esc(draft.label)}" maxlength="80"></div><dl class="review"><dt>Local folder</dt><dd>${esc(draft.localPath)}</dd><dt>Hosting</dt><dd>${esc(values.DEVSYNC_HOST)}:${esc(values.DEVSYNC_PORT)} · ${esc(values.DEVSYNC_TRANSPORT?.toUpperCase())}</dd><dt>Username</dt><dd>${esc(values.DEVSYNC_USERNAME)}</dd><dt>Credentials</dt><dd>${draft.secrets.DEVSYNC_PASSWORD ? 'Password supplied' : 'SSH key supplied'} · stored encrypted</dd><dt>Server folder</dt><dd>${esc(values.DEVSYNC_REMOTE_PATH)}</dd></dl>${state.fileOverrides ? '<div class="notice warning">You changed settings loaded from an environment file. The file stays unchanged and will take priority on future runs. Update that file to keep those changes.</div>' : ''}<p class="snapshot-note">The CLI and dashboard will share this saved project. Your application’s .env is never rewritten or uploaded.</p><div class="actions"><button class="secondary" id="backSetup">Back</button><button class="secondary" id="cancelSetup">Cancel</button><span class="spacer"></span><button class="primary" id="saveSetup">Save project</button></div>`
|
|
132
|
+
}
|
|
133
|
+
$('#pane').innerHTML = `<p class="eyebrow">${draft?.projectId ? 'PROJECT SETTINGS' : 'ADD A PROJECT'}</p><h1>A home for your next deployment.</h1><ol class="steps">${steps.map((name, i) => `<li class="${step === i + 1 ? 'active' : step > i + 1 ? 'done' : ''}" ${step === i + 1 ? 'aria-current="step"' : ''}><span class="step-number">0${i + 1}</span>${name}</li>`).join('')}</ol><div id="message" class="notice"></div><section class="setup-card">${content}</section>`
|
|
134
|
+
$('#cancelSetup').onclick = () => act(async () => { const current = state.current; await discardDraft(); if (current) await openProject(current.id); else welcome() })
|
|
135
|
+
if ($('#backSetup')) $('#backSetup').onclick = () => act(async () => { if (step === 2) await pushHosting(); state.step--; renderWizard() })
|
|
136
|
+
if (step === 1) {
|
|
137
|
+
$('#openLocal').onclick = () => act(() => loadLocal($('#localPath').value || undefined))
|
|
138
|
+
$('#localForm').onsubmit = (event) => { event.preventDefault(); act(async () => {
|
|
139
|
+
const localPath = $('#localPath').value.trim()
|
|
140
|
+
if (!localPath) throw new Error('Choose a local project folder first')
|
|
141
|
+
if (!draft || localPath !== draft.localPath) {
|
|
142
|
+
const loaded = await api('POST', '/api/onboarding', { localPath, ...(draft?.projectId ? { projectId: draft.projectId } : {}) })
|
|
143
|
+
if (draft) try { await api('DELETE', `/api/onboarding/${draft.id}`) } catch {}
|
|
144
|
+
state.draft = loaded
|
|
145
|
+
state.fileOverrides = false
|
|
146
|
+
state.remote = null
|
|
147
|
+
state.verifiedPath = null
|
|
148
|
+
}
|
|
149
|
+
state.step = 2
|
|
150
|
+
renderWizard()
|
|
151
|
+
}, 'Reading hosting settings from the selected folder…') }
|
|
152
|
+
if (state.local) renderLocal()
|
|
153
|
+
}
|
|
154
|
+
if (step === 2) {
|
|
155
|
+
$$('[name],[data-clear]').forEach((element) => element.addEventListener('input', () => { element.dataset.dirty = 'true' }))
|
|
156
|
+
$('#DEVSYNC_TRANSPORT').addEventListener('change', () => {
|
|
157
|
+
if (!$('#DEVSYNC_PORT').value) $('#DEVSYNC_PORT').placeholder = `Automatic (${$('#DEVSYNC_TRANSPORT').value === 'sftp' ? 22 : 21})`
|
|
158
|
+
})
|
|
159
|
+
$('#hostingForm').onsubmit = (event) => { event.preventDefault(); act(async () => {
|
|
160
|
+
await pushHosting()
|
|
161
|
+
state.draft = await api('POST', `/api/onboarding/${state.draft.id}/test`, {})
|
|
162
|
+
state.step = 3
|
|
163
|
+
state.remote = null
|
|
164
|
+
state.verifiedPath = null
|
|
165
|
+
renderWizard()
|
|
166
|
+
await loadRemote(state.draft.values.DEVSYNC_REMOTE_PATH || '/')
|
|
167
|
+
}, 'Testing your hosting connection…') }
|
|
168
|
+
}
|
|
169
|
+
if (step === 3) {
|
|
170
|
+
if (state.remote) renderRemote()
|
|
171
|
+
else renderRemoteAddress()
|
|
172
|
+
$('#reviewSetup').onclick = () => { state.step = 4; renderWizard() }
|
|
173
|
+
}
|
|
174
|
+
if (step === 4) $('#saveSetup').onclick = () => act(async () => {
|
|
175
|
+
const label = $('#projectName').value.trim()
|
|
176
|
+
const saved = await api('POST', `/api/onboarding/${state.draft.id}/save`, { label })
|
|
177
|
+
const overridden = state.fileOverrides
|
|
178
|
+
await discardDraft()
|
|
179
|
+
await refreshProjects()
|
|
180
|
+
await openProject(saved.id)
|
|
181
|
+
message(overridden ? 'Project saved. Its environment file still takes priority over the settings snapshot.' : 'Project saved. Your source and server destination are ready.', overridden ? 'warning' : 'success')
|
|
182
|
+
}, 'Checking the destination and saving your project…')
|
|
183
|
+
setBusy(state.busy)
|
|
184
|
+
if (changedStep) {
|
|
185
|
+
const heading = $('#pane h1')
|
|
186
|
+
heading.tabIndex = -1
|
|
187
|
+
heading.focus({ preventScroll: true })
|
|
188
|
+
window.scrollTo(0, 0)
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function pushHosting() {
|
|
193
|
+
const entries = []
|
|
194
|
+
$$('[name][data-dirty="true"]').forEach((element) => {
|
|
195
|
+
if (element.type === 'password' && !element.value) return
|
|
196
|
+
entries.push({ key: element.name, value: element.value })
|
|
197
|
+
})
|
|
198
|
+
$$('[data-clear]:checked').forEach((element) => entries.push({ key: element.dataset.clear, value: null }))
|
|
199
|
+
if (!entries.length) return
|
|
200
|
+
noteFileOverrides(entries)
|
|
201
|
+
state.draft = await api('PUT', `/api/onboarding/${state.draft.id}`, { entries })
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function noteFileOverrides(entries) {
|
|
205
|
+
if (entries.some((entry) => ['.env', '.devsync.env'].includes(state.draft.sources[entry.key]))) state.fileOverrides = true
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function loadLocal(path) {
|
|
209
|
+
state.local = await api('GET', '/api/local' + (path ? `?path=${encodeURIComponent(path)}` : ''))
|
|
210
|
+
renderLocal()
|
|
211
|
+
}
|
|
212
|
+
function renderLocal() {
|
|
213
|
+
const current = state.local
|
|
214
|
+
$('#localPicker').innerHTML = `<div class="picker"><label class="help" for="localAddress">Browse local folders</label><div class="path-row"><input id="localAddress" value="${esc(current.path)}" aria-label="Local browser path"><button class="secondary" type="button" id="goLocal">Open local path</button></div><div class="picker-toolbar"><button class="secondary" type="button" data-local="${esc(current.home)}">Home</button>${current.parent ? `<button class="secondary" type="button" data-local="${esc(current.parent)}">Up one local folder</button>` : ''}${current.roots.map((root) => `<button class="secondary" type="button" data-local="${esc(root)}">${esc(root)}</button>`).join('')}</div><div class="folder-list">${current.entries.map((entry) => `<button class="folder" type="button" data-local="${esc(entry.path)}">${esc(entry.name)}</button>`).join('') || '<div class="empty">No subfolders here.</div>'}</div><button class="primary" type="button" id="useLocal">Use local folder</button></div>`
|
|
215
|
+
$$('[data-local]').forEach((button) => { button.onclick = () => act(() => loadLocal(button.dataset.local)) })
|
|
216
|
+
$('#goLocal').onclick = () => act(() => loadLocal($('#localAddress').value.trim()))
|
|
217
|
+
$('#localAddress').onkeydown = (event) => { if (event.key === 'Enter') { event.preventDefault(); $('#goLocal').click() } }
|
|
218
|
+
$('#useLocal').onclick = () => { $('#localPath').value = current.path; $('#localPicker').innerHTML = '' }
|
|
219
|
+
setBusy(state.busy)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function renderRemoteAddress() {
|
|
223
|
+
$('#remotePicker').innerHTML = '<div class="picker"><label class="help" for="remoteAddress">Open an existing server folder</label><div class="path-row"><input id="remoteAddress" value="/" aria-label="Server browser path"><button class="secondary" id="goRemote">Open server path</button></div><div id="remoteContents"></div></div>'
|
|
224
|
+
$('#goRemote').onclick = () => act(() => loadRemote($('#remoteAddress').value.trim()))
|
|
225
|
+
$('#remoteAddress').onkeydown = (event) => { if (event.key === 'Enter') { event.preventDefault(); $('#goRemote').click() } }
|
|
226
|
+
}
|
|
227
|
+
async function loadRemote(path) {
|
|
228
|
+
state.remote = await api('POST', `/api/onboarding/${state.draft.id}/browse`, { path })
|
|
229
|
+
if (state.remote.path === state.draft.values.DEVSYNC_REMOTE_PATH) state.verifiedPath = state.remote.path
|
|
230
|
+
renderWizard()
|
|
231
|
+
}
|
|
232
|
+
function renderRemote() {
|
|
233
|
+
const current = state.remote
|
|
234
|
+
renderRemoteAddress()
|
|
235
|
+
$('#remoteAddress').value = current.path
|
|
236
|
+
$('#remoteContents').innerHTML = `<div class="picker-toolbar"><button class="secondary" data-remote="/">Account root</button>${current.parent ? `<button class="secondary" data-remote="${esc(current.parent)}">Up one server folder</button>` : ''}</div><div class="folder-list">${current.entries.map((entry) => `<button class="folder" data-remote="${esc(entry.path)}">${esc(entry.name)}</button>`).join('') || '<div class="empty">No subfolders here. You can still use this folder.</div>'}</div><button class="primary" id="useRemote">Use server folder</button>`
|
|
237
|
+
$$('[data-remote]').forEach((button) => { button.onclick = () => act(() => loadRemote(button.dataset.remote)) })
|
|
238
|
+
$('#useRemote').onclick = () => act(async () => {
|
|
239
|
+
const entries = [{ key: 'DEVSYNC_REMOTE_PATH', value: current.path }]
|
|
240
|
+
noteFileOverrides(entries)
|
|
241
|
+
state.draft = await api('PUT', `/api/onboarding/${state.draft.id}`, { entries })
|
|
242
|
+
state.verifiedPath = current.path
|
|
243
|
+
renderWizard()
|
|
244
|
+
message(`Destination selected: ${current.path}`, 'success')
|
|
245
|
+
})
|
|
246
|
+
setBusy(state.busy)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function openProject(id) {
|
|
250
|
+
deploymentPanel.close()
|
|
251
|
+
const data = await api('GET', `/api/projects/${id}/env`)
|
|
252
|
+
state.current = { id, ...data }
|
|
253
|
+
state.step = 0
|
|
254
|
+
const config = data.configuration
|
|
255
|
+
const values = config.values
|
|
256
|
+
$('#pane').innerHTML = `<p class="eyebrow">SAVED PROJECT</p><div class="summary-head"><div><h1>${esc(data.label)}</h1><p class="sub">${esc(data.localPath)}</p></div><span class="ready">● ${Object.keys(config.errors).length ? 'Setup needed' : 'Project configured'}</span></div><div id="message" class="notice"></div><section class="summary-card"><h2>Source → destination</h2><dl class="review"><dt>Local source</dt><dd>${esc(data.localPath)}</dd><dt>Server folder</dt><dd>${esc(values.DEVSYNC_REMOTE_PATH || 'Not configured')}</dd><dt>Connection</dt><dd>${esc(values.DEVSYNC_TRANSPORT?.toUpperCase())} · ${esc(values.DEVSYNC_HOST || 'Not configured')}:${esc(values.DEVSYNC_PORT)}</dd><dt>Username</dt><dd>${esc(values.DEVSYNC_USERNAME || 'Not configured')}</dd><dt>Credentials</dt><dd>${config.secrets.DEVSYNC_PASSWORD || config.secrets.DEVSYNC_PRIVATE_KEY ? 'Supplied · never shown in this browser' : 'Not configured'}</dd></dl><div class="actions"><button class="primary" id="editProject">Edit setup</button><button class="secondary" id="testProject">Test connection</button><span class="spacer"></span><button class="danger" id="deleteProject">Remove project</button></div></section>`
|
|
257
|
+
if (Object.keys(config.errors).length) message(Object.values(config.errors).join('; '), 'warning')
|
|
258
|
+
$('#editProject').onclick = () => act(async () => { state.draft = await api('POST', '/api/onboarding', { projectId: id }); deploymentPanel.close(); state.fileOverrides = false; state.local = null; state.remote = null; state.verifiedPath = null; state.step = 1; renderWizard() })
|
|
259
|
+
$('#testProject').onclick = () => act(async () => { await api('POST', '/api/test', { projectId: id }); message('Connection test passed.', 'success') }, 'Testing your hosting connection…')
|
|
260
|
+
$('#deleteProject').onclick = () => act(async () => {
|
|
261
|
+
if (!confirm(`Remove "${data.label}" from devsync? Local and server files stay in place.`)) return
|
|
262
|
+
await api('DELETE', `/api/projects/${id}`)
|
|
263
|
+
state.current = null
|
|
264
|
+
await refreshProjects()
|
|
265
|
+
welcome()
|
|
266
|
+
})
|
|
267
|
+
await refreshProjects()
|
|
268
|
+
const deploymentElement = document.createElement('section')
|
|
269
|
+
deploymentElement.id = 'deploymentPanel'
|
|
270
|
+
deploymentElement.className = 'summary-card operation-card'
|
|
271
|
+
$('#pane').appendChild(deploymentElement)
|
|
272
|
+
await deploymentPanel.open(id, config)
|
|
273
|
+
window.scrollTo(0, 0)
|
|
274
|
+
setBusy(state.busy)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function loadAppInfo() {
|
|
278
|
+
const badge = $('#aboutVersion')
|
|
279
|
+
badge.textContent = 'Loading version…'
|
|
280
|
+
try {
|
|
281
|
+
const response = await fetch('/api/health', { cache: 'no-store' })
|
|
282
|
+
const health = await response.json()
|
|
283
|
+
if (!response.ok || typeof health.version !== 'string' || !health.version.trim()) throw new Error('Version unavailable')
|
|
284
|
+
badge.textContent = `v${health.version}`
|
|
285
|
+
if (typeof health.name === 'string' && health.name.trim()) $('#aboutPackage').textContent = health.name
|
|
286
|
+
} catch { badge.textContent = 'Version unavailable' }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
$('#openAboutHelp').onclick = async () => {
|
|
290
|
+
const dialog = $('#aboutHelpDialog')
|
|
291
|
+
if (dialog.open) return
|
|
292
|
+
dialog.showModal()
|
|
293
|
+
await loadAppInfo()
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
$('#addProject').onclick = () => act(startNew)
|
|
297
|
+
;(async () => {
|
|
298
|
+
void loadAppInfo()
|
|
299
|
+
try {
|
|
300
|
+
await refreshProjects()
|
|
301
|
+
$('#serverStatus').textContent = '● Running locally'
|
|
302
|
+
if (state.projects.length === 1) await openProject(state.projects[0].id)
|
|
303
|
+
else welcome()
|
|
304
|
+
} catch (error) {
|
|
305
|
+
$('#pane').innerHTML = '<h1>Could not load your projects</h1><div id="message" class="notice"></div>'
|
|
306
|
+
message(error.message, 'error')
|
|
307
|
+
$('#serverStatus').textContent = 'Connection unavailable'
|
|
308
|
+
}
|
|
309
|
+
})()
|