Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to gulpfile #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 17 additions & 60 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,30 @@
'use strict';

var gulp = require('gulp');
var gutil = require('gulp-util');

var cryptojs = require('crypto-js');
var marked = require('marked');
var FileSystem = require('fs');
var through = require('through2');
var PluginError = gutil.PluginError;
const gulp = require('gulp');
const cryptojs = require('crypto-js');
const marked = require('marked');
const through = require('through2');

/*
START FIREWALL TASKS
*/
function checkEncryptedLayout(frontMatter, filepath) {
var lines = frontMatter.split('\n'),
linesWithoutLayout = [],
hasEncryptedLayout = false;
let lines = frontMatter.split('\n'), hasEncryptedLayout = false;

lines.forEach(function(line) {
var layoutTag = 'layout:',
lines.forEach(function (line) {
const 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 (isLayout)hasEncryptedLayout = isEncryptedLayout;
});

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) {
return through.obj(function (file, encoding, callback) {
if (file.isNull() || file.isDirectory()) {
this.push(file);
return callback();
Expand All @@ -52,59 +33,35 @@ function encrypt(password) {
// No support for streams
if (file.isStream()) {
this.emit('error', new PluginError({
plugin: 'Encrypt',
message: 'Streams are not supported.'
plugin: 'Encrypt', message: 'Streams are not supported.'
}));
return callback();
}

if (file.isBuffer()) {
var delimiter = '---',
chunks = String(file.contents).split(delimiter),
originalBody = chunks[0],
let delimiter = '---\n', 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),
const encryptedBody = cryptojs.AES.encrypt(marked.parse(originalBody), password),
hmac = cryptojs.HmacSHA256(encryptedBody.toString(), cryptojs.SHA256(password).toString()).toString(),
encryptedFrontMatter = 'encrypted: ' + hmac + encryptedBody,
result = [ delimiter, frontMatter, '\n', encryptedFrontMatter, '\n', delimiter ];
result = [delimiter, frontMatter, '\n', encryptedFrontMatter, '\n', delimiter];

file.contents = new Buffer(result.join(''));
file.contents = new Buffer.from(result.join(''));
this.push(file);
return callback();
}
});
}

gulp.task('firewall:encrypt', () => {
gulp.task('default', gulp.series(done => {
return gulp.src('_protected/*.*')
.pipe(encrypt('password'))
.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
.pipe(encrypt('password'))
.pipe(gulp.dest('_posts'));
}));