<commit_before>'use strict';

var tmp = require('tmp');
var fs = require('fs-extra');
var RSVP = require('rsvp');
var path = require('path');
var fsutils = require('./fsutils');
var utils = require('./utils');
var _ = require('lodash');
var chalk = require('chalk');
var archiver = require('archiver');
var filesize = require('filesize');
var FirebaseError = require('./error');
var fstreamIgnore = require('fstream-ignore');
var api = require('./api');
var logger = require('./logger');
var resolveProjectPath = require('./resolveProjectPath');
var fork = require('child_process').fork;
var getProjectId = require('./getProjectId');

var ENV_SRC_FILE = 'env.json';
var ENV_DEST_FILE = 'env.json';

var _prepareSource = function(options) {
  var tmpdir = tmp.dirSync({prefix: 'fbfn_'});
  try {
    fs.copySync(options.config.path(options.config.get('functions.source')), tmpdir.name);
  } catch (err) {
    throw new FirebaseError('Problem preparing functions directory for upload.', {exit: 1});
  }

  var envPath = resolveProjectPath(options.cwd, ENV_SRC_FILE);
  var envDestPath = path.join(tmpdir.name, ENV_DEST_FILE);
  var env = {};

  if (fsutils.fileExistsSync(envPath)) {
    var existing = fs.readFileSync(envPath, 'utf8');
    try {
      env = JSON.parse(existing);
    } catch (err) {
      logger.debug(err);
      throw new FirebaseError(ENV_SRC_FILE + ' could not be parsed', {exit: 1});
    }
  }

  _.assign(env, {
    firebase: {
      database: {
        url: utils.addSubdomain(api.realtimeOrigin, options.instance)
      }
    }
  });

  fs.ensureFileSync(envDestPath);
  fs.writeFileSync(envDestPath, JSON.stringify(env, null, 2));
  return tmpdir;
};

var TRIGGER_PARSER = path.resolve(__dirname, './triggerParser.js');
var _parseTriggers = function(options, tmpdir) {
  return new RSVP.Promise(function(resolve, reject) {
    var parser = fork(TRIGGER_PARSER, [tmpdir.name], {silent: true, env: {GCLOUD_PROJECT: getProjectId(options)}});

    parser.on('message', function(message) {
      if (message.triggers) {
        resolve(message.triggers);
      } else if (message.error) {
        reject(new FirebaseError(message.error, {exit: 1}));
      }
    });

    parser.on('exit', function(code) {
      if (code !== 0) {
        reject(new FirebaseError('There was an unknown problem while trying to parse function triggers.', {exit: 2}));
      }
    });
  });
};

var _packageSource = function(options, tmpdir) {
  var sourceDir = tmpdir.name;
  return new RSVP.Promise(function(resolve, reject) {
    var tmpFile = tmp.fileSync({prefix: 'firebase-functions-', postfix: '.zip'});

    var fileStream = fs.createWriteStream(tmpFile.name, {
      flags: 'w',
      defaultEncoding: 'binary'
    });

    var archive = archiver('zip');<commit_msg>give functions SDK DB instance name instead of project name (#149)<commit_after>'use strict';

var tmp = require('tmp');
var fs = require('fs-extra');
var RSVP = require('rsvp');
var path = require('path');
var fsutils = require('./fsutils');
var utils = require('./utils');
var _ = require('lodash');
var chalk = require('chalk');
var archiver = require('archiver');
var filesize = require('filesize');
var FirebaseError = require('./error');
var fstreamIgnore = require('fstream-ignore');
var api = require('./api');
var logger = require('./logger');
var resolveProjectPath = require('./resolveProjectPath');
var fork = require('child_process').fork;
var getProjectId = require('./getProjectId');

var ENV_SRC_FILE = 'env.json';
var ENV_DEST_FILE = 'env.json';

var _prepareSource = function(options) {
  var tmpdir = tmp.dirSync({prefix: 'fbfn_'});
  try {
    fs.copySync(options.config.path(options.config.get('functions.source')), tmpdir.name);
  } catch (err) {
    throw new FirebaseError('Problem preparing functions directory for upload.', {exit: 1});
  }

  var envPath = resolveProjectPath(options.cwd, ENV_SRC_FILE);
  var envDestPath = path.join(tmpdir.name, ENV_DEST_FILE);
  var env = {};

  if (fsutils.fileExistsSync(envPath)) {
    var existing = fs.readFileSync(envPath, 'utf8');
    try {
      env = JSON.parse(existing);
    } catch (err) {
      logger.debug(err);
      throw new FirebaseError(ENV_SRC_FILE + ' could not be parsed', {exit: 1});
    }
  }

  _.assign(env, {
    firebase: {
      database: {
        url: utils.addSubdomain(api.realtimeOrigin, options.instance)
      }
    }
  });

  fs.ensureFileSync(envDestPath);
  fs.writeFileSync(envDestPath, JSON.stringify(env, null, 2));
  return tmpdir;
};

var TRIGGER_PARSER = path.resolve(__dirname, './triggerParser.js');
var _parseTriggers = function(options, tmpdir) {
  return new RSVP.Promise(function(resolve, reject) {
    var env = {
      GCLOUD_PROJECT: getProjectId(options),
      DB_NAMESPACE: options.instance
    };
    var parser = fork(TRIGGER_PARSER, [tmpdir.name], {silent: true, env: env});

    parser.on('message', function(message) {
      if (message.triggers) {
        resolve(message.triggers);
      } else if (message.error) {
        reject(new FirebaseError(message.error, {exit: 1}));
      }
    });

    parser.on('exit', function(code) {
      if (code !== 0) {
        reject(new FirebaseError('There was an unknown problem while trying to parse function triggers.', {exit: 2}));
      }
    });
  });
};

var _packageSource = function(options, tmpdir) {
  var sourceDir = tmpdir.name;
  return new RSVP.Promise(function(resolve, reject) {
    var tmpFile = tmp.fileSync({prefix: 'firebase-functions-', postfix: '.zip'});

    var fileStream = fs.createWriteStream(tmpFile.name, {
      flags: 'w',
      defaultEncoding: 'binary'
    });

    var archive = archiver('zip');
{'fraction_non_alphanumeric': 0.15655853314527504, 'fraction_numerical': 0.002997179125528914, 'mean_word_length': 3.8820998278829606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4414041', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1805, 'n_words': 420}