Skip to content

Commit

Permalink
Make a backup copy of buffer.txt before migrating to new note library
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Dec 12, 2024
1 parent a7471fc commit 23f463c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
6 changes: 3 additions & 3 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Here are the most notable changes in each release. For a more detailed list of c

### IMPORTANT (breaking change)

The default path of the scratch file has changed. If you are running a previous version of Heynote with the buffer file synchronized across multiple machines using a file synching service such as Dropbox or OneDrive, you should make sure to upgrade all machines to Heynote 2.0 at the same time (closing Heynote before) in order for the file to stay in sync, since the file path for the buffer file has changed. See below for more info.
The default path of the scratch file has changed. The first time you start the new version of Heynote, your existing buffer file will be migrated to the new note library. If you're using the default buffer location, that means that the existing Scratch buffer file will be moved from `%APP_DIR%/buffer.txt` to `%APP_DIR%/notes/scratch.txt`. If you are using a custom buffer location the existing scratch file will be moved from `%CUSTOM_DIR%/buffer.txt` to `%CUSTOM_DIR%/scratch.txt`. Before the migration, the existing buffer file will be backed up to `%APP_DIR%/buffer.txt.bak` or `%CUSTOM_DIR%/buffer.txt.bak`.

If you are running a previous version of Heynote with the buffer file synchronized across multiple machines using a file synching service such as Dropbox or OneDrive, you should make sure to upgrade all machines to Heynote 2.0 at the same time (closing Heynote before) in order for the file to stay in sync, since the file path for the buffer file has changed.

### Support for multiple note buffers.

Apart from the default Scratch buffer, you can now create and switch between multiple note buffers. `Ctrl/Cmd+N` opens up a dialog for creating a new buffer. By pressing `Ctrl/Cmd+S` you can create a new note from the current block (the current block will be moved into the new note). New note buffers are saved to the note library which is basically a directory (with sub dirs) on the disk with a `.txt` file for each buffer. You switch between buffers by pressing `Ctrl/Cmd+P`.

The first time you start the new version of Heynote, your existing buffer file will be migrated to the new note library. If you're using the default buffer location, that means that the existing Scratch buffer file will be moved from `%APP_DIR%/buffer.txt` to `%APP_DIR%/notes/scratch.txt`. If you are using a custom buffer location the existing scratch file will be moved from `%CUSTOM_DIR%/buffer.txt` to `%CUSTOM_DIR%/scratch.txt`.

### Other changes

- The file format for the buffer files has been updated to include some JSON metadata at the top of the file.
Expand Down
24 changes: 23 additions & 1 deletion electron/main/file-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,18 @@ export async function migrateBufferFileToLibrary(app) {
}
}

function getBackupFile(filePath) {
// Get a backup file path by adding a .bak suffix. If the file already exists, add a number suffix.
let backupFile = filePath + ".bak";
for (let i = 1; i < 1000; i++) {
if (jetpack.exists(backupFile) !== "file") {
return backupFile;
}
backupFile = `${filePath}.bak.${i}`;
}
throw new Error(`Unable to find an available file path after 1000 attempts for base path: ${filePath}`);
}

const defaultLibraryPath = join(app.getPath("userData"), NOTES_DIR_NAME)
const customBufferPath = CONFIG.get("settings.bufferPath")
const oldBufferFile = isDev ? "buffer-dev.txt" : "buffer.txt"
Expand All @@ -343,10 +355,15 @@ export async function migrateBufferFileToLibrary(app) {
return
}
const oldBufferFileFullPath = join(customBufferPath, oldBufferFile)
const backupFile = getBackupFile(oldBufferFileFullPath)
if (jetpack.exists(oldBufferFileFullPath) === "file") {
// make a backup copy of the old buffer file
console.log(`Taking backup of ${oldBufferFileFullPath} to ${backupFile}`)
jetpack.copy(oldBufferFileFullPath, backupFile)

// rename buffer file to scratch.txt
const newFileFullPath = join(customBufferPath, SCRATCH_FILE_NAME);
console.log(`Migrating file ${oldBufferFileFullPath} to ${newFileFullPath}`)
// rename buffer file to scratch.txt
jetpack.move(oldBufferFileFullPath, newFileFullPath)
// add metadata to scratch.txt (just to be sure, we'll double check that it's needed first)
await ensureBufferFileMetadata(newFileFullPath)
Expand All @@ -358,7 +375,12 @@ export async function migrateBufferFileToLibrary(app) {
}
// check if the old buffer file exists, while the default *library* path doesn't exist
const oldBufferFileFullPath = join(app.getPath("userData"), oldBufferFile)
const backupFile = getBackupFile(oldBufferFileFullPath)
if (jetpack.exists(oldBufferFileFullPath) === "file" && jetpack.exists(defaultLibraryPath) !== "dir") {
// make a backup copy of the old buffer file
console.log(`Taking backup of ${oldBufferFileFullPath} to ${backupFile}`)
jetpack.copy(oldBufferFileFullPath, backupFile)

const newFileFullPath = join(defaultLibraryPath, SCRATCH_FILE_NAME);
console.log(`Migrating buffer file ${oldBufferFileFullPath} to ${newFileFullPath}`)
// create the default library path
Expand Down

0 comments on commit 23f463c

Please sign in to comment.