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.
- package/.env.example +23 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/PHASES.md +141 -0
- package/PRODUCT.md +90 -0
- package/README.md +407 -2
- package/RELEASING.md +79 -0
- package/package.json +68 -4
- package/src/arguments.js +30 -0
- package/src/bin.js +8 -0
- package/src/checksum.js +18 -0
- package/src/cli.js +511 -0
- package/src/config.js +98 -0
- package/src/core/db.js +61 -0
- package/src/core/deployment.js +22 -0
- package/src/core/envResolver.js +98 -0
- package/src/core/envSchema.js +160 -0
- package/src/core/migrate.js +75 -0
- package/src/core/migrations/001_init.sql +67 -0
- package/src/core/migrations/002_operation_locks.sql +9 -0
- package/src/core/operationLock.js +54 -0
- package/src/core/projects.js +261 -0
- package/src/core/secrets.js +75 -0
- package/src/core/v1Migrate.js +82 -0
- package/src/ignore.js +148 -0
- package/src/metadata.js +3 -0
- package/src/onboarding.js +117 -0
- package/src/paths.js +23 -0
- package/src/prompt.js +54 -0
- package/src/server/browse.js +46 -0
- package/src/server/deployments.js +209 -0
- package/src/server/index.js +79 -0
- package/src/server/local.js +23 -0
- package/src/server/onboarding.js +198 -0
- package/src/server/routes.js +307 -0
- package/src/server/session.js +58 -0
- package/src/transport/ftp.js +100 -0
- package/src/transport/index.js +12 -0
- package/src/transport/reliable.js +50 -0
- package/src/transport/sftp.js +99 -0
- package/src/ui/deployments.js +129 -0
- package/src/ui/index.html +17 -0
- package/src/ui/style.css +129 -0
- package/src/ui/ui.js +289 -0
- package/src/uploader.js +160 -0
- package/src/watcher.js +122 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// projects.js — CRUD for the projects / secrets / env_overrides tables.
|
|
2
|
+
// The CLI, the UI, and the v1→v2 importer all go through this layer.
|
|
3
|
+
//
|
|
4
|
+
// Conventions:
|
|
5
|
+
// - All public functions take an already-open db handle (or call openDb()).
|
|
6
|
+
// - is_secret fields are NEVER returned in plain form to the UI — the
|
|
7
|
+
// route layer replaces them with a presence flag.
|
|
8
|
+
// - Upsert = insert or update by primary key / unique constraint.
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs'
|
|
11
|
+
import path from 'node:path'
|
|
12
|
+
import { openDb } from './db.js'
|
|
13
|
+
import { encrypt, decrypt } from './secrets.js'
|
|
14
|
+
import { defaultsFor, ENV_SCHEMA } from './envSchema.js'
|
|
15
|
+
|
|
16
|
+
const now = () => new Date().toISOString()
|
|
17
|
+
|
|
18
|
+
// ---------- projects ----------
|
|
19
|
+
|
|
20
|
+
export function listProjects({ enabledOnly = false } = {}) {
|
|
21
|
+
const db = openDb()
|
|
22
|
+
const sql = enabledOnly
|
|
23
|
+
? 'SELECT * FROM projects WHERE enabled = 1 ORDER BY label'
|
|
24
|
+
: 'SELECT * FROM projects ORDER BY label'
|
|
25
|
+
return db.prepare(sql).all().map(rowToProject)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getProject(id) {
|
|
29
|
+
const db = openDb()
|
|
30
|
+
const row = db.prepare('SELECT * FROM projects WHERE id = ?').get(id)
|
|
31
|
+
return row ? rowToProject(row) : null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getProjectByLabel(label) {
|
|
35
|
+
const db = openDb()
|
|
36
|
+
const row = db.prepare('SELECT * FROM projects WHERE label = ?').get(label)
|
|
37
|
+
return row ? rowToProject(row) : null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function getProjectByLocalPath(localPath) {
|
|
41
|
+
const db = openDb()
|
|
42
|
+
const row = db.prepare('SELECT * FROM projects WHERE local_path = ?').get(localPath)
|
|
43
|
+
return row ? rowToProject(row) : null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createProject({ label, localPath }) {
|
|
47
|
+
if (!label) throw new Error('label is required')
|
|
48
|
+
if (!localPath) throw new Error('localPath is required')
|
|
49
|
+
if (!fs.existsSync(localPath)) throw new Error(`localPath does not exist: ${localPath}`)
|
|
50
|
+
if (!fs.statSync(localPath).isDirectory()) throw new Error(`localPath is not a directory: ${localPath}`)
|
|
51
|
+
const db = openDb()
|
|
52
|
+
const t = now()
|
|
53
|
+
try {
|
|
54
|
+
const info = db
|
|
55
|
+
.prepare(
|
|
56
|
+
`INSERT INTO projects (label, local_path, enabled, created_at, updated_at)
|
|
57
|
+
VALUES (?, ?, 1, ?, ?)`
|
|
58
|
+
)
|
|
59
|
+
.run(label, localPath, t, t)
|
|
60
|
+
return getProject(info.lastInsertRowid)
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (String(e.message).includes('UNIQUE')) {
|
|
63
|
+
throw new Error(`project "${label}" already exists`)
|
|
64
|
+
}
|
|
65
|
+
throw e
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function updateProject(id, patch) {
|
|
70
|
+
const db = openDb()
|
|
71
|
+
const allowed = ['label', 'local_path', 'enabled']
|
|
72
|
+
const sets = []
|
|
73
|
+
const vals = []
|
|
74
|
+
for (const k of allowed) {
|
|
75
|
+
if (k in patch) {
|
|
76
|
+
sets.push(`${k} = ?`)
|
|
77
|
+
// SQLite needs integers for INTEGER columns; JS booleans don't bind.
|
|
78
|
+
vals.push(toSqlValue(patch[k]))
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!sets.length) return getProject(id)
|
|
82
|
+
sets.push('updated_at = ?')
|
|
83
|
+
vals.push(now())
|
|
84
|
+
vals.push(id)
|
|
85
|
+
db.prepare(`UPDATE projects SET ${sets.join(', ')} WHERE id = ?`).run(...vals)
|
|
86
|
+
return getProject(id)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function toSqlValue(v) {
|
|
90
|
+
if (typeof v === 'boolean') return v ? 1 : 0
|
|
91
|
+
return v
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function deleteProject(id) {
|
|
95
|
+
const db = openDb()
|
|
96
|
+
const info = db.prepare('DELETE FROM projects WHERE id = ?').run(id)
|
|
97
|
+
return info.changes > 0
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function setStatus(id, status, { error = null } = {}) {
|
|
101
|
+
const db = openDb()
|
|
102
|
+
db.prepare(
|
|
103
|
+
`UPDATE projects SET last_status = ?, last_error = ?, updated_at = ? WHERE id = ?`
|
|
104
|
+
).run(status, error, now(), id)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function recordSync(id, bytes) {
|
|
108
|
+
openDb().prepare('UPDATE projects SET last_sync_at = ?, last_upload_bytes = ? WHERE id = ?')
|
|
109
|
+
.run(now(), bytes, id)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ---------- secrets ----------
|
|
113
|
+
|
|
114
|
+
/** Persist creds for a project. Replaces the previous row. */
|
|
115
|
+
export function setSecrets(projectId, secretsObj) {
|
|
116
|
+
const db = openDb()
|
|
117
|
+
const { ciphertext, iv, auth_tag } = encrypt(secretsObj)
|
|
118
|
+
db.prepare(
|
|
119
|
+
`INSERT INTO secrets (project_id, ciphertext, iv, auth_tag, updated_at)
|
|
120
|
+
VALUES (?, ?, ?, ?, ?)
|
|
121
|
+
ON CONFLICT(project_id) DO UPDATE SET
|
|
122
|
+
ciphertext = excluded.ciphertext,
|
|
123
|
+
iv = excluded.iv,
|
|
124
|
+
auth_tag = excluded.auth_tag,
|
|
125
|
+
updated_at = excluded.updated_at`
|
|
126
|
+
).run(projectId, ciphertext, iv, auth_tag, now())
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Get the decrypted secrets object for a project (or null). */
|
|
130
|
+
export function getSecrets(projectId) {
|
|
131
|
+
const db = openDb()
|
|
132
|
+
const row = db.prepare('SELECT ciphertext, iv, auth_tag FROM secrets WHERE project_id = ?').get(projectId)
|
|
133
|
+
if (!row) return null
|
|
134
|
+
return decrypt(row)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Tell whether a project has any secrets stored. */
|
|
138
|
+
export function hasSecrets(projectId) {
|
|
139
|
+
const db = openDb()
|
|
140
|
+
const row = db.prepare('SELECT 1 FROM secrets WHERE project_id = ?').get(projectId)
|
|
141
|
+
return !!row
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function deleteSecrets(projectId) {
|
|
145
|
+
const db = openDb()
|
|
146
|
+
db.prepare('DELETE FROM secrets WHERE project_id = ?').run(projectId)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ---------- env_overrides ----------
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Replace the full env_overrides set for a project.
|
|
153
|
+
* `entries` is [{ key, value, isSecret }]. Pass [] to clear.
|
|
154
|
+
*/
|
|
155
|
+
export function setEnvOverrides(projectId, entries) {
|
|
156
|
+
const db = openDb()
|
|
157
|
+
db.exec('BEGIN')
|
|
158
|
+
try {
|
|
159
|
+
db.prepare('DELETE FROM env_overrides WHERE project_id = ?').run(projectId)
|
|
160
|
+
const ins = db.prepare(
|
|
161
|
+
`INSERT INTO env_overrides (project_id, key, value, is_secret, updated_at)
|
|
162
|
+
VALUES (?, ?, ?, ?, ?)`
|
|
163
|
+
)
|
|
164
|
+
for (const e of entries) {
|
|
165
|
+
ins.run(
|
|
166
|
+
projectId,
|
|
167
|
+
e.key,
|
|
168
|
+
e.isSecret ? null : (e.value ?? null),
|
|
169
|
+
e.isSecret ? 1 : 0,
|
|
170
|
+
now()
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
db.exec('COMMIT')
|
|
174
|
+
} catch (e) {
|
|
175
|
+
try { db.exec('ROLLBACK') } catch {}
|
|
176
|
+
throw e
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function listEnvOverrides(projectId) {
|
|
181
|
+
const db = openDb()
|
|
182
|
+
return db
|
|
183
|
+
.prepare('SELECT key, value, is_secret FROM env_overrides WHERE project_id = ? ORDER BY key')
|
|
184
|
+
.all(projectId)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Save a complete onboarding snapshot atomically, with secrets encrypted. */
|
|
188
|
+
export function saveConfiguration(projectId, values, { sources = {} } = {}) {
|
|
189
|
+
const db = openDb()
|
|
190
|
+
const secrets = {}
|
|
191
|
+
db.exec('BEGIN')
|
|
192
|
+
try {
|
|
193
|
+
db.prepare('DELETE FROM env_overrides WHERE project_id = ?').run(projectId)
|
|
194
|
+
const insert = db.prepare('INSERT INTO env_overrides (project_id, key, value, is_secret, updated_at) VALUES (?, ?, ?, ?, ?)')
|
|
195
|
+
for (const schema of ENV_SCHEMA) {
|
|
196
|
+
const value = values[schema.key]
|
|
197
|
+
if (sources[schema.key] === 'default') continue
|
|
198
|
+
if (value === undefined || value === null || value === '') continue
|
|
199
|
+
const secret = schema.type === 'secret' || schema.key === 'DEVSYNC_PRIVATE_KEY'
|
|
200
|
+
if (secret) secrets[schema.key] = value
|
|
201
|
+
insert.run(projectId, schema.key, secret ? null : String(value), secret ? 1 : 0, now())
|
|
202
|
+
}
|
|
203
|
+
setSecrets(projectId, secrets)
|
|
204
|
+
db.exec('COMMIT')
|
|
205
|
+
} catch (err) {
|
|
206
|
+
db.exec('ROLLBACK')
|
|
207
|
+
throw err
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Build the effective env (key → value) for a project by layering:
|
|
213
|
+
* 1. schema defaults (defaultsFor(transport))
|
|
214
|
+
* 2. env_overrides from the DB (non-secret keys)
|
|
215
|
+
* 3. secrets from the DB (secret keys)
|
|
216
|
+
*
|
|
217
|
+
* Returns { values, sources } where sources maps KEY → 'default'|'override'|'secret'.
|
|
218
|
+
*/
|
|
219
|
+
export function effectiveEnv(projectId, transport) {
|
|
220
|
+
const values = { ...defaultsFor(transport) }
|
|
221
|
+
const sources = {}
|
|
222
|
+
for (const k of Object.keys(values)) sources[k] = 'default'
|
|
223
|
+
// env_overrides (non-secret only)
|
|
224
|
+
const ovs = listEnvOverrides(projectId).filter((r) => !r.is_secret)
|
|
225
|
+
for (const r of ovs) {
|
|
226
|
+
if (r.value !== null && r.value !== undefined && r.value !== '') {
|
|
227
|
+
values[r.key] = r.value
|
|
228
|
+
sources[r.key] = 'override'
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// secrets (everything that is_secret)
|
|
232
|
+
const secrets = getSecrets(projectId) || {}
|
|
233
|
+
for (const [k, v] of Object.entries(secrets)) {
|
|
234
|
+
if (v !== null && v !== undefined && v !== '') {
|
|
235
|
+
values[k] = v
|
|
236
|
+
sources[k] = 'secret'
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return { values, sources }
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ---------- helpers ----------
|
|
243
|
+
|
|
244
|
+
function rowToProject(r) {
|
|
245
|
+
return {
|
|
246
|
+
id: r.id,
|
|
247
|
+
label: r.label,
|
|
248
|
+
localPath: r.local_path,
|
|
249
|
+
enabled: !!r.enabled,
|
|
250
|
+
createdAt: r.created_at,
|
|
251
|
+
updatedAt: r.updated_at,
|
|
252
|
+
lastStatus: r.last_status,
|
|
253
|
+
lastError: r.last_error,
|
|
254
|
+
lastSyncAt: r.last_sync_at,
|
|
255
|
+
lastUploadBytes: r.last_upload_bytes,
|
|
256
|
+
probeShell: r.probe_shell,
|
|
257
|
+
probeTar: r.probe_tar,
|
|
258
|
+
probeUnzip: r.probe_unzip,
|
|
259
|
+
probeAt: r.probe_at
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// secrets.js — AES-256-GCM secret vault.
|
|
2
|
+
//
|
|
3
|
+
// The master key is generated on first use and stored at
|
|
4
|
+
// ~/.devsync/master.key with mode 0600 (Unix). It is NEVER written to the
|
|
5
|
+
// DB, the tarball, or version control. Losing it means losing the
|
|
6
|
+
// encrypted secrets — that's documented honestly.
|
|
7
|
+
//
|
|
8
|
+
// This is obfuscation, not KMS. Anyone with read access to the master.key
|
|
9
|
+
// file can decrypt the secrets. The threat model is "casual snooping" +
|
|
10
|
+
// "laptop theft while the daemon isn't running", not "skilled attacker
|
|
11
|
+
// with our process address space".
|
|
12
|
+
|
|
13
|
+
import fs from 'node:fs'
|
|
14
|
+
import path from 'node:path'
|
|
15
|
+
import crypto from 'node:crypto'
|
|
16
|
+
import { HOME } from '../config.js'
|
|
17
|
+
|
|
18
|
+
const KEY_FILE = path.join(HOME, 'master.key')
|
|
19
|
+
const ALGO = 'aes-256-gcm'
|
|
20
|
+
const IV_BYTES = 12
|
|
21
|
+
const TAG_BYTES = 16
|
|
22
|
+
|
|
23
|
+
function ensureHome() {
|
|
24
|
+
if (!fs.existsSync(HOME)) fs.mkdirSync(HOME, { recursive: true })
|
|
25
|
+
return HOME
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Read or create the master key file. Mode 0600 on Unix. */
|
|
29
|
+
export function loadMasterKey() {
|
|
30
|
+
ensureHome()
|
|
31
|
+
if (fs.existsSync(KEY_FILE)) {
|
|
32
|
+
return fs.readFileSync(KEY_FILE)
|
|
33
|
+
}
|
|
34
|
+
const key = crypto.randomBytes(32)
|
|
35
|
+
fs.writeFileSync(KEY_FILE, key, { mode: 0o600 })
|
|
36
|
+
// chmod to 0600 if it was created with looser perms (Windows ignores mode).
|
|
37
|
+
try {
|
|
38
|
+
fs.chmodSync(KEY_FILE, 0o600)
|
|
39
|
+
} catch {
|
|
40
|
+
// Windows: chmod is a no-op for some filesystems. Acceptable.
|
|
41
|
+
}
|
|
42
|
+
return key
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Encrypt a JS object into the on-disk shape used by the secrets table. */
|
|
46
|
+
export function encrypt(plaintextObj) {
|
|
47
|
+
const key = loadMasterKey()
|
|
48
|
+
const iv = crypto.randomBytes(IV_BYTES)
|
|
49
|
+
const cipher = crypto.createCipheriv(ALGO, key, iv)
|
|
50
|
+
const json = JSON.stringify(plaintextObj)
|
|
51
|
+
const ciphertext = Buffer.concat([cipher.update(json, 'utf8'), cipher.final()])
|
|
52
|
+
const tag = cipher.getAuthTag()
|
|
53
|
+
return {
|
|
54
|
+
ciphertext: ciphertext.toString('base64'),
|
|
55
|
+
iv: iv.toString('base64'),
|
|
56
|
+
auth_tag: tag.toString('base64')
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Decrypt a row from the secrets table back into a JS object. */
|
|
61
|
+
export function decrypt({ ciphertext, iv, auth_tag }) {
|
|
62
|
+
const key = loadMasterKey()
|
|
63
|
+
const decipher = crypto.createDecipheriv(ALGO, key, Buffer.from(iv, 'base64'))
|
|
64
|
+
decipher.setAuthTag(Buffer.from(auth_tag, 'base64'))
|
|
65
|
+
const plaintext = Buffer.concat([
|
|
66
|
+
decipher.update(Buffer.from(ciphertext, 'base64')),
|
|
67
|
+
decipher.final()
|
|
68
|
+
])
|
|
69
|
+
return JSON.parse(plaintext.toString('utf8'))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Path to the master key file (for diagnostics / doctor). */
|
|
73
|
+
export function masterKeyPath() {
|
|
74
|
+
return KEY_FILE
|
|
75
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// v1Migrate.js — import the v1 ~/.devsync/config.json + .env into the v2 DB.
|
|
2
|
+
//
|
|
3
|
+
// Strategy: one-time on first open of the DB, if v1 files are present and
|
|
4
|
+
// the DB has no projects yet, copy them in. The v1 .env is split into
|
|
5
|
+
// env_overrides (non-secret) + secrets (PASSWORD).
|
|
6
|
+
//
|
|
7
|
+
// Idempotent: if there are already projects in the DB, do nothing.
|
|
8
|
+
//
|
|
9
|
+
// Side-effects on disk: NONE. The v1 files are left in place; if the user
|
|
10
|
+
// wants to delete them they can after running `devsync doctor` and seeing
|
|
11
|
+
// that v2 is self-sufficient.
|
|
12
|
+
|
|
13
|
+
import fs from 'node:fs'
|
|
14
|
+
import path from 'node:path'
|
|
15
|
+
import dotenv from 'dotenv'
|
|
16
|
+
import { HOME, CONFIG_FILE, ENV_FILE } from '../config.js'
|
|
17
|
+
import { openDb } from './db.js'
|
|
18
|
+
import { listProjects, createProject, setSecrets, setEnvOverrides } from './projects.js'
|
|
19
|
+
|
|
20
|
+
const V1_TO_V2_KEYS = {
|
|
21
|
+
PROTO: 'DEVSYNC_TRANSPORT',
|
|
22
|
+
HOST: 'DEVSYNC_HOST',
|
|
23
|
+
PORT: 'DEVSYNC_PORT',
|
|
24
|
+
USERNAME: 'DEVSYNC_USERNAME',
|
|
25
|
+
PASSWORD: 'DEVSYNC_PASSWORD',
|
|
26
|
+
PRIVATE_KEY_PATH: 'DEVSYNC_PRIVATE_KEY',
|
|
27
|
+
REMOTE_PATH: 'DEVSYNC_REMOTE_PATH',
|
|
28
|
+
IGNORE_DELETES: 'DEVSYNC_IGNORE_DELETES',
|
|
29
|
+
DRY_RUN: 'DEVSYNC_DRY_RUN',
|
|
30
|
+
DEBOUNCE_MS: 'DEVSYNC_DEBOUNCE_MS'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const SECRET_KEYS = new Set(['DEVSYNC_PASSWORD', 'DEVSYNC_PRIVATE_KEY'])
|
|
34
|
+
|
|
35
|
+
export function maybeRunV1Import() {
|
|
36
|
+
if (!fs.existsSync(CONFIG_FILE)) return { imported: false, reason: 'no v1 config' }
|
|
37
|
+
let cfg
|
|
38
|
+
try {
|
|
39
|
+
cfg = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'))
|
|
40
|
+
} catch {
|
|
41
|
+
return { imported: false, reason: 'v1 config unreadable' }
|
|
42
|
+
}
|
|
43
|
+
const existing = listProjects()
|
|
44
|
+
if (existing.length > 0) return { imported: false, reason: 'db already populated' }
|
|
45
|
+
const v1Projects = Array.isArray(cfg.projects) ? cfg.projects : []
|
|
46
|
+
if (!v1Projects.length) return { imported: false, reason: 'no v1 projects' }
|
|
47
|
+
|
|
48
|
+
// Read v1 .env once (the creds are shared across projects in v1).
|
|
49
|
+
let envVals = {}
|
|
50
|
+
if (fs.existsSync(ENV_FILE)) {
|
|
51
|
+
envVals = dotenv.parse(fs.readFileSync(ENV_FILE, 'utf8'))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const imported = []
|
|
55
|
+
for (const p of v1Projects) {
|
|
56
|
+
const localPath = path.resolve(p.localPath)
|
|
57
|
+
if (!fs.existsSync(localPath)) continue // skip — v1 record pointed at a path that's gone
|
|
58
|
+
const proj = createProject({
|
|
59
|
+
label: p.label || p.id,
|
|
60
|
+
localPath
|
|
61
|
+
})
|
|
62
|
+
// env overrides from v1 .env (per-project transport default could differ)
|
|
63
|
+
const entries = []
|
|
64
|
+
const transport = (p.transport || cfg.transport || envVals.PROTO || 'sftp').toLowerCase()
|
|
65
|
+
entries.push({ key: 'DEVSYNC_TRANSPORT', value: transport, isSecret: false })
|
|
66
|
+
if (envVals.HOST) entries.push({ key: 'DEVSYNC_HOST', value: envVals.HOST, isSecret: false })
|
|
67
|
+
if (envVals.PORT) entries.push({ key: 'DEVSYNC_PORT', value: envVals.PORT, isSecret: false })
|
|
68
|
+
if (envVals.USERNAME) entries.push({ key: 'DEVSYNC_USERNAME', value: envVals.USERNAME, isSecret: false })
|
|
69
|
+
if (p.remotePath) entries.push({ key: 'DEVSYNC_REMOTE_PATH', value: p.remotePath, isSecret: false })
|
|
70
|
+
if (envVals.PRIVATE_KEY_PATH)
|
|
71
|
+
entries.push({ key: 'DEVSYNC_PRIVATE_KEY', value: envVals.PRIVATE_KEY_PATH, isSecret: true })
|
|
72
|
+
setEnvOverrides(proj.id, entries)
|
|
73
|
+
|
|
74
|
+
// secrets blob — only what we actually have
|
|
75
|
+
const secrets = {}
|
|
76
|
+
if (envVals.PASSWORD) secrets.DEVSYNC_PASSWORD = envVals.PASSWORD
|
|
77
|
+
if (Object.keys(secrets).length) setSecrets(proj.id, secrets)
|
|
78
|
+
imported.push(proj.label)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { imported: true, projects: imported, source: 'v1-config' }
|
|
82
|
+
}
|
package/src/ignore.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import ignore from 'ignore'
|
|
4
|
+
|
|
5
|
+
export const DEPLOYIGNORE = '.deployignore'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Never upload these, regardless of what .deployignore says.
|
|
9
|
+
* This is the control that stops a secret reaching a live host.
|
|
10
|
+
*/
|
|
11
|
+
export const HARD_BLOCKLIST = [
|
|
12
|
+
'.devsync.env',
|
|
13
|
+
'.env',
|
|
14
|
+
'.env.*',
|
|
15
|
+
'*.pem',
|
|
16
|
+
'*.key',
|
|
17
|
+
'*.p12',
|
|
18
|
+
'*.pfx',
|
|
19
|
+
'id_rsa',
|
|
20
|
+
'id_rsa.pub',
|
|
21
|
+
'id_ed25519',
|
|
22
|
+
'.git/',
|
|
23
|
+
'.svn/',
|
|
24
|
+
'.hg/',
|
|
25
|
+
'node_modules/'
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
export const TEMPLATE = `# devsync — what gets uploaded.
|
|
29
|
+
# gitignore syntax. One pattern per line, ! negates.
|
|
30
|
+
# This is NOT .gitignore: these files are usually gitignored but still needed live.
|
|
31
|
+
#
|
|
32
|
+
# The hard blocklist always wins and cannot be overridden here:
|
|
33
|
+
${HARD_BLOCKLIST.map((p) => `# ${p}`).join('\n')}
|
|
34
|
+
|
|
35
|
+
# --- secrets and local state (keep off the server) ---
|
|
36
|
+
*.local.php
|
|
37
|
+
*.sql
|
|
38
|
+
*.sqlite
|
|
39
|
+
*.sqlite3
|
|
40
|
+
*.dump
|
|
41
|
+
.env.production.local
|
|
42
|
+
config.local.php
|
|
43
|
+
|
|
44
|
+
# --- build scratch ---
|
|
45
|
+
/scratch/
|
|
46
|
+
/tmp/
|
|
47
|
+
/backups/
|
|
48
|
+
*.log
|
|
49
|
+
.devsync-uploading
|
|
50
|
+
.deployignore
|
|
51
|
+
|
|
52
|
+
# --- editor noise ---
|
|
53
|
+
.DS_Store
|
|
54
|
+
Thumbs.db
|
|
55
|
+
*.swp
|
|
56
|
+
*~
|
|
57
|
+
.idea/
|
|
58
|
+
.vscode/
|
|
59
|
+
|
|
60
|
+
# --- editorconfig: deploy it, it changes behaviour ---
|
|
61
|
+
# (.editorconfig is intentionally NOT ignored)
|
|
62
|
+
`
|
|
63
|
+
|
|
64
|
+
/** Always ignored, on top of the user's list. */
|
|
65
|
+
function alwaysIgnored() {
|
|
66
|
+
return ignore().add(HARD_BLOCKLIST)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function ensureDeployignore(projectRoot) {
|
|
70
|
+
const file = path.join(projectRoot, DEPLOYIGNORE)
|
|
71
|
+
if (!fs.existsSync(file)) fs.writeFileSync(file, TEMPLATE)
|
|
72
|
+
return file
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Resolve which files are eligible for upload.
|
|
77
|
+
* Returns { allowed(relativePath) -> bool, blocked(relativePath) -> string|null, ig }
|
|
78
|
+
*/
|
|
79
|
+
export function buildMatcher(projectRoot, { useGitignore = false } = {}) {
|
|
80
|
+
ensureDeployignore(projectRoot)
|
|
81
|
+
|
|
82
|
+
const base = alwaysIgnored()
|
|
83
|
+
const user = ignore()
|
|
84
|
+
|
|
85
|
+
const deployFile = path.join(projectRoot, DEPLOYIGNORE)
|
|
86
|
+
user.add(fs.readFileSync(deployFile, 'utf8'))
|
|
87
|
+
|
|
88
|
+
// .gitignore is opt-in. It is a starting template, never the authority:
|
|
89
|
+
// .env and vendor/ are usually gitignored but still required live.
|
|
90
|
+
const gitFile = path.join(projectRoot, '.gitignore')
|
|
91
|
+
const git = ignore()
|
|
92
|
+
if (useGitignore && fs.existsSync(gitFile)) {
|
|
93
|
+
git.add(fs.readFileSync(gitFile, 'utf8'))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const toPosix = (abs) => path.relative(projectRoot, abs).split(path.sep).join('/')
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
/**
|
|
100
|
+
* null = upload, otherwise the reason it was skipped.
|
|
101
|
+
* A pattern like `node_modules/` only matches a path ending in `/`, so a
|
|
102
|
+
* directory is tested in both forms — otherwise the subtree is never gated.
|
|
103
|
+
*/
|
|
104
|
+
reason(relPath) {
|
|
105
|
+
if (relPath === '' || relPath === '.') return 'directory'
|
|
106
|
+
const asDir = relPath.endsWith('/') ? relPath : relPath + '/'
|
|
107
|
+
if (base.ignores(relPath) || base.ignores(asDir)) return 'blocklist'
|
|
108
|
+
if (user.ignores(relPath) || user.ignores(asDir)) return DEPLOYIGNORE
|
|
109
|
+
if (useGitignore && (git.ignores(relPath) || git.ignores(asDir))) return '.gitignore'
|
|
110
|
+
return null
|
|
111
|
+
},
|
|
112
|
+
allowed(relPath) {
|
|
113
|
+
return this.reason(relPath) === null
|
|
114
|
+
},
|
|
115
|
+
/** Chokidar predicate. Excluded directories are refused so chokidar never opens them. */
|
|
116
|
+
shouldWatch(absPath) {
|
|
117
|
+
const rel = toPosix(absPath)
|
|
118
|
+
if (rel === '' || rel === '.') return true
|
|
119
|
+
return this.reason(rel) === null
|
|
120
|
+
},
|
|
121
|
+
/** Directories excluded by policy — surfaced so they are visible, not silent. */
|
|
122
|
+
blockedSample(limit = 20) {
|
|
123
|
+
const out = []
|
|
124
|
+
const walk = (dir, depth) => {
|
|
125
|
+
if (depth > 3 || out.length >= limit) return
|
|
126
|
+
let entries = []
|
|
127
|
+
try {
|
|
128
|
+
entries = fs.readdirSync(dir, { withFileTypes: true })
|
|
129
|
+
} catch {
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
for (const e of entries) {
|
|
133
|
+
const abs = path.join(dir, e.name)
|
|
134
|
+
const rel = toPosix(abs)
|
|
135
|
+
if (e.isDirectory()) {
|
|
136
|
+
if (base.ignores(rel + '/')) out.push(rel + '/')
|
|
137
|
+
else walk(abs, depth + 1)
|
|
138
|
+
} else if (base.ignores(rel)) {
|
|
139
|
+
out.push(rel)
|
|
140
|
+
}
|
|
141
|
+
if (out.length >= limit) return
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
walk(projectRoot, 0)
|
|
145
|
+
return out
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
package/src/metadata.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import { ENV_SCHEMA, defaultsFor } from './core/envSchema.js'
|
|
5
|
+
import { credsFromResolved, settingError } from './core/envResolver.js'
|
|
6
|
+
import { createTransport } from './transport/index.js'
|
|
7
|
+
import { validateRemotePath } from './paths.js'
|
|
8
|
+
|
|
9
|
+
const clean = (text) => String(text).replace(/[\x00-\x1f\x7f]/g, '')
|
|
10
|
+
|
|
11
|
+
/** Browse existing server directories without creating or uploading anything. */
|
|
12
|
+
export async function selectRemoteFolder(transport, ask, log = console.log, initialEntries) {
|
|
13
|
+
let current = '/'
|
|
14
|
+
let entries = initialEntries
|
|
15
|
+
while (true) {
|
|
16
|
+
if (!entries) entries = await transport.list(current)
|
|
17
|
+
const folders = entries.filter((entry) => entry.type === 'dir' && !['.', '..'].includes(entry.name))
|
|
18
|
+
log(`Server folder: ${clean(current)}`)
|
|
19
|
+
folders.forEach((entry, i) => log(` ${i + 1}. ${clean(entry.name)}/`))
|
|
20
|
+
const selection = (await ask({ key: 'DEVSYNC_REMOTE_PATH', label: 'Folder number, .., /absolute/path, . to select, or q to cancel' })).trim()
|
|
21
|
+
if (selection === 'q') throw new Error('Setup cancelled')
|
|
22
|
+
if (selection === '.') return current
|
|
23
|
+
let next
|
|
24
|
+
if (selection === '..') next = path.posix.dirname(current)
|
|
25
|
+
else if (/^[1-9]\d*$/.test(selection) && folders[Number(selection) - 1]) {
|
|
26
|
+
const name = folders[Number(selection) - 1].name
|
|
27
|
+
if (/[\\/\x00-\x1f\x7f]/.test(name)) { log('Unsupported server folder name'); continue }
|
|
28
|
+
next = path.posix.join(current, name)
|
|
29
|
+
} else {
|
|
30
|
+
try { next = validateRemotePath(selection) }
|
|
31
|
+
catch { log('Choose a listed folder, an absolute path, or . to select this folder'); continue }
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const nextEntries = await transport.list(next)
|
|
35
|
+
current = next
|
|
36
|
+
entries = nextEntries
|
|
37
|
+
} catch { log('Cannot open that server folder. Choose another folder or q to cancel.') }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Resolve missing/invalid settings and test a connection before saving a project. */
|
|
42
|
+
export async function prepareConfiguration(resolved, {
|
|
43
|
+
localPath, ask, log = console.log, connect = createTransport, edit = false, browse = false
|
|
44
|
+
} = {}) {
|
|
45
|
+
const values = { ...resolved.values }
|
|
46
|
+
const sources = { ...resolved.sources }
|
|
47
|
+
if (!ask && (edit || browse)) throw new Error('--edit and --browse require an interactive terminal')
|
|
48
|
+
const problems = ENV_SCHEMA.filter((s) => settingError(s.key, values[s.key], values)).map((s) => s.key)
|
|
49
|
+
if (sources.DEVSYNC_TRANSPORT === 'default') problems.unshift('DEVSYNC_TRANSPORT')
|
|
50
|
+
if (!ask && problems.length) {
|
|
51
|
+
throw new Error(`Missing or invalid ${[...new Set(problems)].join(', ') || 'setup choices'}. Set DEVSYNC_* values in the project .env, or run devsync setup in an interactive terminal.`)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function fill(key, { requiredChoice = false, defaultValue } = {}) {
|
|
55
|
+
const schema = ENV_SCHEMA.find((s) => s.key === key)
|
|
56
|
+
let error = settingError(key, values[key], values)
|
|
57
|
+
while (requiredChoice || error) {
|
|
58
|
+
if (error) log(error)
|
|
59
|
+
const previousSource = sources[key]
|
|
60
|
+
let answer = await ask({ key, label: schema.label, secret: schema.type === 'secret', defaultValue })
|
|
61
|
+
if (key !== 'DEVSYNC_PASSWORD') answer = answer.trim()
|
|
62
|
+
if (key === 'DEVSYNC_PRIVATE_KEY' && answer) {
|
|
63
|
+
answer = answer.startsWith('~/') || answer.startsWith('~\\')
|
|
64
|
+
? path.join(os.homedir(), answer.slice(2)) : path.resolve(localPath, answer)
|
|
65
|
+
}
|
|
66
|
+
values[key] = answer
|
|
67
|
+
sources[key] = 'prompt'
|
|
68
|
+
if (previousSource?.includes('env')) log(`Using entered ${key} for this run; update its environment file to keep this correction.`)
|
|
69
|
+
requiredChoice = false
|
|
70
|
+
error = settingError(key, values[key], values)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
await fill('DEVSYNC_TRANSPORT', { requiredChoice: edit || sources.DEVSYNC_TRANSPORT === 'default', defaultValue: edit ? values.DEVSYNC_TRANSPORT : 'ftps' })
|
|
75
|
+
if (sources.DEVSYNC_PORT === 'default') values.DEVSYNC_PORT = defaultsFor(values.DEVSYNC_TRANSPORT).DEVSYNC_PORT
|
|
76
|
+
for (const key of ['DEVSYNC_HOST', 'DEVSYNC_PORT', 'DEVSYNC_USERNAME']) {
|
|
77
|
+
await fill(key, { requiredChoice: edit, defaultValue: edit ? values[key] : key === 'DEVSYNC_PORT' ? defaultsFor(values.DEVSYNC_TRANSPORT).DEVSYNC_PORT : undefined })
|
|
78
|
+
}
|
|
79
|
+
if (values.DEVSYNC_TRANSPORT === 'sftp') {
|
|
80
|
+
await fill('DEVSYNC_PRIVATE_KEY', { requiredChoice: edit, defaultValue: edit ? values.DEVSYNC_PRIVATE_KEY || '' : '' })
|
|
81
|
+
}
|
|
82
|
+
await fill('DEVSYNC_PASSWORD', {
|
|
83
|
+
requiredChoice: edit,
|
|
84
|
+
defaultValue: edit ? values.DEVSYNC_PASSWORD || (values.DEVSYNC_TRANSPORT === 'sftp' && values.DEVSYNC_PRIVATE_KEY ? '' : undefined) : undefined
|
|
85
|
+
})
|
|
86
|
+
for (const s of ENV_SCHEMA.filter((s) => !['DEVSYNC_TRANSPORT', 'DEVSYNC_HOST', 'DEVSYNC_PORT', 'DEVSYNC_USERNAME', 'DEVSYNC_PRIVATE_KEY', 'DEVSYNC_PASSWORD', 'DEVSYNC_REMOTE_PATH'].includes(s.key))) await fill(s.key)
|
|
87
|
+
|
|
88
|
+
let transport
|
|
89
|
+
try {
|
|
90
|
+
transport = await connect(credsFromResolved(values))
|
|
91
|
+
const entries = await transport.list('/')
|
|
92
|
+
log('Connection test passed')
|
|
93
|
+
if (browse || settingError('DEVSYNC_REMOTE_PATH', values.DEVSYNC_REMOTE_PATH, values)) {
|
|
94
|
+
values.DEVSYNC_REMOTE_PATH = await selectRemoteFolder(transport, ask, log, entries)
|
|
95
|
+
sources.DEVSYNC_REMOTE_PATH = 'prompt'
|
|
96
|
+
}
|
|
97
|
+
values.DEVSYNC_REMOTE_PATH = validateRemotePath(values.DEVSYNC_REMOTE_PATH)
|
|
98
|
+
} catch (err) {
|
|
99
|
+
const password = values.DEVSYNC_PASSWORD
|
|
100
|
+
const message = password ? err.message.split(password).join('[redacted]') : err.message
|
|
101
|
+
throw new Error(`Setup could not complete: ${message}. Check the hosting details or run devsync setup --edit.`)
|
|
102
|
+
} finally {
|
|
103
|
+
if (transport) await transport.close()
|
|
104
|
+
}
|
|
105
|
+
for (const s of ENV_SCHEMA) {
|
|
106
|
+
if (values[s.key] === undefined || values[s.key] === '') continue
|
|
107
|
+
const value = s.type === 'secret' || s.key === 'DEVSYNC_PRIVATE_KEY' ? '[set]' : clean(values[s.key])
|
|
108
|
+
log(` ${s.key}=${value} (${sources[s.key]})`)
|
|
109
|
+
}
|
|
110
|
+
return { values, sources }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function assertLocalFolder(localPath) {
|
|
114
|
+
const real = fs.realpathSync(localPath)
|
|
115
|
+
if (!fs.statSync(real).isDirectory()) throw new Error('Local source must be a directory')
|
|
116
|
+
return real
|
|
117
|
+
}
|