shared-hosting-sync 0.0.0-stage → 0.1.1

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.
Files changed (46) hide show
  1. package/.env.example +23 -0
  2. package/CHANGELOG.md +24 -0
  3. package/LICENSE +21 -0
  4. package/PHASES.md +147 -0
  5. package/PRODUCT.md +90 -0
  6. package/README.md +398 -2
  7. package/RELEASING.md +79 -0
  8. package/package.json +68 -4
  9. package/src/arguments.js +30 -0
  10. package/src/bin.js +8 -0
  11. package/src/checksum.js +18 -0
  12. package/src/cli.js +511 -0
  13. package/src/config.js +98 -0
  14. package/src/core/db.js +61 -0
  15. package/src/core/deployment.js +22 -0
  16. package/src/core/envResolver.js +98 -0
  17. package/src/core/envSchema.js +160 -0
  18. package/src/core/migrate.js +75 -0
  19. package/src/core/migrations/001_init.sql +67 -0
  20. package/src/core/migrations/002_operation_locks.sql +9 -0
  21. package/src/core/operationLock.js +54 -0
  22. package/src/core/projects.js +261 -0
  23. package/src/core/secrets.js +75 -0
  24. package/src/core/v1Migrate.js +82 -0
  25. package/src/ignore.js +148 -0
  26. package/src/metadata.js +3 -0
  27. package/src/onboarding.js +117 -0
  28. package/src/paths.js +23 -0
  29. package/src/prompt.js +54 -0
  30. package/src/server/browse.js +46 -0
  31. package/src/server/deployments.js +209 -0
  32. package/src/server/index.js +79 -0
  33. package/src/server/local.js +23 -0
  34. package/src/server/onboarding.js +203 -0
  35. package/src/server/routes.js +307 -0
  36. package/src/server/session.js +58 -0
  37. package/src/transport/ftp.js +100 -0
  38. package/src/transport/index.js +12 -0
  39. package/src/transport/reliable.js +50 -0
  40. package/src/transport/sftp.js +99 -0
  41. package/src/ui/deployments.js +129 -0
  42. package/src/ui/index.html +17 -0
  43. package/src/ui/style.css +129 -0
  44. package/src/ui/ui.js +289 -0
  45. package/src/uploader.js +160 -0
  46. package/src/watcher.js +122 -0
package/src/cli.js ADDED
@@ -0,0 +1,511 @@
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
+
497
+ async function main() {
498
+ try {
499
+ if (flags.version || cmd === 'version') { console.log(`${name} ${version}`); return }
500
+ if (flags.help || cmd === 'help') return COMMANDS.help()
501
+ if (!cmd) return flags.sync ? await COMMANDS.sync() : await COMMANDS.plan()
502
+ if (!Object.hasOwn(COMMANDS, cmd)) throw new Error(`unknown command: ${cmd}. Run devsync help`)
503
+ return await COMMANDS[cmd]()
504
+ } finally {
505
+ await prompt.close()
506
+ }
507
+ }
508
+ main().catch((err) => {
509
+ console.error(C.r('\n✗ ') + (err?.message || String(err)))
510
+ process.exitCode = 1
511
+ })
package/src/config.js ADDED
@@ -0,0 +1,98 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import os from 'node:os'
4
+ import dotenv from 'dotenv'
5
+
6
+
7
+ /**
8
+ * Everything lives under ~/.devsync so no project directory is ever touched.
9
+ * Override via DEVSYNC_HOME (used by tests, and by anyone who wants to keep
10
+ * devsync state on a separate drive).
11
+ */
12
+ export const HOME = process.env.DEVSYNC_HOME || path.join(os.homedir(), '.devsync')
13
+ export const CONFIG_FILE = path.join(HOME, 'config.json')
14
+ export const ENV_FILE = path.join(HOME, '.env')
15
+
16
+ const DEFAULT_CONFIG = {
17
+ transport: 'sftp',
18
+ defaults: {
19
+ port: null,
20
+ username: '',
21
+ remotePath: '',
22
+ ignoreDeletes: true,
23
+ dryRunByDefault: true
24
+ },
25
+ projects: []
26
+ }
27
+
28
+ export function ensureHome() {
29
+ if (!fs.existsSync(HOME)) fs.mkdirSync(HOME, { recursive: true })
30
+ return HOME
31
+ }
32
+
33
+ export function loadConfig() {
34
+ ensureHome()
35
+ if (!fs.existsSync(CONFIG_FILE)) {
36
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(DEFAULT_CONFIG, null, 2))
37
+ }
38
+ try {
39
+ const parsed = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'))
40
+ return {
41
+ transport: parsed.transport ?? 'sftp',
42
+ defaults: { ...DEFAULT_CONFIG.defaults, ...(parsed.defaults ?? {}) },
43
+ projects: Array.isArray(parsed.projects) ? parsed.projects : []
44
+ }
45
+ } catch (err) {
46
+ throw new Error(`Config file is not valid JSON: ${CONFIG_FILE}\n ${err.message}`)
47
+ }
48
+ }
49
+
50
+ export function saveConfig(config) {
51
+ ensureHome()
52
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2))
53
+ }
54
+
55
+ /** Credentials come from ~/.devsync/.env, never from config.json or a project .env. */
56
+ export function loadCredentials() {
57
+ ensureHome()
58
+ if (!fs.existsSync(ENV_FILE)) {
59
+ // Preserve the v1 helper's format; the shipped example is now project-scoped.
60
+ fs.writeFileSync(ENV_FILE, '# Legacy devsync v1 settings\nPROTO=sftp\nHOST=\nPORT=\nUSERNAME=\nPASSWORD=\n')
61
+ throw new Error(
62
+ `Created ${ENV_FILE} from the template.\n` +
63
+ `Edit it with your host details, then re-run the same command.`
64
+ )
65
+ }
66
+ const parsed = dotenv.parse(fs.readFileSync(ENV_FILE))
67
+ const missing = ['PROTO', 'HOST', 'USERNAME', 'PASSWORD'].filter((k) => !parsed[k])
68
+ if (missing.length) {
69
+ throw new Error(`${ENV_FILE} is missing: ${missing.join(', ')}`)
70
+ }
71
+ const proto = (parsed.PROTO || 'sftp').toLowerCase()
72
+ const defaultPort = proto === 'sftp' ? 22 : 21
73
+ return {
74
+ transport: proto,
75
+ host: parsed.HOST,
76
+ port: Number(parsed.PORT || defaultPort),
77
+ username: parsed.USERNAME,
78
+ password: parsed.PASSWORD || undefined,
79
+ privateKeyPath: parsed.PRIVATE_KEY_PATH || undefined,
80
+ secure: proto === 'ftps',
81
+ timeoutMs: Number(parsed.TIMEOUT_MS || 20000)
82
+ }
83
+ }
84
+
85
+ export function upsertProject(config, project) {
86
+ const idx = config.projects.findIndex((p) => p.id === project.id)
87
+ if (idx >= 0) config.projects[idx] = { ...config.projects[idx], ...project }
88
+ else config.projects.push(project)
89
+ saveConfig(config)
90
+ return project
91
+ }
92
+
93
+ export function removeProject(config, id) {
94
+ const before = config.projects.length
95
+ config.projects = config.projects.filter((p) => p.id !== id)
96
+ saveConfig(config)
97
+ return before !== config.projects.length
98
+ }
package/src/core/db.js ADDED
@@ -0,0 +1,61 @@
1
+ // db.js — thin node:sqlite wrapper around ~/.devsync/devsync.db
2
+ //
3
+ // Responsibilities:
4
+ // - Open the DB at ~/.devsync/devsync.db with WAL + foreign keys
5
+ // - Apply any pending migrations on open
6
+ // - Expose prepared statements as a singleton (one connection per process)
7
+ // - Run small helper queries (getProject, listProjects, …)
8
+ //
9
+ // Everything else (CRUD for projects/secrets/env_overrides/events) is built
10
+ // on top of this in core/projects.js, core/secrets.js, etc.
11
+
12
+ import fs from 'node:fs'
13
+ import path from 'node:path'
14
+ import { DatabaseSync } from 'node:sqlite'
15
+ import { HOME } from '../config.js'
16
+ import { runMigrations } from './migrate.js'
17
+
18
+ const DB_FILE = path.join(HOME, 'devsync.db')
19
+
20
+ let _db = null
21
+
22
+ function ensureHome() {
23
+ if (!fs.existsSync(HOME)) fs.mkdirSync(HOME, { recursive: true })
24
+ return HOME
25
+ }
26
+
27
+ /**
28
+ * Open (or return cached) DB connection. Migrations run on first open.
29
+ *
30
+ * Foreign keys are ON. WAL is enabled so a slow UI doesn't lock the CLI.
31
+ * synchronous=NORMAL is the sqlite default and is what we want.
32
+ */
33
+ export function openDb() {
34
+ if (_db) return _db
35
+ ensureHome()
36
+ const db = new DatabaseSync(DB_FILE, { open: true })
37
+ db.exec('PRAGMA busy_timeout = 5000')
38
+ db.exec('PRAGMA journal_mode = WAL')
39
+ db.exec('PRAGMA foreign_keys = ON')
40
+ db.exec('PRAGMA synchronous = NORMAL')
41
+ runMigrations(db)
42
+ _db = db
43
+ return db
44
+ }
45
+
46
+ /** Close the cached connection. Mainly used in tests. */
47
+ export function closeDb() {
48
+ if (!_db) return
49
+ _db.close()
50
+ _db = null
51
+ }
52
+
53
+ /** Path to the DB file (for diagnostics, doctor, the UI's GET /api/health). */
54
+ export function dbPath() {
55
+ return DB_FILE
56
+ }
57
+
58
+ /** Whether the DB has ever been opened on disk — quick check without paying open cost. */
59
+ export function dbExists() {
60
+ return fs.existsSync(DB_FILE)
61
+ }
@@ -0,0 +1,22 @@
1
+ import { resolveEnv, credsFromResolved } from './envResolver.js'
2
+ import { assertLocalFolder } from '../onboarding.js'
3
+ import { validateRemotePath } from '../paths.js'
4
+ import { buildMatcher } from '../ignore.js'
5
+
6
+ /** Shared validated deployment context for terminal and dashboard operations. */
7
+ export function resolveDeployment(project, options = {}) {
8
+ const resolved = resolveEnv(project.id, options)
9
+ if (resolved.warnings.length) throw new Error(resolved.warnings.join('; '))
10
+ const target = {
11
+ ...project, localPath: assertLocalFolder(project.localPath),
12
+ remotePath: validateRemotePath(resolved.values.DEVSYNC_REMOTE_PATH)
13
+ }
14
+ return {
15
+ project: target,
16
+ matcher: buildMatcher(target.localPath),
17
+ creds: credsFromResolved(resolved.values),
18
+ dryRun: ['true', '1', true].includes(resolved.values.DEVSYNC_DRY_RUN),
19
+ debounceMs: Number(resolved.values.DEVSYNC_DEBOUNCE_MS),
20
+ values: resolved.values
21
+ }
22
+ }