From f1d0e52ff79dc0d05be652c7b6cb589d05825e3b Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Fri, 5 Jul 2024 18:40:59 +0100 Subject: [PATCH] New commands --- editions/multiwikiserver/tiddlywiki.info | 22 ++- package-lock.json | 4 +- package.json | 2 +- .../modules/commands/mws-create-bag.js | 47 +++++++ .../modules/commands/mws-create-recipe.js | 50 +++++++ .../modules/commands/mws-load-plugin-bags.js | 81 +++++++++++ .../modules/commands/mws-load-wiki-folder.js | 93 +++++++++++++ .../modules/commands/mws-save-tiddler-text.js | 44 ++++++ .../multiwikiserver/modules/startup.js | 131 ------------------ 9 files changed, 333 insertions(+), 141 deletions(-) create mode 100644 plugins/tiddlywiki/multiwikiserver/modules/commands/mws-create-bag.js create mode 100644 plugins/tiddlywiki/multiwikiserver/modules/commands/mws-create-recipe.js create mode 100644 plugins/tiddlywiki/multiwikiserver/modules/commands/mws-load-plugin-bags.js create mode 100644 plugins/tiddlywiki/multiwikiserver/modules/commands/mws-load-wiki-folder.js create mode 100644 plugins/tiddlywiki/multiwikiserver/modules/commands/mws-save-tiddler-text.js diff --git a/editions/multiwikiserver/tiddlywiki.info b/editions/multiwikiserver/tiddlywiki.info index b190fa7d7..94a6717e6 100644 --- a/editions/multiwikiserver/tiddlywiki.info +++ b/editions/multiwikiserver/tiddlywiki.info @@ -11,12 +11,20 @@ "tiddlywiki/snowwhite" ], "build": { - "index": [ - "--render","$:/plugins/tiddlywiki/tiddlyweb/save/offline","index.html","text/plain"], - "static": [ - "--render","$:/core/templates/static.template.html","static.html","text/plain", - "--render","$:/core/templates/alltiddlers.template.html","alltiddlers.html","text/plain", - "--render","[!is[system]]","[encodeuricomponent[]addprefix[static/]addsuffix[.html]]","text/plain","$:/core/templates/static.tiddler.html", - "--render","$:/core/templates/static.template.css","static/static.css","text/plain"] + "load-mws-demo-data": [ + "--mws-load-wiki-folder","./editions/tw5.com","docs", "TiddlyWiki Documentation from https://tiddlywiki.com","docs","TiddlyWiki Documentation from https://tiddlywiki.com", + "--mws-load-wiki-folder","./editions/dev","dev","TiddlyWiki Developer Documentation from https://tiddlywiki.com/dev","dev-docs", "TiddlyWiki Developer Documentation from https://tiddlywiki.com/dev", + "--mws-load-wiki-folder","./editions/tour","tour","TiddlyWiki Interactive Tour from https://tiddlywiki.com","tour", "TiddlyWiki Interactive Tour from https://tiddlywiki.com", + "--mws-create-bag","bag-alpha","A test bag", + "--mws-create-bag","bag-beta","Another test bag", + "--mws-create-bag","bag-gamma","A further test bag", + "--mws-create-recipe","recipe-rho","bag-alpha bag-beta","First wiki", + "--mws-create-recipe","recipe-sigma","bag-alpha bag-gamma","Second Wiki", "--mws-create-recipe","recipe-tau","bag-alpha","Third Wiki", + "--mws-create-recipe","recipe-upsilon","bag-alpha bag-gamma bag-beta","Fourth Wiki", + "--mws-save-tiddler-text","bag-alpha","$:/SiteTitle","bag-alpha", + "--mws-save-tiddler-text","bag-alpha","😀😃😄😁😆🥹😅😂","bag-alpha", + "--mws-save-tiddler-text","bag-beta","$:/SiteTitle","bag-beta", + "--mws-save-tiddler-text","bag-gamma","$:/SiteTitle","bag-gamma" + ] } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f03ed5458..70fe286b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "tiddlywiki", - "version": "5.3.4-prerelease", + "version": "5.3.5-prerelease", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "tiddlywiki", - "version": "5.3.4-prerelease", + "version": "5.3.5-prerelease", "license": "BSD", "dependencies": { "better-sqlite3": "^9.4.3", diff --git a/package.json b/package.json index 74807c304..fcfb7046c 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "node": ">=0.8.2" }, "scripts": { - "start": "node ./tiddlywiki.js ./editions/multiwikiserver --mws-listen", + "start": "node ./tiddlywiki.js ./editions/multiwikiserver --mws-load-plugin-bags --build load-mws-demo-data --mws-listen", "test": "node ./tiddlywiki.js ./editions/test --verbose --version --build index && node ./tiddlywiki ./editions/multiwikiserver/ --mws-listen --mws-test-server http://127.0.0.1:8080/ --quit", "lint:fix": "eslint . --fix", "lint": "eslint ." diff --git a/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-create-bag.js b/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-create-bag.js new file mode 100644 index 000000000..c90088a87 --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-create-bag.js @@ -0,0 +1,47 @@ +/*\ +title: $:/plugins/tiddlywiki/multiwikiserver/commands/mws-create-bag.js +type: application/javascript +module-type: command + +Command to load archive of recipes, bags and tiddlers from a directory + +--mws-create-bag + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "mws-create-bag", + synchronous: true +}; + +var Command = function(params,commander,callback) { + this.params = params; + this.commander = commander; + this.callback = callback; +}; + +Command.prototype.execute = function() { + var self = this; + // Check parameters + if(this.params.length < 1) { + return "Missing bag name"; + } + var bagName = this.params[0], + bagDescription = this.params[1] || bagName; + // Create bag + var result = $tw.mws.store.createBag(bagName,bagDescription); + if(result) { + return result.message; + } else { + return null; + } +}; + +exports.Command = Command; + +})(); diff --git a/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-create-recipe.js b/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-create-recipe.js new file mode 100644 index 000000000..6515c817a --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-create-recipe.js @@ -0,0 +1,50 @@ +/*\ +title: $:/plugins/tiddlywiki/multiwikiserver/commands/mws-create-recipe.js +type: application/javascript +module-type: command + +Command to load archive of recipes, bags and tiddlers from a directory + +--mws-create-recipe + +The parameter "bag-list" should be a space delimited list of bags + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "mws-create-recipe", + synchronous: true +}; + +var Command = function(params,commander,callback) { + this.params = params; + this.commander = commander; + this.callback = callback; +}; + +Command.prototype.execute = function() { + var self = this; + // Check parameters + if(this.params.length < 1) { + return "Missing recipe name"; + } + var recipeName = this.params[0], + bagList = (this.params[1] || "").split(" "), + recipeDescription = this.params[2] || recipeNameName; + // Create recipe + var result = $tw.mws.store.createRecipe(recipeName,bagList,recipeDescription); + if(result) { + return result.message; + } else { + return null; + } +}; + +exports.Command = Command; + +})(); diff --git a/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-load-plugin-bags.js b/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-load-plugin-bags.js new file mode 100644 index 000000000..f6bfeac52 --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-load-plugin-bags.js @@ -0,0 +1,81 @@ +/*\ +title: $:/plugins/tiddlywiki/multiwikiserver/commands/mws-load-plugin-bags.js +type: application/javascript +module-type: command + +Command to create and load a bag for each plugin in the repo + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "mws-load-plugin-bags", + synchronous: true +}; + +var Command = function(params,commander,callback) { + this.params = params; + this.commander = commander; + this.callback = callback; +}; + +Command.prototype.execute = function() { + var self = this; + loadPluginBags(); + return null; +}; + +function loadPluginBags() { + const path = require("path"), + fs = require("fs"); + // Copy plugins + var makePluginBagName = function(type,publisher,name) { + return "$:/" + type + "/" + (publisher ? publisher + "/" : "") + name; + }, + savePlugin = function(pluginFields,type,publisher,name) { + const bagName = makePluginBagName(type,publisher,name); + const result = $tw.mws.store.createBag(bagName,pluginFields.description || "(no description)",{allowPrivilegedCharacters: true}); + if(result) { + console.log(`Error creating plugin bag ${bagname}: ${JSON.stringify(result)}`); + } + $tw.mws.store.saveBagTiddler(pluginFields,bagName); + }, + collectPlugins = function(folder,type,publisher) { + var pluginFolders = $tw.utils.getSubdirectories(folder) || []; + for(var p=0; p + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "mws-load-wiki-folder", + synchronous: true +}; + +var Command = function(params,commander,callback) { + this.params = params; + this.commander = commander; + this.callback = callback; +}; + +Command.prototype.execute = function() { + var self = this; + // Check parameters + if(this.params.length < 5) { + return "Missing parameters for --mws-load-wiki-folder command"; + } + var archivePath = this.params[0]; + loadWikiFolder({ + wikiPath: this.params[0], + bagName: this.params[1], + bagDescription: this.params[2], + recipeName: this.params[3], + recipeDescription: this.params[4] + }); + return null; +}; + +// Function to convert a plugin name to a bag name +function makePluginBagName(type,publisher,name) { + return "$:/" + type + "/" + (publisher ? publisher + "/" : "") + name; +} + +// Copy TiddlyWiki core editions +function loadWikiFolder(options) { + const path = require("path"), + fs = require("fs"); + // Read the tiddlywiki.info file + const wikiInfoPath = path.resolve(options.wikiPath,$tw.config.wikiInfo); + let wikiInfo; + if(fs.existsSync(wikiInfoPath)) { + wikiInfo = $tw.utils.parseJSONSafe(fs.readFileSync(wikiInfoPath,"utf8"),function() {return null;}); + } + if(wikiInfo) { + // Create the bag + const result = $tw.mws.store.createBag(options.bagName,options.bagDescription); + if(result) { + console.log(`Error creating bag ${options.bagName} for edition ${options.wikiPath}: ${JSON.stringify(result)}`); + } + // Add plugins to the recipe list + const recipeList = []; + const processPlugins = function(type,plugins) { + $tw.utils.each(plugins,function(pluginName) { + const parts = pluginName.split("/"); + let publisher, name; + if(parts.length === 2) { + publisher = parts[0]; + name = parts[1]; + } else { + name = parts[0]; + } + recipeList.push(makePluginBagName(type,publisher,name)); + }); + }; + processPlugins("plugin",wikiInfo.plugins); + processPlugins("theme",wikiInfo.themes); + processPlugins("language",wikiInfo.languages); + // Create the recipe + recipeList.push(options.bagName); + $tw.mws.store.createRecipe(options.recipeName,recipeList,options.recipeDescription); + $tw.mws.store.saveTiddlersFromPath(path.resolve($tw.boot.corePath,$tw.config.editionsPath,options.wikiPath,$tw.config.wikiTiddlersSubDir),options.bagName); + } +} + +exports.Command = Command; + +})(); diff --git a/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-save-tiddler-text.js b/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-save-tiddler-text.js new file mode 100644 index 000000000..524ec948a --- /dev/null +++ b/plugins/tiddlywiki/multiwikiserver/modules/commands/mws-save-tiddler-text.js @@ -0,0 +1,44 @@ +/*\ +title: $:/plugins/tiddlywiki/multiwikiserver/commands/mws-save-tiddler-text.js +type: application/javascript +module-type: command + +Command to load archive of recipes, bags and tiddlers from a directory + +--mws-save-tiddler-text + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "mws-save-tiddler-text", + synchronous: true +}; + +var Command = function(params,commander,callback) { + this.params = params; + this.commander = commander; + this.callback = callback; +}; + +Command.prototype.execute = function() { + var self = this; + // Check parameters + if(this.params.length < 3) { + return "Missing parameters"; + } + var bagName = this.params[0], + tiddlerTitle = this.params[1], + tiddlerText = this.params[2]; + // Save tiddler + $tw.mws.store.saveBagTiddler({title: tiddlerTitle,text: tiddlerText},bagName); + return null; +}; + +exports.Command = Command; + +})(); diff --git a/plugins/tiddlywiki/multiwikiserver/modules/startup.js b/plugins/tiddlywiki/multiwikiserver/modules/startup.js index b8a6de800..42686e889 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/startup.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/startup.js @@ -20,7 +20,6 @@ exports.synchronous = true; exports.startup = function() { const store = setupStore(); - loadStore(store); $tw.mws = { store: store, serverManager: new ServerManager({ @@ -45,136 +44,6 @@ function setupStore() { return store; } -function loadStore(store) { - const path = require("path"), - fs = require("fs"); - // Performance timing - console.time("mws-initial-load"); - // Copy plugins - var makePluginBagName = function(type,publisher,name) { - return "$:/" + type + "/" + (publisher ? publisher + "/" : "") + name; - }, - savePlugin = function(pluginFields,type,publisher,name) { - const bagName = makePluginBagName(type,publisher,name); - const result = store.createBag(bagName,pluginFields.description || "(no description)",{allowPrivilegedCharacters: true}); - if(result) { - console.log(`Error creating plugin bag ${bagname}: ${JSON.stringify(result)}`); - } - console.log(`saveBagTiddler of ${pluginFields.title} to ${bagName}`); - store.saveBagTiddler(pluginFields,bagName); - }, - collectPlugins = function(folder,type,publisher) { - var pluginFolders = $tw.utils.getSubdirectories(folder) || []; - for(var p=0; p