-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (46 loc) · 1.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const mandrill = require('mandrill-api/mandrill');
const through = require('through2');
const gutil = require('gulp-util');
const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities();
const path = require('path');
module.exports = function (options) {
const client = new mandrill.Mandrill(options.key);
return through.obj(function(file, encoding, next) {
const code = file.contents.toString();
const subject = /<title>(.*?)<\/title>/.test(code)
? entities.decode(code.match(/<title>(.*?)<\/title>/)[1])
: '';
const name = path.parse(file.path).name;
client.templates.info(
{ name: name },
function (info) {
if (info.code == code) {
gutil.log(gutil.colors.cyan("[skip]"), name);
next(null, file);
} else {
client.templates.update(
{ name: name, code: code, publish: true, subject: subject },
function (result) {
gutil.log(gutil.colors.green("[update]"), file.stem);
next(null, file);
}
);
}
},
function (error) {
if (error.name === 'Unknown_Template') {
client.templates.add(
{ name: name, code: code, publish: true, subject: subject },
function () {
gutil.log(gutil.colors.green("[add]"), name);
next(null, file);
}
);
} else {
throw error;
}
}
);
});
};