shared-hosting-sync 0.1.3 → 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 +13 -0
- package/README.md +20 -6
- package/package.json +2 -2
- package/src/arguments.js +1 -1
- package/src/cli.js +531 -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 +7 -6
- package/src/ui/logo.svg +7 -0
- package/src/ui/style.css +8 -2
- package/src/ui/ui.js +303 -293
- package/src/uploader.js +37 -9
package/src/cli.js
CHANGED
|
@@ -1,499 +1,519 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// devsync v2 CLI.
|
|
3
|
-
//
|
|
4
|
-
// Bare invocation (`devsync` with no command) is the nodemon-style flow:
|
|
5
|
-
// - canonicalise cwd
|
|
6
|
-
// - look up project by local_path in the DB
|
|
7
|
-
// - found → preview the upload plan without uploading
|
|
8
|
-
// - not found → onboard from project .env plus prompts, then preview
|
|
9
|
-
//
|
|
10
|
-
// All commands use the v2 DB (core/db.js) as source of truth. v1's
|
|
11
|
-
// ~/.devsync/config.json + .env are imported on first open by v1Migrate.
|
|
12
|
-
|
|
13
|
-
import fs from 'node:fs'
|
|
14
|
-
import path from 'node:path'
|
|
15
|
-
import { createTransport } from './transport/index.js'
|
|
16
|
-
import { buildMatcher, ensureDeployignore } from './ignore.js'
|
|
17
|
-
import { buildPlan, runPlan } from './uploader.js'
|
|
18
|
-
import { ProjectWatcher } from './watcher.js'
|
|
19
|
-
import { openDb } from './core/db.js'
|
|
20
|
-
import { maybeRunV1Import } from './core/v1Migrate.js'
|
|
21
|
-
import {
|
|
22
|
-
listProjects,
|
|
23
|
-
getProjectByLabel,
|
|
24
|
-
getProjectByLocalPath,
|
|
25
|
-
createProject,
|
|
26
|
-
deleteProject,
|
|
27
|
-
setEnvOverrides,
|
|
28
|
-
saveConfiguration,
|
|
29
|
-
setStatus,
|
|
30
|
-
recordSync,
|
|
31
|
-
hasSecrets
|
|
32
|
-
} from './core/projects.js'
|
|
33
|
-
import { resolveEnv, credsFromResolved, settingError } from './core/envResolver.js'
|
|
34
|
-
import { ENV_SCHEMA } from './core/envSchema.js'
|
|
35
|
-
import { resolveDeployment } from './core/deployment.js'
|
|
36
|
-
import { acquireOperation, assertProjectIdle, mutateProject } from './core/operationLock.js'
|
|
37
|
-
import { startServer } from './server/index.js'
|
|
38
|
-
import { HOME } from './config.js'
|
|
39
|
-
import { validateRemotePath } from './paths.js'
|
|
40
|
-
import { prepareConfiguration, assertLocalFolder } from './onboarding.js'
|
|
41
|
-
import { createPrompter } from './prompt.js'
|
|
42
|
-
import { parseArgs } from './arguments.js'
|
|
43
|
-
import { name, version } from './metadata.js'
|
|
44
|
-
|
|
45
|
-
const C = {
|
|
46
|
-
dim: (s) => `\x1b[2m${s}\x1b[0m`,
|
|
47
|
-
b: (s) => `\x1b[1m${s}\x1b[0m`,
|
|
48
|
-
g: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
49
|
-
r: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
50
|
-
y: (s) => `\x1b[33m${s}\x1b[0m`,
|
|
51
|
-
c: (s) => `\x1b[36m${s}\x1b[0m`
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let cmd, flags, positional
|
|
55
|
-
try {
|
|
56
|
-
const parsed = parseArgs(process.argv.slice(2))
|
|
57
|
-
cmd = parsed.command
|
|
58
|
-
flags = parsed.flags
|
|
59
|
-
positional = parsed.positional
|
|
60
|
-
} catch (err) {
|
|
61
|
-
console.error(err.message)
|
|
62
|
-
process.exit(1)
|
|
63
|
-
}
|
|
64
|
-
const sessionOverrides = new Map()
|
|
65
|
-
const prompt = createPrompter()
|
|
66
|
-
const flagOverrides = () => Object.fromEntries([
|
|
67
|
-
['transport', 'DEVSYNC_TRANSPORT'], ['host', 'DEVSYNC_HOST'],
|
|
68
|
-
['username', 'DEVSYNC_USERNAME'], ['port', 'DEVSYNC_PORT'], ['remote-path', 'DEVSYNC_REMOTE_PATH']
|
|
69
|
-
].filter(([flag]) => flags[flag] !== undefined).map(([flag, key]) => [key, flags[flag]]))
|
|
70
|
-
|
|
71
|
-
const kb = (n) => (n / 1024).toFixed(1) + ' KB'
|
|
72
|
-
const mb = (n) => (n / 1024 / 1024).toFixed(2) + ' MB'
|
|
73
|
-
|
|
74
|
-
function die(msg, code = 1) {
|
|
75
|
-
console.error(C.r('✗ ') + msg)
|
|
76
|
-
process.exit(code)
|
|
77
|
-
}
|
|
78
|
-
function ok(msg) { console.log(C.g('✓ ') + msg) }
|
|
79
|
-
function info(msg) { console.log(C.c('· ') + msg) }
|
|
80
|
-
function warn(msg) { console.log(C.y('! ') + msg) }
|
|
81
|
-
function retryNotice({ attempt, retries }) { warn(`Connection interrupted; reconnecting (${attempt}/${retries})`) }
|
|
82
|
-
|
|
83
|
-
function openAndMigrate() {
|
|
84
|
-
const db = openDb()
|
|
85
|
-
const mig = maybeRunV1Import()
|
|
86
|
-
if (mig.imported) {
|
|
87
|
-
info(`imported ${mig.projects.length} project(s) from v1 ~/.devsync/config.json`)
|
|
88
|
-
info(`v1 files left in place; remove them manually when ready`)
|
|
89
|
-
}
|
|
90
|
-
return db
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async function setupProject(project) {
|
|
94
|
-
if (project) assertProjectIdle(project.id)
|
|
95
|
-
const localPath = assertLocalFolder(project?.localPath || process.cwd())
|
|
96
|
-
const resolved = resolveEnv(project?.id, { localPath, envFile: flags['env-file'], overrides: flagOverrides() })
|
|
97
|
-
const prepared = await prepareConfiguration(resolved, {
|
|
98
|
-
localPath,
|
|
99
|
-
ask: prompt.interactive ? (question) => prompt.ask(question) : undefined,
|
|
100
|
-
log: info, edit: !!flags.edit, browse: !!flags.browse
|
|
101
|
-
})
|
|
102
|
-
await prompt.close()
|
|
103
|
-
const created = !project
|
|
104
|
-
if (!project) {
|
|
105
|
-
const base = flags.label || path.basename(localPath) || 'project'
|
|
106
|
-
let label = base
|
|
107
|
-
let suffix = 2
|
|
108
|
-
while (getProjectByLabel(label)) label = `${base}-${suffix++}`
|
|
109
|
-
project = createProject({ label, localPath })
|
|
110
|
-
}
|
|
111
|
-
try {
|
|
112
|
-
mutateProject(project.id, () => saveConfiguration(project.id, prepared.values, { sources: prepared.sources }))
|
|
113
|
-
ensureDeployignore(localPath)
|
|
114
|
-
} catch (err) {
|
|
115
|
-
if (created) deleteProject(project.id)
|
|
116
|
-
throw err
|
|
117
|
-
}
|
|
118
|
-
sessionOverrides.set(project.id, Object.fromEntries(Object.entries(prepared.values)
|
|
119
|
-
.filter(([key]) => prepared.sources[key] === 'prompt')))
|
|
120
|
-
ok(`saved "${project.label}" — ${localPath} → ${prepared.values.DEVSYNC_REMOTE_PATH}`)
|
|
121
|
-
return project
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async function resolveProjectOrCwd(label) {
|
|
125
|
-
if (label) {
|
|
126
|
-
const project = getProjectByLabel(label)
|
|
127
|
-
if (!project) throw new Error(`no project named "${label}" — run devsync projects`)
|
|
128
|
-
return project
|
|
129
|
-
}
|
|
130
|
-
const cwd = fs.realpathSync(process.cwd())
|
|
131
|
-
return getProjectByLocalPath(cwd) || setupProject(null)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async function credsForProject(project, { deployment = false } = {}) {
|
|
135
|
-
if (deployment) {
|
|
136
|
-
const context = resolveDeployment(project, {
|
|
137
|
-
envFile: flags['env-file'], overrides: { ...flagOverrides(), ...sessionOverrides.get(project.id) }
|
|
138
|
-
})
|
|
139
|
-
project
|
|
140
|
-
return { ...context.creds, dryRun: context.dryRun, debounceMs: context.debounceMs }
|
|
141
|
-
}
|
|
142
|
-
const r = resolveEnv(project.id, {
|
|
143
|
-
envFile: flags['env-file'], overrides: { ...flagOverrides(), ...sessionOverrides.get(project.id) }
|
|
144
|
-
})
|
|
145
|
-
const errors = ENV_SCHEMA.filter((s) => s.key !== 'DEVSYNC_REMOTE_PATH')
|
|
146
|
-
.map((s) => settingError(s.key, r.values[s.key], r.values)).filter(Boolean)
|
|
147
|
-
if (errors.length) throw new Error(`${errors.join('; ')}. Update the project .env or run devsync setup ${project.label}.`)
|
|
148
|
-
return {
|
|
149
|
-
...credsFromResolved(r.values),
|
|
150
|
-
dryRun: ['true', '1', true].includes(r.values.DEVSYNC_DRY_RUN),
|
|
151
|
-
debounceMs: Number(r.values.DEVSYNC_DEBOUNCE_MS)
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/* ------------------------------------------------------------------ */
|
|
156
|
-
|
|
157
|
-
const COMMANDS = {
|
|
158
|
-
async init() {
|
|
159
|
-
openAndMigrate()
|
|
160
|
-
ok('v2 ready')
|
|
161
|
-
info('next: devsync setup (configure the current folder in the terminal)')
|
|
162
|
-
info(' or: cd <project> && devsync')
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
async setup() {
|
|
166
|
-
openAndMigrate()
|
|
167
|
-
const project = positional[0] ? getProjectByLabel(positional[0]) : getProjectByLocalPath(fs.realpathSync(process.cwd()))
|
|
168
|
-
if (positional[0] && !project) throw new Error(`no project named "${positional[0]}"`)
|
|
169
|
-
const saved = await setupProject(project)
|
|
170
|
-
await prompt.close()
|
|
171
|
-
return COMMANDS.plan(saved)
|
|
172
|
-
},
|
|
173
|
-
|
|
174
|
-
async deploy() {
|
|
175
|
-
return COMMANDS.sync(undefined, { force: true })
|
|
176
|
-
},
|
|
177
|
-
|
|
178
|
-
async add() {
|
|
179
|
-
openAndMigrate()
|
|
180
|
-
const [label, localArg, remoteArg] = positional
|
|
181
|
-
if (!label || !localArg) die('usage: devsync add <label> <localPath> [remotePath]')
|
|
182
|
-
const localPath = path.resolve(localArg)
|
|
183
|
-
if (!fs.existsSync(localPath)) die(`local path does not exist: ${localPath}`)
|
|
184
|
-
if (!fs.statSync(localPath).isDirectory()) die(`local path is not a directory: ${localPath}`)
|
|
185
|
-
|
|
186
|
-
const remotePath = remoteArg === undefined ? null : validateRemotePath(remoteArg)
|
|
187
|
-
const proj = createProject({ label, localPath: fs.realpathSync(localPath) })
|
|
188
|
-
if (remotePath) setEnvOverrides(proj.id, [{ key: 'DEVSYNC_REMOTE_PATH', value: remotePath, isSecret: false }])
|
|
189
|
-
ensureDeployignore(localPath)
|
|
190
|
-
info(`added "${label}" — ${localPath}`)
|
|
191
|
-
info(`next: devsync setup ${label} (configure hosting entirely in the terminal)`)
|
|
192
|
-
if (remotePath) info(`destination: ${remotePath}`)
|
|
193
|
-
},
|
|
194
|
-
|
|
195
|
-
remove() {
|
|
196
|
-
openAndMigrate()
|
|
197
|
-
const [label] = positional
|
|
198
|
-
if (!label) die('usage: devsync remove <label>')
|
|
199
|
-
const p = getProjectByLabel(label)
|
|
200
|
-
if (!p) die(`no such project: ${label}`)
|
|
201
|
-
assertProjectIdle(p.id)
|
|
202
|
-
mutateProject(p.id, () => deleteProject(p.id))
|
|
203
|
-
ok(`removed "${label}"`)
|
|
204
|
-
},
|
|
205
|
-
|
|
206
|
-
projects() {
|
|
207
|
-
openAndMigrate()
|
|
208
|
-
const all = listProjects()
|
|
209
|
-
if (!all.length) {
|
|
210
|
-
info('No projects yet. Add one:')
|
|
211
|
-
console.log(` ${C.c('devsync add myapp C:\\path\\to\\app /public_html/myapp')}`)
|
|
212
|
-
console.log(` ${C.c('devsync ui')} (web form — recommended)`)
|
|
213
|
-
return
|
|
214
|
-
}
|
|
215
|
-
console.log(C.b('Projects') + C.dim(` (${all.length})`))
|
|
216
|
-
for (const p of all) {
|
|
217
|
-
const e = resolveEnv(p.id).values
|
|
218
|
-
console.log(
|
|
219
|
-
` ${C.b(p.label.padEnd(14))} ${p.localPath}\n` +
|
|
220
|
-
` ${' '.repeat(14)} ${C.dim('→')} ${e.DEVSYNC_REMOTE_PATH || '(not configured)'} ${C.dim('[' + (e.DEVSYNC_TRANSPORT || 'sftp') + ']')} ${p.lastStatus === 'error' ? C.r(p.lastStatus) : C.dim(p.lastStatus)}`
|
|
221
|
-
)
|
|
222
|
-
}
|
|
223
|
-
console.log()
|
|
224
|
-
console.log(C.dim(`stored in ${path.join(HOME, 'devsync.db')}`))
|
|
225
|
-
},
|
|
226
|
-
|
|
227
|
-
async test() {
|
|
228
|
-
openAndMigrate()
|
|
229
|
-
const proj = await resolveProjectOrCwd(positional[0])
|
|
230
|
-
const creds = await credsForProject(proj)
|
|
231
|
-
info(`connecting ${creds.transport}://${creds.username}@${creds.host}:${creds.port}`)
|
|
232
|
-
let t
|
|
233
|
-
try {
|
|
234
|
-
t = await createTransport(creds, flags.transport)
|
|
235
|
-
} catch (e) {
|
|
236
|
-
die(`connection failed: ${e.message}`)
|
|
237
|
-
}
|
|
238
|
-
ok('connected')
|
|
239
|
-
try {
|
|
240
|
-
const entries = await t.list(flags.path || '/')
|
|
241
|
-
info(`${entries.length} entries in ${flags.path || '/'}`)
|
|
242
|
-
for (const e of entries.slice(0, 20)) {
|
|
243
|
-
console.log(` ${e.type === 'dir' ? C.c('dir ') : ' '} ${e.name}`)
|
|
244
|
-
}
|
|
245
|
-
if (entries.length > 20) console.log(C.dim(` … ${entries.length - 20} more`))
|
|
246
|
-
} finally {
|
|
247
|
-
await t.close()
|
|
248
|
-
}
|
|
249
|
-
},
|
|
250
|
-
|
|
251
|
-
async ls() {
|
|
252
|
-
openAndMigrate()
|
|
253
|
-
const proj = await resolveProjectOrCwd(positional[0])
|
|
254
|
-
const creds = await credsForProject(proj)
|
|
255
|
-
const t = await createTransport(creds, flags.transport).catch((e) => die(e.message))
|
|
256
|
-
try {
|
|
257
|
-
const entries = await t.list(positional[1] || '/')
|
|
258
|
-
for (const e of entries) {
|
|
259
|
-
console.log(` ${e.type === 'dir' ? C.c('dir ') : ' '} ${e.name}`)
|
|
260
|
-
}
|
|
261
|
-
} finally {
|
|
262
|
-
await t.close()
|
|
263
|
-
}
|
|
264
|
-
},
|
|
265
|
-
|
|
266
|
-
async plan(project) {
|
|
267
|
-
openAndMigrate()
|
|
268
|
-
const proj = project || await resolveProjectOrCwd(positional[0])
|
|
269
|
-
const creds = await credsForProject(proj, { deployment: true })
|
|
270
|
-
const matcher = buildMatcher(proj.localPath, { useGitignore: false })
|
|
271
|
-
const release = acquireOperation(proj.id, 'preview', { creds, remotePath: proj.remotePath })
|
|
272
|
-
let t
|
|
273
|
-
try {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
console.log()
|
|
277
|
-
console.log(
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
)
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
console.log(C.dim(
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
console.log()
|
|
301
|
-
console.log(C.dim(`
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
const
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
})
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
const
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
console.log(
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
console.log(`
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
${
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
${C.
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
${C.b('
|
|
490
|
-
devsync
|
|
491
|
-
devsync
|
|
492
|
-
devsync
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// devsync v2 CLI.
|
|
3
|
+
//
|
|
4
|
+
// Bare invocation (`devsync` with no command) is the nodemon-style flow:
|
|
5
|
+
// - canonicalise cwd
|
|
6
|
+
// - look up project by local_path in the DB
|
|
7
|
+
// - found → preview the upload plan without uploading
|
|
8
|
+
// - not found → onboard from project .env plus prompts, then preview
|
|
9
|
+
//
|
|
10
|
+
// All commands use the v2 DB (core/db.js) as source of truth. v1's
|
|
11
|
+
// ~/.devsync/config.json + .env are imported on first open by v1Migrate.
|
|
12
|
+
|
|
13
|
+
import fs from 'node:fs'
|
|
14
|
+
import path from 'node:path'
|
|
15
|
+
import { createTransport } from './transport/index.js'
|
|
16
|
+
import { buildMatcher, ensureDeployignore } from './ignore.js'
|
|
17
|
+
import { buildPlan, runPlan, scanServer } from './uploader.js'
|
|
18
|
+
import { ProjectWatcher } from './watcher.js'
|
|
19
|
+
import { openDb } from './core/db.js'
|
|
20
|
+
import { maybeRunV1Import } from './core/v1Migrate.js'
|
|
21
|
+
import {
|
|
22
|
+
listProjects,
|
|
23
|
+
getProjectByLabel,
|
|
24
|
+
getProjectByLocalPath,
|
|
25
|
+
createProject,
|
|
26
|
+
deleteProject,
|
|
27
|
+
setEnvOverrides,
|
|
28
|
+
saveConfiguration,
|
|
29
|
+
setStatus,
|
|
30
|
+
recordSync,
|
|
31
|
+
hasSecrets
|
|
32
|
+
} from './core/projects.js'
|
|
33
|
+
import { resolveEnv, credsFromResolved, settingError } from './core/envResolver.js'
|
|
34
|
+
import { ENV_SCHEMA } from './core/envSchema.js'
|
|
35
|
+
import { resolveDeployment } from './core/deployment.js'
|
|
36
|
+
import { acquireOperation, assertProjectIdle, mutateProject } from './core/operationLock.js'
|
|
37
|
+
import { startServer } from './server/index.js'
|
|
38
|
+
import { HOME } from './config.js'
|
|
39
|
+
import { validateRemotePath } from './paths.js'
|
|
40
|
+
import { prepareConfiguration, assertLocalFolder } from './onboarding.js'
|
|
41
|
+
import { createPrompter } from './prompt.js'
|
|
42
|
+
import { parseArgs } from './arguments.js'
|
|
43
|
+
import { name, version } from './metadata.js'
|
|
44
|
+
|
|
45
|
+
const C = {
|
|
46
|
+
dim: (s) => `\x1b[2m${s}\x1b[0m`,
|
|
47
|
+
b: (s) => `\x1b[1m${s}\x1b[0m`,
|
|
48
|
+
g: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
49
|
+
r: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
50
|
+
y: (s) => `\x1b[33m${s}\x1b[0m`,
|
|
51
|
+
c: (s) => `\x1b[36m${s}\x1b[0m`
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let cmd, flags, positional
|
|
55
|
+
try {
|
|
56
|
+
const parsed = parseArgs(process.argv.slice(2))
|
|
57
|
+
cmd = parsed.command
|
|
58
|
+
flags = parsed.flags
|
|
59
|
+
positional = parsed.positional
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.error(err.message)
|
|
62
|
+
process.exit(1)
|
|
63
|
+
}
|
|
64
|
+
const sessionOverrides = new Map()
|
|
65
|
+
const prompt = createPrompter()
|
|
66
|
+
const flagOverrides = () => Object.fromEntries([
|
|
67
|
+
['transport', 'DEVSYNC_TRANSPORT'], ['host', 'DEVSYNC_HOST'],
|
|
68
|
+
['username', 'DEVSYNC_USERNAME'], ['port', 'DEVSYNC_PORT'], ['remote-path', 'DEVSYNC_REMOTE_PATH']
|
|
69
|
+
].filter(([flag]) => flags[flag] !== undefined).map(([flag, key]) => [key, flags[flag]]))
|
|
70
|
+
|
|
71
|
+
const kb = (n) => (n / 1024).toFixed(1) + ' KB'
|
|
72
|
+
const mb = (n) => (n / 1024 / 1024).toFixed(2) + ' MB'
|
|
73
|
+
|
|
74
|
+
function die(msg, code = 1) {
|
|
75
|
+
console.error(C.r('✗ ') + msg)
|
|
76
|
+
process.exit(code)
|
|
77
|
+
}
|
|
78
|
+
function ok(msg) { console.log(C.g('✓ ') + msg) }
|
|
79
|
+
function info(msg) { console.log(C.c('· ') + msg) }
|
|
80
|
+
function warn(msg) { console.log(C.y('! ') + msg) }
|
|
81
|
+
function retryNotice({ attempt, retries }) { warn(`Connection interrupted; reconnecting (${attempt}/${retries})`) }
|
|
82
|
+
|
|
83
|
+
function openAndMigrate() {
|
|
84
|
+
const db = openDb()
|
|
85
|
+
const mig = maybeRunV1Import()
|
|
86
|
+
if (mig.imported) {
|
|
87
|
+
info(`imported ${mig.projects.length} project(s) from v1 ~/.devsync/config.json`)
|
|
88
|
+
info(`v1 files left in place; remove them manually when ready`)
|
|
89
|
+
}
|
|
90
|
+
return db
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function setupProject(project) {
|
|
94
|
+
if (project) assertProjectIdle(project.id)
|
|
95
|
+
const localPath = assertLocalFolder(project?.localPath || process.cwd())
|
|
96
|
+
const resolved = resolveEnv(project?.id, { localPath, envFile: flags['env-file'], overrides: flagOverrides() })
|
|
97
|
+
const prepared = await prepareConfiguration(resolved, {
|
|
98
|
+
localPath,
|
|
99
|
+
ask: prompt.interactive ? (question) => prompt.ask(question) : undefined,
|
|
100
|
+
log: info, edit: !!flags.edit, browse: !!flags.browse
|
|
101
|
+
})
|
|
102
|
+
await prompt.close()
|
|
103
|
+
const created = !project
|
|
104
|
+
if (!project) {
|
|
105
|
+
const base = flags.label || path.basename(localPath) || 'project'
|
|
106
|
+
let label = base
|
|
107
|
+
let suffix = 2
|
|
108
|
+
while (getProjectByLabel(label)) label = `${base}-${suffix++}`
|
|
109
|
+
project = createProject({ label, localPath })
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
mutateProject(project.id, () => saveConfiguration(project.id, prepared.values, { sources: prepared.sources }))
|
|
113
|
+
ensureDeployignore(localPath)
|
|
114
|
+
} catch (err) {
|
|
115
|
+
if (created) deleteProject(project.id)
|
|
116
|
+
throw err
|
|
117
|
+
}
|
|
118
|
+
sessionOverrides.set(project.id, Object.fromEntries(Object.entries(prepared.values)
|
|
119
|
+
.filter(([key]) => prepared.sources[key] === 'prompt')))
|
|
120
|
+
ok(`saved "${project.label}" — ${localPath} → ${prepared.values.DEVSYNC_REMOTE_PATH}`)
|
|
121
|
+
return project
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function resolveProjectOrCwd(label) {
|
|
125
|
+
if (label) {
|
|
126
|
+
const project = getProjectByLabel(label)
|
|
127
|
+
if (!project) throw new Error(`no project named "${label}" — run devsync projects`)
|
|
128
|
+
return project
|
|
129
|
+
}
|
|
130
|
+
const cwd = fs.realpathSync(process.cwd())
|
|
131
|
+
return getProjectByLocalPath(cwd) || setupProject(null)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function credsForProject(project, { deployment = false } = {}) {
|
|
135
|
+
if (deployment) {
|
|
136
|
+
const context = resolveDeployment(project, {
|
|
137
|
+
envFile: flags['env-file'], overrides: { ...flagOverrides(), ...sessionOverrides.get(project.id) }
|
|
138
|
+
})
|
|
139
|
+
Object.assign(project, context.project)
|
|
140
|
+
return { ...context.creds, dryRun: context.dryRun, debounceMs: context.debounceMs }
|
|
141
|
+
}
|
|
142
|
+
const r = resolveEnv(project.id, {
|
|
143
|
+
envFile: flags['env-file'], overrides: { ...flagOverrides(), ...sessionOverrides.get(project.id) }
|
|
144
|
+
})
|
|
145
|
+
const errors = ENV_SCHEMA.filter((s) => s.key !== 'DEVSYNC_REMOTE_PATH')
|
|
146
|
+
.map((s) => settingError(s.key, r.values[s.key], r.values)).filter(Boolean)
|
|
147
|
+
if (errors.length) throw new Error(`${errors.join('; ')}. Update the project .env or run devsync setup ${project.label}.`)
|
|
148
|
+
return {
|
|
149
|
+
...credsFromResolved(r.values),
|
|
150
|
+
dryRun: ['true', '1', true].includes(r.values.DEVSYNC_DRY_RUN),
|
|
151
|
+
debounceMs: Number(r.values.DEVSYNC_DEBOUNCE_MS)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/* ------------------------------------------------------------------ */
|
|
156
|
+
|
|
157
|
+
const COMMANDS = {
|
|
158
|
+
async init() {
|
|
159
|
+
openAndMigrate()
|
|
160
|
+
ok('v2 ready')
|
|
161
|
+
info('next: devsync setup (configure the current folder in the terminal)')
|
|
162
|
+
info(' or: cd <project> && devsync')
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
async setup() {
|
|
166
|
+
openAndMigrate()
|
|
167
|
+
const project = positional[0] ? getProjectByLabel(positional[0]) : getProjectByLocalPath(fs.realpathSync(process.cwd()))
|
|
168
|
+
if (positional[0] && !project) throw new Error(`no project named "${positional[0]}"`)
|
|
169
|
+
const saved = await setupProject(project)
|
|
170
|
+
await prompt.close()
|
|
171
|
+
return COMMANDS.plan(saved)
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
async deploy() {
|
|
175
|
+
return COMMANDS.sync(undefined, { force: true })
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
async add() {
|
|
179
|
+
openAndMigrate()
|
|
180
|
+
const [label, localArg, remoteArg] = positional
|
|
181
|
+
if (!label || !localArg) die('usage: devsync add <label> <localPath> [remotePath]')
|
|
182
|
+
const localPath = path.resolve(localArg)
|
|
183
|
+
if (!fs.existsSync(localPath)) die(`local path does not exist: ${localPath}`)
|
|
184
|
+
if (!fs.statSync(localPath).isDirectory()) die(`local path is not a directory: ${localPath}`)
|
|
185
|
+
|
|
186
|
+
const remotePath = remoteArg === undefined ? null : validateRemotePath(remoteArg)
|
|
187
|
+
const proj = createProject({ label, localPath: fs.realpathSync(localPath) })
|
|
188
|
+
if (remotePath) setEnvOverrides(proj.id, [{ key: 'DEVSYNC_REMOTE_PATH', value: remotePath, isSecret: false }])
|
|
189
|
+
ensureDeployignore(localPath)
|
|
190
|
+
info(`added "${label}" — ${localPath}`)
|
|
191
|
+
info(`next: devsync setup ${label} (configure hosting entirely in the terminal)`)
|
|
192
|
+
if (remotePath) info(`destination: ${remotePath}`)
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
remove() {
|
|
196
|
+
openAndMigrate()
|
|
197
|
+
const [label] = positional
|
|
198
|
+
if (!label) die('usage: devsync remove <label>')
|
|
199
|
+
const p = getProjectByLabel(label)
|
|
200
|
+
if (!p) die(`no such project: ${label}`)
|
|
201
|
+
assertProjectIdle(p.id)
|
|
202
|
+
mutateProject(p.id, () => deleteProject(p.id))
|
|
203
|
+
ok(`removed "${label}"`)
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
projects() {
|
|
207
|
+
openAndMigrate()
|
|
208
|
+
const all = listProjects()
|
|
209
|
+
if (!all.length) {
|
|
210
|
+
info('No projects yet. Add one:')
|
|
211
|
+
console.log(` ${C.c('devsync add myapp C:\\path\\to\\app /public_html/myapp')}`)
|
|
212
|
+
console.log(` ${C.c('devsync ui')} (web form — recommended)`)
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
console.log(C.b('Projects') + C.dim(` (${all.length})`))
|
|
216
|
+
for (const p of all) {
|
|
217
|
+
const e = resolveEnv(p.id).values
|
|
218
|
+
console.log(
|
|
219
|
+
` ${C.b(p.label.padEnd(14))} ${p.localPath}\n` +
|
|
220
|
+
` ${' '.repeat(14)} ${C.dim('→')} ${e.DEVSYNC_REMOTE_PATH || '(not configured)'} ${C.dim('[' + (e.DEVSYNC_TRANSPORT || 'sftp') + ']')} ${p.lastStatus === 'error' ? C.r(p.lastStatus) : C.dim(p.lastStatus)}`
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
console.log()
|
|
224
|
+
console.log(C.dim(`stored in ${path.join(HOME, 'devsync.db')}`))
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
async test() {
|
|
228
|
+
openAndMigrate()
|
|
229
|
+
const proj = await resolveProjectOrCwd(positional[0])
|
|
230
|
+
const creds = await credsForProject(proj)
|
|
231
|
+
info(`connecting ${creds.transport}://${creds.username}@${creds.host}:${creds.port}`)
|
|
232
|
+
let t
|
|
233
|
+
try {
|
|
234
|
+
t = await createTransport(creds, flags.transport)
|
|
235
|
+
} catch (e) {
|
|
236
|
+
die(`connection failed: ${e.message}`)
|
|
237
|
+
}
|
|
238
|
+
ok('connected')
|
|
239
|
+
try {
|
|
240
|
+
const entries = await t.list(flags.path || '/')
|
|
241
|
+
info(`${entries.length} entries in ${flags.path || '/'}`)
|
|
242
|
+
for (const e of entries.slice(0, 20)) {
|
|
243
|
+
console.log(` ${e.type === 'dir' ? C.c('dir ') : ' '} ${e.name}`)
|
|
244
|
+
}
|
|
245
|
+
if (entries.length > 20) console.log(C.dim(` … ${entries.length - 20} more`))
|
|
246
|
+
} finally {
|
|
247
|
+
await t.close()
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
async ls() {
|
|
252
|
+
openAndMigrate()
|
|
253
|
+
const proj = await resolveProjectOrCwd(positional[0])
|
|
254
|
+
const creds = await credsForProject(proj)
|
|
255
|
+
const t = await createTransport(creds, flags.transport).catch((e) => die(e.message))
|
|
256
|
+
try {
|
|
257
|
+
const entries = await t.list(positional[1] || '/')
|
|
258
|
+
for (const e of entries) {
|
|
259
|
+
console.log(` ${e.type === 'dir' ? C.c('dir ') : ' '} ${e.name}`)
|
|
260
|
+
}
|
|
261
|
+
} finally {
|
|
262
|
+
await t.close()
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
|
|
266
|
+
async plan(project) {
|
|
267
|
+
openAndMigrate()
|
|
268
|
+
const proj = project || await resolveProjectOrCwd(positional[0])
|
|
269
|
+
const creds = await credsForProject(proj, { deployment: true })
|
|
270
|
+
const matcher = buildMatcher(proj.localPath, { useGitignore: false })
|
|
271
|
+
const release = acquireOperation(proj.id, 'preview', { creds, remotePath: proj.remotePath })
|
|
272
|
+
let t
|
|
273
|
+
try {
|
|
274
|
+
const p = await buildPlan(proj, matcher, null, { force: !!flags.force })
|
|
275
|
+
console.log()
|
|
276
|
+
console.log(C.b(`Plan for "${proj.label}"`) + C.dim(` ${proj.localPath} → ${proj.remotePath}`))
|
|
277
|
+
console.log(
|
|
278
|
+
C.dim(
|
|
279
|
+
` ${p.totalFiles} file(s) considered · ${p.uploads.length} to upload · ${p.skipped.length} unchanged · ${p.excludedCount} excluded entries · ${mb(p.comparedBytes)} outbound`
|
|
280
|
+
)
|
|
281
|
+
)
|
|
282
|
+
const blocked = matcher.blockedSample()
|
|
283
|
+
if (blocked.length) {
|
|
284
|
+
console.log()
|
|
285
|
+
warn(`blocked by hard policy (never uploadable): ${blocked.join(', ')}${blocked.length >= 20 ? ' …' : ''}`)
|
|
286
|
+
}
|
|
287
|
+
if (p.uploads.length) {
|
|
288
|
+
console.log()
|
|
289
|
+
for (const u of p.uploads.slice(0, 40)) {
|
|
290
|
+
console.log(` ${C.g('+')} ${u.rel} ${C.dim('(' + kb(u.bytes) + ' → ' + u.remote + ')')}`)
|
|
291
|
+
}
|
|
292
|
+
if (p.uploads.length > 40) console.log(C.dim(` … ${p.uploads.length - 40} more`))
|
|
293
|
+
}
|
|
294
|
+
if (p.excluded.length) {
|
|
295
|
+
console.log(C.dim('\n excluded (directories represent their contents):'))
|
|
296
|
+
for (const entry of p.excluded.slice(0, 40)) console.log(C.dim(` - ${entry.rel} (${entry.reason})`))
|
|
297
|
+
if (p.excludedCount > 40) console.log(C.dim(` … ${p.excludedCount - 40} more`))
|
|
298
|
+
}
|
|
299
|
+
console.log()
|
|
300
|
+
console.log(C.dim(` run: devsync sync ${proj.label} (apply)`))
|
|
301
|
+
console.log(C.dim(` watch: devsync watch ${proj.label} (continuous)`))
|
|
302
|
+
} finally {
|
|
303
|
+
try { if (t) await t.close() } finally { release() }
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
async sync(project, { force = !!flags.force } = {}) {
|
|
308
|
+
openAndMigrate()
|
|
309
|
+
const proj = project || await resolveProjectOrCwd(positional[0])
|
|
310
|
+
const creds = await credsForProject(proj, { deployment: true })
|
|
311
|
+
const matcher = buildMatcher(proj.localPath, { useGitignore: false })
|
|
312
|
+
let t
|
|
313
|
+
const release = acquireOperation(proj.id, force ? 'deploy' : 'sync', { creds, remotePath: proj.remotePath })
|
|
314
|
+
setStatus(proj.id, 'syncing')
|
|
315
|
+
|
|
316
|
+
const dryRun = creds.dryRun || !!flags['dry-run'] || !!flags.plan
|
|
317
|
+
|
|
318
|
+
try {
|
|
319
|
+
const p = await buildPlan(proj, matcher, null, { force })
|
|
320
|
+
console.log()
|
|
321
|
+
console.log(C.b(`${cmd === 'deploy' ? 'Deploy' : 'Sync'} "${proj.label}"`) + C.dim(` ${p.uploads.length} file(s) · ${mb(p.comparedBytes)}`))
|
|
322
|
+
if (dryRun) {
|
|
323
|
+
console.log(C.y(' dry run — nothing was uploaded'))
|
|
324
|
+
for (const u of p.uploads.slice(0, 40)) console.log(` ${C.g('+')} ${u.rel}`)
|
|
325
|
+
setStatus(proj.id, 'idle')
|
|
326
|
+
return
|
|
327
|
+
}
|
|
328
|
+
if (!p.uploads.length) {
|
|
329
|
+
ok('nothing to upload — already up to date')
|
|
330
|
+
recordSync(proj.id, 0)
|
|
331
|
+
setStatus(proj.id, 'idle')
|
|
332
|
+
return
|
|
333
|
+
}
|
|
334
|
+
t = await createTransport(creds, undefined, { onRetry: retryNotice })
|
|
335
|
+
let lastPct = -1
|
|
336
|
+
const r = await runPlan(p, t, {
|
|
337
|
+
dryRun,
|
|
338
|
+
onProgress: ({ index, total, item }) => {
|
|
339
|
+
const pct = Math.floor(((index + 1) / total) * 100)
|
|
340
|
+
if (pct !== lastPct && pct % 10 === 0) {
|
|
341
|
+
lastPct = pct
|
|
342
|
+
process.stdout.write(C.dim(` ${pct}% ${item.rel}\n`))
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
})
|
|
346
|
+
ok(`uploaded ${r.done.length} file(s), ${mb(r.bytes)}`)
|
|
347
|
+
if (r.failed.length) {
|
|
348
|
+
setStatus(proj.id, 'error', { error: r.failed.map((f) => `${f.rel}: ${f.error}`).join('; ') })
|
|
349
|
+
warn(`${r.failed.length} failed`)
|
|
350
|
+
for (const f of r.failed) console.log(` ${C.r('✗')} ${f.rel} — ${f.error}`)
|
|
351
|
+
process.exitCode = 1
|
|
352
|
+
} else {
|
|
353
|
+
if (!dryRun) recordSync(proj.id, r.bytes)
|
|
354
|
+
setStatus(proj.id, 'idle', { error: null })
|
|
355
|
+
}
|
|
356
|
+
} catch (e) {
|
|
357
|
+
setStatus(proj.id, 'error', { error: e.message })
|
|
358
|
+
throw e
|
|
359
|
+
} finally {
|
|
360
|
+
try { if (t) await t.close() } finally { release() }
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
async scan() {
|
|
365
|
+
openAndMigrate()
|
|
366
|
+
const proj = await resolveProjectOrCwd(positional[0])
|
|
367
|
+
const creds = await credsForProject(proj, { deployment: true })
|
|
368
|
+
if (!flags.confirm) {
|
|
369
|
+
if (!prompt.interactive) throw new Error('Server scan needs confirmation. Use devsync scan [label] --confirm for first-time sync.')
|
|
370
|
+
const answer = await prompt.ask({ label: 'Are you sure? Use this button only if you are syncing for the first time. This scan reads server files and rebuilds the sync baseline without uploading. Continue (yes/no)', defaultValue: 'no' })
|
|
371
|
+
if (answer.trim().toLowerCase() !== 'yes') { info('Server scan cancelled'); return }
|
|
372
|
+
}
|
|
373
|
+
const release = acquireOperation(proj.id, 'scan', { creds, remotePath: proj.remotePath })
|
|
374
|
+
let transport
|
|
375
|
+
try {
|
|
376
|
+
transport = await createTransport(creds, undefined, { onRetry: retryNotice })
|
|
377
|
+
const plan = await scanServer(proj, buildMatcher(proj.localPath), transport)
|
|
378
|
+
ok(`Server scan complete: ${plan.skipped.length} matching, ${plan.uploads.length} new or changed. No files uploaded.`)
|
|
379
|
+
info(`Next: devsync sync ${proj.label}`)
|
|
380
|
+
} finally { try { if (transport) await transport.close() } finally { release() } }
|
|
381
|
+
},
|
|
382
|
+
|
|
383
|
+
async watch() {
|
|
384
|
+
openAndMigrate()
|
|
385
|
+
const proj = await resolveProjectOrCwd(positional[0])
|
|
386
|
+
const creds = await credsForProject(proj, { deployment: true })
|
|
387
|
+
const matcher = buildMatcher(proj.localPath, { useGitignore: false })
|
|
388
|
+
ensureDeployignore(proj.localPath)
|
|
389
|
+
const release = acquireOperation(proj.id, 'watch', { creds, remotePath: proj.remotePath })
|
|
390
|
+
let t
|
|
391
|
+
try { t = await createTransport(creds, flags.transport, { onRetry: retryNotice }) }
|
|
392
|
+
catch (error) { release(); throw error }
|
|
393
|
+
setStatus(proj.id, 'watching')
|
|
394
|
+
|
|
395
|
+
console.log()
|
|
396
|
+
console.log(C.b(`Watching "${proj.label}"`))
|
|
397
|
+
console.log(C.dim(` ${proj.localPath} → ${proj.remotePath} (${creds.transport})`))
|
|
398
|
+
console.log(C.dim(' Ctrl+C to stop\n'))
|
|
399
|
+
|
|
400
|
+
const w = new ProjectWatcher(proj, matcher, t, {
|
|
401
|
+
dryRun: creds.dryRun || !!flags['dry-run'],
|
|
402
|
+
debounceMs: creds.debounceMs,
|
|
403
|
+
onEvent: (e) => {
|
|
404
|
+
if (e.type === 'error') {
|
|
405
|
+
setStatus(proj.id, 'error', { error: e.message })
|
|
406
|
+
console.log(C.r(' ✗ ') + e.message)
|
|
407
|
+
}
|
|
408
|
+
else if (e.type === 'change') console.log(C.dim(` ~ ${e.kind} ${e.file}`))
|
|
409
|
+
else if (e.type === 'retry') warn(e.message)
|
|
410
|
+
},
|
|
411
|
+
onSettle: (s) => {
|
|
412
|
+
setStatus(proj.id, s.failed.length ? 'error' : 'watching', {
|
|
413
|
+
error: s.failed.length ? s.failed.map((f) => `${f.rel}: ${f.error}`).join('; ') : null
|
|
414
|
+
})
|
|
415
|
+
if (!s.dryRun && !s.failed.length) recordSync(proj.id, s.bytes)
|
|
416
|
+
if (s.uploaded || s.dryRun && s.planned) {
|
|
417
|
+
console.log(C.g(` ${s.dryRun ? 'preview' : '↑'} ${s.dryRun ? s.planned : s.uploaded} file(s), ${kb(s.bytes)}`) + C.dim(` (${s.considered} scanned, ${s.skipped} unchanged)`))
|
|
418
|
+
}
|
|
419
|
+
for (const f of s.failed) console.log(C.r(` ✗ ${f.rel} — ${f.error}`))
|
|
420
|
+
}
|
|
421
|
+
})
|
|
422
|
+
try { w.start() } catch (error) { try { await t.close() } finally { release() }; throw error }
|
|
423
|
+
|
|
424
|
+
let stopping = false
|
|
425
|
+
const shutdown = async () => {
|
|
426
|
+
if (stopping) return
|
|
427
|
+
stopping = true
|
|
428
|
+
console.log(C.dim('\nstopping…'))
|
|
429
|
+
try {
|
|
430
|
+
await w.stop()
|
|
431
|
+
await t.close()
|
|
432
|
+
setStatus(proj.id, 'idle')
|
|
433
|
+
} finally { release() }
|
|
434
|
+
process.exit(0)
|
|
435
|
+
}
|
|
436
|
+
process.on('SIGINT', shutdown)
|
|
437
|
+
process.on('SIGTERM', shutdown)
|
|
438
|
+
},
|
|
439
|
+
|
|
440
|
+
async ui() {
|
|
441
|
+
openAndMigrate()
|
|
442
|
+
const port = flags.port || 5180
|
|
443
|
+
const { url } = await startServer({ port: Number(port), openBrowser: flags.browser !== false })
|
|
444
|
+
console.log()
|
|
445
|
+
console.log(C.b('devsync UI'))
|
|
446
|
+
console.log(` ${C.c(url)}`)
|
|
447
|
+
console.log()
|
|
448
|
+
console.log(C.dim(' loopback only — session token required for mutations'))
|
|
449
|
+
console.log(C.dim(' Ctrl+C to stop'))
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
doctor() {
|
|
453
|
+
openAndMigrate()
|
|
454
|
+
const all = listProjects()
|
|
455
|
+
console.log(C.b('devsync doctor') + '\n')
|
|
456
|
+
console.log(` node ${process.version}`)
|
|
457
|
+
console.log(` home ${HOME}`)
|
|
458
|
+
console.log(` db ${fs.existsSync(path.join(HOME, 'devsync.db')) ? C.g('present') : C.y('missing')} ${path.join(HOME, 'devsync.db')}`)
|
|
459
|
+
console.log(` master.key ${fs.existsSync(path.join(HOME, 'master.key')) ? C.g('present') : C.y('missing')}`)
|
|
460
|
+
console.log(` projects ${all.length}`)
|
|
461
|
+
console.log()
|
|
462
|
+
console.log(C.b('projects:'))
|
|
463
|
+
for (const p of all) {
|
|
464
|
+
const exists = fs.existsSync(p.localPath)
|
|
465
|
+
const ig = fs.existsSync(path.join(p.localPath, '.deployignore'))
|
|
466
|
+
const hasS = hasSecrets(p.id)
|
|
467
|
+
console.log(
|
|
468
|
+
` ${exists ? C.g('✓') : C.r('✗')} ${p.label.padEnd(14)} local=${exists ? 'ok' : C.r('missing')} deployignore=${ig ? 'ok' : C.y('none')} creds=${hasS ? C.g('set') : C.y('none')}`
|
|
469
|
+
)
|
|
470
|
+
}
|
|
471
|
+
},
|
|
472
|
+
|
|
473
|
+
help() {
|
|
474
|
+
console.log(`
|
|
475
|
+
${C.b(name)} ${version} — local projects → shared hosting
|
|
476
|
+
Run commands as shared-hosting-sync or its shorter alias devsync.
|
|
477
|
+
|
|
478
|
+
${C.b('first-time setup')}
|
|
479
|
+
devsync onboard the current folder and preview
|
|
480
|
+
devsync setup [label] fill missing settings and preview
|
|
481
|
+
devsync ui open the web UI
|
|
482
|
+
devsync add <label> <localPath> [remotePath]
|
|
483
|
+
register a project; then run setup
|
|
484
|
+
devsync projects list registered projects
|
|
485
|
+
|
|
486
|
+
${C.b('bare invocation')}
|
|
487
|
+
cd <project> && devsync preview the cwd's project
|
|
488
|
+
|
|
489
|
+
${C.b('per-project')}
|
|
490
|
+
devsync test [label] check connectivity
|
|
491
|
+
devsync ls [label] [remotePath] browse the remote
|
|
492
|
+
devsync plan [label] preview, no upload
|
|
493
|
+
devsync deploy [label] upload all eligible files
|
|
494
|
+
devsync sync [label] upload changes using the saved database baseline
|
|
495
|
+
devsync scan [label] first-time server scan (asks for confirmation)
|
|
496
|
+
devsync watch [label] continuous, debounced
|
|
497
|
+
|
|
498
|
+
${C.b('options')}
|
|
499
|
+
--env-file <path> read a specific environment file
|
|
500
|
+
--transport=ftp|ftps|sftp override protocol
|
|
501
|
+
--host <host> --username <user> --port <port>
|
|
502
|
+
--remote-path <path> override server destination
|
|
503
|
+
--edit setup: edit connection settings
|
|
504
|
+
--browse setup: choose a different server folder
|
|
505
|
+
--label <name> name a new project
|
|
506
|
+
--confirm confirm a first-time server scan
|
|
507
|
+
--dry-run --force preview / include unchanged files
|
|
508
|
+
|
|
509
|
+
${C.b('other')}
|
|
510
|
+
devsync remove <label>
|
|
511
|
+
devsync doctor health check
|
|
512
|
+
devsync help
|
|
513
|
+
`)
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
497
517
|
function printBanner() {
|
|
498
518
|
const color = process.stdout.isTTY && !('NO_COLOR' in process.env)
|
|
499
519
|
const paint = (style, value) => color ? style(value) : value
|
|
@@ -524,19 +544,19 @@ function printBanner() {
|
|
|
524
544
|
console.log()
|
|
525
545
|
}
|
|
526
546
|
|
|
527
|
-
async function main() {
|
|
528
|
-
try {
|
|
529
|
-
if (flags.version || cmd === 'version') { console.log(`${name} ${version}`); return }
|
|
547
|
+
async function main() {
|
|
548
|
+
try {
|
|
549
|
+
if (flags.version || cmd === 'version') { console.log(`${name} ${version}`); return }
|
|
530
550
|
printBanner()
|
|
531
|
-
if (flags.help || cmd === 'help') return COMMANDS.help()
|
|
532
|
-
if (!cmd) return flags.sync ? await COMMANDS.sync() : await COMMANDS.plan()
|
|
533
|
-
if (!Object.hasOwn(COMMANDS, cmd)) throw new Error(`unknown command: ${cmd}. Run devsync help`)
|
|
534
|
-
return await COMMANDS[cmd]()
|
|
535
|
-
} finally {
|
|
536
|
-
await prompt.close()
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
main().catch((err) => {
|
|
540
|
-
console.error(C.r('\n✗ ') + (err?.message || String(err)))
|
|
541
|
-
process.exitCode = 1
|
|
542
|
-
})
|
|
551
|
+
if (flags.help || cmd === 'help') return COMMANDS.help()
|
|
552
|
+
if (!cmd) return flags.sync ? await COMMANDS.sync() : await COMMANDS.plan()
|
|
553
|
+
if (!Object.hasOwn(COMMANDS, cmd)) throw new Error(`unknown command: ${cmd}. Run devsync help`)
|
|
554
|
+
return await COMMANDS[cmd]()
|
|
555
|
+
} finally {
|
|
556
|
+
await prompt.close()
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
main().catch((err) => {
|
|
560
|
+
console.error(C.r('\n✗ ') + (err?.message || String(err)))
|
|
561
|
+
process.exitCode = 1
|
|
562
|
+
})
|