docpad-plugin-sunny
Version:
DocPad plugin which adds the ability to output to Amazon S3, Google Storage or and other service supported by SunnyJS.
227 lines (207 loc) • 8.34 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var TaskGroup, doUpload, handle, handleEnv, handleEnvPrefix, http, mime, sunny, uploadData, util,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
sunny = require('sunny');
mime = require('mime');
http = require('http');
util = require('util');
TaskGroup = require('taskgroup').TaskGroup;
uploadData = function(container, path, headers, data, retryLimit, retries, next) {
var doIt, writeStream;
retries = retries != null ? retries : 0;
retryLimit = retryLimit != null ? retryLimit : 2;
doIt = !retryLimit || (retryLimit && (retries <= retryLimit)) || retryLimit === -1 ? true : false;
if (doIt) {
if (retries) {
console.log("Retrying upload of " + path + " to " + container.name + ": Attempts: " + retries);
}
writeStream = container.putBlob(path, headers);
writeStream.on('error', function(err) {
console.log("Error uploading " + path + " to " + container.name);
return uploadData(container, path, headers, data, retryLimit, retries + 1, next);
});
writeStream.on('end', function(results, meta) {
console.log("Uploaded " + path + " to " + container.name);
return next();
});
writeStream.write(data);
return writeStream.end();
} else {
return next("Upload for " + path + " to " + container.name + " has failed " + retries + " times. Giving up.");
}
};
doUpload = function(docpad, container, acl, retryLimit, next) {
var cloudHeaders, tasks;
if (acl != null) {
if (acl === false) {
cloudHeaders = {};
} else {
cloudHeaders = {
"acl": acl
};
}
} else {
cloudHeaders = {
"acl": 'public-read'
};
}
tasks = new TaskGroup().once('complete', function(err) {
return next(err);
});
docpad.getFiles({
write: true
}).forEach(function(file) {
var data, err, headers, key, length, path, type, value, _ref;
path = file.attributes.relativeOutPath;
data = file.get('contentRendered') || file.get('content') || (file.getData && file.getData()) || file.getContent() || "";
if (!data) {
return next("No data for file " + path);
}
length = data.length;
type = mime.lookup(path);
headers = {
"Content-Length": length,
"Content-Type": type
};
try {
if (file.get('headers') != null) {
_ref = file.get('headers');
for (key in _ref) {
value = _ref[key];
headers[key] = value;
}
}
} catch (_error) {
err = _error;
console.log(err);
console.dir(file);
}
return tasks.addTask(function(complete) {
return uploadData(container, path, {
headers: headers,
cloudHeaders: cloudHeaders
}, data, retryLimit, 0, complete);
});
});
return tasks.run();
};
handle = function(docpad, sunnyConfig, sunnyContainer, defaultACL, retryLimit, next) {
var connection, containerReq;
if ((sunnyConfig.provider != null) && (sunnyConfig.account != null) && (sunnyConfig.secretKey != null) && (sunnyContainer != null)) {
connection = sunny.Configuration.fromObj(sunnyConfig).connection;
containerReq = connection.getContainer(sunnyContainer, {
validate: true
});
containerReq.on('error', function(err) {
return console.log("Received error trying to connect to provider: \n " + err);
});
containerReq.on('end', function(results, meta) {
var container;
if (results) {
container = results.container;
console.log("Got container " + container.name + ".");
return doUpload(docpad, container, defaultACL, retryLimit, next);
}
});
return containerReq.end();
} else {
return next("One of the config variables is missing. Printing config:\n" + (util.inspect(sunnyConfig)) + "\nContainer is " + sunnyContainer);
}
};
handleEnvPrefix = function(docpad, prefix, next) {
var sunnyACL, sunnyConfig, sunnyContainer, sunnyRetryLimit;
sunnyConfig = {
provider: process.env["" + prefix + "PROVIDER"],
account: process.env["" + prefix + "ACCOUNT"],
secretKey: process.env["" + prefix + "SECRETKEY"],
ssl: process.env["" + prefix + "SSL"],
authUrl: process.env["" + prefix + "AUTHURL"]
};
sunnyContainer = process.env["" + prefix + "CONTAINER"];
sunnyACL = process.env["" + prefix + "ACL"];
sunnyRetryLimit = process.env["" + prefix + "RETRY_LIMIT"];
sunnyConfig.ssl = (typeof sunnyConfig.ssl === 'string') && (sunnyConfig.ssl.toLowerCase() === 'true');
return handle(docpad, sunnyConfig, sunnyContainer, sunnyACL, sunnyRetryLimit, next);
};
handleEnv = function(docpad, config, next) {
var prefix, _i, _len, _ref, _results;
if (config.envPrefixes.length > 0) {
_ref = config.envPrefixes;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
prefix = _ref[_i];
_results.push(handleEnvPrefix(docpad, prefix, next));
}
return _results;
} else {
return handleEnvPrefix(docpad, "DOCPAD_SUNNY_", next);
}
};
module.exports = function(BasePlugin) {
var docpadSunnyPlugin, _ref;
return docpadSunnyPlugin = (function(_super) {
__extends(docpadSunnyPlugin, _super);
function docpadSunnyPlugin() {
this.consoleSetup = __bind(this.consoleSetup, this);
this.deployWithSunny = __bind(this.deployWithSunny, this);
_ref = docpadSunnyPlugin.__super__.constructor.apply(this, arguments);
return _ref;
}
docpadSunnyPlugin.prototype.name = "sunny";
docpadSunnyPlugin.prototype.config = {
defaultACL: 'public-read',
onlyIfProduction: true,
configFromEnv: false,
envPrefixes: [],
cloudConfigs: []
};
docpadSunnyPlugin.prototype.deployWithSunny = function(next) {
var config, docpad, errMsg;
docpad = this.docpad;
config = this.getConfig();
if (config.cloudConfigs.length > 0 || config.configFromEnv) {
docpad.log('info', "Found " + config.cloudConfigs.length + " configurations in file.");
return this.docpad.generate(function(err) {
var cloudConfig, tasks, _i, _len, _ref1;
if (err) {
return next(err);
}
tasks = new TaskGroup().once('complete', function(err) {
return next(err);
});
if (config.configFromEnv) {
docpad.log('info', "Grabbing configs from environment.");
tasks.addTask(function(complete) {
return handleEnv(docpad, config, complete);
});
}
_ref1 = config.cloudConfigs;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
cloudConfig = _ref1[_i];
tasks.addTask(function(complete) {
return handle(docpad, cloudConfig.sunny, cloudConfig.container, cloudConfig.acl, cloudConfig.retryLimit, complete);
});
}
return tasks.run();
});
} else {
errMsg = 'No configs found';
docpad.log('warn', errMsg);
return next(errMsg);
}
};
docpadSunnyPlugin.prototype.consoleSetup = function(opts) {
var commander, config, consoleInterface, docpad;
docpad = this.docpad;
config = this.getConfig();
consoleInterface = opts.consoleInterface, commander = opts.commander;
commander.command('deploy-sunny').description("Deploys your website to any provider allowed by Sunny.").action(consoleInterface.wrapAction(this.deployWithSunny));
return this;
};
return docpadSunnyPlugin;
})(BasePlugin);
};
}).call(this);