From 73138b79aaaafa32b3a0abf8ba80c96eb3a18d56 Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Wed, 6 Apr 2022 17:18:13 +0100 Subject: [PATCH] Save command exit with error when encountering missing tiddlers Fixes #6603 --- core/modules/commands/save.js | 92 +++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/core/modules/commands/save.js b/core/modules/commands/save.js index d8c44e1f3..21839177a 100644 --- a/core/modules/commands/save.js +++ b/core/modules/commands/save.js @@ -8,46 +8,54 @@ Saves individual tiddlers in their raw text or binary format to the specified fi \*/ (function(){ -/*jslint node: true, browser: true */ -/*global $tw: false */ -"use strict"; - -exports.info = { - name: "save", - synchronous: true -}; - -var Command = function(params,commander,callback) { - this.params = params; - this.commander = commander; - this.callback = callback; -}; - -Command.prototype.execute = function() { - if(this.params.length < 1) { - return "Missing filename filter"; - } - var self = this, - fs = require("fs"), - path = require("path"), - wiki = this.commander.wiki, - tiddlerFilter = this.params[0], - filenameFilter = this.params[1] || "[is[tiddler]]", - tiddlers = wiki.filterTiddlers(tiddlerFilter); - $tw.utils.each(tiddlers,function(title) { - var tiddler = self.commander.wiki.getTiddler(title), - type = tiddler.fields.type || "text/vnd.tiddlywiki", - contentTypeInfo = $tw.config.contentTypeInfo[type] || {encoding: "utf8"}, - filepath = path.resolve(self.commander.outputPath,wiki.filterTiddlers(filenameFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0]); - if(self.commander.verbose) { - console.log("Saving \"" + title + "\" to \"" + filepath + "\""); + /*jslint node: true, browser: true */ + /*global $tw: false */ + "use strict"; + + exports.info = { + name: "save", + synchronous: true + }; + + var Command = function(params,commander,callback) { + this.params = params; + this.commander = commander; + this.callback = callback; + }; + + Command.prototype.execute = function() { + if(this.params.length < 1) { + return "Missing filename filter"; } - $tw.utils.createFileDirectories(filepath); - fs.writeFileSync(filepath,tiddler.fields.text,contentTypeInfo.encoding); - }); - return null; -}; - -exports.Command = Command; - -})(); + var self = this, + fs = require("fs"), + path = require("path"), + result = null, + wiki = this.commander.wiki, + tiddlerFilter = this.params[0], + filenameFilter = this.params[1] || "[is[tiddler]]", + tiddlers = wiki.filterTiddlers(tiddlerFilter); + $tw.utils.each(tiddlers,function(title) { + if(!result) { + var tiddler = self.commander.wiki.getTiddler(title); + if(tiddler) { + var type = tiddler.fields.type || "text/vnd.tiddlywiki", + contentTypeInfo = $tw.config.contentTypeInfo[type] || {encoding: "utf8"}, + filepath = path.resolve(self.commander.outputPath,wiki.filterTiddlers(filenameFilter,$tw.rootWidget,wiki.makeTiddlerIterator([title]))[0]); + if(self.commander.verbose) { + console.log("Saving \"" + title + "\" to \"" + filepath + "\""); + } + $tw.utils.createFileDirectories(filepath); + fs.writeFileSync(filepath,tiddler.fields.text,contentTypeInfo.encoding); + } else { + result = "Tiddler '" + title + "' not found"; + } + } + }); + return result; + }; + + exports.Command = Command; + + })(); + \ No newline at end of file