Skip to content

Commit bee8935

Browse files
committed
Restore backup download
- Fix git fs promises usage
1 parent a6fefc1 commit bee8935

File tree

9 files changed

+61
-14
lines changed

9 files changed

+61
-14
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Fix regressions, lost features:
1919
- Likes
2020
- Preview while running
2121
- Glacier Backup
22-
- Github Backup Downloads
2322
- Destructive Rotation
2423
- Misc commands: Cancel?!
2524
- Local FFMpeg when Github disabled

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"watch": "tsx watch --env-file=.env src/main.ts",
1414
"test": "tsx --env-file=.env src/test.ts",
1515
"test-sunmoon": "tsx --env-file=.env src/services/SunMoonTime.ts",
16-
"test-df": "tsx watch src/lib/df.ts"
16+
"test-df": "tsx watch src/lib/df.ts",
17+
"download-backup": "tsx --env-file=.env src/downloadBackups.ts"
1718
},
1819
"repository": {
1920
"type": "git",

src/downloadBackups.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Injector } from '@joist/di';
2+
import { Logger, consoleInjector, Repository, FileSystem } from './services/index.js';
3+
4+
// defineEnvironment
5+
const injector = new Injector([], consoleInjector);
6+
const logger = injector.get(Logger);
7+
const fs = injector.get(FileSystem);
8+
const repo = injector.get(Repository);
9+
10+
const DAY_MS = 24 * 60 * 60 * 1000;
11+
12+
async function main() {
13+
// TODO? restore await InventoryStorage.create(`${process.env.CONTENT_STORAGE_NAME_PREFIX}`);
14+
for (let d = 7; d > 0; d--) {
15+
const yesterday = new Date(Date.now() - DAY_MS * d).toISOString().slice(0, 10);
16+
const repoName = `${process.env.CONTENT_STORAGE_NAME_PREFIX}-${yesterday}`;
17+
try {
18+
await repo.checkout(repoName);
19+
await fs.setupPath(repoName);
20+
logger.log('Storage has', (await fs.list()).length - 1, 'files in', repoName);
21+
} catch (error: any) {
22+
logger.log('Failed to checkout repo:', repoName, error?.message);
23+
}
24+
}
25+
}
26+
27+
main();

src/services/Director.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ export class Director {
7474
return this.#fs().setupPath(name);
7575
}
7676

77+
async switchRepo(name: string, checkout = true) {
78+
if (checkout) await this.#repo().checkout(name);
79+
return this.#switchPath(name);
80+
}
81+
7782
async photo(presetName: string) {
7883
const logger = this.#logger();
7984
const camera = this.#camera();

src/services/FileSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class FileSystem {
8686

8787
async list() {
8888
const list = await readdir(this.path);
89-
this.#logger().log('[Storage] List:\n °', list.join('\n ° '));
89+
// this.#logger().log('[Storage] List:\n °', list.join('\n ° '));
9090
return list;
9191
}
9292

src/services/Git.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import axios from 'axios';
22
import sodium from 'libsodium-wrappers';
33
import git from 'isomorphic-git';
44
import http from 'isomorphic-git/http/node/index.js';
5-
import fs from 'fs/promises';
5+
import { promises } from 'fs';
66
import { Octokit } from '@octokit/rest';
77
import { inject, injectable } from '@joist/di';
88
import { FileSystem } from './FileSystem.js';
@@ -61,22 +61,23 @@ export class Git {
6161

6262
/**
6363
* Attempt `git clone` including checkout
64+
* @returns {Promise<boolean>} success
6465
*/
65-
async checkout(repo: string) {
66+
async checkout(repo: string, branch = this.ref) {
6667
const { log } = this.#logger();
6768
const dir = this.#fs().getAbsolutePath(repo);
6869
try {
6970
log('Checking out:', this.publicUrlPrefix + repo);
7071
await this.git.clone({
71-
fs,
72+
fs: { promises },
7273
dir,
7374
http,
7475
url: this.privateUrlPrefix + repo,
7576
singleBranch: true,
7677
depth: 1,
7778
});
7879
log('Successfully cloned:', dir);
79-
await this.branch(this.ref);
80+
if (branch) await this.branch(this.ref);
8081
return true;
8182
} catch (err: any) {
8283
log('Checkout failed:', dir, err?.message);
@@ -93,23 +94,24 @@ export class Git {
9394
*/
9495
async log() {
9596
const dir = this.#fs().absolutePath;
96-
return this.git.log({ fs, dir });
97+
return this.git.log({ fs: { promises }, dir });
9798
}
99+
98100
async status(filepath: string) {
99101
const dir = this.#fs().absolutePath;
100-
return this.git.status({ fs, dir, filepath });
102+
return this.git.status({ fs: { promises }, dir, filepath });
101103
}
102104

103105
async add(fileName: string) {
104106
const dir = this.#fs().absolutePath;
105-
await this.git.add({ fs, dir, filepath: fileName });
107+
await this.git.add({ fs: { promises }, dir, filepath: fileName });
106108
}
107109

108110
async branch(name: string) {
109111
const logger = this.#logger();
110112
const dir = this.#fs().absolutePath;
111113
try {
112-
await this.git.branch({ fs, dir, ref: name, checkout: true });
114+
await this.git.branch({ fs: { promises }, dir, ref: name, checkout: true });
113115
} catch (e: any) {
114116
if (e?.code === 'AlreadyExistsError') {
115117
logger.log(`Branch "${name}" already exists`);
@@ -123,7 +125,7 @@ export class Git {
123125

124126
async commit(message: string) {
125127
const dir = this.#fs().absolutePath;
126-
return this.git.commit({ fs, dir, message, author: this.author });
128+
return this.git.commit({ fs: { promises }, dir, message, author: this.author });
127129
}
128130

129131
/**
@@ -132,7 +134,7 @@ export class Git {
132134
async push() {
133135
const dir = this.#fs().absolutePath;
134136
return this.git.push({
135-
fs,
137+
fs: { promises },
136138
dir,
137139
http,
138140
// ref: this.ref,

src/services/Producer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export class Producer {
1313
#director = inject(Director);
1414
#hd = inject(Hardware);
1515

16+
async timelapse(chat: MessengerChat) {
17+
const director = this.#director();
18+
await director.setupPublicRepo(director.repoTimelapse);
19+
await director.timelapse('default', { count: 100, intervalMS: 2000 });
20+
}
21+
1622
scheduleDailySunset(chat: MessengerChat) {
1723
const director = this.#director();
1824
director.scheduleSunset(

src/services/Repository.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export class Repository {
3636
await fs.setupPath(repo);
3737
}
3838

39+
/**
40+
* Download an existing repository
41+
*/
42+
async checkout(repo: string) {
43+
return this.#git().checkout(repo);
44+
}
45+
3946
async upload(file: string) {
4047
const git = this.#git();
4148
await git.upload(file);

sunset-backup.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@echo off
22
echo Run Sunset Backup now...
33
echo =================================================================================================================
4-
cmd /c ts-node -r dotenv/config %USERPROFILE%/Projects/philo/src/lib/storage/downloadGithubRepos.ts
4+
cmd /c node --env-file=.env %USERPROFILE%/Projects/philo/dist/downloadBackups.js
55
echo =================================================================================================================
66
echo Done. Press any key to close!
77
pause>nul

0 commit comments

Comments
 (0)