diff --git a/docs/changelog.md b/docs/changelog.md index 6ce1d87..358e461 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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. diff --git a/electron/main/file-library.js b/electron/main/file-library.js index 6999523..73877d8 100644 --- a/electron/main/file-library.js +++ b/electron/main/file-library.js @@ -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" @@ -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) @@ -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