portfolio/gulpfile.js

167 lines
4.7 KiB
JavaScript

var gulp = require('gulp');
var sass = require('gulp-sass');
var babel = require('gulp-babel');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var rollup = require('rollup-stream');
var sourcemaps = require('gulp-sourcemaps');
var browserSync = require('browser-sync');
var jsonld = require('jsonld');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var exec = require('child_process').exec;
var through = require('through2')
// todo: rollup for d3 & possibly jsonld
const server = browserSync.create();
function reload(done) {
server.reload();
done();
}
var paths = {
"styles": {
"src": "./src/scss/*.scss",
"dest": "./assets/css/"
},
"scripts": {
"src": "./src/js/portfolio.js",
"dest": "./assets/js/",
"d3src": "./src/js/d3.bundle.js",
"d3destDir": "./assets/js/"
},
"data": {
"src": "./rubenvandeven.jsonld",
"dest": "./assets/js/"
}
};
gulp.task('styles', function(done) {
// console.log(done);
return gulp.src(paths.styles.src, { sourcemaps: true })
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(paths.styles.dest))
.pipe(server.reload({ stream: true }));
});
gulp.task('scripts', function() {
return gulp.src(paths.scripts.src)
.pipe(sourcemaps.init())
.pipe(babel({
ignore: [
'./src/js/d3.v5.js',
]
}))
.pipe(concat('portfolio.js'))
.pipe(gulp.dest(paths.scripts.dest)) // save .js
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
// .pipe(sourcemaps.write('maps'))
.pipe(sourcemaps.write(''))
.pipe(gulp.dest(paths.scripts.d3destDir)) // save .min.js
// .pipe(server.reload())
});
gulp.task('d3', function() {
return rollup( 'rollup.config.js' )
.pipe(source('d3.bundle.js'))
.pipe(gulp.dest(paths.scripts.d3destDir)) // save .js
.pipe(buffer())
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(sourcemaps.write('maps'))
.pipe(gulp.dest(paths.scripts.d3destDir)) // save .min.js
});
function createHtaccessFromFlattenedJsonld(flattened) {
let contentLines = [
"# Don't edit: generated using gulpfile.js",
"RewriteEngine On"
];
let typeLines = {}; // prevent duplicates
for(let el of flattened['@graph']) {
try{
let url = new URL(el['@id']);
if(url.origin != 'https://rubenvandeven.com') {
console.error("Invalid url in id", el['@id']);
continue;
}
if(url.pathname == '' || url.pathname == '/') continue;
let pathname = url.pathname.substr(1); // remove preceding slash ("/ruben" -> "ruben")
contentLines.push(`RewriteRule "^${pathname}$" "index.html"`);
let type = el['@type'].replace(/.*[#|\/]/, "");
typeLines[type] = `RewriteRule "^@type/${type}$" "index.html"`;
} catch(err) {
// not an url
}
}
require('fs').writeFileSync('.htaccess', contentLines.join("\n")+"\n"+Object.values(typeLines).join("\n") + "\n");
}
function createPagerankFromFlattenedJsonld(done){
exec('ranking/venv/bin/python ranking/jsonld_ranking.py -i assets/js/rubenvandeven.jsonld -o assets/js/ranking.json', function (err, stdout, stderr) {
if(stdout)
console.log(stdout);
if(stderr)
console.error(stderr);
done(err);
});
}
gulp.task('flattenJsonld', function() {
return gulp.src(paths.data.src)
.pipe(through.obj(function (file, enc, callback) {
let data = JSON.parse(file.contents.toString(enc));
var transformedFile = file.clone();
// console.log(data);
jsonld.flatten(data, {}, (err, flattened)=> {
if(err) {
console.error(err);
}
transformedFile.contents = Buffer.from(JSON.stringify(flattened), enc);
createHtaccessFromFlattenedJsonld(flattened);
callback(null, transformedFile)
});
}))
.pipe(gulp.dest(paths.data.dest))
});
gulp.task('jsonld', gulp.series(
'flattenJsonld',
createPagerankFromFlattenedJsonld
));
var watchStylesAndScripts = function() {
gulp.watch(paths.styles.src, gulp.series('styles'));
gulp.watch(paths.scripts.src,gulp.series('scripts', reload));
gulp.watch(paths.scripts.d3src,gulp.series('d3', reload));
gulp.watch([paths.data.src], gulp.series('jsonld', reload));
}
gulp.task('watch', watchStylesAndScripts);
// watch files for changes and reload
gulp.task('serve', function() {
server.init({
server: {
baseDir: '.'
}
});
gulp.watch(['index.html','rubenvandeven.jsonld'], reload);
watchStylesAndScripts();
});
gulp.task('default', function() {
gulp.parallel('styles', 'scripts');
});