2017-03-24 1 views
0

Comment obtenir le nom du dossier parent en utilisant gulp-data? Actuellement, je suis en utilisant les éléments suivants:Chemin de raccordement avec Gulp-data

Dans mon affaire devant

--- 
title: 'some title' 
---- 

de mon dossier engouffreur:

function fm2json() { 
    return gulp.src('src/pages/**/*.html') 
    .pipe(require('gulp-gray-matter')()) 
    .pipe($.data(function(file){ 
     file.data.relative = file.relative, 
     file.data.basename = file.basename, 
    })) 
    .pipe($.pluck('data', 'new.json')) 
    .pipe($.data(function(file){ 
     file.contents = new Buffer(JSON.stringify(file.data)) 
    })) 
    .pipe(require('gulp-json-format')(2)) 
    .pipe(gulp.dest('src/data')); 
} 

qui produit les éléments suivants à new.json

[ 
    { 
    "title":"some title" 
    "relative":"lesson01\\file.html" 
    "basename":"file.html" 
    }, 
    { 
    "title":"some title 2" 
    "relative":"lesson02\\file2.html" 
    "basename":"file2.html" 
    } 
] 

Je ne peux pas comprendre comment obtenir le dossier parent du fichier de sorte que relative serait "relative":"lesson01" et "relative":"lesson02".

Répondre

0

Ce n'est pas la manière la plus efficace de le faire. Si ça aide quelqu'un c'est ce que j'ai fini avec.

function fm2json() { 
    return gulp.src('src/pages/**/*.html') 
    .pipe(require('gulp-gray-matter')()) 
    .pipe($.data(function(file){ 

     // What I ended up with 
     var relpath = file.relative; 
     var path = relpath.replace(/\\/g,"/"); //flip slashes 
     var split = path.split('/'); //split the path into an array 
     var parent = split[split.length - 2]; // find the array position 

     file.data.parent = parent, 
     file.data.file = file.basename, 
     file.data.path = path, 

    })) 
    .pipe($.pluck('data', 'new.json')) 
    .pipe($.data(function(file){ 
     file.contents = new Buffer(JSON.stringify(file.data)) 
    })) 
    .pipe(require('gulp-json-format')(2)) 
    .pipe(gulp.dest('src/data')); 
}