1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-09 03:19:56 +00:00
TiddlyWiki5/plugins/tiddlywiki/multiwikiserver/modules/command-mws-load.js
Jeremy Ruston 3283d38867 First draft of a command to read tiddlers, bags and recipes from an archive
The archive format is a legacy format that I used with Xememex, and will need to be updated to iron out the wrinkles
2024-01-28 21:27:12 +00:00

68 lines
2.2 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/multiwikiserver/mws-load.js
type: application/javascript
module-type: command
Command to load tiddlers from a file or directory
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.info = {
name: "mws-load",
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 filename";
}
var archivePath = this.params[0];
loadBackupArchive(archivePath);
return null;
};
function loadBackupArchive(archivePath) {
const fs = require("fs"),
path = require("path");
// Iterate the bags
const bagNames = fs.readdirSync(path.resolve(archivePath,"bags")).filter(filename => filename !== ".DS_Store");
for(const bagName of bagNames) {
console.log(`Reading bag ${bagName}`);
$tw.sqlTiddlerStore.createBag(decodeURIComponent(bagName),"No description for " + bagName);
// Read the metadata
const jsonInfo = JSON.parse(fs.readFileSync(path.resolve(archivePath,"bags",bagName,"meta/info.json"),"utf8"));
if(fs.existsSync(path.resolve(archivePath,"bags",bagName,"tiddlers"))) {
// Read each tiddler
const tiddlerFilenames = fs.readdirSync(path.resolve(archivePath,"bags",bagName,"tiddlers")).filter(filename => filename !== ".DS_Store");
for(const tiddlerFilename of tiddlerFilenames) {
const jsonTiddler = fs.readFileSync(path.resolve(archivePath,"bags",bagName,"tiddlers",tiddlerFilename),"utf8"),
tiddler = JSON.parse(jsonTiddler);
$tw.sqlTiddlerStore.saveBagTiddler(tiddler,decodeURIComponent(bagName));
}
}
}
// Iterate the recipes
const recipeNames = fs.readdirSync(path.resolve(archivePath,"recipes")).filter(filename => filename !== ".DS_Store");
for(const recipeName of recipeNames) {
const jsonInfo = JSON.parse(fs.readFileSync(path.resolve(archivePath,"recipes",recipeName,"info.json"),"utf8"));
$tw.sqlTiddlerStore.createRecipe(decodeURIComponent(recipeName),jsonInfo.recipe.map(recipeLine => recipeLine[0]),"No description for " + recipeName);
}
}
exports.Command = Command;
})();