Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a backup copy of buffer.txt before migrating to new note library #280

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading