mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-11-03 09:02:59 +00:00
Reorganise JS modules into subdirectories
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/multiwikiserver/commands/mws-load-archive.js
|
||||
type: application/javascript
|
||||
module-type: command
|
||||
|
||||
Command to load archive of recipes, bags and tiddlers from a directory
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
exports.info = {
|
||||
name: "mws-load-archive",
|
||||
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 pathname";
|
||||
}
|
||||
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 bagFilename of bagNames) {
|
||||
const bagName = decodeURIComponent(bagFilename);
|
||||
console.log(`Reading bag ${bagName}`);
|
||||
const bagInfo = JSON.parse(fs.readFileSync(path.resolve(archivePath,"bags",bagFilename,"meta.json"),"utf8"));
|
||||
$tw.mws.store.createBag(bagName,bagInfo.description,bagInfo.accesscontrol);
|
||||
if(fs.existsSync(path.resolve(archivePath,"bags",bagFilename,"tiddlers"))) {
|
||||
const tiddlerFilenames = fs.readdirSync(path.resolve(archivePath,"bags",bagFilename,"tiddlers"));
|
||||
for(const tiddlerFilename of tiddlerFilenames) {
|
||||
if(tiddlerFilename.endsWith(".json")) {
|
||||
const tiddlerPath = path.resolve(archivePath,"bags",bagFilename,"tiddlers",tiddlerFilename),
|
||||
jsonTiddler = fs.readFileSync(tiddlerPath,"utf8"),
|
||||
tiddler = sanitiseTiddler(JSON.parse(jsonTiddler));
|
||||
if(tiddler && tiddler.title) {
|
||||
$tw.mws.store.saveBagTiddler(tiddler,bagName);
|
||||
} else {
|
||||
console.log(`Malformed JSON tiddler in file ${tiddlerPath}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Iterate the recipes
|
||||
const recipeNames = fs.readdirSync(path.resolve(archivePath,"recipes"));
|
||||
for(const recipeFilename of recipeNames) {
|
||||
if(recipeFilename.endsWith(".json")) {
|
||||
const recipeName = decodeURIComponent(recipeFilename.substring(0,recipeFilename.length - ".json".length));
|
||||
const jsonInfo = JSON.parse(fs.readFileSync(path.resolve(archivePath,"recipes",recipeFilename),"utf8"));
|
||||
$tw.mws.store.createRecipe(recipeName,jsonInfo.bag_names,jsonInfo.description,jsonInfo.accesscontrol);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function sanitiseTiddler(tiddler) {
|
||||
var sanitisedFields = Object.create(null);
|
||||
for(const fieldName in tiddler) {
|
||||
const fieldValue = tiddler[fieldName];
|
||||
let sanitisedValue = "";
|
||||
if(typeof fieldValue === "string") {
|
||||
sanitisedValue = fieldValue;
|
||||
} else if($tw.utils.isDate(fieldValue)) {
|
||||
sanitisedValue = $tw.utils.stringifyDate(fieldValue);
|
||||
} else if($tw.utils.isArray(fieldValue)) {
|
||||
sanitisedValue = $tw.utils.stringifyList(fieldValue);
|
||||
}
|
||||
sanitisedFields[fieldName] = sanitisedValue;
|
||||
}
|
||||
return sanitisedFields;
|
||||
}
|
||||
|
||||
exports.Command = Command;
|
||||
|
||||
})();
|
||||
@@ -0,0 +1,40 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/multiwikiserver/commands/mws-load-tiddlers.js
|
||||
type: application/javascript
|
||||
module-type: command
|
||||
|
||||
Command to recursively load a directory of tiddler files into a bag
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
exports.info = {
|
||||
name: "mws-load-tiddlers",
|
||||
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 < 2) {
|
||||
return "Missing pathname and/or bagname";
|
||||
}
|
||||
var tiddlersPath = this.params[0],
|
||||
bagName = this.params[1];
|
||||
$tw.mws.store.saveTiddlersFromPath(tiddlersPath,bagName);
|
||||
return null;
|
||||
};
|
||||
|
||||
exports.Command = Command;
|
||||
|
||||
})();
|
||||
@@ -0,0 +1,62 @@
|
||||
/*\
|
||||
title: $:/plugins/tiddlywiki/multiwikiserver/commands/mws-save-archive.js
|
||||
type: application/javascript
|
||||
module-type: command
|
||||
|
||||
Command to load an archive of recipes, bags and tiddlers to a directory
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
exports.info = {
|
||||
name: "mws-save-archive",
|
||||
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 pathname";
|
||||
}
|
||||
var archivePath = this.params[0];
|
||||
saveArchive(archivePath);
|
||||
return null;
|
||||
};
|
||||
|
||||
function saveArchive(archivePath) {
|
||||
const fs = require("fs"),
|
||||
path = require("path");
|
||||
function saveJsonFile(filename,json) {
|
||||
const filepath = path.resolve(archivePath,filename);
|
||||
console.log(filepath);
|
||||
$tw.utils.createFileDirectories(filepath);
|
||||
fs.writeFileSync(filepath,JSON.stringify(json,null,4));
|
||||
}
|
||||
for(const recipeInfo of $tw.mws.store.listRecipes()) {
|
||||
console.log(`Recipe ${recipeInfo.recipe_name}`);
|
||||
saveJsonFile(`recipes/${encodeURIComponent(recipeInfo.recipe_name)}.json`,recipeInfo);
|
||||
}
|
||||
for(const bagInfo of $tw.mws.store.listBags()) {
|
||||
console.log(`Bag ${bagInfo.bag_name}`);
|
||||
saveJsonFile(`bags/${encodeURIComponent(bagInfo.bag_name)}/meta.json`,bagInfo);
|
||||
for(const title of $tw.mws.store.getBagTiddlers(bagInfo.bag_name)) {
|
||||
const tiddlerInfo = $tw.mws.store.getBagTiddler(title,bagInfo.bag_name);
|
||||
saveJsonFile(`bags/${encodeURIComponent(bagInfo.bag_name)}/tiddlers/${encodeURIComponent(title)}.json`,tiddlerInfo.tiddler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.Command = Command;
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user