depops 0.2.0 → 0.3.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/README.md +12 -0
- package/package.json +1 -1
- package/src/cli.js +7 -0
- package/src/update.js +142 -0
package/README.md
CHANGED
|
@@ -83,6 +83,7 @@ depops edit add/remove projects, steps, server, sudo, re
|
|
|
83
83
|
depops env list|set KEY|rm KEY|import [file] [--target=name]
|
|
84
84
|
depops show [--reveal] summary (server details hidden unless --reveal)
|
|
85
85
|
depops passwd change master password
|
|
86
|
+
depops update update to the newest version
|
|
86
87
|
```
|
|
87
88
|
|
|
88
89
|
For scripts, set `DEPOPS_PASSWORD` in the environment instead of typing it.
|
|
@@ -120,6 +121,17 @@ server. During deploy the server logs in, pulls, and **logs out again**, so cred
|
|
|
120
121
|
**The vault is only as strong as the master password.** Use a long, unique one and share it through a
|
|
121
122
|
password manager. Anyone with the repo *and* the password can deploy.
|
|
122
123
|
|
|
124
|
+
## Updates
|
|
125
|
+
|
|
126
|
+
depops checks npm for a new version at most every 12 hours (1.5s timeout, never blocks when offline):
|
|
127
|
+
|
|
128
|
+
- **npx:** runs the newest version automatically.
|
|
129
|
+
- **Global install** (`npm i -g depops`): updates itself, then re-runs your command.
|
|
130
|
+
- **Project install** (`node_modules`): shows the update command, never edits your `package.json`.
|
|
131
|
+
|
|
132
|
+
`depops update` updates on demand; `depops --version` shows the installed version.
|
|
133
|
+
Turn it off with `--no-update` or `DEPOPS_NO_UPDATE=1`. It is always off in CI.
|
|
134
|
+
|
|
123
135
|
## Requirements
|
|
124
136
|
|
|
125
137
|
- Node.js 18.17+
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -6,6 +6,7 @@ import path from 'node:path';
|
|
|
6
6
|
import { VAULT_FILE, VaultError, findVault, readVault, writeVault, checkPasswordStrength } from './vault.js';
|
|
7
7
|
import { connect, exec, runScript, installPublicKey, generateKeyPair, upload, shQuote as q, scrub } from './ssh.js';
|
|
8
8
|
import { APP_TYPES, presetSteps } from './presets.js';
|
|
9
|
+
import { autoUpdate, manualUpdate, currentVersion } from './update.js';
|
|
9
10
|
import { detectProjects, findCompose, loadCompose, analyze, buildArgv, serverCompose } from './compose.js';
|
|
10
11
|
|
|
11
12
|
// ───────────────────────── helpers ─────────────────────────
|
|
@@ -894,8 +895,11 @@ Usage:
|
|
|
894
895
|
depops env [list|set KEY|rm KEY|import [file]] [--target=name]
|
|
895
896
|
depops show [--reveal] Vault summary (server details hidden by default)
|
|
896
897
|
depops passwd Change the master password
|
|
898
|
+
depops update Update depops to the newest version
|
|
899
|
+
depops --version Show the installed version
|
|
897
900
|
|
|
898
901
|
Non-interactive (scripts): set DEPOPS_PASSWORD in the environment.
|
|
902
|
+
Auto-update: checks npm every 12h. Turn off with --no-update or DEPOPS_NO_UPDATE=1 (always off in CI).
|
|
899
903
|
`;
|
|
900
904
|
|
|
901
905
|
export async function main(argv) {
|
|
@@ -907,8 +911,11 @@ export async function main(argv) {
|
|
|
907
911
|
else flags[a.slice(2)] = true;
|
|
908
912
|
}
|
|
909
913
|
const [cmd, ...rest] = args;
|
|
914
|
+
if (flags.version || cmd === 'version') return process.stdout.write(`${await currentVersion()}\n`);
|
|
915
|
+
if (cmd !== 'update') await autoUpdate(argv, flags);
|
|
910
916
|
try {
|
|
911
917
|
switch (cmd) {
|
|
918
|
+
case 'update': return await manualUpdate();
|
|
912
919
|
case 'init': return await cmdInit();
|
|
913
920
|
case 'deploy': return await cmdDeploy(rest, flags);
|
|
914
921
|
case 'test': return await cmdTest();
|
package/src/update.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Self-update: checks the npm registry (at most every 12h) and moves to the newest version.
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import fs from 'node:fs/promises';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
const PKG = 'depops';
|
|
9
|
+
const REGISTRY_URL = `https://registry.npmjs.org/${PKG}/latest`;
|
|
10
|
+
const CHECK_EVERY_MS = 12 * 60 * 60 * 1000;
|
|
11
|
+
const CACHE_FILE = path.join(os.homedir(), '.depops', 'update.json');
|
|
12
|
+
const isWin = process.platform === 'win32';
|
|
13
|
+
|
|
14
|
+
export async function currentVersion() {
|
|
15
|
+
const pkg = JSON.parse(await fs.readFile(new URL('../package.json', import.meta.url), 'utf8'));
|
|
16
|
+
return pkg.version;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** true when a > b for plain x.y.z versions (pre-releases are never offered). */
|
|
20
|
+
export function isNewer(a, b) {
|
|
21
|
+
const pa = String(a).split('.').map(Number);
|
|
22
|
+
const pb = String(b).split('.').map(Number);
|
|
23
|
+
if (pa.length !== 3 || pa.some(Number.isNaN) || /-/.test(a)) return false;
|
|
24
|
+
for (let i = 0; i < 3; i++) if (pa[i] !== pb[i]) return pa[i] > pb[i];
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function readCache() {
|
|
29
|
+
try {
|
|
30
|
+
return JSON.parse(await fs.readFile(CACHE_FILE, 'utf8'));
|
|
31
|
+
} catch {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function writeCache(data) {
|
|
37
|
+
try {
|
|
38
|
+
await fs.mkdir(path.dirname(CACHE_FILE), { recursive: true, mode: 0o700 });
|
|
39
|
+
await fs.writeFile(CACHE_FILE, JSON.stringify(data));
|
|
40
|
+
} catch {}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function fetchLatest(timeoutMs = 1500) {
|
|
44
|
+
const res = await fetch(REGISTRY_URL, { signal: AbortSignal.timeout(timeoutMs), headers: { accept: 'application/json' } });
|
|
45
|
+
if (!res.ok) throw new Error(`registry responded ${res.status}`);
|
|
46
|
+
const { version } = await res.json();
|
|
47
|
+
if (!/^\d+\.\d+\.\d+$/.test(version)) throw new Error('unexpected version from registry');
|
|
48
|
+
return version;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** npx | global | local — decides how an update can be applied. */
|
|
52
|
+
export async function installMode() {
|
|
53
|
+
const here = fileURLToPath(import.meta.url);
|
|
54
|
+
if (/[\\/]_npx[\\/]/.test(here)) return 'npx';
|
|
55
|
+
const globalRoot = await new Promise((resolve) => {
|
|
56
|
+
let out = '';
|
|
57
|
+
const c = spawn('npm', ['root', '-g'], { shell: isWin, stdio: ['ignore', 'pipe', 'ignore'] });
|
|
58
|
+
c.stdout.on('data', (d) => (out += d));
|
|
59
|
+
c.on('close', () => resolve(out.trim()));
|
|
60
|
+
c.on('error', () => resolve(''));
|
|
61
|
+
});
|
|
62
|
+
if (globalRoot && path.resolve(here).startsWith(path.resolve(globalRoot) + path.sep)) return 'global';
|
|
63
|
+
return 'local';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function run(cmd, args, env = process.env) {
|
|
67
|
+
return new Promise((resolve) => {
|
|
68
|
+
const c = spawn(cmd, args, { stdio: 'inherit', shell: isWin, env });
|
|
69
|
+
c.on('close', (code) => resolve(code ?? 1));
|
|
70
|
+
c.on('error', () => resolve(1));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const say = (msg) => process.stderr.write(`\x1b[36m›\x1b[0m ${msg}\n`);
|
|
75
|
+
|
|
76
|
+
/** Re-run the same command with the new version, then exit with its code. */
|
|
77
|
+
async function rerun(mode, latest, argv) {
|
|
78
|
+
const env = { ...process.env, DEPOPS_UPDATED: '1' };
|
|
79
|
+
const code = mode === 'npx' ? await run('npx', ['-y', `${PKG}@${latest}`, ...argv], env) : await run(PKG, argv, env);
|
|
80
|
+
process.exit(code);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function installLatest(latest) {
|
|
84
|
+
return run('npm', ['install', '-g', `${PKG}@${latest}`]);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Called on every start. Never throws and never blocks for more than ~1.5s.
|
|
89
|
+
* Skipped in CI, when DEPOPS_NO_UPDATE is set, with --no-update, and inside an already-updated run.
|
|
90
|
+
*/
|
|
91
|
+
export async function autoUpdate(argv, flags) {
|
|
92
|
+
if (process.env.CI || process.env.DEPOPS_NO_UPDATE || process.env.DEPOPS_UPDATED || flags['no-update']) return;
|
|
93
|
+
try {
|
|
94
|
+
const current = await currentVersion();
|
|
95
|
+
const cache = await readCache();
|
|
96
|
+
let latest = cache.latest;
|
|
97
|
+
if (!latest || Date.now() - (cache.checkedAt || 0) > CHECK_EVERY_MS) {
|
|
98
|
+
latest = await fetchLatest();
|
|
99
|
+
await writeCache({ checkedAt: Date.now(), latest });
|
|
100
|
+
}
|
|
101
|
+
if (!isNewer(latest, current)) return;
|
|
102
|
+
|
|
103
|
+
const mode = await installMode();
|
|
104
|
+
if (mode === 'npx') {
|
|
105
|
+
say(`depops ${latest} is available (you have ${current}) — running the new version…`);
|
|
106
|
+
return rerun(mode, latest, argv);
|
|
107
|
+
}
|
|
108
|
+
if (mode === 'global') {
|
|
109
|
+
say(`Updating depops ${current} → ${latest}…`);
|
|
110
|
+
if ((await installLatest(latest)) === 0) {
|
|
111
|
+
say(`Updated to ${latest}.`);
|
|
112
|
+
return rerun(mode, latest, argv);
|
|
113
|
+
}
|
|
114
|
+
say(`Auto-update failed. Run manually: npm install -g ${PKG}@latest`);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
say(`depops ${latest} is available (you have ${current}). Update with: npm install -D ${PKG}@latest`);
|
|
118
|
+
} catch {
|
|
119
|
+
// Offline or registry down: never block the actual command.
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** `depops update` */
|
|
124
|
+
export async function manualUpdate() {
|
|
125
|
+
const current = await currentVersion();
|
|
126
|
+
let latest;
|
|
127
|
+
try {
|
|
128
|
+
latest = await fetchLatest(5000);
|
|
129
|
+
} catch (e) {
|
|
130
|
+
say(`Could not reach the npm registry: ${e.message}`);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
}
|
|
133
|
+
await writeCache({ checkedAt: Date.now(), latest });
|
|
134
|
+
if (!isNewer(latest, current)) return say(`depops ${current} is the latest version.`);
|
|
135
|
+
const mode = await installMode();
|
|
136
|
+
if (mode === 'npx') return say(`npx always uses the newest version on the next run (${latest}).`);
|
|
137
|
+
if (mode === 'global') {
|
|
138
|
+
say(`Updating depops ${current} → ${latest}…`);
|
|
139
|
+
process.exit(await installLatest(latest));
|
|
140
|
+
}
|
|
141
|
+
say(`This project has depops ${current} installed locally. Update with: npm install -D ${PKG}@latest`);
|
|
142
|
+
}
|