snapvault 0.0.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/install.js +92 -0
- package/package.json +20 -0
package/install.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const { execSync } = require("child_process");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const https = require("https");
|
|
5
|
+
|
|
6
|
+
const SNAPSHOT_REPO = "sunneydev/snapshot";
|
|
7
|
+
const RESTIC_REPO = "restic/restic";
|
|
8
|
+
const RESTIC_VERSION = "0.18.1";
|
|
9
|
+
const BIN_DIR = path.join(__dirname, "bin");
|
|
10
|
+
|
|
11
|
+
const PLATFORM_MAP = { linux: "linux", darwin: "darwin", win32: "windows" };
|
|
12
|
+
const ARCH_MAP = { x64: "amd64", arm64: "arm64" };
|
|
13
|
+
|
|
14
|
+
function getPlatform() {
|
|
15
|
+
const platform = PLATFORM_MAP[process.platform];
|
|
16
|
+
const arch = ARCH_MAP[process.arch];
|
|
17
|
+
if (!platform || !arch) {
|
|
18
|
+
console.error(`unsupported platform: ${process.platform}/${process.arch}`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
return { platform, arch };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function follow(url) {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
https.get(url, { headers: { "User-Agent": "snapvault" } }, (res) => {
|
|
27
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
28
|
+
return follow(res.headers.location).then(resolve, reject);
|
|
29
|
+
}
|
|
30
|
+
if (res.statusCode !== 200) {
|
|
31
|
+
return reject(new Error(`download failed (${url}): ${res.statusCode}`));
|
|
32
|
+
}
|
|
33
|
+
const chunks = [];
|
|
34
|
+
res.on("data", (chunk) => chunks.push(chunk));
|
|
35
|
+
res.on("end", () => resolve(Buffer.concat(chunks)));
|
|
36
|
+
res.on("error", reject);
|
|
37
|
+
}).on("error", reject);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function downloadAndExtract(url, filename) {
|
|
42
|
+
return follow(url).then((data) => {
|
|
43
|
+
const tmp = path.join(__dirname, filename);
|
|
44
|
+
fs.writeFileSync(tmp, data);
|
|
45
|
+
|
|
46
|
+
if (filename.endsWith(".tar.gz")) {
|
|
47
|
+
execSync(`tar xzf "${tmp}" -C "${BIN_DIR}"`, { stdio: "inherit" });
|
|
48
|
+
} else if (filename.endsWith(".bz2")) {
|
|
49
|
+
execSync(`bunzip2 -f "${tmp}"`, { stdio: "inherit" });
|
|
50
|
+
const extracted = tmp.replace(/\.bz2$/, "");
|
|
51
|
+
fs.renameSync(extracted, path.join(BIN_DIR, "restic"));
|
|
52
|
+
} else if (filename.endsWith(".zip")) {
|
|
53
|
+
execSync(`unzip -o "${tmp}" -d "${BIN_DIR}"`, { stdio: "inherit" });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (fs.existsSync(tmp)) fs.unlinkSync(tmp);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function main() {
|
|
61
|
+
const { platform, arch } = getPlatform();
|
|
62
|
+
fs.mkdirSync(BIN_DIR, { recursive: true });
|
|
63
|
+
|
|
64
|
+
const snapshotExt = platform === "linux" ? "tar.gz" : "zip";
|
|
65
|
+
const snapshotFile = `snapshot_${platform}_${arch}.${snapshotExt}`;
|
|
66
|
+
const snapshotUrl = `https://github.com/${SNAPSHOT_REPO}/releases/latest/download/${snapshotFile}`;
|
|
67
|
+
|
|
68
|
+
const isWindows = platform === "windows";
|
|
69
|
+
const resticExt = isWindows ? "zip" : "bz2";
|
|
70
|
+
const resticFile = `restic_${RESTIC_VERSION}_${platform}_${arch}.${resticExt}`;
|
|
71
|
+
const resticUrl = `https://github.com/${RESTIC_REPO}/releases/download/v${RESTIC_VERSION}/${resticFile}`;
|
|
72
|
+
|
|
73
|
+
console.log("downloading snapshot...");
|
|
74
|
+
console.log("downloading restic...");
|
|
75
|
+
|
|
76
|
+
await Promise.all([
|
|
77
|
+
downloadAndExtract(snapshotUrl, snapshotFile),
|
|
78
|
+
downloadAndExtract(resticUrl, resticFile),
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
for (const name of ["snapshot", "restic"]) {
|
|
82
|
+
const bin = path.join(BIN_DIR, isWindows ? `${name}.exe` : name);
|
|
83
|
+
if (fs.existsSync(bin)) fs.chmodSync(bin, 0o755);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
console.log("snapshot + restic installed successfully");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
main().catch((err) => {
|
|
90
|
+
console.error(err.message);
|
|
91
|
+
process.exit(1);
|
|
92
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "snapvault",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "protect your workspaces from destructive ai agents and accidental changes with automatic periodic backups",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/sunneydev/snapshot"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"snapshot": "bin/snapshot"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"postinstall": "node install.js"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"install.js",
|
|
18
|
+
"bin/"
|
|
19
|
+
]
|
|
20
|
+
}
|