Alexander
Alexander

Reputation: 449

How to create strcuture folder with gulp

I'd like to create my initial structure folders in web project using gulp, Are there is any package that help me to make this task ?

Example:

-css
-img
--content
--icons
-fonts
-js

with one command in bash create that strcuture

Upvotes: 16

Views: 17396

Answers (3)

Jakob E
Jakob E

Reputation: 4936

You can use fs (Node.js - File System)

const gulp = require('gulp');
const fs   = require('fs');


gulp.task('default', () => {

    const folders = [
        'css',
        'img',
        'img/content',
        'img/icons',
        'fonts',
        'js'
    ];

    folders.forEach(dir => {
        if(!fs.existsSync(dir)) {
            fs.mkdirSync(dir);
            console.log('📁  folder created:', dir);    
        }   
    });
});

Upvotes: 11

Ariel Łowczynski
Ariel Łowczynski

Reputation: 341

Gulp can create structure folders without any package with this trick:

gulp.task('directories', function () {
    return gulp.src('*.*', {read: false})
        .pipe(gulp.dest('./css'))
        .pipe(gulp.dest('./img'))
        .pipe(gulp.dest('./img/content'))
        .pipe(gulp.dest('./img/icons'))
        .pipe(gulp.dest('./fonts'))
        .pipe(gulp.dest('./js'));
});

Upvotes: 21

Mark
Mark

Reputation: 2071

Well the cmd to create directories is mkdir

So you could create a gulp task using gulp-shell to create your folder structure.


EDIT for comment

  gulp.task('directory', function () {
    return gulp.src('*.js', {read: false})
    .pipe(shell([
      'mkdir -p  css img img/content img/icons fonts js'
    ]));
  });

This will output

css
img
| - content
| - icons
fonts
js

  gulp.task('directory', function () {
    return gulp.src('*.js', {read: false})
    .pipe(shell([
      'mkdir -css -img --content --icons -fonts -js'
    ]));
  });

Upvotes: 0

Related Questions