backdot 1.5.0 → 1.6.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.
- package/README.md +11 -8
- package/dist/cli.js +64 -34
- package/dist/commands/backup.js +4 -3
- package/dist/commands/history.d.ts +1 -0
- package/dist/commands/history.js +35 -0
- package/dist/commands/init.js +16 -1
- package/dist/commands/restore.d.ts +5 -1
- package/dist/commands/restore.js +50 -27
- package/dist/commands/schedule.js +1 -1
- package/dist/commands/status.js +6 -5
- package/dist/commitUrl.js +2 -1
- package/dist/errorMessage.d.ts +1 -0
- package/dist/errorMessage.js +3 -0
- package/dist/git.d.ts +22 -1
- package/dist/git.js +132 -33
- package/dist/launchd.d.ts +3 -0
- package/dist/launchd.js +112 -0
- package/dist/log.d.ts +5 -1
- package/dist/log.js +17 -6
- package/dist/notify.js +4 -3
- package/dist/paths.d.ts +3 -0
- package/dist/paths.js +8 -0
- package/dist/plist.js +14 -6
- package/dist/resolve.js +6 -4
- package/dist/resolveFiles.d.ts +6 -0
- package/dist/resolveFiles.js +44 -0
- package/dist/staging.d.ts +3 -3
- package/dist/staging.js +18 -16
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +9 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -51,14 +51,17 @@ Prefix a pattern with `!` to exclude matching files:
|
|
|
51
51
|
|
|
52
52
|
## Commands
|
|
53
53
|
|
|
54
|
-
| Command
|
|
55
|
-
|
|
|
56
|
-
| `--init`
|
|
57
|
-
| `--backup`
|
|
58
|
-
| `--restore`
|
|
59
|
-
| `--
|
|
60
|
-
| `--
|
|
61
|
-
| `--
|
|
54
|
+
| Command | Description |
|
|
55
|
+
| -------------------------------- | ---------------------------------------------- |
|
|
56
|
+
| `--init` | Set up backdot for the first time |
|
|
57
|
+
| `--backup` | Run a backup now |
|
|
58
|
+
| `--restore` | Restore latest backup from the configured repo |
|
|
59
|
+
| `--restore <url>` | Restore from a specific repo URL |
|
|
60
|
+
| `--restore [url] --commit <sha>` | Restore from a specific backup commit |
|
|
61
|
+
| `--history [url]` | Browse and restore a previous backup |
|
|
62
|
+
| `--schedule` | Schedule automatic daily backup (Mac-only) |
|
|
63
|
+
| `--unschedule` | Unschedule the daily backup |
|
|
64
|
+
| `--status` | Show schedule and resolved file list |
|
|
62
65
|
|
|
63
66
|
## Development
|
|
64
67
|
|
package/dist/cli.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
4
5
|
import chalk from "chalk";
|
|
5
6
|
import { CONFIG_PATH } from "./config.js";
|
|
6
7
|
import { backup } from "./commands/backup.js";
|
|
7
8
|
import { status } from "./commands/status.js";
|
|
8
9
|
import { schedule, unschedule } from "./commands/schedule.js";
|
|
9
10
|
import { restore } from "./commands/restore.js";
|
|
11
|
+
import { history } from "./commands/history.js";
|
|
10
12
|
import { init } from "./commands/init.js";
|
|
11
13
|
import { logger } from "./log.js";
|
|
14
|
+
import { errorMessage } from "./utils.js";
|
|
12
15
|
import { sendNotification } from "./notify.js";
|
|
13
16
|
function getVersion() {
|
|
14
17
|
const pkgPath = path.resolve(path.dirname(new URL(import.meta.url).pathname), "../package.json");
|
|
@@ -20,62 +23,89 @@ function getVersion() {
|
|
|
20
23
|
return "unknown";
|
|
21
24
|
}
|
|
22
25
|
}
|
|
26
|
+
function printHelp() {
|
|
27
|
+
console.log();
|
|
28
|
+
console.log(" Usage: backdot <command>");
|
|
29
|
+
console.log();
|
|
30
|
+
console.log(" Commands:");
|
|
31
|
+
console.log();
|
|
32
|
+
console.log(" --init Set up backdot for the first time");
|
|
33
|
+
console.log(" --backup Run a backup now");
|
|
34
|
+
console.log(" --restore [url] Restore files from the backup repo");
|
|
35
|
+
console.log(" --restore [url] --commit <sha> Restore from a specific backup commit");
|
|
36
|
+
console.log(" --restore [url] --yes (-y) Accept defaults without prompting");
|
|
37
|
+
console.log(" --history [url] Browse and restore a previous backup");
|
|
38
|
+
console.log(" --schedule Install daily backup schedule (macOS launchd)");
|
|
39
|
+
console.log(" --unschedule Remove the daily backup schedule");
|
|
40
|
+
console.log(" --status Show schedule and resolved files");
|
|
41
|
+
console.log(" --version Show version");
|
|
42
|
+
console.log();
|
|
43
|
+
}
|
|
23
44
|
async function main() {
|
|
24
|
-
|
|
25
|
-
|
|
45
|
+
let values;
|
|
46
|
+
let positionals;
|
|
47
|
+
try {
|
|
48
|
+
({ values, positionals } = parseArgs({
|
|
49
|
+
args: process.argv.slice(2),
|
|
50
|
+
options: {
|
|
51
|
+
init: { type: "boolean" },
|
|
52
|
+
backup: { type: "boolean" },
|
|
53
|
+
restore: { type: "boolean" },
|
|
54
|
+
history: { type: "boolean" },
|
|
55
|
+
schedule: { type: "boolean" },
|
|
56
|
+
unschedule: { type: "boolean" },
|
|
57
|
+
status: { type: "boolean" },
|
|
58
|
+
version: { type: "boolean" },
|
|
59
|
+
help: { type: "boolean" },
|
|
60
|
+
commit: { type: "string" },
|
|
61
|
+
yes: { type: "boolean", short: "y" },
|
|
62
|
+
},
|
|
63
|
+
allowPositionals: true,
|
|
64
|
+
strict: true,
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
console.error(`\n Error: ${errorMessage(err)}\n`);
|
|
69
|
+
printHelp();
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
26
72
|
try {
|
|
27
|
-
if (
|
|
73
|
+
if (values.init) {
|
|
28
74
|
init();
|
|
29
75
|
}
|
|
30
|
-
else if (
|
|
76
|
+
else if (values.backup) {
|
|
31
77
|
await backup();
|
|
32
78
|
}
|
|
33
|
-
else if (
|
|
79
|
+
else if (values.schedule) {
|
|
34
80
|
schedule();
|
|
35
81
|
}
|
|
36
|
-
else if (
|
|
82
|
+
else if (values.unschedule) {
|
|
37
83
|
unschedule();
|
|
38
84
|
}
|
|
39
|
-
else if (
|
|
85
|
+
else if (values.status) {
|
|
40
86
|
await status();
|
|
41
87
|
}
|
|
42
|
-
else if (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
88
|
+
else if (values.restore) {
|
|
89
|
+
await restore(positionals[0], values.commit, {
|
|
90
|
+
yes: !!values.yes,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else if (values.history) {
|
|
94
|
+
await history(positionals[0]);
|
|
48
95
|
}
|
|
49
|
-
else if (
|
|
96
|
+
else if (values.version) {
|
|
50
97
|
console.log(getVersion());
|
|
51
98
|
}
|
|
52
99
|
else {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
console.log();
|
|
56
|
-
console.log(" Commands:");
|
|
57
|
-
console.log();
|
|
58
|
-
console.log(" --init Set up backdot for the first time");
|
|
59
|
-
console.log(" --backup Run a backup now");
|
|
60
|
-
console.log(" --restore [url] Restore files from the backup repo");
|
|
61
|
-
console.log(" --schedule Install daily backup schedule (macOS launchd)");
|
|
62
|
-
console.log(" --unschedule Remove the daily backup schedule");
|
|
63
|
-
console.log(" --status Show schedule and resolved files");
|
|
64
|
-
console.log(" --version Show version");
|
|
65
|
-
console.log();
|
|
66
|
-
if (command && command !== "--help") {
|
|
67
|
-
console.error(` Unknown command: ${command}`);
|
|
68
|
-
console.log();
|
|
69
|
-
process.exit(1);
|
|
70
|
-
}
|
|
71
|
-
else if (!fs.existsSync(CONFIG_PATH)) {
|
|
100
|
+
printHelp();
|
|
101
|
+
if (!fs.existsSync(CONFIG_PATH)) {
|
|
72
102
|
console.log(` No config found. Run ${chalk.bold("backdot --init")} to get started.`);
|
|
73
103
|
console.log();
|
|
74
104
|
}
|
|
75
105
|
}
|
|
76
106
|
}
|
|
77
107
|
catch (err) {
|
|
78
|
-
const msg =
|
|
108
|
+
const msg = errorMessage(err);
|
|
79
109
|
logger.error(msg);
|
|
80
110
|
console.error(`\n Error: ${msg}\n`);
|
|
81
111
|
if (!process.stdout.isTTY) {
|
package/dist/commands/backup.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import ora from "ora";
|
|
2
2
|
import { loadConfig, CONFIG_PATH } from "../config.js";
|
|
3
|
-
import { resolveFiles } from "../
|
|
3
|
+
import { resolveFiles } from "../resolveFiles.js";
|
|
4
4
|
import { cleanStaging, copyToStaging, writeRepoReadme } from "../staging.js";
|
|
5
5
|
import { gitPull, gitCommitAndPush } from "../git.js";
|
|
6
6
|
import { logger } from "../log.js";
|
|
7
|
+
import { pluralize } from "../utils.js";
|
|
7
8
|
export async function backup() {
|
|
8
9
|
logger.info("Starting backup");
|
|
9
10
|
const config = loadConfig();
|
|
@@ -12,7 +13,7 @@ export async function backup() {
|
|
|
12
13
|
const spinner = ora("Resolving files").start();
|
|
13
14
|
try {
|
|
14
15
|
const userFiles = resolveFiles(config);
|
|
15
|
-
logger.info(`Resolved ${userFiles.length
|
|
16
|
+
logger.info(`Resolved ${pluralize(userFiles.length, "file")}`);
|
|
16
17
|
if (userFiles.length === 0) {
|
|
17
18
|
spinner.info("No files resolved, nothing to back up");
|
|
18
19
|
return;
|
|
@@ -20,7 +21,7 @@ export async function backup() {
|
|
|
20
21
|
const files = [...userFiles, CONFIG_PATH];
|
|
21
22
|
spinner.text = "Syncing with remote";
|
|
22
23
|
await gitPull(config.repository);
|
|
23
|
-
spinner.text = `Copying ${files.length
|
|
24
|
+
spinner.text = `Copying ${pluralize(files.length, "file")} to staging`;
|
|
24
25
|
cleanStaging(config.machine);
|
|
25
26
|
copyToStaging(files, config.machine);
|
|
26
27
|
writeRepoReadme(config.repository);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function history(repoUrl?: string): Promise<void>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import ora from "ora";
|
|
2
|
+
import { select } from "@inquirer/prompts";
|
|
3
|
+
import { loadConfig } from "../config.js";
|
|
4
|
+
import { gitPull, gitLog } from "../git.js";
|
|
5
|
+
import { restore } from "./restore.js";
|
|
6
|
+
import { logger } from "../log.js";
|
|
7
|
+
export async function history(repoUrl) {
|
|
8
|
+
logger.info("Starting history");
|
|
9
|
+
const repository = repoUrl ?? loadConfig().repository;
|
|
10
|
+
const spinner = ora("Fetching backup history").start();
|
|
11
|
+
let commits;
|
|
12
|
+
try {
|
|
13
|
+
await gitPull(repository);
|
|
14
|
+
commits = await gitLog();
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
spinner.fail("Failed to fetch backup history");
|
|
18
|
+
throw err;
|
|
19
|
+
}
|
|
20
|
+
spinner.stop();
|
|
21
|
+
if (commits.length === 0) {
|
|
22
|
+
console.log("\n No backup history found.\n");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const selected = await select({
|
|
26
|
+
message: "Select a backup to restore from:",
|
|
27
|
+
loop: false,
|
|
28
|
+
choices: commits.map((c) => ({
|
|
29
|
+
name: `${c.hash.slice(0, 7)} ${c.date.split("T")[0]} ${c.message}`,
|
|
30
|
+
value: c.hash,
|
|
31
|
+
})),
|
|
32
|
+
});
|
|
33
|
+
console.log();
|
|
34
|
+
await restore(repoUrl, selected);
|
|
35
|
+
}
|
package/dist/commands/init.js
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
1
2
|
import fs from "node:fs";
|
|
2
3
|
import os from "node:os";
|
|
3
4
|
import chalk from "chalk";
|
|
4
5
|
import { CONFIG_PATH } from "../config.js";
|
|
6
|
+
function getMachineName() {
|
|
7
|
+
if (process.platform === "darwin") {
|
|
8
|
+
try {
|
|
9
|
+
return execFileSync("scutil", ["--get", "LocalHostName"], {
|
|
10
|
+
encoding: "utf-8",
|
|
11
|
+
stdio: "pipe",
|
|
12
|
+
}).trim();
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
// fall through
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return os.hostname().replace(/\.(local|localdomain)$/, "");
|
|
19
|
+
}
|
|
5
20
|
const TEMPLATE = {
|
|
6
21
|
repository: "git@github.com:USERNAME/backdot-backup.git",
|
|
7
|
-
machine:
|
|
22
|
+
machine: getMachineName(),
|
|
8
23
|
paths: ["~/.zshrc", "~/.gitconfig"],
|
|
9
24
|
};
|
|
10
25
|
export function init() {
|
package/dist/commands/restore.js
CHANGED
|
@@ -2,11 +2,12 @@ import fs from "node:fs";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import ora from "ora";
|
|
5
|
-
import { checkbox, select } from "@inquirer/prompts";
|
|
5
|
+
import { checkbox, select, Separator } from "@inquirer/prompts";
|
|
6
6
|
import { loadConfig } from "../config.js";
|
|
7
7
|
import { gitPull } from "../git.js";
|
|
8
8
|
import { STAGING_DIR, machineDir } from "../staging.js";
|
|
9
9
|
import { logger } from "../log.js";
|
|
10
|
+
import { pluralize } from "../utils.js";
|
|
10
11
|
const HOME = os.homedir();
|
|
11
12
|
function walkDir(dir) {
|
|
12
13
|
return fs
|
|
@@ -18,20 +19,27 @@ function walkDir(dir) {
|
|
|
18
19
|
});
|
|
19
20
|
}
|
|
20
21
|
function listMachines() {
|
|
21
|
-
if (!fs.existsSync(STAGING_DIR))
|
|
22
|
+
if (!fs.existsSync(STAGING_DIR)) {
|
|
22
23
|
return [];
|
|
24
|
+
}
|
|
23
25
|
return fs
|
|
24
26
|
.readdirSync(STAGING_DIR, { withFileTypes: true })
|
|
25
27
|
.filter((e) => e.isDirectory() && e.name !== ".git")
|
|
26
28
|
.map((e) => e.name);
|
|
27
29
|
}
|
|
28
|
-
async function resolveRepoAndMachine(repoUrl) {
|
|
30
|
+
async function resolveRepoAndMachine(repoUrl, commit) {
|
|
29
31
|
if (!repoUrl) {
|
|
30
32
|
const config = loadConfig();
|
|
31
33
|
return { repository: config.repository, machine: config.machine };
|
|
32
34
|
}
|
|
33
35
|
const spinner = ora("Cloning backup repository").start();
|
|
34
|
-
|
|
36
|
+
try {
|
|
37
|
+
await gitPull(repoUrl, commit);
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
spinner.fail("Failed to clone backup repository");
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
35
43
|
spinner.stop();
|
|
36
44
|
const machines = listMachines();
|
|
37
45
|
if (machines.length === 0) {
|
|
@@ -44,18 +52,25 @@ async function resolveRepoAndMachine(repoUrl) {
|
|
|
44
52
|
else {
|
|
45
53
|
machine = await select({
|
|
46
54
|
message: "Multiple machines found. Which one do you want to restore?",
|
|
55
|
+
loop: false,
|
|
47
56
|
choices: machines.map((m) => ({ name: m, value: m })),
|
|
48
57
|
});
|
|
49
58
|
}
|
|
50
59
|
return { repository: repoUrl, machine };
|
|
51
60
|
}
|
|
52
|
-
export async function restore(repoUrl) {
|
|
61
|
+
export async function restore(repoUrl, commit, options = {}) {
|
|
53
62
|
logger.info("Starting restore");
|
|
54
|
-
const { repository, machine } = await resolveRepoAndMachine(repoUrl);
|
|
63
|
+
const { repository, machine } = await resolveRepoAndMachine(repoUrl, commit);
|
|
55
64
|
const spinner = ora("Fetching latest backup").start();
|
|
56
65
|
const baseDir = machineDir(machine);
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
try {
|
|
67
|
+
if (!repoUrl) {
|
|
68
|
+
await gitPull(repository, commit);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
spinner.fail("Failed to fetch latest backup");
|
|
73
|
+
throw err;
|
|
59
74
|
}
|
|
60
75
|
spinner.text = "Resolving files";
|
|
61
76
|
if (!fs.existsSync(baseDir)) {
|
|
@@ -71,7 +86,7 @@ export async function restore(repoUrl) {
|
|
|
71
86
|
return;
|
|
72
87
|
}
|
|
73
88
|
const stagedFiles = walkDir(baseDir);
|
|
74
|
-
logger.info(`Found ${stagedFiles.length
|
|
89
|
+
logger.info(`Found ${pluralize(stagedFiles.length, "file")} in backup repository`);
|
|
75
90
|
if (stagedFiles.length === 0) {
|
|
76
91
|
spinner.stop();
|
|
77
92
|
console.log("No files found in backup repository.");
|
|
@@ -86,25 +101,33 @@ export async function restore(repoUrl) {
|
|
|
86
101
|
logger.info(`${fresh.length} new, ${existing.length} already exist`);
|
|
87
102
|
spinner.stop();
|
|
88
103
|
console.log();
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
console.log(` ${
|
|
104
|
+
let toRestore;
|
|
105
|
+
if (options.yes) {
|
|
106
|
+
toRestore = fresh;
|
|
107
|
+
if (existing.length > 0) {
|
|
108
|
+
console.log(` Skipped ${pluralize(existing.length, "existing file")}. Run without --yes to select them.`);
|
|
109
|
+
console.log();
|
|
94
110
|
}
|
|
95
|
-
console.log();
|
|
96
111
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
name: f.rel,
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
112
|
+
else {
|
|
113
|
+
const choices = [];
|
|
114
|
+
if (fresh.length > 0) {
|
|
115
|
+
choices.push(new Separator(`── New files (${fresh.length}) ──`));
|
|
116
|
+
for (const f of fresh) {
|
|
117
|
+
choices.push({ name: f.rel, value: f, checked: true });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (existing.length > 0) {
|
|
121
|
+
choices.push(new Separator(`── Existing files — will overwrite (${existing.length}) ──`));
|
|
122
|
+
for (const f of existing) {
|
|
123
|
+
choices.push({ name: f.rel, value: f, checked: false });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
toRestore = await checkbox({
|
|
127
|
+
message: "Select files to restore:",
|
|
128
|
+
loop: false,
|
|
129
|
+
choices,
|
|
106
130
|
});
|
|
107
|
-
toRestore = [...fresh, ...selected];
|
|
108
131
|
console.log();
|
|
109
132
|
}
|
|
110
133
|
if (toRestore.length === 0) {
|
|
@@ -116,7 +139,7 @@ export async function restore(repoUrl) {
|
|
|
116
139
|
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
117
140
|
fs.copyFileSync(src, dest);
|
|
118
141
|
}
|
|
119
|
-
copySpinner.succeed(`Restored ${toRestore.length
|
|
142
|
+
copySpinner.succeed(`Restored ${pluralize(toRestore.length, "file")}`);
|
|
120
143
|
console.log();
|
|
121
|
-
logger.info(`Restored ${toRestore.length
|
|
144
|
+
logger.info(`Restored ${pluralize(toRestore.length, "file")}`);
|
|
122
145
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { setupLaunchd, uninstallLaunchd } from "../
|
|
1
|
+
import { setupLaunchd, uninstallLaunchd } from "../launchd.js";
|
|
2
2
|
function requireMacOS() {
|
|
3
3
|
if (process.platform !== "darwin") {
|
|
4
4
|
throw new Error("Scheduling is only supported on macOS (launchd). Use cron or systemd on Linux.");
|
package/dist/commands/status.js
CHANGED
|
@@ -2,9 +2,10 @@ import os from "node:os";
|
|
|
2
2
|
import chalk from "chalk";
|
|
3
3
|
import ora from "ora";
|
|
4
4
|
import { loadConfig, CONFIG_PATH } from "../config.js";
|
|
5
|
-
import { resolveFiles } from "../
|
|
5
|
+
import { resolveFiles } from "../resolveFiles.js";
|
|
6
6
|
import { compareFiles } from "../staging.js";
|
|
7
|
-
import { isScheduled } from "../
|
|
7
|
+
import { isScheduled } from "../launchd.js";
|
|
8
|
+
import { pluralize } from "../utils.js";
|
|
8
9
|
function tildePath(filePath) {
|
|
9
10
|
const home = os.homedir();
|
|
10
11
|
return filePath.startsWith(home) ? "~" + filePath.slice(home.length) : filePath;
|
|
@@ -12,7 +13,7 @@ function tildePath(filePath) {
|
|
|
12
13
|
export async function status() {
|
|
13
14
|
const scheduled = isScheduled();
|
|
14
15
|
console.log();
|
|
15
|
-
console.log(` Schedule: ${scheduled ? "active (daily at 02:00)" :
|
|
16
|
+
console.log(` Schedule: ${scheduled ? "active (daily at 02:00)" : `not active (run ${chalk.bold("backdot --schedule")} to enable)`}`);
|
|
16
17
|
const config = loadConfig();
|
|
17
18
|
console.log(` Repo: ${config.repository}`);
|
|
18
19
|
console.log(` Machine: ${config.machine}`);
|
|
@@ -27,14 +28,14 @@ export async function status() {
|
|
|
27
28
|
}
|
|
28
29
|
const files = [...userFiles, CONFIG_PATH];
|
|
29
30
|
spinner.text = "Comparing with remote backup";
|
|
30
|
-
const { backedUp, modified, notBackedUp, error } = await compareFiles(files, config.machine);
|
|
31
|
+
const { backedUp, modified, notBackedUp, error } = await compareFiles(files, config.machine, config.repository);
|
|
31
32
|
spinner.stop();
|
|
32
33
|
if (error) {
|
|
33
34
|
console.log(chalk.yellow(` Could not fetch status: ${error}`));
|
|
34
35
|
return;
|
|
35
36
|
}
|
|
36
37
|
if (modified.length === 0 && notBackedUp.length === 0) {
|
|
37
|
-
console.log(chalk.green(` All ${files.length
|
|
38
|
+
console.log(chalk.green(` All ${pluralize(files.length, "file")} are backed up ✓`));
|
|
38
39
|
}
|
|
39
40
|
else {
|
|
40
41
|
if (modified.length > 0) {
|
package/dist/commitUrl.js
CHANGED
|
@@ -6,8 +6,9 @@ const PROVIDERS = {
|
|
|
6
6
|
export function getCommitUrl(remoteUrl, sha) {
|
|
7
7
|
for (const [host, buildUrl] of Object.entries(PROVIDERS)) {
|
|
8
8
|
const idx = remoteUrl.indexOf(host);
|
|
9
|
-
if (idx === -1)
|
|
9
|
+
if (idx === -1) {
|
|
10
10
|
continue;
|
|
11
|
+
}
|
|
11
12
|
let repoPath = remoteUrl.slice(idx + host.length + 1).trim();
|
|
12
13
|
if (repoPath.endsWith(".git")) {
|
|
13
14
|
repoPath = repoPath.slice(0, -4);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function errorMessage(err: unknown): string;
|
package/dist/git.d.ts
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
import { type SimpleGit } from "simple-git";
|
|
2
|
+
interface FileChangeSummary {
|
|
3
|
+
created: string[];
|
|
4
|
+
deleted: string[];
|
|
5
|
+
modified: string[];
|
|
6
|
+
renamed: Array<{
|
|
7
|
+
from: string;
|
|
8
|
+
to: string;
|
|
9
|
+
}>;
|
|
10
|
+
}
|
|
11
|
+
export declare function buildCommitMessage(changes: FileChangeSummary, maxLen?: number): string;
|
|
12
|
+
export declare function ensureRemoteUrl(repository: string): Promise<void>;
|
|
13
|
+
export declare function getCurrentBranch(git: SimpleGit): Promise<string>;
|
|
14
|
+
export declare function friendlyGitError(raw: string, repository: string): string;
|
|
15
|
+
export declare function gitError(err: unknown, repository: string): Error;
|
|
16
|
+
export declare function gitPull(repository: string, commit?: string): Promise<void>;
|
|
17
|
+
export declare function gitLog(limit?: number): Promise<Array<{
|
|
18
|
+
hash: string;
|
|
19
|
+
date: string;
|
|
20
|
+
message: string;
|
|
21
|
+
}>>;
|
|
2
22
|
export declare function gitCommitAndPush(): Promise<{
|
|
3
23
|
commitUrl: string | null;
|
|
4
24
|
} | null>;
|
|
25
|
+
export {};
|
package/dist/git.js
CHANGED
|
@@ -3,29 +3,123 @@ import path from "node:path";
|
|
|
3
3
|
import pRetry from "p-retry";
|
|
4
4
|
import { simpleGit, CleanOptions } from "simple-git";
|
|
5
5
|
import { logger } from "./log.js";
|
|
6
|
-
import { STAGING_DIR } from "./
|
|
6
|
+
import { STAGING_DIR, STAGING_GIT_DIR } from "./paths.js";
|
|
7
7
|
import { getCommitUrl } from "./commitUrl.js";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
import { errorMessage, pluralize, uniq } from "./utils.js";
|
|
9
|
+
export function buildCommitMessage(changes, maxLen = 250) {
|
|
10
|
+
const unique = (paths) => uniq(paths.map((f) => path.basename(f)));
|
|
11
|
+
const removed = unique(changes.deleted);
|
|
12
|
+
const added = unique(changes.created);
|
|
13
|
+
const modified = unique([...changes.modified, ...changes.renamed.map((r) => r.to)]);
|
|
14
|
+
const categories = [
|
|
15
|
+
{ label: "removed", files: removed },
|
|
16
|
+
{ label: "added", files: added },
|
|
17
|
+
{ label: "modified", files: modified },
|
|
18
|
+
].filter((c) => c.files.length > 0);
|
|
19
|
+
if (categories.length === 0) {
|
|
20
|
+
return "backup"; // fallback commit message when no changes are categorized
|
|
21
|
+
}
|
|
22
|
+
function format(cat, listFiles) {
|
|
23
|
+
if (listFiles) {
|
|
24
|
+
return `${cat.label}: ${cat.files.join(", ")}`;
|
|
25
|
+
}
|
|
26
|
+
return `${cat.label}: ${pluralize(cat.files.length, "file")}`;
|
|
27
|
+
}
|
|
28
|
+
function build(listFlags) {
|
|
29
|
+
return categories.map((cat, i) => format(cat, listFlags[i])).join("; ");
|
|
30
|
+
}
|
|
31
|
+
const flags = categories.map(() => true);
|
|
32
|
+
let msg = build(flags);
|
|
33
|
+
if (msg.length <= maxLen) {
|
|
34
|
+
return msg;
|
|
35
|
+
}
|
|
36
|
+
for (const label of ["modified", "added", "removed"]) {
|
|
37
|
+
const idx = categories.findIndex((c) => c.label === label);
|
|
38
|
+
if (idx !== -1 && flags[idx]) {
|
|
39
|
+
flags[idx] = false;
|
|
40
|
+
msg = build(flags);
|
|
41
|
+
if (msg.length <= maxLen) {
|
|
42
|
+
return msg;
|
|
43
|
+
}
|
|
19
44
|
}
|
|
20
|
-
|
|
21
|
-
|
|
45
|
+
}
|
|
46
|
+
return msg.slice(0, maxLen - 3) + "...";
|
|
47
|
+
}
|
|
48
|
+
export async function ensureRemoteUrl(repository) {
|
|
49
|
+
const git = simpleGit(STAGING_DIR);
|
|
50
|
+
const currentUrl = (await git.remote(["get-url", "origin"]))?.trim();
|
|
51
|
+
if (currentUrl !== repository) {
|
|
52
|
+
await git.remote(["set-url", "origin", repository]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export async function getCurrentBranch(git) {
|
|
56
|
+
return (await git.revparse(["--abbrev-ref", "HEAD"])).trim();
|
|
57
|
+
}
|
|
58
|
+
export function friendlyGitError(raw, repository) {
|
|
59
|
+
const msg = raw.toLowerCase();
|
|
60
|
+
if (msg.includes("not found") ||
|
|
61
|
+
msg.includes("does not exist") ||
|
|
62
|
+
msg.includes("does not appear to be a git repository")) {
|
|
63
|
+
return `Repository "${repository}" not found. Check the URL and that you have access.`;
|
|
64
|
+
}
|
|
65
|
+
if (msg.includes("authentication failed") || msg.includes("could not read username")) {
|
|
66
|
+
return `Authentication failed for "${repository}". Check your credentials or SSH key.`;
|
|
67
|
+
}
|
|
68
|
+
if (msg.includes("could not resolve host") ||
|
|
69
|
+
msg.includes("connection refused") ||
|
|
70
|
+
msg.includes("connection timed out")) {
|
|
71
|
+
return "Could not connect to remote host. Check your internet connection.";
|
|
72
|
+
}
|
|
73
|
+
return raw;
|
|
74
|
+
}
|
|
75
|
+
export function gitError(err, repository) {
|
|
76
|
+
const raw = errorMessage(err);
|
|
77
|
+
return new Error(friendlyGitError(raw, repository), { cause: err });
|
|
78
|
+
}
|
|
79
|
+
export async function gitPull(repository, commit) {
|
|
80
|
+
try {
|
|
81
|
+
if (fs.existsSync(STAGING_GIT_DIR)) {
|
|
22
82
|
const git = simpleGit(STAGING_DIR);
|
|
23
|
-
await
|
|
24
|
-
await git.
|
|
83
|
+
await ensureRemoteUrl(repository);
|
|
84
|
+
await git.fetch("origin");
|
|
85
|
+
const target = commit ?? `origin/${await getCurrentBranch(git)}`;
|
|
86
|
+
await git.reset(["--hard", target]);
|
|
87
|
+
await git.clean(CleanOptions.FORCE, ["-d"]);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
try {
|
|
91
|
+
await simpleGit().clone(repository, STAGING_DIR);
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
if (!errorMessage(err).includes("empty repository")) {
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
fs.mkdirSync(STAGING_DIR, { recursive: true });
|
|
98
|
+
const git = simpleGit(STAGING_DIR);
|
|
99
|
+
await git.init();
|
|
100
|
+
await git.addRemote("origin", repository);
|
|
101
|
+
}
|
|
102
|
+
if (commit) {
|
|
103
|
+
const git = simpleGit(STAGING_DIR);
|
|
104
|
+
await git.reset(["--hard", commit]);
|
|
105
|
+
await git.clean(CleanOptions.FORCE, ["-d"]);
|
|
106
|
+
}
|
|
25
107
|
}
|
|
26
108
|
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
throw gitError(err, repository);
|
|
111
|
+
}
|
|
27
112
|
logger.info("Synced staging directory from remote");
|
|
28
113
|
}
|
|
114
|
+
export async function gitLog(limit = 20) {
|
|
115
|
+
const git = simpleGit(STAGING_DIR);
|
|
116
|
+
const log = await git.log({ maxCount: limit });
|
|
117
|
+
return log.all.map((entry) => ({
|
|
118
|
+
hash: entry.hash,
|
|
119
|
+
date: entry.date,
|
|
120
|
+
message: entry.message,
|
|
121
|
+
}));
|
|
122
|
+
}
|
|
29
123
|
export async function gitCommitAndPush() {
|
|
30
124
|
const git = simpleGit(STAGING_DIR);
|
|
31
125
|
await git.add(".");
|
|
@@ -34,24 +128,29 @@ export async function gitCommitAndPush() {
|
|
|
34
128
|
logger.info("No changes to commit");
|
|
35
129
|
return null;
|
|
36
130
|
}
|
|
37
|
-
const
|
|
38
|
-
const message = `Automated backup: ${date}`;
|
|
131
|
+
const message = buildCommitMessage(status);
|
|
39
132
|
await git.commit(message);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
133
|
+
try {
|
|
134
|
+
await pRetry(async () => git.push(["-u", "origin", "HEAD"]), {
|
|
135
|
+
retries: 5,
|
|
136
|
+
onFailedAttempt: async ({ attemptNumber, retriesLeft }) => {
|
|
137
|
+
logger.info(`Push failed (attempt ${attemptNumber}, ${retriesLeft} retries left), rebasing`);
|
|
138
|
+
await git.fetch("origin");
|
|
139
|
+
const branch = await getCurrentBranch(git);
|
|
140
|
+
try {
|
|
141
|
+
await git.rebase([`origin/${branch}`]);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
await git.rebase(["--abort"]);
|
|
145
|
+
throw new Error("Rebase conflict, aborting retry");
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
const remoteUrl = ((await git.remote(["get-url", "origin"])) ?? "").trim();
|
|
152
|
+
throw gitError(err, remoteUrl);
|
|
153
|
+
}
|
|
55
154
|
logger.info(`Committed and pushed: ${message}`);
|
|
56
155
|
const sha = (await git.revparse(["HEAD"])).trim();
|
|
57
156
|
const remoteUrl = (await git.remote(["get-url", "origin"])) ?? "";
|
package/dist/launchd.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import ora from "ora";
|
|
6
|
+
import { logger } from "./log.js";
|
|
7
|
+
import { errorMessage } from "./utils.js";
|
|
8
|
+
function escapeXml(s) {
|
|
9
|
+
return s
|
|
10
|
+
.replace(/&/g, "&")
|
|
11
|
+
.replace(/</g, "<")
|
|
12
|
+
.replace(/>/g, ">")
|
|
13
|
+
.replace(/"/g, """);
|
|
14
|
+
}
|
|
15
|
+
const LABEL = "com.backdot.daemon";
|
|
16
|
+
const PLIST_PATH = path.join(os.homedir(), "Library", "LaunchAgents", `${LABEL}.plist`);
|
|
17
|
+
function getScriptPath() {
|
|
18
|
+
const currentDir = path.dirname(new URL(import.meta.url).pathname);
|
|
19
|
+
return path.resolve(currentDir, "cli.js");
|
|
20
|
+
}
|
|
21
|
+
function buildPlist() {
|
|
22
|
+
const nodePath = process.execPath;
|
|
23
|
+
const scriptPath = getScriptPath();
|
|
24
|
+
const workingDir = path.dirname(scriptPath);
|
|
25
|
+
const logPath = path.join(os.homedir(), ".backdot", "launchd.log");
|
|
26
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
27
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
28
|
+
<plist version="1.0">
|
|
29
|
+
<dict>
|
|
30
|
+
<key>Label</key>
|
|
31
|
+
<string>${LABEL}</string>
|
|
32
|
+
<key>ProgramArguments</key>
|
|
33
|
+
<array>
|
|
34
|
+
<string>${escapeXml(nodePath)}</string>
|
|
35
|
+
<string>${escapeXml(scriptPath)}</string>
|
|
36
|
+
<string>--backup</string>
|
|
37
|
+
</array>
|
|
38
|
+
<key>WorkingDirectory</key>
|
|
39
|
+
<string>${escapeXml(workingDir)}</string>
|
|
40
|
+
<key>StartCalendarInterval</key>
|
|
41
|
+
<dict>
|
|
42
|
+
<key>Hour</key>
|
|
43
|
+
<integer>2</integer>
|
|
44
|
+
<key>Minute</key>
|
|
45
|
+
<integer>0</integer>
|
|
46
|
+
</dict>
|
|
47
|
+
<key>StandardOutPath</key>
|
|
48
|
+
<string>${escapeXml(logPath)}</string>
|
|
49
|
+
<key>StandardErrorPath</key>
|
|
50
|
+
<string>${escapeXml(logPath)}</string>
|
|
51
|
+
<key>RunAtLoad</key>
|
|
52
|
+
<false/>
|
|
53
|
+
</dict>
|
|
54
|
+
</plist>`;
|
|
55
|
+
}
|
|
56
|
+
export function isScheduled() {
|
|
57
|
+
if (!fs.existsSync(PLIST_PATH)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const output = execFileSync("launchctl", ["list", LABEL], { encoding: "utf-8", stdio: "pipe" });
|
|
62
|
+
return output.includes(LABEL);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export function setupLaunchd() {
|
|
69
|
+
const spinner = ora("Installing schedule").start();
|
|
70
|
+
const plistContent = buildPlist();
|
|
71
|
+
const dir = path.dirname(PLIST_PATH);
|
|
72
|
+
if (!fs.existsSync(dir)) {
|
|
73
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
execFileSync("launchctl", ["unload", PLIST_PATH], { stdio: "pipe" });
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// Not loaded, that's fine
|
|
80
|
+
}
|
|
81
|
+
fs.writeFileSync(PLIST_PATH, plistContent);
|
|
82
|
+
logger.info(`Plist written to ${PLIST_PATH}`);
|
|
83
|
+
try {
|
|
84
|
+
execFileSync("launchctl", ["load", PLIST_PATH], { stdio: "pipe" });
|
|
85
|
+
spinner.succeed("Daily backup scheduled (02:00)");
|
|
86
|
+
console.log();
|
|
87
|
+
logger.info("Launchd job loaded");
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
const msg = errorMessage(err);
|
|
91
|
+
spinner.fail(`Failed to load launchd job: ${msg}`);
|
|
92
|
+
console.log();
|
|
93
|
+
logger.error(`Failed to load launchd job: ${msg}`);
|
|
94
|
+
throw new Error(`Failed to load launchd job: ${msg}`, { cause: err });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export function uninstallLaunchd() {
|
|
98
|
+
const spinner = ora("Removing schedule").start();
|
|
99
|
+
try {
|
|
100
|
+
execFileSync("launchctl", ["unload", PLIST_PATH], { stdio: "pipe" });
|
|
101
|
+
logger.info("Launchd job unloaded");
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
logger.info("Launchd job was not loaded");
|
|
105
|
+
}
|
|
106
|
+
if (fs.existsSync(PLIST_PATH)) {
|
|
107
|
+
fs.unlinkSync(PLIST_PATH);
|
|
108
|
+
logger.info(`Plist removed: ${PLIST_PATH}`);
|
|
109
|
+
}
|
|
110
|
+
spinner.succeed("Schedule removed");
|
|
111
|
+
console.log();
|
|
112
|
+
}
|
package/dist/log.d.ts
CHANGED
package/dist/log.js
CHANGED
|
@@ -3,10 +3,21 @@ import path from "node:path";
|
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import winston from "winston";
|
|
5
5
|
const LOG_DIR = path.join(os.homedir(), ".backdot");
|
|
6
|
-
fs.mkdirSync(LOG_DIR, { recursive: true });
|
|
7
6
|
const LOG_FILE = path.join(LOG_DIR, "backup.log");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
let _logger;
|
|
8
|
+
function getLogger() {
|
|
9
|
+
if (!_logger) {
|
|
10
|
+
fs.mkdirSync(LOG_DIR, { recursive: true });
|
|
11
|
+
_logger = winston.createLogger({
|
|
12
|
+
level: "info",
|
|
13
|
+
format: winston.format.combine(winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), winston.format.printf(({ timestamp, level, message }) => `${timestamp} [${level}] ${message}`)),
|
|
14
|
+
transports: [new winston.transports.File({ filename: LOG_FILE })],
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return _logger;
|
|
18
|
+
}
|
|
19
|
+
export const logger = {
|
|
20
|
+
info: (message) => getLogger().info(message),
|
|
21
|
+
warn: (message) => getLogger().warn(message),
|
|
22
|
+
error: (message) => getLogger().error(message),
|
|
23
|
+
};
|
package/dist/notify.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
2
|
import { logger } from "./log.js";
|
|
3
|
+
import { errorMessage } from "./utils.js";
|
|
3
4
|
function escapeAppleScript(str) {
|
|
4
5
|
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
5
6
|
}
|
|
6
7
|
export function sendNotification(title, message) {
|
|
7
|
-
if (process.platform !== "darwin")
|
|
8
|
+
if (process.platform !== "darwin") {
|
|
8
9
|
return;
|
|
10
|
+
}
|
|
9
11
|
const escaped = escapeAppleScript(message);
|
|
10
12
|
const titleEscaped = escapeAppleScript(title);
|
|
11
13
|
try {
|
|
@@ -15,7 +17,6 @@ export function sendNotification(title, message) {
|
|
|
15
17
|
], { stdio: "pipe" });
|
|
16
18
|
}
|
|
17
19
|
catch (err) {
|
|
18
|
-
|
|
19
|
-
logger.warn(`Failed to send notification: ${msg}`);
|
|
20
|
+
logger.warn(`Failed to send notification: ${errorMessage(err)}`);
|
|
20
21
|
}
|
|
21
22
|
}
|
package/dist/paths.d.ts
ADDED
package/dist/paths.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
const HOME = os.homedir();
|
|
4
|
+
export const STAGING_DIR = path.join(HOME, ".backdot", "repo");
|
|
5
|
+
export const STAGING_GIT_DIR = path.join(STAGING_DIR, ".git");
|
|
6
|
+
export function machineDir(machine) {
|
|
7
|
+
return path.join(STAGING_DIR, machine);
|
|
8
|
+
}
|
package/dist/plist.js
CHANGED
|
@@ -4,6 +4,14 @@ import path from "node:path";
|
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import ora from "ora";
|
|
6
6
|
import { logger } from "./log.js";
|
|
7
|
+
import { errorMessage } from "./utils.js";
|
|
8
|
+
function escapeXml(s) {
|
|
9
|
+
return s
|
|
10
|
+
.replace(/&/g, "&")
|
|
11
|
+
.replace(/</g, "<")
|
|
12
|
+
.replace(/>/g, ">")
|
|
13
|
+
.replace(/"/g, """);
|
|
14
|
+
}
|
|
7
15
|
const LABEL = "com.backdot.daemon";
|
|
8
16
|
const PLIST_PATH = path.join(os.homedir(), "Library", "LaunchAgents", `${LABEL}.plist`);
|
|
9
17
|
function getScriptPath() {
|
|
@@ -23,12 +31,12 @@ function buildPlist() {
|
|
|
23
31
|
<string>${LABEL}</string>
|
|
24
32
|
<key>ProgramArguments</key>
|
|
25
33
|
<array>
|
|
26
|
-
<string>${nodePath}</string>
|
|
27
|
-
<string>${scriptPath}</string>
|
|
34
|
+
<string>${escapeXml(nodePath)}</string>
|
|
35
|
+
<string>${escapeXml(scriptPath)}</string>
|
|
28
36
|
<string>--backup</string>
|
|
29
37
|
</array>
|
|
30
38
|
<key>WorkingDirectory</key>
|
|
31
|
-
<string>${workingDir}</string>
|
|
39
|
+
<string>${escapeXml(workingDir)}</string>
|
|
32
40
|
<key>StartCalendarInterval</key>
|
|
33
41
|
<dict>
|
|
34
42
|
<key>Hour</key>
|
|
@@ -37,9 +45,9 @@ function buildPlist() {
|
|
|
37
45
|
<integer>0</integer>
|
|
38
46
|
</dict>
|
|
39
47
|
<key>StandardOutPath</key>
|
|
40
|
-
<string>${logPath}</string>
|
|
48
|
+
<string>${escapeXml(logPath)}</string>
|
|
41
49
|
<key>StandardErrorPath</key>
|
|
42
|
-
<string>${logPath}</string>
|
|
50
|
+
<string>${escapeXml(logPath)}</string>
|
|
43
51
|
<key>RunAtLoad</key>
|
|
44
52
|
<false/>
|
|
45
53
|
</dict>
|
|
@@ -79,7 +87,7 @@ export function setupLaunchd() {
|
|
|
79
87
|
logger.info("Launchd job loaded");
|
|
80
88
|
}
|
|
81
89
|
catch (err) {
|
|
82
|
-
const msg =
|
|
90
|
+
const msg = errorMessage(err);
|
|
83
91
|
spinner.fail(`Failed to load launchd job: ${msg}`);
|
|
84
92
|
console.log();
|
|
85
93
|
logger.error(`Failed to load launchd job: ${msg}`);
|
package/dist/resolve.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import fg from "fast-glob";
|
|
3
3
|
import { logger } from "./log.js";
|
|
4
|
+
import { errorMessage, uniq } from "./utils.js";
|
|
4
5
|
function resolveGlobs(patterns) {
|
|
5
|
-
if (patterns.length === 0)
|
|
6
|
+
if (patterns.length === 0) {
|
|
6
7
|
return [];
|
|
8
|
+
}
|
|
7
9
|
try {
|
|
8
10
|
return fg.sync(patterns, { absolute: true, dot: true, onlyFiles: true });
|
|
9
11
|
}
|
|
10
|
-
catch {
|
|
11
|
-
logger.warn(
|
|
12
|
+
catch (err) {
|
|
13
|
+
logger.warn(`Glob pattern resolution failed: ${errorMessage(err)}`);
|
|
12
14
|
return [];
|
|
13
15
|
}
|
|
14
16
|
}
|
|
@@ -18,7 +20,7 @@ const LARGE_FILE_THRESHOLD = 10 * 1024 * 1024; // 10 MB
|
|
|
18
20
|
* Skips entries that fail resolution and logs warnings.
|
|
19
21
|
*/
|
|
20
22
|
export function resolveFiles(config) {
|
|
21
|
-
const unique =
|
|
23
|
+
const unique = uniq(resolveGlobs(config.paths));
|
|
22
24
|
return unique.filter((filePath) => {
|
|
23
25
|
try {
|
|
24
26
|
fs.accessSync(filePath, fs.constants.R_OK);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import fg from "fast-glob";
|
|
3
|
+
import { logger } from "./log.js";
|
|
4
|
+
import { errorMessage, uniq } from "./utils.js";
|
|
5
|
+
function resolveGlobs(patterns) {
|
|
6
|
+
if (patterns.length === 0) {
|
|
7
|
+
return [];
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
return fg.sync(patterns, { absolute: true, dot: true, onlyFiles: true });
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
logger.warn(`Glob pattern resolution failed: ${errorMessage(err)}`);
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const LARGE_FILE_THRESHOLD = 10 * 1024 * 1024; // 10 MB
|
|
18
|
+
/**
|
|
19
|
+
* Resolve all file entries to absolute paths.
|
|
20
|
+
* Skips entries that fail resolution and logs warnings.
|
|
21
|
+
*/
|
|
22
|
+
export function resolveFiles(config) {
|
|
23
|
+
const unique = uniq(resolveGlobs(config.paths));
|
|
24
|
+
return unique.filter((filePath) => {
|
|
25
|
+
try {
|
|
26
|
+
fs.accessSync(filePath, fs.constants.R_OK);
|
|
27
|
+
const stat = fs.statSync(filePath);
|
|
28
|
+
if (!stat.isFile()) {
|
|
29
|
+
logger.warn(`Not a regular file, skipping: ${filePath}`);
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (stat.size > LARGE_FILE_THRESHOLD) {
|
|
33
|
+
const sizeMB = (stat.size / 1024 / 1024).toFixed(1);
|
|
34
|
+
logger.warn(`Large file (${sizeMB} MB), skipping: ${filePath}`);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
logger.warn(`File not readable, skipping: ${filePath}`);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
package/dist/staging.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
import { STAGING_DIR, STAGING_GIT_DIR, machineDir } from "./paths.js";
|
|
2
|
+
export { STAGING_DIR, STAGING_GIT_DIR, machineDir };
|
|
3
3
|
export declare function getStagedPath(filePath: string, machine: string): string;
|
|
4
4
|
export declare function cleanStaging(machine: string): void;
|
|
5
5
|
export declare function copyToStaging(files: string[], machine: string): void;
|
|
@@ -9,5 +9,5 @@ export interface ComparisonResult {
|
|
|
9
9
|
notBackedUp: string[];
|
|
10
10
|
error?: string;
|
|
11
11
|
}
|
|
12
|
-
export declare function compareFiles(files: string[], machine: string): Promise<ComparisonResult>;
|
|
12
|
+
export declare function compareFiles(files: string[], machine: string, repository: string): Promise<ComparisonResult>;
|
|
13
13
|
export declare function writeRepoReadme(repository: string): void;
|
package/dist/staging.js
CHANGED
|
@@ -4,11 +4,11 @@ import os from "node:os";
|
|
|
4
4
|
import { execFileSync } from "node:child_process";
|
|
5
5
|
import { simpleGit } from "simple-git";
|
|
6
6
|
import { logger } from "./log.js";
|
|
7
|
+
import { errorMessage, pluralize } from "./utils.js";
|
|
8
|
+
import { ensureRemoteUrl, getCurrentBranch, gitError } from "./git.js";
|
|
9
|
+
import { STAGING_DIR, STAGING_GIT_DIR, machineDir } from "./paths.js";
|
|
10
|
+
export { STAGING_DIR, STAGING_GIT_DIR, machineDir };
|
|
7
11
|
const HOME = os.homedir();
|
|
8
|
-
export const STAGING_DIR = path.join(HOME, ".backdot", "repo");
|
|
9
|
-
export function machineDir(machine) {
|
|
10
|
-
return path.join(STAGING_DIR, machine);
|
|
11
|
-
}
|
|
12
12
|
export function getStagedPath(filePath, machine) {
|
|
13
13
|
const rel = path.relative(HOME, filePath);
|
|
14
14
|
const destRel = rel.startsWith("..") ? filePath.slice(1) : rel;
|
|
@@ -16,8 +16,9 @@ export function getStagedPath(filePath, machine) {
|
|
|
16
16
|
}
|
|
17
17
|
export function cleanStaging(machine) {
|
|
18
18
|
const dir = machineDir(machine);
|
|
19
|
-
if (!fs.existsSync(dir))
|
|
19
|
+
if (!fs.existsSync(dir)) {
|
|
20
20
|
return;
|
|
21
|
+
}
|
|
21
22
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
22
23
|
logger.info(`Cleaned staging directory for machine "${machine}"`);
|
|
23
24
|
}
|
|
@@ -36,29 +37,29 @@ export function copyToStaging(files, machine) {
|
|
|
36
37
|
logger.warn(`Failed to copy: ${filePath} -> ${dest}`);
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
|
-
logger.info(`Copied ${copied
|
|
40
|
+
logger.info(`Copied ${pluralize(copied, "file")} to staging`);
|
|
40
41
|
}
|
|
41
42
|
function failedComparisonResult(err) {
|
|
42
|
-
|
|
43
|
-
return { backedUp: [], modified: [], notBackedUp: [], error: errorMessage };
|
|
43
|
+
return { backedUp: [], modified: [], notBackedUp: [], error: errorMessage(err) };
|
|
44
44
|
}
|
|
45
|
-
export async function compareFiles(files, machine) {
|
|
46
|
-
if (files.length === 0)
|
|
45
|
+
export async function compareFiles(files, machine, repository) {
|
|
46
|
+
if (files.length === 0) {
|
|
47
47
|
return { backedUp: [], modified: [], notBackedUp: [] };
|
|
48
|
-
|
|
49
|
-
if (!fs.existsSync(
|
|
48
|
+
}
|
|
49
|
+
if (!fs.existsSync(STAGING_GIT_DIR)) {
|
|
50
50
|
return failedComparisonResult(new Error("Backup repository not found. Run backdot --backup first."));
|
|
51
51
|
}
|
|
52
52
|
const git = simpleGit(STAGING_DIR);
|
|
53
53
|
try {
|
|
54
|
+
await ensureRemoteUrl(repository);
|
|
54
55
|
await git.fetch("origin");
|
|
55
56
|
}
|
|
56
57
|
catch (err) {
|
|
57
|
-
return failedComparisonResult(err);
|
|
58
|
+
return failedComparisonResult(gitError(err, repository));
|
|
58
59
|
}
|
|
59
60
|
let branch;
|
|
60
61
|
try {
|
|
61
|
-
branch =
|
|
62
|
+
branch = await getCurrentBranch(git);
|
|
62
63
|
}
|
|
63
64
|
catch (err) {
|
|
64
65
|
return failedComparisonResult(err);
|
|
@@ -80,8 +81,9 @@ export async function compareFiles(files, machine) {
|
|
|
80
81
|
}
|
|
81
82
|
let sourceHashes;
|
|
82
83
|
try {
|
|
83
|
-
const hashOutput = execFileSync("git", ["hash-object", "--"
|
|
84
|
+
const hashOutput = execFileSync("git", ["hash-object", "--stdin-paths"], {
|
|
84
85
|
encoding: "utf-8",
|
|
86
|
+
input: files.join("\n") + "\n",
|
|
85
87
|
});
|
|
86
88
|
sourceHashes = hashOutput.trim().split("\n");
|
|
87
89
|
}
|
|
@@ -106,7 +108,7 @@ export async function compareFiles(files, machine) {
|
|
|
106
108
|
function repoReadme(repository) {
|
|
107
109
|
return `# Backdot Backup
|
|
108
110
|
|
|
109
|
-
This repository contains
|
|
111
|
+
This repository contains files backed up automatically using [backdot](https://github.com/sorenlouv/backdot).
|
|
110
112
|
|
|
111
113
|
## Restore
|
|
112
114
|
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function errorMessage(err) {
|
|
2
|
+
return err instanceof Error ? err.message : String(err);
|
|
3
|
+
}
|
|
4
|
+
export function uniq(items) {
|
|
5
|
+
return [...new Set(items)];
|
|
6
|
+
}
|
|
7
|
+
export function pluralize(count, word) {
|
|
8
|
+
return `${count} ${word}${count !== 1 ? "s" : ""}`;
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "backdot",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "Lightweight CLI to backup dotfiles and gitignored files to a Git repo on a daily schedule",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,8 +32,10 @@
|
|
|
32
32
|
"lint:fix": "eslint src/ --fix",
|
|
33
33
|
"format": "prettier --write src/",
|
|
34
34
|
"format:check": "prettier --check src/",
|
|
35
|
-
"test": "vitest run --exclude src/e2e.test.ts",
|
|
35
|
+
"test": "vitest run --exclude src/e2e.test.ts --exclude src/git.integration.test.ts",
|
|
36
|
+
"test:integration": "vitest run src/git.integration.test.ts",
|
|
36
37
|
"test:e2e": "npm run build && vitest run src/e2e.test.ts",
|
|
38
|
+
"test:all": "npm run build && vitest run",
|
|
37
39
|
"test:watch": "vitest",
|
|
38
40
|
"prepare": "husky"
|
|
39
41
|
},
|