2016-11-04 7 views
0

パイプで収集しているyamlデータに基づいて特定のファイルのみを書き出すgulpの方法を見つけようとしています。私はファイルのデータを見ることができましたが、期待した出力を得ることができませんでした。以前のパイプからのデータに基づくGulpコントロールの出力

このタスクでは、マークアップファイルのグロブを収集し、gulp-dataを使用してyamlを読み取り、それに他のデータを追加するパイプに渡します。私はそれをSwigを通してパイプします。

gulp.destにパイプする前に、何らかの条件要素を追加しようとしています。私はこれが私が現在どこにいるのかわかったexampleを見つけました。

私が得ている最も近い

は以下の通りです:私はにconsole.logコマンドから得ている何

.pipe(tap(function(file) { 
    if (new Date(file.data.date) >= new Date(buildRange)) { 
     console.log(file.path); 
     gulp.src(file.path) 
     .pipe(gulp.dest(config.paths.dest + '/underreview/')); 
    } 
    })) 

は(それが2 50のファイルを示した)正しいです。しかし、何も宛先に書き込まれません。 gulp.destをこのパイプの外に移動すると、すべてのファイルが書き込まれます。

gulp-ifまたはgulp-ignoreを使用しようとしましたが、どちらのモジュールにもfile.data.dateを取得できませんでした。


編集:私は移動

.pipe(gulp.dest(config.paths.dest + '/underreview/')) 
     .pipe(tap(function(file) { 
     if (new Date(file.data.date) < new Date(buildRange)) { 
      console.log(file.data.path); 
      del(config.paths.dest + '/underreview/' + file.data.path) 
     } 
     })) 

:ここでは、おそらく完全なタスクだから、

module.exports = function(gulp, config, env) { 


var gulpSwig = require('gulp-swig'), 
    swig = require('swig'), 
    data = require('gulp-data'), 
    matter = require('gray-matter'), 
    runSequence = require('run-sequence'), 
    // BrowserSync 
    reload = config.browserSync.reload, 
    _ = require('lodash'), 
    Path = require('path'), 
    requireDir = require('require-dir'), 
    marked = require('marked'), 
    readingTime = require('reading-time'), 
    postsData = [], 
    postsTags = [], 
    pdate = null, 
    buildRange = new Date(new Date().setDate(new Date().getDate()-14)); 
    sitebuilddate = null, 
    through = require('through2'), 
    gutil = require('gulp-util'), 
    rename = require('gulp-rename'), 
    File = require('vinyl'), 
    $if = require('gulp-if'), 
    ignore = require('gulp-ignore'), 
    tap = require('gulp-tap'); 

    var opts = { 
    defaults: { 
     cache: false 
    }, 
    setup: function(Swig) { 
     Swig.setDefaults({ 
     loader: Swig.loaders.fs(config.paths.source + '/templates')}); 
    } 
    }; 

    // Full of the compiled HTML file 
    function targetPathFull(path, data) { 
    return Path.join(Path.dirname(path), targetPath(data)); 
    } 

gulp.task('templates2:under', function() { 
    return gulp.src(config.paths.source + '/content/**/*.md') 
     .pipe(data(function(file) { 
     postData = []; 
     var matterObject = matter(String(file.contents)), // extract front matter data 
      type = matterObject.data.type, // page type 
      body = matterObject.content, 
      postData = matterObject.data, 
      moreData = requireDir(config.paths.data), 
      data = {}, 
      bodySwig; 

     bodySwig = swig.compile(body, opts); 
     // Use swig to render partials first 
     body = bodySwig(data); 
     // Process markdown 
     if (Path.extname(file.path) === '.md') { 
      body = marked(body); 
     } 
     // Inherit the correct template based on type 
     if (type) { 
      var compiled = _.template(
      "{% extends 'pages/${type}.html' %}{% block body %}${body}{% endblock %}" 
      // Always use longform until a different template for different types is needed 
      //"{% extends 'pages/longform.html' %}{% block body %}${body}{% endblock %}" 
     ); 
      body = compiled({ 
      "type": type, 
      "body": body 
      }); 
     } 

     file.path = targetPathFull(file.path, postData); 
     moreData.path = targetPath(postData); 
     _.merge(data, postData, moreData); 

     data.url = data.site.domain + "/" + data.slug; 
     // Copy the processed body text back into the file object so Gulp can keep piping 
     file.contents = new Buffer(body); 

     return data; 
     })) 
     .pipe(gulpSwig(opts)) 
     .pipe(tap(function(file) { 
     if (new Date(file.data.date) >= new Date(buildRange)) { 
      console.log(file.path); 
      gulp.src(file.path) 
      .pipe(gulp.dest(config.paths.dest + '/underreview/')); 
     } 
     })) 
     .pipe(gulp.dest(config.paths.dest + '/underreview/')); 
    }); 
} 
+0

[mcve]を提供した場合、これはおそらく簡単に答えるでしょう。 –

+0

@SvenSchoenung私は完全な仕事を投稿しました。新しい唯一の部分は私が求めている部分です。私は過去8ヶ月間にわたって夜間に他のすべてのコードを作成してきました。 – AGarrett

答えて

0

ではなく、最善の解決策ですが、いくつかのリファクタリング後、私はこの思い付きましたgulp-tapを押して出力してから、書き込んだファイルを削除しています。

関連する問題