shared-hosting-sync 0.1.3 → 0.1.5

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/src/cli.js CHANGED
@@ -1,499 +1,523 @@
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.remotePath = context.project.remotePath
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
- t = await createTransport(creds, flags.transport, { onRetry: retryNotice })
275
- const p = await buildPlan(proj, matcher, t, { force: !!flags.force })
276
- console.log()
277
- console.log(C.b(`Plan for "${proj.label}"`) + C.dim(` ${proj.localPath} → ${proj.remotePath}`))
278
- console.log(
279
- C.dim(
280
- ` ${p.totalFiles} file(s) considered · ${p.uploads.length} to upload · ${p.skipped.length} unchanged · ${p.excludedCount} excluded entries · ${mb(p.comparedBytes)} outbound`
281
- )
282
- )
283
- const blocked = matcher.blockedSample()
284
- if (blocked.length) {
285
- console.log()
286
- warn(`blocked by hard policy (never uploadable): ${blocked.join(', ')}${blocked.length >= 20 ? ' …' : ''}`)
287
- }
288
- if (p.uploads.length) {
289
- console.log()
290
- for (const u of p.uploads.slice(0, 40)) {
291
- console.log(` ${C.g('+')} ${u.rel} ${C.dim('(' + kb(u.bytes) + ' → ' + u.remote + ')')}`)
292
- }
293
- if (p.uploads.length > 40) console.log(C.dim(` … ${p.uploads.length - 40} more`))
294
- }
295
- if (p.excluded.length) {
296
- console.log(C.dim('\n excluded (directories represent their contents):'))
297
- for (const entry of p.excluded.slice(0, 40)) console.log(C.dim(` - ${entry.rel} (${entry.reason})`))
298
- if (p.excludedCount > 40) console.log(C.dim(` … ${p.excludedCount - 40} more`))
299
- }
300
- console.log()
301
- console.log(C.dim(` run: devsync sync ${proj.label} (apply)`))
302
- console.log(C.dim(` watch: devsync watch ${proj.label} (continuous)`))
303
- } finally {
304
- try { if (t) await t.close() } finally { release() }
305
- }
306
- },
307
-
308
- async sync(project, { force = !!flags.force } = {}) {
309
- openAndMigrate()
310
- const proj = project || await resolveProjectOrCwd(positional[0])
311
- const creds = await credsForProject(proj, { deployment: true })
312
- const matcher = buildMatcher(proj.localPath, { useGitignore: false })
313
- let t
314
- const release = acquireOperation(proj.id, force ? 'deploy' : 'sync', { creds, remotePath: proj.remotePath })
315
- setStatus(proj.id, 'syncing')
316
-
317
- const dryRun = creds.dryRun || !!flags['dry-run'] || !!flags.plan
318
-
319
- try {
320
- t = await createTransport(creds, undefined, { onRetry: retryNotice })
321
- const p = await buildPlan(proj, matcher, t, { force })
322
- console.log()
323
- console.log(C.b(`${cmd === 'deploy' ? 'Deploy' : 'Sync'} "${proj.label}"`) + C.dim(` ${p.uploads.length} file(s) · ${mb(p.comparedBytes)}`))
324
- if (dryRun) {
325
- console.log(C.y(' dry run — nothing was uploaded'))
326
- for (const u of p.uploads.slice(0, 40)) console.log(` ${C.g('+')} ${u.rel}`)
327
- setStatus(proj.id, 'idle')
328
- return
329
- }
330
- if (!p.uploads.length) {
331
- ok('nothing to upload — already up to date')
332
- recordSync(proj.id, 0)
333
- setStatus(proj.id, 'idle')
334
- return
335
- }
336
- let lastPct = -1
337
- const r = await runPlan(p, t, {
338
- dryRun,
339
- onProgress: ({ index, total, item }) => {
340
- const pct = Math.floor(((index + 1) / total) * 100)
341
- if (pct !== lastPct && pct % 10 === 0) {
342
- lastPct = pct
343
- process.stdout.write(C.dim(` ${pct}% ${item.rel}\n`))
344
- }
345
- }
346
- })
347
- ok(`uploaded ${r.done.length} file(s), ${mb(r.bytes)}`)
348
- if (r.failed.length) {
349
- setStatus(proj.id, 'error', { error: r.failed.map((f) => `${f.rel}: ${f.error}`).join('; ') })
350
- warn(`${r.failed.length} failed`)
351
- for (const f of r.failed) console.log(` ${C.r('✗')} ${f.rel} — ${f.error}`)
352
- process.exitCode = 1
353
- } else {
354
- if (!dryRun) recordSync(proj.id, r.bytes)
355
- setStatus(proj.id, 'idle', { error: null })
356
- }
357
- } catch (e) {
358
- setStatus(proj.id, 'error', { error: e.message })
359
- throw e
360
- } finally {
361
- try { if (t) await t.close() } finally { release() }
362
- }
363
- },
364
-
365
- async watch() {
366
- openAndMigrate()
367
- const proj = await resolveProjectOrCwd(positional[0])
368
- const creds = await credsForProject(proj, { deployment: true })
369
- const matcher = buildMatcher(proj.localPath, { useGitignore: false })
370
- ensureDeployignore(proj.localPath)
371
- const release = acquireOperation(proj.id, 'watch', { creds, remotePath: proj.remotePath })
372
- let t
373
- try { t = await createTransport(creds, flags.transport, { onRetry: retryNotice }) }
374
- catch (error) { release(); throw error }
375
- setStatus(proj.id, 'watching')
376
-
377
- console.log()
378
- console.log(C.b(`Watching "${proj.label}"`))
379
- console.log(C.dim(` ${proj.localPath} → ${proj.remotePath} (${creds.transport})`))
380
- console.log(C.dim(' Ctrl+C to stop\n'))
381
-
382
- const w = new ProjectWatcher(proj, matcher, t, {
383
- dryRun: creds.dryRun || !!flags['dry-run'],
384
- debounceMs: creds.debounceMs,
385
- onEvent: (e) => {
386
- if (e.type === 'error') {
387
- setStatus(proj.id, 'error', { error: e.message })
388
- console.log(C.r(' ✗ ') + e.message)
389
- }
390
- else if (e.type === 'change') console.log(C.dim(` ~ ${e.kind} ${e.file}`))
391
- else if (e.type === 'retry') warn(e.message)
392
- },
393
- onSettle: (s) => {
394
- setStatus(proj.id, s.failed.length ? 'error' : 'watching', {
395
- error: s.failed.length ? s.failed.map((f) => `${f.rel}: ${f.error}`).join('; ') : null
396
- })
397
- if (!s.dryRun && !s.failed.length) recordSync(proj.id, s.bytes)
398
- if (s.uploaded || s.dryRun && s.planned) {
399
- 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)`))
400
- }
401
- for (const f of s.failed) console.log(C.r(` ✗ ${f.rel} — ${f.error}`))
402
- }
403
- })
404
- try { w.start() } catch (error) { try { await t.close() } finally { release() }; throw error }
405
-
406
- let stopping = false
407
- const shutdown = async () => {
408
- if (stopping) return
409
- stopping = true
410
- console.log(C.dim('\nstopping…'))
411
- try {
412
- await w.stop()
413
- await t.close()
414
- setStatus(proj.id, 'idle')
415
- } finally { release() }
416
- process.exit(0)
417
- }
418
- process.on('SIGINT', shutdown)
419
- process.on('SIGTERM', shutdown)
420
- },
421
-
422
- async ui() {
423
- openAndMigrate()
424
- const port = flags.port || 5180
425
- const { url } = await startServer({ port: Number(port), openBrowser: flags.browser !== false })
426
- console.log()
427
- console.log(C.b('devsync UI'))
428
- console.log(` ${C.c(url)}`)
429
- console.log()
430
- console.log(C.dim(' loopback only — session token required for mutations'))
431
- console.log(C.dim(' Ctrl+C to stop'))
432
- },
433
-
434
- doctor() {
435
- openAndMigrate()
436
- const all = listProjects()
437
- console.log(C.b('devsync doctor') + '\n')
438
- console.log(` node ${process.version}`)
439
- console.log(` home ${HOME}`)
440
- console.log(` db ${fs.existsSync(path.join(HOME, 'devsync.db')) ? C.g('present') : C.y('missing')} ${path.join(HOME, 'devsync.db')}`)
441
- console.log(` master.key ${fs.existsSync(path.join(HOME, 'master.key')) ? C.g('present') : C.y('missing')}`)
442
- console.log(` projects ${all.length}`)
443
- console.log()
444
- console.log(C.b('projects:'))
445
- for (const p of all) {
446
- const exists = fs.existsSync(p.localPath)
447
- const ig = fs.existsSync(path.join(p.localPath, '.deployignore'))
448
- const hasS = hasSecrets(p.id)
449
- console.log(
450
- ` ${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')}`
451
- )
452
- }
453
- },
454
-
455
- help() {
456
- console.log(`
457
- ${C.b(name)} ${version} — local projects → shared hosting
458
- Run commands as shared-hosting-sync or its shorter alias devsync.
459
-
460
- ${C.b('first-time setup')}
461
- devsync onboard the current folder and preview
462
- devsync setup [label] fill missing settings and preview
463
- devsync ui open the web UI
464
- devsync add <label> <localPath> [remotePath]
465
- register a project; then run setup
466
- devsync projects list registered projects
467
-
468
- ${C.b('bare invocation')}
469
- cd <project> && devsync preview the cwd's project
470
-
471
- ${C.b('per-project')}
472
- devsync test [label] check connectivity
473
- devsync ls [label] [remotePath] browse the remote
474
- devsync plan [label] preview, no upload
475
- devsync deploy [label] upload all eligible files
476
- devsync sync [label] upload changed files
477
- devsync watch [label] continuous, debounced
478
-
479
- ${C.b('options')}
480
- --env-file <path> read a specific environment file
481
- --transport=ftp|ftps|sftp override protocol
482
- --host <host> --username <user> --port <port>
483
- --remote-path <path> override server destination
484
- --edit setup: edit connection settings
485
- --browse setup: choose a different server folder
486
- --label <name> name a new project
487
- --dry-run --force preview / include unchanged files
488
-
489
- ${C.b('other')}
490
- devsync remove <label>
491
- devsync doctor health check
492
- devsync help
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
+ ['concurrency', 'DEVSYNC_CONCURRENCY'], ['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, concurrency: context.concurrency }
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
+ let lastPct = -1
335
+ const r = await runPlan(p, t, {
336
+ dryRun, concurrency: creds.concurrency,
337
+ createWorker: () => createTransport(creds, undefined, { retries: 0 }),
338
+ onRetry: event => warn(`Queue ${event.workerId}: ${event.connectionFailure ? 'connection unavailable' : event.item.rel + ' interrupted'}; returned to the pending queue.`),
339
+ onFileResult: ({ index, total, item }) => {
340
+ const pct = Math.floor(((index + 1) / total) * 100)
341
+ if (pct !== lastPct && pct % 10 === 0) {
342
+ lastPct = pct
343
+ process.stdout.write(C.dim(` ${pct}% ${item.rel}\n`))
344
+ }
345
+ }
346
+ })
347
+ ok(`uploaded ${r.done.length} file(s), ${mb(r.bytes)}`)
348
+ if (r.failed.length) {
349
+ setStatus(proj.id, 'error', { error: r.failed.map((f) => `${f.rel}: ${f.error}`).join('; ') })
350
+ warn(`${r.failed.length} failed`)
351
+ for (const f of r.failed) console.log(` ${C.r('✗')} ${f.rel} — ${f.error}`)
352
+ process.exitCode = 1
353
+ } else {
354
+ if (!dryRun) recordSync(proj.id, r.bytes)
355
+ setStatus(proj.id, 'idle', { error: null })
356
+ }
357
+ } catch (e) {
358
+ setStatus(proj.id, 'error', { error: e.message })
359
+ throw e
360
+ } finally {
361
+ try { if (t) await t.close() } finally { release() }
362
+ }
363
+ },
364
+
365
+ async scan() {
366
+ openAndMigrate()
367
+ const proj = await resolveProjectOrCwd(positional[0])
368
+ const creds = await credsForProject(proj, { deployment: true })
369
+ if (!flags.confirm) {
370
+ if (!prompt.interactive) throw new Error('Server scan needs confirmation. Use devsync scan [label] --confirm for first-time sync.')
371
+ 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' })
372
+ if (answer.trim().toLowerCase() !== 'yes') { info('Server scan cancelled'); return }
373
+ }
374
+ const release = acquireOperation(proj.id, 'scan', { creds, remotePath: proj.remotePath })
375
+ let transport
376
+ try {
377
+ transport = await createTransport(creds, undefined, { onRetry: retryNotice })
378
+ const plan = await scanServer(proj, buildMatcher(proj.localPath), transport)
379
+ ok(`Server scan complete: ${plan.skipped.length} matching, ${plan.uploads.length} new or changed. No files uploaded.`)
380
+ info(`Next: devsync sync ${proj.label}`)
381
+ } finally { try { if (transport) await transport.close() } finally { release() } }
382
+ },
383
+
384
+ async watch() {
385
+ openAndMigrate()
386
+ const proj = await resolveProjectOrCwd(positional[0])
387
+ const creds = await credsForProject(proj, { deployment: true })
388
+ const matcher = buildMatcher(proj.localPath, { useGitignore: false })
389
+ ensureDeployignore(proj.localPath)
390
+ const release = acquireOperation(proj.id, 'watch', { creds, remotePath: proj.remotePath })
391
+ let t
392
+ try { t = await createTransport(creds, flags.transport, { onRetry: retryNotice }) }
393
+ catch (error) { release(); throw error }
394
+ setStatus(proj.id, 'watching')
395
+
396
+ console.log()
397
+ console.log(C.b(`Watching "${proj.label}"`))
398
+ console.log(C.dim(` ${proj.localPath} → ${proj.remotePath} (${creds.transport})`))
399
+ console.log(C.dim(' Ctrl+C to stop\n'))
400
+
401
+ const w = new ProjectWatcher(proj, matcher, t, {
402
+ dryRun: creds.dryRun || !!flags['dry-run'],
403
+ debounceMs: creds.debounceMs,
404
+ concurrency: creds.concurrency,
405
+ createWorker: () => createTransport(creds, flags.transport, { retries: 0 }),
406
+ onEvent: (e) => {
407
+ if (e.type === 'error') {
408
+ setStatus(proj.id, 'error', { error: e.message })
409
+ console.log(C.r(' ✗ ') + e.message)
410
+ }
411
+ else if (e.type === 'change') console.log(C.dim(` ~ ${e.kind} ${e.file}`))
412
+ else if (e.type === 'retry') warn(e.message)
413
+ },
414
+ onSettle: (s) => {
415
+ setStatus(proj.id, s.failed.length ? 'error' : 'watching', {
416
+ error: s.failed.length ? s.failed.map((f) => `${f.rel}: ${f.error}`).join('; ') : null
417
+ })
418
+ if (!s.dryRun && !s.failed.length) recordSync(proj.id, s.bytes)
419
+ if (s.uploaded || s.dryRun && s.planned) {
420
+ 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)`))
421
+ }
422
+ for (const f of s.failed) console.log(C.r(` ✗ ${f.rel} — ${f.error}`))
423
+ }
424
+ })
425
+ try { w.start() } catch (error) { try { await t.close() } finally { release() }; throw error }
426
+
427
+ let stopping = false
428
+ const shutdown = async () => {
429
+ if (stopping) return
430
+ stopping = true
431
+ console.log(C.dim('\nstopping…'))
432
+ try {
433
+ await w.stop()
434
+ await t.close()
435
+ setStatus(proj.id, 'idle')
436
+ } finally { release() }
437
+ process.exit(0)
438
+ }
439
+ process.on('SIGINT', shutdown)
440
+ process.on('SIGTERM', shutdown)
441
+ },
442
+
443
+ async ui() {
444
+ openAndMigrate()
445
+ const port = flags.port || 5180
446
+ const { url } = await startServer({ port: Number(port), openBrowser: flags.browser !== false })
447
+ console.log()
448
+ console.log(C.b('devsync UI'))
449
+ console.log(` ${C.c(url)}`)
450
+ console.log()
451
+ console.log(C.dim(' loopback only — session token required for mutations'))
452
+ console.log(C.dim(' Ctrl+C to stop'))
453
+ },
454
+
455
+ doctor() {
456
+ openAndMigrate()
457
+ const all = listProjects()
458
+ console.log(C.b('devsync doctor') + '\n')
459
+ console.log(` node ${process.version}`)
460
+ console.log(` home ${HOME}`)
461
+ console.log(` db ${fs.existsSync(path.join(HOME, 'devsync.db')) ? C.g('present') : C.y('missing')} ${path.join(HOME, 'devsync.db')}`)
462
+ console.log(` master.key ${fs.existsSync(path.join(HOME, 'master.key')) ? C.g('present') : C.y('missing')}`)
463
+ console.log(` projects ${all.length}`)
464
+ console.log()
465
+ console.log(C.b('projects:'))
466
+ for (const p of all) {
467
+ const exists = fs.existsSync(p.localPath)
468
+ const ig = fs.existsSync(path.join(p.localPath, '.deployignore'))
469
+ const hasS = hasSecrets(p.id)
470
+ console.log(
471
+ ` ${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')}`
472
+ )
473
+ }
474
+ },
475
+
476
+ help() {
477
+ console.log(`
478
+ ${C.b(name)} ${version} — local projects → shared hosting
479
+ Run commands as shared-hosting-sync or its shorter alias devsync.
480
+
481
+ ${C.b('first-time setup')}
482
+ devsync onboard the current folder and preview
483
+ devsync setup [label] fill missing settings and preview
484
+ devsync ui open the web UI
485
+ devsync add <label> <localPath> [remotePath]
486
+ register a project; then run setup
487
+ devsync projects list registered projects
488
+
489
+ ${C.b('bare invocation')}
490
+ cd <project> && devsync preview the cwd's project
491
+
492
+ ${C.b('per-project')}
493
+ devsync test [label] check connectivity
494
+ devsync ls [label] [remotePath] browse the remote
495
+ devsync plan [label] preview, no upload
496
+ devsync deploy [label] upload all eligible files
497
+ devsync sync [label] upload changes using the saved database baseline
498
+ devsync scan [label] first-time server scan (asks for confirmation)
499
+ devsync watch [label] continuous, debounced
500
+
501
+ ${C.b('options')}
502
+ --env-file <path> read a specific environment file
503
+ --transport=ftp|ftps|sftp override protocol
504
+ --host <host> --username <user> --port <port>
505
+ --remote-path <path> override server destination
506
+ --edit setup: edit connection settings
507
+ --browse setup: choose a different server folder
508
+ --label <name> name a new project
509
+ --concurrency <1-8> parallel upload queues (default: 3)
510
+ --confirm confirm a first-time server scan
511
+ --dry-run --force preview / include unchanged files
512
+
513
+ ${C.b('other')}
514
+ devsync remove <label>
515
+ devsync doctor health check
516
+ devsync help
517
+ `)
518
+ }
519
+ }
520
+
497
521
  function printBanner() {
498
522
  const color = process.stdout.isTTY && !('NO_COLOR' in process.env)
499
523
  const paint = (style, value) => color ? style(value) : value
@@ -524,19 +548,19 @@ function printBanner() {
524
548
  console.log()
525
549
  }
526
550
 
527
- async function main() {
528
- try {
529
- if (flags.version || cmd === 'version') { console.log(`${name} ${version}`); return }
551
+ async function main() {
552
+ try {
553
+ if (flags.version || cmd === 'version') { console.log(`${name} ${version}`); return }
530
554
  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
- })
555
+ if (flags.help || cmd === 'help') return COMMANDS.help()
556
+ if (!cmd) return flags.sync ? await COMMANDS.sync() : await COMMANDS.plan()
557
+ if (!Object.hasOwn(COMMANDS, cmd)) throw new Error(`unknown command: ${cmd}. Run devsync help`)
558
+ return await COMMANDS[cmd]()
559
+ } finally {
560
+ await prompt.close()
561
+ }
562
+ }
563
+ main().catch((err) => {
564
+ console.error(C.r('\n✗ ') + (err?.message || String(err)))
565
+ process.exitCode = 1
566
+ })