1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-11-16 15:28:24 +00:00

New commands

This commit is contained in:
Jeremy Ruston
2024-07-05 18:40:59 +01:00
parent d1edf6424d
commit f1d0e52ff7
9 changed files with 333 additions and 141 deletions

View File

@@ -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 <name> <description>
\*/
(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;
})();

View File

@@ -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 <name> <bag-list> <description>
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;
})();

View File

@@ -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<pluginFolders.length; p++) {
const pluginFolderName = pluginFolders[p];
if(!$tw.boot.excludeRegExp.test(pluginFolderName)) {
var pluginFields = $tw.loadPluginFolder(path.resolve(folder,pluginFolderName));
if(pluginFields && pluginFields.title) {
savePlugin(pluginFields,type,publisher,pluginFolderName);
}
}
}
},
collectPublisherPlugins = function(folder,type) {
var publisherFolders = $tw.utils.getSubdirectories(folder) || [];
for(var t=0; t<publisherFolders.length; t++) {
const publisherFolderName = publisherFolders[t];
if(!$tw.boot.excludeRegExp.test(publisherFolderName)) {
collectPlugins(path.resolve(folder,publisherFolderName),type,publisherFolderName);
}
}
};
$tw.utils.each($tw.getLibraryItemSearchPaths($tw.config.pluginsPath,$tw.config.pluginsEnvVar),function(folder) {
collectPublisherPlugins(folder,"plugin");
});
$tw.utils.each($tw.getLibraryItemSearchPaths($tw.config.themesPath,$tw.config.themesEnvVar),function(folder) {
collectPublisherPlugins(folder,"theme");
});
$tw.utils.each($tw.getLibraryItemSearchPaths($tw.config.languagesPath,$tw.config.languagesEnvVar),function(folder) {
collectPlugins(folder,"language");
});
}
exports.Command = Command;
})();

View File

@@ -0,0 +1,93 @@
/*\
title: $:/plugins/tiddlywiki/multiwikiserver/commands/mws-load-wiki-folder.js
type: application/javascript
module-type: command
Command to create and load a bag for the specified core editions
--mws-load-wiki-folder <path> <bag-name> <bag-description> <recipe-name> <recipe-description>
\*/
(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;
})();

View File

@@ -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 <bag-name> <tiddler-title> <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;
})();