1const fs = require('fs') 2 3class FeatureWriterStream { 4 constructor (file) { 5 this.file = file 6 this.stream = fs.createWriteStream(file) 7 this.stream.write('{"type":"FeatureCollection","features":[') 8 this.numFeatures = 0 9 } 10 11 add (stringifiedFeature) { 12 if (this.numFeatures > 0) { 13 this.stream.write(',') 14 } 15 this.stream.write(stringifiedFeature) 16 this.numFeatures++ 17 } 18 19 end (cb) { 20 console.log(`Closing out file ${this.file}`) 21 this.stream.end(']}', cb) 22 } 23} 24 25module.exports = FeatureWriterStream 26