ゴール
現在、Gulp タスクで簡単に使用できるNPM Flatの Gulp ラッパーを作成しようとしています。これは Node コミュニティに役立ち、私の目標も達成できると思います。リポジトリは、誰もが閲覧、投稿、プレイ、プルリクエストできるようになっています。複数の JSON ファイルのフラット化された (ドット表記を使用して) コピーを作成しようとしています。次に、それらを同じフォルダーにコピーし、ファイル拡張子を *.json から *.flat.json に変更します。
私の問題
JSON ファイルに返される結果は、ビニール ファイルまたはバイト コードのように見えます。たとえば、次のような出力が期待さ
れますが、 ...など "views.login.usernamepassword.login.text": "Login"
のような結果が得られます{"0":123,"1":13,"2":10,"3":9,"4":34,"5":100,"6":105
私のアプローチ
私は Gulp タスクとノード モジュールの開発にまったく慣れていないので、根本的に間違っていることに注意してください。
リポジトリは最新のコードになりますが、質問も最新の状態に保つように努めます。
Gulp タスク ファイル
var gulp = require('gulp'),
plugins = require('gulp-load-plugins')({camelize: true});
var gulpFlat = require('gulp-flat');
var gulpRename = require('gulp-rename');
var flatten = require('flat');
gulp.task('language:file:flatten', function () {
return gulp.src(gulp.files.lang_file_src)
.pipe(gulpFlat())
.pipe(gulpRename( function (path){
path.extname = '.flat.json'
}))
.pipe(gulp.dest("App/Languages"));
});
ノード モジュールの index.js (別名、gulp-flat になることを望んでいます)
var through = require('through2');
var gutil = require('gulp-util');
var flatten = require('flat');
var PluginError = gutil.PluginError;
// consts
const PLUGIN_NAME = 'gulp-flat';
// plugin level function (dealing with files)
function flattenGulp() {
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
if (file.isBuffer()) {
//FIXME: I believe this is the problem line!!
var flatJSON = new Buffer(JSON.stringify(
flatten(file.contents)));
file.contents = flatJSON;
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported! NYI'));
return cb();
}
// make sure the file goes through the next gulp plugin
this.push(file);
// tell the stream engine that we are done with this file
cb();
});
// returning the file stream
return stream;
}
// exporting the plugin main function
module.exports = flattenGulp;