Skip to content

Instantly share code, notes, and snippets.

@joemidi
Created December 10, 2018 23:10
Show Gist options
  • Save joemidi/0e60db5bc30c6e1541b42a54604b4184 to your computer and use it in GitHub Desktop.
Save joemidi/0e60db5bc30c6e1541b42a54604b4184 to your computer and use it in GitHub Desktop.
Gulp Script to flatten Bannertime files into a single directory.
'use strict';
var config = require('../config');
var gulp = require('gulp');
var flatten = require('gulp-flatten');
var del = require('del');
var fs = require('fs');
var path = require('path');
var dir = require('node-dir');
var replace = require('gulp-replace-path');
var rename = require('gulp-rename');
gulp.task('replace-html', function() {
dir.subdirs(config.root.dest , function(err, subdirs) {
if (err) throw err;
var tasks = subdirs.map(function(folder) {
var patt = new RegExp(/[0-9]*x[0-9]*(-flat)$/);
if (patt.test(folder)) {
var parsep = path.basename(folder);
var basePath = path.relative(config.root.dest, path.dirname(folder));
var relativeFilePath = path.join(path.sep, basePath, parsep);
return gulp.src(path.join(folder, '/**/*.html'))
.pipe(replace(/(styles|images|js)\/(.*?(jpe?g|png|gif|css|js))/gi, '$2'))
.pipe(rename({dirname: ''}))
.pipe(gulp.dest(path.join(config.root.dest, relativeFilePath)));
}
});
return tasks;
});
});
gulp.task('replace-js', function() {
dir.subdirs(config.root.dest , function(err, subdirs) {
if (err) throw err;
var tasks = subdirs.map(function(folder) {
var patt = new RegExp(/[0-9]*x[0-9]*(-flat)$/);
if (patt.test(folder)) {
var parsep = path.basename(folder);
var basePath = path.relative(config.root.dest, path.dirname(folder));
var relativeFilePath = path.join(path.sep, basePath, parsep);
return gulp.src(path.join(folder, '/**/js/*.js'))
.pipe(replace(/(images)\/(.*?(jpe?g|png|gif))/gi, '$2'))
.pipe(rename({dirname: ''}))
.pipe(gulp.dest(path.join(config.root.dest, relativeFilePath)));
}
});
return tasks;
});
});
gulp.task('replace-css', function() {
dir.subdirs(config.root.dest , function(err, subdirs) {
if (err) throw err;
var tasks = subdirs.map(function(folder) {
var patt = new RegExp(/[0-9]*x[0-9]*(-flat)$/);
if (patt.test(folder)) {
var parsep = path.basename(folder);
var basePath = path.relative(config.root.dest, path.dirname(folder));
var relativeFilePath = path.join(path.sep, basePath, parsep);
return gulp.src(path.join(folder, '/**/styles/*.css'))
.pipe(replace(/(\.\.\/images)\/(.*(jpe?g|png|gif))/gi, '$2'))
.pipe(rename({dirname: ''}))
.pipe(gulp.dest(path.join(config.root.dest, relativeFilePath)));
}
});
return tasks;
});
});
gulp.task('flatten', ['replace-css', 'replace-js', 'replace-html'], function() {
dir.subdirs(config.root.dest , function(err, subdirs) {
if (err) throw err;
var tasks = subdirs.map(function(folder) {
var patt = new RegExp(/[0-9]*x[0-9]*(-flat)$/);
if (patt.test(folder)) {
var parsep = path.basename(folder);
var basePath = path.relative(config.root.dest, path.dirname(folder));
var relativeFilePath = path.join(path.sep, basePath, parsep);
return gulp.src(path.join(folder, '/**/images/*'))
.pipe(flatten())
.pipe(gulp.dest(path.join(config.root.dest, relativeFilePath)))
.on('end', function(){
del([
path.join(config.root.dest, relativeFilePath, 'images'),
path.join(config.root.dest, relativeFilePath, 'styles'),
path.join(config.root.dest, relativeFilePath, 'js'),
])
});
}
});
return tasks;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment