From 1a3d08bc7e511bec8b817d64104a6e45d0f50f2d Mon Sep 17 00:00:00 2001 From: Felipe Roucher Iglesias Date: Thu, 6 Aug 2020 00:09:24 +0200 Subject: [PATCH] fix: get git diff all at once Issue #263 --- lib/cli/changeset.js | 38 +++++++++++++++++++++++--------------- lib/diff.js | 23 ++++++++--------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/lib/cli/changeset.js b/lib/cli/changeset.js index 57d23342..39141f54 100755 --- a/lib/cli/changeset.js +++ b/lib/cli/changeset.js @@ -11,7 +11,8 @@ var Manifest = require('../manifest'); var path = require('path'); var fs = require('fs-extra'); var vinylFs = require('vinyl-fs'); -var mergeStream = require('merge-stream'); +var miss = require('mississippi'); +var Readable = require('stream').Readable; var doc = "Usage:\n" + " force-dev-tool changeset create [...] [options]\n" + @@ -71,20 +72,27 @@ SubCommand.prototype.process = function(proc, callback) { } }); - var stdin = proc.stdin - .pipe(Diff.stream({ - ignoreWhitespace: !!self.opts['--ignore-whitespace'] - })) - .pipe(MetadataContainer.diffStream()) + var diff = {}; // first get the stdin stream with all git diff changes + proc.stdin.pipe(miss.concat(function(result) { + diff = Diff.parse(result, { ignoreWhitespace: !!self.opts['--ignore-whitespace'] }); + })); - mergeStream(stdin, metadataContainer.getStream()) - .pipe(MetadataContainer.completeMetadataStream()) - .pipe(MetadataContainer.outputStream({ - apiVersion: apiVersion - })) - .pipe(vinylFs.dest(deploymentPath)) - .on('end', function() { - return callback(null, "exported metadata container to " + path.relative(proc.cwd, deploymentPath)); - }); + miss.finished(proc.stdin, function(err) { + if (err) return console.trace('stream had an error or closed early', err); + + var th = new Readable({ objectMode: true }); // new stream with git diff changes + th.push(diff); + th.push(null); // indicates end-of-file basically - the end of the stream + + th.pipe(MetadataContainer.diffStream()) + .pipe(MetadataContainer.completeMetadataStream()) + .pipe(MetadataContainer.outputStream({ + apiVersion: apiVersion + })) + .pipe(vinylFs.dest(deploymentPath)) + .on('end', function() { + return callback(null, "exported metadata container to " + path.relative(proc.cwd, deploymentPath)); + }); + }); }; diff --git a/lib/diff.js b/lib/diff.js index 3932699a..04c3e081 100755 --- a/lib/diff.js +++ b/lib/diff.js @@ -6,7 +6,6 @@ var MetadataContainer = require('./metadata-container'); var Git = require('./git'); var Utils = require('./utils'); var path = require('path'); -var miss = require('mississippi'); var Diff = module.exports = function(data) { this.data = data; @@ -56,16 +55,12 @@ Diff.prototype.getMetadataContainers = function(opts) { }; }; -Diff.stream = function(opts) { +Diff.parse = function(diff, opts) { var git = new Git(process.cwd()); var unpackagedPath = path.join(process.cwd(), 'src'); opts = opts || {}; - - return miss.through.obj(function(diff, enc, cb) { - var files = parseDiff(diff.toString()); - - if (files.length <= 0) return cb(); - + var files = parseDiff(diff.toString()); + if (files.length >= 0) { var containerFrom = new MetadataContainer(); var containerTo = new MetadataContainer(); files.forEach(function(file) { @@ -94,14 +89,12 @@ Diff.stream = function(opts) { } containerFrom.add(fileFrom, []); containerTo.add(fileTo, []); - } }); + } - cb(null, { - source: containerFrom, - target: containerTo - }); - - }); + return { + source: containerFrom, + target: containerTo + }; }