shared-hosting-sync 0.0.0-stage → 0.1.0

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 +16 -0
  3. package/LICENSE +21 -0
  4. package/PHASES.md +141 -0
  5. package/PRODUCT.md +90 -0
  6. package/README.md +407 -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 +198 -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
@@ -0,0 +1,160 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import { validateRemotePath, relativeFile } from './paths.js'
4
+ import { hashFile } from './checksum.js'
5
+ import { isTransient } from './transport/reliable.js'
6
+
7
+ const TMP_SUFFIX = '.devsync-uploading'
8
+
9
+ /** Local Windows path -> remote POSIX path, relative to the project root. */
10
+ export function toRemotePath(projectRoot, remoteRoot, localAbsPath) {
11
+ const rel = relativeFile(projectRoot, localAbsPath)
12
+ const root = validateRemotePath(remoteRoot)
13
+ return `${root === '/' ? '' : root}/${rel}`
14
+ }
15
+
16
+ /** Ancestor directories of a remote file path, outermost first. */
17
+ export function ancestorsOf(remotePath) {
18
+ const parts = remotePath.split('/').filter(Boolean)
19
+ const out = []
20
+ let acc = remotePath.startsWith('/') ? '' : '.'
21
+ for (let i = 0; i < parts.length - 1; i++) {
22
+ acc = acc === '' || acc === '.' ? `${acc}/${parts[i]}` : `${acc}/${parts[i]}`
23
+ out.push(acc.startsWith('/') ? acc : `/${acc}`)
24
+ }
25
+ return out
26
+ }
27
+
28
+ /** Every eligible local file, as absolute paths. */
29
+ export function collectFiles(projectRoot, matcher, { includeIgnored = false, onExcluded } = {}) {
30
+ const out = []
31
+ const relOf = (abs) => path.relative(projectRoot, abs).split(path.sep).join('/')
32
+ const walk = (dir) => {
33
+ let entries = []
34
+ try {
35
+ entries = fs.readdirSync(dir, { withFileTypes: true })
36
+ } catch (err) {
37
+ throw new Error(`Cannot read local folder ${dir}: ${err.message}`)
38
+ }
39
+ for (const e of entries) {
40
+ const abs = path.join(dir, e.name)
41
+ if (e.isSymbolicLink()) continue
42
+ if (e.isDirectory()) {
43
+ if (includeIgnored || matcher.shouldWatch(abs)) walk(abs)
44
+ else if (onExcluded) onExcluded({ rel: relOf(abs) + '/', reason: matcher.reason(relOf(abs)) })
45
+ } else if (e.isFile()) {
46
+ // includeIgnored is a diagnostic mode: show everything, filter nothing.
47
+ const reason = matcher.reason(relOf(abs))
48
+ if (includeIgnored || reason === null) out.push(abs)
49
+ else if (onExcluded) onExcluded({ rel: relOf(abs), reason })
50
+ }
51
+ }
52
+ }
53
+ walk(projectRoot)
54
+ return out
55
+ }
56
+
57
+ /**
58
+ * Build the list of uploads without touching the network for writes.
59
+ * Size differences upload immediately; equal sizes require a content match.
60
+ */
61
+ export async function buildPlan(project, matcher, transport, { force = false, onProgress } = {}) {
62
+ const { localPath, remotePath } = project
63
+ validateRemotePath(remotePath)
64
+ if (!fs.statSync(localPath).isDirectory()) throw new Error('Local source must be a directory')
65
+ const excluded = []
66
+ let excludedCount = 0
67
+ const files = collectFiles(localPath, matcher, { onExcluded: (entry) => {
68
+ excludedCount++
69
+ if (excluded.length < 200) excluded.push(entry)
70
+ } })
71
+ const uploads = []
72
+ const skipped = []
73
+ let comparedBytes = 0
74
+
75
+ for (const [index, abs] of files.entries()) {
76
+ const rel = path.relative(localPath, abs).split(path.sep).join('/')
77
+ const remote = toRemotePath(localPath, remotePath, abs)
78
+ let localStat
79
+ try {
80
+ relativeFile(fs.realpathSync(localPath), fs.realpathSync(abs))
81
+ localStat = fs.statSync(abs)
82
+ } catch (err) {
83
+ if (err.code === 'ENOENT') continue
84
+ throw err
85
+ }
86
+
87
+ if (onProgress) onProgress({ phase: 'planning', item: { rel, remote, bytes: localStat.size }, index, total: files.length })
88
+
89
+ const remoteStat = await transport.stat(remote)
90
+ if (remoteStat?.isDir) throw new Error(`Remote file destination is a directory: ${remote}`)
91
+ if (!force && remoteStat && remoteStat.size === localStat.size &&
92
+ await transport.checksum(remote) === await hashFile(abs)) {
93
+ skipped.push({ rel, bytes: localStat.size, reason: 'same content' })
94
+ continue
95
+ }
96
+
97
+ uploads.push({
98
+ abs,
99
+ rel,
100
+ remote,
101
+ bytes: localStat.size,
102
+ dirs: ancestorsOf(remote)
103
+ })
104
+ comparedBytes += localStat.size
105
+ }
106
+
107
+ return { uploads, skipped, excluded, excludedCount, totalFiles: files.length, comparedBytes }
108
+ }
109
+
110
+ /**
111
+ * Upload one file atomically: write to <name>.devsync-uploading, then rename.
112
+ * Without this, PHP can parse a half-written file and 500 mid-transfer.
113
+ */
114
+ export async function uploadOne(transport, item) {
115
+ if (transport.transaction) return transport.transaction((raw) => uploadAttempt(raw, item))
116
+ return uploadAttempt(transport, item)
117
+ }
118
+
119
+ async function uploadAttempt(transport, item) {
120
+ for (const dir of item.dirs) {
121
+ await transport.ensureDir(dir)
122
+ }
123
+ const tmp = item.remote + TMP_SUFFIX
124
+ try {
125
+ await transport.delete(tmp).catch(() => {})
126
+ await transport.put(item.abs, tmp)
127
+ await transport.rename(tmp, item.remote)
128
+ } catch (err) {
129
+ await transport.delete(tmp).catch(() => {})
130
+ throw err
131
+ }
132
+ }
133
+
134
+ export async function runPlan(plan, transport, { dryRun = false, onProgress, onFileResult } = {}) {
135
+ const done = []
136
+ const failed = []
137
+ let bytes = 0
138
+
139
+ for (let i = 0; i < plan.uploads.length; i++) {
140
+ const item = plan.uploads[i]
141
+ if (onProgress) onProgress({ phase: 'upload', item, index: i, total: plan.uploads.length })
142
+ if (dryRun) {
143
+ done.push({ rel: item.rel, bytes: item.bytes })
144
+ bytes += item.bytes
145
+ if (onFileResult) onFileResult({ item, index: i, total: plan.uploads.length, success: true, dryRun: true, bytes })
146
+ continue
147
+ }
148
+ try {
149
+ await uploadOne(transport, item)
150
+ done.push({ rel: item.rel, bytes: item.bytes })
151
+ bytes += item.bytes
152
+ if (onFileResult) onFileResult({ item, index: i, total: plan.uploads.length, success: true, dryRun: false, bytes })
153
+ } catch (err) {
154
+ failed.push({ rel: item.rel, error: err.message, retryable: isTransient(err) })
155
+ if (onFileResult) onFileResult({ item, index: i, total: plan.uploads.length, success: false, error: err.message, dryRun: false, bytes })
156
+ }
157
+ }
158
+
159
+ return { done, failed, bytes }
160
+ }
package/src/watcher.js ADDED
@@ -0,0 +1,122 @@
1
+ import chokidar from 'chokidar'
2
+ import path from 'node:path'
3
+ import { buildPlan, runPlan } from './uploader.js'
4
+ import { isTransient } from './transport/reliable.js'
5
+
6
+ /** Debounced serial cycles shared by the CLI and dashboard. */
7
+ export class ProjectWatcher {
8
+ constructor(project, matcher, transport, opts = {}) {
9
+ this.project = project
10
+ this.matcher = matcher
11
+ this.transport = transport
12
+ this.debounceMs = opts.debounceMs ?? 400
13
+ this.dryRun = opts.dryRun ?? false
14
+ this.onEvent = opts.onEvent ?? (() => {})
15
+ this.onSettle = opts.onSettle ?? (() => {})
16
+ this.onPlan = opts.onPlan ?? (() => {})
17
+ this.onProgress = opts.onProgress ?? (() => {})
18
+ this.onFileResult = opts.onFileResult ?? (() => {})
19
+ this.timer = null
20
+ this.retryTimer = null
21
+ this.retryDelayMs = opts.retryDelayMs ?? 5000
22
+ this.pending = new Set()
23
+ this.dirty = false
24
+ this.running = false
25
+ this.stopped = false
26
+ this.watcher = null
27
+ this.cycle = Promise.resolve()
28
+ this.ready = Promise.resolve()
29
+ this.cancelReady = () => {}
30
+ this.stats = { cycles: 0, uploaded: 0, failed: 0 }
31
+ }
32
+
33
+ start() {
34
+ this.stopped = false
35
+ let resolveReady, rejectReady
36
+ this.ready = new Promise((resolve, reject) => { resolveReady = resolve; rejectReady = reject })
37
+ this.cancelReady = resolveReady
38
+ this.ready.catch(() => {}) // CLI callers may use events rather than awaiting readiness.
39
+ this.watcher = chokidar.watch(this.project.localPath, {
40
+ ignoreInitial: true, persistent: true,
41
+ ignored: (file) => !this.matcher.shouldWatch(file)
42
+ })
43
+ this.watcher
44
+ .on('add', (file) => this.queue(file, 'added'))
45
+ .on('change', (file) => this.queue(file, 'changed'))
46
+ .on('unlink', (file) => this.queue(file, 'removed'))
47
+ .on('error', (error) => { rejectReady(error); this.onEvent({ type: 'error', message: error.message }) })
48
+ .on('ready', () => {
49
+ resolveReady()
50
+ this.onEvent({ type: 'ready', message: `Watching ${this.project.localPath}` })
51
+ })
52
+ return this
53
+ }
54
+
55
+ queue(filePath, kind) {
56
+ if (this.stopped) return
57
+ this.pending.add(filePath)
58
+ this.dirty = true
59
+ this.onEvent({ type: 'change', file: path.relative(this.project.localPath, filePath), kind })
60
+ clearTimeout(this.timer)
61
+ this.timer = setTimeout(() => this.flush(), this.debounceMs)
62
+ }
63
+
64
+ flush() {
65
+ if (this.stopped || !this.dirty) return this.cycle
66
+ if (this.running) return this.cycle
67
+ clearTimeout(this.timer)
68
+ this.running = true
69
+ this.dirty = false
70
+ this.pending.clear()
71
+ this.cycle = this.runCycle().finally(() => {
72
+ this.running = false
73
+ if (!this.stopped && this.pending.size) {
74
+ clearTimeout(this.timer)
75
+ this.timer = setTimeout(() => this.flush(), this.debounceMs)
76
+ }
77
+ })
78
+ return this.cycle
79
+ }
80
+
81
+ async runCycle() {
82
+ clearTimeout(this.retryTimer)
83
+ try {
84
+ const plan = await buildPlan(this.project, this.matcher, this.transport, { onProgress: this.onProgress })
85
+ this.onPlan(plan)
86
+ const result = await runPlan(plan, this.transport, {
87
+ dryRun: this.dryRun, onProgress: this.onProgress, onFileResult: this.onFileResult
88
+ })
89
+ this.stats.cycles++
90
+ if (!this.dryRun) this.stats.uploaded += result.done.length
91
+ this.stats.failed += result.failed.length
92
+ this.onSettle({
93
+ type: 'settle', dryRun: this.dryRun, uploaded: this.dryRun ? 0 : result.done.length,
94
+ planned: result.done.length, failed: result.failed, bytes: result.bytes,
95
+ considered: plan.totalFiles, skipped: plan.skipped.length,
96
+ files: result.done.map((file) => file.rel)
97
+ })
98
+ if (result.failed.some((file) => file.retryable)) this.retry()
99
+ } catch (error) {
100
+ this.onEvent({ type: 'error', message: error.message })
101
+ if (isTransient(error)) this.retry()
102
+ }
103
+ }
104
+
105
+ retry() {
106
+ if (this.stopped) return
107
+ this.onEvent({ type: 'retry', message: 'Connection unavailable; watch will retry automatically.' })
108
+ clearTimeout(this.retryTimer)
109
+ this.retryTimer = setTimeout(() => this.queue(this.project.localPath, 'retry'), this.retryDelayMs)
110
+ }
111
+
112
+ async stop() {
113
+ this.stopped = true
114
+ this.cancelReady()
115
+ clearTimeout(this.timer)
116
+ clearTimeout(this.retryTimer)
117
+ this.pending.clear()
118
+ this.dirty = false
119
+ if (this.watcher) await this.watcher.close()
120
+ await this.cycle
121
+ }
122
+ }