
const fs = require('fs');
const path = require('path');

// handler for all metatdata streams
module.exports.MetaDataFiles = function MetaDataFiles( metaDir ){
  let streams = {};
  this.stats = {};
  this.write = function( row ){
    let keys = Object.keys(row);

    // first time writing to this meta file
    if( !streams.hasOwnProperty( row.placetype ) ){

      // create write stream
      streams[row.placetype] = fs.createWriteStream(
        path.join( metaDir, `wof-${row.placetype}-latest.csv` )
      );

      // init stats
      this.stats[row.placetype] = 0;

      // write csv header
      streams[row.placetype].write( keys.join(',') + '\n' );
    }

    // write csv row
    streams[row.placetype].write( keys.map(key => {
      // quote fields containing comma or newline, escape internal quotes
      // https://gist.github.com/getify/3667624
      if( /[,\n]/.test( row[key] ) ) {
        return '"' + row[key].replace(/\\([\s\S])|(")/g,'\\$1$2') + '"';
      }
      return row[key];
    }).join(',') + '\n' );

    // increment stats
    this.stats[row.placetype]++;
  };
};


const fs = require('fs');
const path = require('path');

// handler for all metatdata streams
module.exports.MetaDataFiles = function MetaDataFiles( metaDir ){
  let streams = {};
  this.stats = {};
  this.write = function( row ){
    let keys = Object.keys(row);

    // first time writing to this meta file
    if( !streams.hasOwnProperty( row.placetype ) ){

      // create write stream
      streams[row.placetype] = fs.createWriteStream(
        path.join( metaDir, `whosonfirst-data-${row.placetype}-latest.csv` )
      );

      // init stats
      this.stats[row.placetype] = 0;

      // write csv header
      streams[row.placetype].write( keys.join(',') + '\n' );
    }

    // write csv row
    streams[row.placetype].write( keys.map(key => {
      // quote fields containing comma or newline, escape internal quotes
      // https://gist.github.com/getify/3667624
      if( /[,\n]/.test( row[key] ) ) {
        return '"' + row[key].replace(/\\([\s\S])|(")/g,'\\$1$2') + '"';
      }
      return row[key];
    }).join(',') + '\n' );

    // increment stats
    this.stats[row.placetype]++;
  };
};

Update metadata filename when writing
fix(sqlite): Update metadata filename when writing

After https://github.com/pelias/whosonfirst/pull/389 and
https://github.com/pelias/wof-admin-lookup/pull/229, WOF metafiles are
expected to follow the pattern `whosonfirst-data-${layer}-latest.csv`. We
missed the code that creates fake metafiles after exporting from SQLite.
