From 5f74f4c2fa520b0c7a639cd0dcd5cc450b63418d Mon Sep 17 00:00:00 2001 From: Joshua Fontany Date: Thu, 11 Apr 2024 13:54:46 -0700 Subject: [PATCH] Fix bug 7878: Save command (#8140) * first pass at fixing bug 7878, needs testing * clarify default behaviour in comment * fix property typo, tested and works as intended * remove debugger --- core/modules/commands/save.js | 4 +++- core/modules/utils/filesystem.js | 23 +++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/core/modules/commands/save.js b/core/modules/commands/save.js index 9769cec69..3cb7ef08c 100644 --- a/core/modules/commands/save.js +++ b/core/modules/commands/save.js @@ -43,7 +43,9 @@ Saves individual tiddlers in their raw text or binary format to the specified fi directory: path.resolve(self.commander.outputPath), pathFilters: [filenameFilter], wiki: wiki, - fileInfo: {} + fileInfo: { + overwrite: true + } }); if(self.commander.verbose) { console.log("Saving \"" + title + "\" to \"" + fileInfo.filepath + "\""); diff --git a/core/modules/utils/filesystem.js b/core/modules/utils/filesystem.js index 1ba34323e..5319e0481 100644 --- a/core/modules/utils/filesystem.js +++ b/core/modules/utils/filesystem.js @@ -316,11 +316,13 @@ Options include: pathFilters: optional array of filters to be used to generate the base path wiki: optional wiki for evaluating the pathFilters fileInfo: an existing fileInfo object to check against + fileInfo.overwrite: if true, turns off filename clash numbers (defaults to false) */ exports.generateTiddlerFilepath = function(title,options) { var directory = options.directory || "", extension = options.extension || "", originalpath = (options.fileInfo && options.fileInfo.originalpath) ? options.fileInfo.originalpath : "", + overwrite = options.fileInfo && options.fileInfo.overwrite || false, filepath; // Check if any of the pathFilters applies if(options.pathFilters && options.wiki) { @@ -381,19 +383,20 @@ exports.generateTiddlerFilepath = function(title,options) { filepath += char.charCodeAt(0).toString(); }); } - // Add a uniquifier if the file already exists - var fullPath, oldPath = (options.fileInfo) ? options.fileInfo.filepath : undefined, + // Add a uniquifier if the file already exists (default) + var fullPath = path.resolve(directory, filepath + extension); + if (!overwrite) { + var oldPath = (options.fileInfo) ? options.fileInfo.filepath : undefined, count = 0; - do { - fullPath = path.resolve(directory,filepath + (count ? "_" + count : "") + extension); - if(oldPath && oldPath == fullPath) { - break; - } - count++; - } while(fs.existsSync(fullPath)); + do { + fullPath = path.resolve(directory,filepath + (count ? "_" + count : "") + extension); + if(oldPath && oldPath == fullPath) break; + count++; + } while(fs.existsSync(fullPath)); + } // If the last write failed with an error, or if path does not start with: // the resolved options.directory, the resolved wikiPath directory, the wikiTiddlersPath directory, - // or the 'originalpath' directory, then $tw.utils.encodeURIComponentExtended() and resolve to tiddler directory. + // or the 'originalpath' directory, then $tw.utils.encodeURIComponentExtended() and resolve to options.directory. var writePath = $tw.hooks.invokeHook("th-make-tiddler-path",fullPath,fullPath), encode = (options.fileInfo || {writeError: false}).writeError == true; if(!encode) {