mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-16 20:22:52 +00:00
a346888213
Tools for working with Amazon Web Services: * Templates for saving a TiddlyWiki as a single JavaScript file in a ZIP file that can be executed as an AWS Lambda function. In this form, TiddlyWiki is a self contained single file containing both code and data, just like the standalone HTML file configuration * Commands that can be used to interact with AWS services, under both the Node.js and Lambda configurations of TiddlyWiki
122 lines
2.9 KiB
JavaScript
122 lines
2.9 KiB
JavaScript
/*\
|
|
title: $:/plugins/tiddlywiki/aws/command.js
|
|
type: application/javascript
|
|
module-type: command
|
|
|
|
--aws command
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
var async,
|
|
awsUtils;
|
|
|
|
exports.info = {
|
|
name: "aws",
|
|
synchronous: false
|
|
};
|
|
|
|
var Command = function(params,commander,callback) {
|
|
async = require("$:/plugins/tiddlywiki/async/async.js");
|
|
awsUtils = require("$:/plugins/tiddlywiki/aws/utils.js");
|
|
this.params = params;
|
|
this.commander = commander;
|
|
this.callback = callback;
|
|
};
|
|
|
|
Command.prototype.execute = function() {
|
|
var self = this,
|
|
wiki = this.commander.wiki,
|
|
subCommand = this.params[0],
|
|
fn = this.subCommands[subCommand];
|
|
if(!fn) {
|
|
return this.callback("AWS: Unknown subcommand")
|
|
}
|
|
fn.bind(this)();
|
|
return null;
|
|
};
|
|
|
|
Command.prototype.subCommands = {};
|
|
|
|
// Render a tiddler to an S3 bucket
|
|
Command.prototype.subCommands["s3-rendertiddler"] = function() {
|
|
var self = this,
|
|
wiki = this.commander.wiki,
|
|
title = this.params[1],
|
|
region = this.params[2],
|
|
bucket = this.params[3],
|
|
filename = this.params[4],
|
|
type = this.params[5] || "text/html",
|
|
template = this.params[6],
|
|
zipfilename = this.params[7],
|
|
saveType = this.params[8] || type,
|
|
variables = {};
|
|
// Process the template if present
|
|
if(template) {
|
|
variables.currentTiddler = title;
|
|
title = template;
|
|
}
|
|
// Render the tiddler
|
|
var text = this.commander.wiki.renderTiddler(type,title,{variables: variables}),
|
|
type = "text/plain",
|
|
encoding = ($tw.config.contentTypeInfo[type] || {encoding: "utf8"}).encoding;
|
|
// Zip it if needed
|
|
if(zipfilename) {
|
|
var JSZip = require("$:/plugins/tiddlywiki/jszip/jszip.js"),
|
|
zip = new JSZip();
|
|
zip.file(filename,new Buffer(text,encoding));
|
|
text = zip.generate({type: "base64"});
|
|
type = "application/zip";
|
|
filename = zipfilename;
|
|
}
|
|
// Save the file
|
|
async.series([
|
|
awsUtils.putFile.bind(null,region,bucket,filename,text,saveType)
|
|
],
|
|
function(err,results){
|
|
self.callback(err,results);
|
|
});
|
|
return null;
|
|
};
|
|
|
|
// Save a tiddler to an S3 bucket
|
|
Command.prototype.subCommands["s3-savetiddler"] = function() {
|
|
var self = this,
|
|
wiki = this.commander.wiki,
|
|
title = this.params[1],
|
|
region = this.params[2],
|
|
bucket = this.params[3],
|
|
filename = this.params[4],
|
|
zipfilename = this.params[5],
|
|
tiddler = wiki.getTiddler(title),
|
|
text = tiddler.fields.text,
|
|
type = tiddler.fields.type,
|
|
encoding = ($tw.config.contentTypeInfo[type] || {encoding: "utf8"}).encoding;
|
|
// Zip it if needed
|
|
if(zipfilename) {
|
|
var JSZip = require("$:/plugins/tiddlywiki/jszip/jszip.js"),
|
|
zip = new JSZip();
|
|
zip.file(filename,new Buffer(text,encoding));
|
|
text = zip.generate({type: "base64"});
|
|
type = "application/zip";
|
|
filename = zipfilename;
|
|
}
|
|
// Save the file
|
|
async.series([
|
|
awsUtils.putFile.bind(null,region,bucket,filename,text,type)
|
|
],
|
|
function(err,results){
|
|
self.callback(err,results);
|
|
});
|
|
return null;
|
|
};
|
|
|
|
exports.Command = Command;
|
|
|
|
})();
|
|
|