Skip to content

Commit

Permalink
Merge f207915 into 5bf2194
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenzlong committed Jan 3, 2018
2 parents 5bf2194 + f207915 commit 091a965
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/functionsEmulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ FunctionsEmulator.prototype.start = function(shellMode) {
var controller = this.controller;

utils.logBullet(chalk.cyan.bold('functions:') + ' Preparing to emulate functions.');
logger.debug('Fetching environmenmt');
logger.debug('Fetching environment');
ensureDefaultCredentials();
return functionsConfig.getFirebaseConfig(projectId, options.instance)
.then(function(result) {
Expand Down
3 changes: 2 additions & 1 deletion lib/parseTriggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ var RSVP = require('rsvp');

var TRIGGER_PARSER = path.resolve(__dirname, './triggerParser.js');

module.exports = function(projectId, sourceDir, firebaseConfig) {
module.exports = function(projectId, sourceDir, configValues, firebaseConfig) {
return new RSVP.Promise(function(resolve, reject) {
var env = {
CLOUD_RUNTIME_CONFIG: JSON.stringify(configValues),
GCLOUD_PROJECT: projectId
};
if (firebaseConfig) {
Expand Down
42 changes: 10 additions & 32 deletions lib/prepareFunctionsUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,7 @@ var logger = require('./logger');
var utils = require('./utils');
var parseTriggers = require('./parseTriggers');

var CONFIG_DEST_FILE = '.runtimeconfig.json';

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

var _getFunctionsConfig = function(context) {
var next = RSVP.resolve({});
if (context.runtimeConfigEnabled) {
next = functionsConfig.materializeAll(context.firebaseConfig.projectId).catch(function(err) {
Expand All @@ -46,14 +35,11 @@ var _prepareSource = function(context, options) {
return next.then(function(config) {
var firebaseConfig = _.get(context, 'firebaseConfig');
_.set(config, 'firebase', firebaseConfig);
fs.ensureFileSync(configDest);
fs.writeFileSync(configDest, JSON.stringify(config, null, 2));
return tmpdir;
return config;
});
};

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

Expand Down Expand Up @@ -89,8 +75,6 @@ var _packageSource = function(options, tmpdir) {
// you're in the public dir when you deploy
reader.addIgnoreRules(['firebase-debug.log']);
reader.addIgnoreRules(options.config.get('functions.ignore', ['**/node_modules/**']));
// We want to always upload the env file regardless of ignore rules
reader.addIgnoreRules(['!' + CONFIG_DEST_FILE]);

reader.on('child', function(file) {
if (file.type !== 'Directory') {
Expand All @@ -106,29 +90,23 @@ var _packageSource = function(options, tmpdir) {
});

reader.on('end', function() {
archive.append(JSON.stringify(configValues), { name: '.runtimeconfig.json' });
archive.finalize();
try {
fs.removeSync(tmpdir.name);
} catch (e) {
utils.logWarning('Unable to delete temporary directory ' + tmpdir.name +
'. You may want to manually delete it to save disk space.');
logger.debug(e);
}
});
});
};

module.exports = function(context, options) {
var tmpdir;
return _prepareSource(context, options).then(function(result) {
tmpdir = result;

return parseTriggers(getProjectId(options), tmpdir.name);
var configValues;
var sourceDir = options.config.path(options.config.get('functions.source'));
return _getFunctionsConfig(context).then(function(result) {
configValues = result;
return parseTriggers(getProjectId(options), sourceDir, configValues);
}).then(function(triggers) {
options.config.set('functions.triggers', triggers);
if (options.config.get('functions.triggers').length === 0) {
return RSVP.resolve(null);
}
return _packageSource(options, tmpdir);
return _packageSource(options, sourceDir, configValues);
});
};
2 changes: 2 additions & 0 deletions lib/triggerParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var EXIT = function() { process.exit(0); };
if (e.code === 'MODULE_NOT_FOUND') {
process.send({error: 'Error parsing triggers: ' + e.message + '\n\nTry running "npm install" in your functions directory before deploying.'}, EXIT);
return;
} else if (/Firebase config variables are not available/.test(e.message)) {
process.send({error: 'Error occurred while parsing your function triggers. Please ensure you have the latest firebase-functions SDK by running "npm i --save firebase-functions@latest" inside your functions folder.\n\n' + e.stack}, EXIT);
}

process.send({error: 'Error occurred while parsing your function triggers.\n\n' + e.stack}, EXIT);
Expand Down

0 comments on commit 091a965

Please sign in to comment.