Skip to content

Commit

Permalink
update paths and backup api
Browse files Browse the repository at this point in the history
- see changelog for detailed explanation
  • Loading branch information
astudentinearth committed Nov 22, 2024
1 parent f3dc9a2 commit e24df95
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 40 deletions.
15 changes: 15 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# 0.2.0-alpha.1

## ⚠️ Breaking changes

- Directories in which application data is stored has changed. App data will be stored in a subdirectory named `darkwrite-data` when running production builds, and `darkwrite-data-nightly` when running development builds. If you are upgrading from 0.1.0-alpha.x, navigate to Darkwrite's data directory (you can use the application menu for this), move `data.db`, `settings.json` and `notes/` into `darkwrite-data/`.
- Alternatively, create a backup before upgrading and restore from the backup after upgrading.

## ✨ Improvements and fixes

- New backups now include a date in their file names by default.

## 🛠️ Technical changes

- App data is now stored in an isolated folder, which opens the door to safe development sessions without risking data and simplifies the code responsible from backups.

# 0.1.0-alpha.2

## Features
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "darkwrite",
"version": "0.1.0-alpha.1",
"version": "0.2.0-alpha.1",
"description": "",
"scripts": {
"test:all": "yarn workspaces foreach -A run test",
Expand Down
2 changes: 1 addition & 1 deletion packages/app-desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "The eye candy note-taking app for all desktops.",
"productName": "Darkwrite",
"private": true,
"version": "0.1.0-alpha.2",
"version": "0.2.0-alpha.1",
"type": "module",
"license": "AGPL-3.0-or-later",
"main": "dist-electron/main.js",
Expand Down
81 changes: 46 additions & 35 deletions packages/app-desktop/src/electron/api/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import log from "electron-log";
import extract from "extract-zip";
import fse from "fs-extra";
import { join } from "node:path";
import os from "os";
import { zip } from "zip-a-folder";
import { DB } from "../db";
import { rmIfExists } from "../lib/fs";
import {
BACKUP_CACHE_DIR,
DATA_DIR,
DB_PATH,
DATA_SNAPSHOT_DIR,
EXPORTER_CACHE_DIR,
NOTE_CONTENTS_DIR,
RESTORE_CACHE_DIR,
SETTINGS_PATH,
} from "../lib/paths";
import { saveFile } from "./dialog";

Expand Down Expand Up @@ -70,17 +69,18 @@ export const BackupAPI = {
await fse.ensureDir(BACKUP_CACHE_DIR);

// copy user data
await fse.copy(DB_PATH, join(BACKUP_CACHE_DIR, "data.db"));
await fse.copy(NOTE_CONTENTS_DIR, join(BACKUP_CACHE_DIR, "notes/"));
await fse.copy(
SETTINGS_PATH,
join(BACKUP_CACHE_DIR, "settings.json"),
);
await fse.copy(DATA_DIR, BACKUP_CACHE_DIR);
// await fse.copy(DB_PATH, join(BACKUP_CACHE_DIR, "data.db"));
// await fse.copy(NOTE_CONTENTS_DIR, join(BACKUP_CACHE_DIR, "notes/"));
// await fse.copy(
// SETTINGS_PATH,
// join(BACKUP_CACHE_DIR, "settings.json"),
// );

const saveResult = await saveFile({
buttonLabel: "Export",
title: "Backup your data",
defaultPath: "darkwrite-backup.zip",
defaultPath: `darkwrite-backup-${new Date().toDateString()}.zip`,
});
if (saveResult.canceled || !saveResult.path) return;
await zip(BACKUP_CACHE_DIR, saveResult.path);
Expand All @@ -90,9 +90,9 @@ export const BackupAPI = {
}
},
async restore(archivePath: string) {
const dbBackupPath = join(DATA_DIR, "data.db.old");
const notesBackupPath = join(DATA_DIR, "notes_old");
const settingsBackupPath = join(DATA_DIR, "settings.json.old");
// const dbBackupPath = join(DATA_DIR, "data.db.old");
// const notesBackupPath = join(DATA_DIR, "notes_old");
// const settingsBackupPath = join(DATA_DIR, "settings.json.old");
let didRename = false;
await DB.disconnect();
try {
Expand All @@ -107,43 +107,54 @@ export const BackupAPI = {
"This does not seem to be a Darkwrite backup archive.",
);

// before we do anything else, we will rename the old files so we can rollback if something goes wrong.
// before we do anything else, we will rename the old directory so we can rollback if something goes wrong.
try {
await fse.rename(DB_PATH, dbBackupPath);
await fse.rename(NOTE_CONTENTS_DIR, notesBackupPath);
await fse.rename(SETTINGS_PATH, settingsBackupPath);
await fse.move(DATA_DIR, DATA_SNAPSHOT_DIR, {
overwrite: true,
});
// await fse.rename(DB_PATH, dbBackupPath);
// await fse.rename(NOTE_CONTENTS_DIR, notesBackupPath);
// await fse.rename(SETTINGS_PATH, settingsBackupPath);
} catch (error) {
/*empty */
/*empty*/
}
didRename = true;

await fse.copy(join(RESTORE_CACHE_DIR, "data.db"), DB_PATH, {
overwrite: true,
});
await fse.copy(
join(RESTORE_CACHE_DIR, "settings.json"),
SETTINGS_PATH,
{ overwrite: true },
);
await fse.copy(
join(RESTORE_CACHE_DIR, "notes"),
NOTE_CONTENTS_DIR,
{ overwrite: true },
);
await fse.ensureDir(DATA_DIR);
await fse.copy(RESTORE_CACHE_DIR, DATA_DIR, { overwrite: true });

// await fse.copy(join(RESTORE_CACHE_DIR, "data.db"), DB_PATH, {
// overwrite: true,
// });
// await fse.copy(
// join(RESTORE_CACHE_DIR, "settings.json"),
// SETTINGS_PATH,
// { overwrite: true },
// );
// await fse.copy(
// join(RESTORE_CACHE_DIR, "notes"),
// NOTE_CONTENTS_DIR,
// { overwrite: true },
// );

dialog.showMessageBoxSync({
message:
"We restored your backup, we will relaunch Darkwrite for the changes to take effect.",
os.type() === "Linux"
? "We restored your backup. Darkwrite needs to relaunch for the changes to take effect."
: "We restored your backup, we will relaunch Darkwrite for the changes to take effect.",
});
app.relaunch();
app.exit();
} catch (error) {
log.error("!!! Restore failed. Rolling back...");
if (error instanceof Error) log.error(error.message);
if (didRename) {
await fse.rename(dbBackupPath, DB_PATH);
await fse.rename(notesBackupPath, NOTE_CONTENTS_DIR);
await fse.rename(settingsBackupPath, SETTINGS_PATH);
await fse.move(DATA_SNAPSHOT_DIR, DATA_DIR, {
overwrite: true,
});
// await fse.rename(dbBackupPath, DB_PATH);
// await fse.rename(notesBackupPath, NOTE_CONTENTS_DIR);
// await fse.rename(settingsBackupPath, SETTINGS_PATH);
}
await DB.init();
dialog.showMessageBoxSync({
Expand Down
14 changes: 12 additions & 2 deletions packages/app-desktop/src/electron/lib/paths.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { is } from "@electron-toolkit/utils";
import { app } from "electron";
import { join } from "node:path";

/** The user data directory, as defined by Electron. */
export const DATA_DIR = app.getPath("userData");
/** The folder to store Darkwrite's user data.
* It will point to `darkwrite-data/` on production and `darkwrite-data-nightly/` on development. */
export const DATA_DIR = join(
app.getPath("userData"),
is.dev ? "darkwrite-data-nightly/" : "darkwrite-data/",
);
/** The folder to rollback from if a restore operation fails. */
export const DATA_SNAPSHOT_DIR = join(
app.getPath("userData"),
"darkwrite-data-old",
);
/** The directory in which note contents are stored. */
export const NOTE_CONTENTS_DIR = join(DATA_DIR, "notes/");
/** Path to the SQLite database which holds the note entries. */
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@darkwrite/common",
"version": "0.1.0-alpha.2",
"version": "0.2.0-alpha.1",
"types": "./dist/index.d.ts",
"main": "./dist/index.js",
"scripts": {
Expand Down

0 comments on commit e24df95

Please sign in to comment.