-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
112 lines (92 loc) · 3.15 KB
/
gulpfile.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var rename = require('gulp-rename');
var cryptojs = require('crypto-js');
var marked = require('marked');
var FileSystem = require('fs');
var through = require('through2');
var PluginError = gutil.PluginError;
/*
START FIREWALL TASKS
*/
function checkEncryptedLayout(frontMatter, filepath) {
var lines = frontMatter.split('\n'),
linesWithoutLayout = [],
hasEncryptedLayout = false;
lines.forEach(function(line) {
var layoutTag = 'layout:',
isLayoutIndex = line.indexOf(layoutTag),
isLayout = isLayoutIndex >= 0,
isEncryptedLayout = line.indexOf('encrypted') >= (isLayoutIndex + layoutTag.length);
if (isLayout) {
// in case of multiple instances of layout
hasEncryptedLayout = isEncryptedLayout ? true : false;
}
});
if (!hasEncryptedLayout) {
console.log('[WARNING] ' + filepath + ': protected file not using encrypted layout.');
}
// var linesWithLayout = linesWithoutLayout
// .splice(0, 1)
// .concat('layout: encrypted')
// .concat(linesWithoutLayout);
// var frontMatterWithEncryptedLayout = linesWithLayout.join('\n');
// return frontMatterWithEncryptedLayout;
}
function encrypt(password) {
return through.obj(function(file, encoding, callback) {
if (file.isNull() || file.isDirectory()) {
this.push(file);
return callback();
}
// No support for streams
if (file.isStream()) {
this.emit('error', new PluginError({
plugin: 'Encrypt',
message: 'Streams are not supported.'
}));
return callback();
}
if (file.isBuffer()) {
var delimiter = '---',
chunks = String(file.contents).split(delimiter),
originalBody = chunks[0],
frontMatter = '';
if (chunks.length === 3) {
checkEncryptedLayout(chunks[1], file.path);
frontMatter = chunks[1];
originalBody = chunks[2];
} else if (chunks.length > 1) {
this.emit('error', new PluginError({
plugin: 'Encrypt',
message: file.path + ': protected file has invalid front matter.'
}));
return callback();
}
var encryptedBody = cryptojs.AES.encrypt(marked(originalBody), password),
hmac = cryptojs.HmacSHA256(encryptedBody.toString(), cryptojs.SHA256(password).toString()).toString(),
encryptedFrontMatter = 'encrypted: ' + hmac + encryptedBody,
result = [ delimiter, frontMatter, '\n', encryptedFrontMatter, '\n', delimiter ];
file.contents = new Buffer(result.join(''));
this.push(file);
return callback();
}
});
}
gulp.task('firewall:encrypt', () => {
return gulp.src('_protected/*.*')
.pipe(encrypt('supernodesecurities'))
.pipe(rename({extname: '.md.priv'}))
.pipe(gulp.dest('_posts'));
});
gulp.task('firewall:watch', () => {
gulp.watch('_protected/*.*', gulp.series('firewall:encrypt'));
});
gulp.task('firewall', gulp.series('firewall:encrypt', 'firewall:watch',() => {}));
/*
END FIREWALL TASKS
*/
gulp.task('default', gulp.series('firewall', () => {
// your tasks here
}));