Fixed minor code issues caught by jslint. Added more code comments to app.js.

master
kts of kettek 2015-12-19 22:39:29 -08:00
parent 3c1a783bca
commit 01458f1e2e
1 changed files with 33 additions and 14 deletions

47
app.js
View File

@ -39,7 +39,7 @@ IMPORTANT NOTE:
such as using git or subversion repositories.
In general, newsletters are composed of a directory in the newsletters/
directory with index.js populated with an extended Newsletter module
directory with index.js populated with an extended Newsletter module
inheriting from this Newsletter object.
The Newsletter object has the following properties:
@ -77,21 +77,28 @@ These are:
*/
function Newsletter(data) {
this.temp = {};
this.temp = {}; // temporary Letter object (html, text, subject)
this.repo = {};
this.subscribers = [];
this.options = {};
this.transport_options = {
this.subscribers = []; // array of email addresses
this.options = {}; // key=>value pairs used during building
this.transport = null; // future Transport object
this.transport_options = { // options to be passed to the Transport object
host: 'localhost',
port: 25
};
this.transport = null;
// merge passed Object into Newsletter
for (attr in data) {
for (var attr in data) {
if (this[attr]) this[attr] = mergeObjects(this[attr], data[attr]);
else this[attr] = data[attr];
}
}
/* Method: Newsletter.build(file, options, cb)
````````````````````````````````
This method is the first step during the sending of a newsletter. It attempts
to read in the given file name, optionally replacing particular keys in the file
with the variables stored in the options object, and eventually calling cb once
finished.
*/
Newsletter.prototype.build = function(file, options, cb) {
// clear potential old newsletter
this.temp = {};
@ -104,6 +111,13 @@ Newsletter.prototype.build = function(file, options, cb) {
this.temp.text = content;
this.temp.html = content;
};
/* Method: Newsletter.send()
````````````````````````````````
This method is the second step of sending a newsletter. It takes the data built
during the `build` method, as stored in the `temp` property, and attempts to
send it to all the emails stored in the `subscribers` property through the
nodemailer Transport as stored in the `transport` property.
*/
Newsletter.prototype.send = function() {
var i = 0;
var len = this.subscribers.length;
@ -141,6 +155,11 @@ Newsletter.prototype.send = function() {
html: (this.temp.html ? this.temp.html : '')
});
};
/* Method: archive(letter, file)
````````````````````````````````
This method is the third step of sending a newsletter. It simply archives the
given file in the 'archive/' directory of the provided newsletter name.
*/
Newsletter.prototype.archive = function(letter, file) {
fs.writeFileSync('./newsletters/'+letter+'/archive/'+path.basename(file), fs.readFileSync(file));
};
@ -155,7 +174,7 @@ var mergeObjects = function(obj1, obj2) {
else obj3 = {};
for (var attr in obj1) { obj3[attr] = obj1[attr]; }
for (var attr in obj2) { obj3[attr] = obj2[attr]; }
for (attr in obj2) { obj3[attr] = obj2[attr]; }
return obj3;
};
@ -163,9 +182,9 @@ exports.mergeObjects = mergeObjects;
/* Function: sendNewsletter(newsletter object, options, letter, file)
````````````````````````````````
General function that iteratively calls:
build
send
archive
newsletter.build
newsletter.send
newsletter.archive
*/
var sendNewsletter = function(newsletter, options, letter, file) {
newsletter.build(file, options, function(err) {});
@ -183,7 +202,7 @@ if (require.main == module) { // run as an app only if this is the main module
var letter = argv._[0];
var cmd = argv._[1];
var file = argv._[2];
// remove letter, cmd, and file from argv so we just pass argv as the options
delete argv._;
// attempt to load the given letter
@ -195,7 +214,7 @@ if (require.main == module) { // run as an app only if this is the main module
if (nl.archive == Newsletter.prototype.archive) {
console.log('I: Newsletter is using default `archive` step');
}
if (nl.transport == null) {
if (nl.transport === null) {
// create default transport
console.log('I: Newsletter has no custom transport, creating default');
nl.transport = nodemailer.createTransport(nl.transport_options);
@ -215,7 +234,7 @@ if (require.main == module) { // run as an app only if this is the main module
}
break;
default:
console.log('err, unhandled cmd: '+cmd);
console.log('err, unhandled cmd: '+cmd);
break;
}
}