From daaac0545f40d705b36ce20003cf172a9ee58bc0 Mon Sep 17 00:00:00 2001 From: Joshua Fontany Date: Tue, 9 Apr 2024 19:23:00 -0700 Subject: [PATCH 1/4] first pass at fixing bug 7878, needs testing --- core/modules/commands/save.js | 4 +++- core/modules/utils/filesystem.js | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/core/modules/commands/save.js b/core/modules/commands/save.js index 9769cec696f..3cb7ef08c6c 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 1ba34323e12..4af3b35f235 100644 --- a/core/modules/utils/filesystem.js +++ b/core/modules/utils/filesystem.js @@ -321,6 +321,7 @@ exports.generateTiddlerFilepath = function(title,options) { var directory = options.directory || "", extension = options.extension || "", originalpath = (options.fileInfo && options.fileInfo.originalpath) ? options.fileInfo.originalpath : "", + overwrite = options.overwrite || false, filepath; // Check if any of the pathFilters applies if(options.pathFilters && options.wiki) { @@ -382,18 +383,19 @@ exports.generateTiddlerFilepath = function(title,options) { }); } // Add a uniquifier if the file already exists - var fullPath, oldPath = (options.fileInfo) ? options.fileInfo.filepath : undefined, + 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) { From 5b349d02c2d57e30d925dba76399f2fb16c0a1af Mon Sep 17 00:00:00 2001 From: Joshua Fontany Date: Tue, 9 Apr 2024 19:24:59 -0700 Subject: [PATCH 2/4] clarify default behaviour in comment --- core/modules/utils/filesystem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules/utils/filesystem.js b/core/modules/utils/filesystem.js index 4af3b35f235..554ef954335 100644 --- a/core/modules/utils/filesystem.js +++ b/core/modules/utils/filesystem.js @@ -382,7 +382,7 @@ exports.generateTiddlerFilepath = function(title,options) { filepath += char.charCodeAt(0).toString(); }); } - // Add a uniquifier if the file already exists + // 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, From 7f096e6808219ffeffdfc0eb8ce977703f366c31 Mon Sep 17 00:00:00 2001 From: Joshua Fontany Date: Wed, 10 Apr 2024 19:23:50 -0700 Subject: [PATCH 3/4] fix property typo, tested and works as intended --- core/modules/commands/save.js | 2 +- core/modules/utils/filesystem.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/modules/commands/save.js b/core/modules/commands/save.js index 3cb7ef08c6c..a42536fb34b 100644 --- a/core/modules/commands/save.js +++ b/core/modules/commands/save.js @@ -38,7 +38,7 @@ Saves individual tiddlers in their raw text or binary format to the specified fi $tw.utils.each(tiddlers,function(title) { if(!result) { var tiddler = self.commander.wiki.getTiddler(title); - if(tiddler) { + if(tiddler) { debugger; var fileInfo = $tw.utils.generateTiddlerFileInfo(tiddler,{ directory: path.resolve(self.commander.outputPath), pathFilters: [filenameFilter], diff --git a/core/modules/utils/filesystem.js b/core/modules/utils/filesystem.js index 554ef954335..5319e048147 100644 --- a/core/modules/utils/filesystem.js +++ b/core/modules/utils/filesystem.js @@ -316,12 +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.overwrite || false, + overwrite = options.fileInfo && options.fileInfo.overwrite || false, filepath; // Check if any of the pathFilters applies if(options.pathFilters && options.wiki) { From 5ef410a43d743508d1e0fa0535fbf4db774f2b19 Mon Sep 17 00:00:00 2001 From: Joshua Fontany Date: Wed, 10 Apr 2024 20:06:17 -0700 Subject: [PATCH 4/4] remove debugger --- core/modules/commands/save.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules/commands/save.js b/core/modules/commands/save.js index a42536fb34b..3cb7ef08c6c 100644 --- a/core/modules/commands/save.js +++ b/core/modules/commands/save.js @@ -38,7 +38,7 @@ Saves individual tiddlers in their raw text or binary format to the specified fi $tw.utils.each(tiddlers,function(title) { if(!result) { var tiddler = self.commander.wiki.getTiddler(title); - if(tiddler) { debugger; + if(tiddler) { var fileInfo = $tw.utils.generateTiddlerFileInfo(tiddler,{ directory: path.resolve(self.commander.outputPath), pathFilters: [filenameFilter],