Fixed minor code issues caught by jslint. Added more code comments to app.js.
parent
3c1a783bca
commit
01458f1e2e
47
app.js
47
app.js
|
@ -39,7 +39,7 @@ IMPORTANT NOTE:
|
||||||
such as using git or subversion repositories.
|
such as using git or subversion repositories.
|
||||||
|
|
||||||
In general, newsletters are composed of a directory in the newsletters/
|
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.
|
inheriting from this Newsletter object.
|
||||||
|
|
||||||
The Newsletter object has the following properties:
|
The Newsletter object has the following properties:
|
||||||
|
@ -77,21 +77,28 @@ These are:
|
||||||
|
|
||||||
*/
|
*/
|
||||||
function Newsletter(data) {
|
function Newsletter(data) {
|
||||||
this.temp = {};
|
this.temp = {}; // temporary Letter object (html, text, subject)
|
||||||
this.repo = {};
|
this.repo = {};
|
||||||
this.subscribers = [];
|
this.subscribers = []; // array of email addresses
|
||||||
this.options = {};
|
this.options = {}; // key=>value pairs used during building
|
||||||
this.transport_options = {
|
this.transport = null; // future Transport object
|
||||||
|
this.transport_options = { // options to be passed to the Transport object
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
port: 25
|
port: 25
|
||||||
};
|
};
|
||||||
this.transport = null;
|
|
||||||
// merge passed Object into Newsletter
|
// merge passed Object into Newsletter
|
||||||
for (attr in data) {
|
for (var attr in data) {
|
||||||
if (this[attr]) this[attr] = mergeObjects(this[attr], data[attr]);
|
if (this[attr]) this[attr] = mergeObjects(this[attr], data[attr]);
|
||||||
else 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) {
|
Newsletter.prototype.build = function(file, options, cb) {
|
||||||
// clear potential old newsletter
|
// clear potential old newsletter
|
||||||
this.temp = {};
|
this.temp = {};
|
||||||
|
@ -104,6 +111,13 @@ Newsletter.prototype.build = function(file, options, cb) {
|
||||||
this.temp.text = content;
|
this.temp.text = content;
|
||||||
this.temp.html = 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() {
|
Newsletter.prototype.send = function() {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var len = this.subscribers.length;
|
var len = this.subscribers.length;
|
||||||
|
@ -141,6 +155,11 @@ Newsletter.prototype.send = function() {
|
||||||
html: (this.temp.html ? this.temp.html : '')
|
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) {
|
Newsletter.prototype.archive = function(letter, file) {
|
||||||
fs.writeFileSync('./newsletters/'+letter+'/archive/'+path.basename(file), fs.readFileSync(file));
|
fs.writeFileSync('./newsletters/'+letter+'/archive/'+path.basename(file), fs.readFileSync(file));
|
||||||
};
|
};
|
||||||
|
@ -155,7 +174,7 @@ var mergeObjects = function(obj1, obj2) {
|
||||||
else obj3 = {};
|
else obj3 = {};
|
||||||
|
|
||||||
for (var attr in obj1) { obj3[attr] = obj1[attr]; }
|
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;
|
return obj3;
|
||||||
};
|
};
|
||||||
|
@ -163,9 +182,9 @@ exports.mergeObjects = mergeObjects;
|
||||||
/* Function: sendNewsletter(newsletter object, options, letter, file)
|
/* Function: sendNewsletter(newsletter object, options, letter, file)
|
||||||
````````````````````````````````
|
````````````````````````````````
|
||||||
General function that iteratively calls:
|
General function that iteratively calls:
|
||||||
build
|
newsletter.build
|
||||||
send
|
newsletter.send
|
||||||
archive
|
newsletter.archive
|
||||||
*/
|
*/
|
||||||
var sendNewsletter = function(newsletter, options, letter, file) {
|
var sendNewsletter = function(newsletter, options, letter, file) {
|
||||||
newsletter.build(file, options, function(err) {});
|
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 letter = argv._[0];
|
||||||
var cmd = argv._[1];
|
var cmd = argv._[1];
|
||||||
var file = argv._[2];
|
var file = argv._[2];
|
||||||
|
|
||||||
// remove letter, cmd, and file from argv so we just pass argv as the options
|
// remove letter, cmd, and file from argv so we just pass argv as the options
|
||||||
delete argv._;
|
delete argv._;
|
||||||
// attempt to load the given letter
|
// 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) {
|
if (nl.archive == Newsletter.prototype.archive) {
|
||||||
console.log('I: Newsletter is using default `archive` step');
|
console.log('I: Newsletter is using default `archive` step');
|
||||||
}
|
}
|
||||||
if (nl.transport == null) {
|
if (nl.transport === null) {
|
||||||
// create default transport
|
// create default transport
|
||||||
console.log('I: Newsletter has no custom transport, creating default');
|
console.log('I: Newsletter has no custom transport, creating default');
|
||||||
nl.transport = nodemailer.createTransport(nl.transport_options);
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log('err, unhandled cmd: '+cmd);
|
console.log('err, unhandled cmd: '+cmd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue