Diego de Oliveira
Diego de Oliveira

Reputation: 681

Gulp - build files with custom folders

I'm creating a workflow to build custom e-mail templates. In my folder, I have this structure:

folder/
    templates/
        template-001.html
        image-001.jpg
    images/
        image-default.jpg
    src/
        style.scss

I'm using gulp-inline-css to get the compile css file and insert all the styles inline in the file template-001.html. Then, the task gets all these files and put inside a build folder. Like this:

folder/
    templates/
        template-001.html
        image-001.jpg
    images/
        image-default.jpg
    src/
        style.scss
    build/
        template-001.html
        image-default.jpg
        image-001.jpg

This is ok for an individual project. But I want to be able to build several templates and organize the files the proper way. I would like to have a folder structure like this:

folder/
    templates/
        template-001/
            template-001.html
            image-001.jpg
    images/
        image-default.jpg
    src/
        style.scss
    build/
        template-001/
            template-001.html
            image-default.jpg
            image-001.jpg

In other words, I want to be able to get the folder of the template-001.html and reproduce it in the build folder. With my current setup, I don't know how to make it possible.

Here's my gulpfile.js:

var gulp = require( 'gulp' ),
        sass = require( 'gulp-sass' ),
        autoprefixer = require( 'gulp-autoprefixer' ),
        minifycss = require( 'gulp-minify-css' ),
        imagemin = require( 'gulp-imagemin' ),
        rename = require( 'gulp-rename' ),
        concat = require( 'gulp-concat' ),
        plumber = require( 'gulp-plumber' ),
        notify = require( 'gulp-notify' ),
        inlineCss = require('gulp-inline-css'),
        projectTitle = 'E-mail Templates';

// styles task
gulp.task( 'styles', function() {
    return gulp.src( 'src/*.scss' )
        .pipe( plumber() )
        .pipe( sass({ paths: ['src/'] }) )
        .pipe( notify( {
        title: projectTitle,
        message: 'File: <%= file.relative %> was compiled!'
        } ) )
        .pipe( autoprefixer( 'last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4' ) )
        .pipe( gulp.dest( 'src/' ) )
        .pipe( notify( {
        title: projectTitle,
        message: 'Minified file: <%= file.relative %> was created / updated!'
        } ) )

        console.log( '<%= file %>' );
} );

// styles task
gulp.task( 'html-build', function() {
    return gulp.src( 'templates/**/*.html' )
        .pipe( inlineCss({
                applyStyleTags: false,
                applyLinkTags: true,
                removeStyleTags: false,
                removeLinkTags: true
        }) )
        .pipe( gulp.dest('build/') )
        .pipe( notify( {
        title: projectTitle,
        message: 'Template File: <%= file.relative %> was created / updated!'
        } ) )
} );

// images task
gulp.task( 'images', function() {
    return gulp.src( 'images/**/*' )
        .pipe( plumber() )
        .pipe( imagemin({ optimizationLevel: 7, progressive: true, interlaced: true }) )
        .pipe( gulp.dest( 'build/' ) )
        .pipe( notify( {
        title: projectTitle,
        message: 'Image: <%= file.relative %> was optimized!'
        } ) )
} );

// watch task
gulp.task( 'watch', [ 'styles' ], function() {

    // Watch .scss files
    gulp.watch( 'src/**/*.scss', [ 'styles', 'html-build' ] );

    // Watch .html files
    gulp.watch( 'templates/**/*.html', [ 'html-build' ] );

    // Watch image files
    gulp.watch('images/**/*', ['images'] );

});

Any tips or advices about hot to make it possible? Thanks!

Upvotes: 2

Views: 6573

Answers (1)

ddprrt
ddprrt

Reputation: 7574

So, you want to copy all files from the template folder which aren't HTML to the dest folder?

gulp.task('copy', function() {
    return gulp.src(['templates/**/*','!templates/**/*.html'])
        .pipe(gulp.dest('build'));
});

This will take this:

folder/
  templates/
    template-001/
      template-001.html
      image-001.jpg
    template-002/
      template-002.html
      image-002.jpg
    template-003/
      template-003.html
      image-003.jpg

and create this:

folder/
  build/
    template-001/
      image-001.jpg
    template-002/
      image-002.jpg
    template-003/
      image-003.jpg

This should do the trick and work with your other tasks. When I interpreted your Gulpfile correctly, the HTML files should already be in place. Do you also need help in copying the files in images?

Update

I reworked the images task. Please read the comments to know what's going on:

var fs = require('fs');
gulp.task('images', function(done) {
    // get all the directories inside templates. this will return
    // an array of folders
    var templates = fs.readdirSync('templates').filter(function(el) {
        return fs.statSync('templates/' + el).isDirectory()
    });

    // we start our stream with all the files from images we want to have
    var stream = gulp.src( 'images/**/*' )
        .pipe(plumber())
        .pipe(imagemin({ optimizationLevel: 7, progressive: true, interlaced: true }));

    // now we add a destination dir for each one of our
    // templates
    templates.forEach(function(el) {
        stream.pipe(gulp.dest('templates/' + el));
    });

    // we add the rest of our pipes
    stream.pipe(notify({
        title: projectTitle,
        message: 'Image: <%= file.relative %> was optimized!'
    }));

    // returning the stream kicks it off
    return stream;
});

Upvotes: 1

Related Questions