From 95f21922bc86bdf847083f381cf58312af18bf46 Mon Sep 17 00:00:00 2001 From: Michael Bleigh Date: Mon, 28 Sep 2015 14:19:25 -0700 Subject: [PATCH] Allow for Bolt rules parsing. --- commands/deploy.js | 4 ++++ lib/loadRules.js | 10 +++++++++- lib/parseBoltRules.js | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lib/parseBoltRules.js diff --git a/commands/deploy.js b/commands/deploy.js index 4815f0fb..5928e0aa 100644 --- a/commands/deploy.js +++ b/commands/deploy.js @@ -50,6 +50,10 @@ module.exports = new Command('deploy') return prepareUpload(options, config); }).then(function(upload) { + if (!upload.manifest.length) { + return utils.reject('Must have at least one file in public directory to deploy.', {exit: 1}); + } + payload.hosting = { version: versionId, prefix: versionId + '/', diff --git a/lib/loadRules.js b/lib/loadRules.js index e49c4c0a..0578521d 100644 --- a/lib/loadRules.js +++ b/lib/loadRules.js @@ -4,8 +4,10 @@ var FirebaseError = require('./error'); var detectProjectRoot = require('./detectProjectRoot'); var cjson = require('cjson'); var loadConfig = require('./loadConfig'); +var parseBoltRules = require('./parseBoltRules'); var resolveProjectPath = require('./resolveProjectPath'); var chalk = require('chalk'); +var path = require('path'); module.exports = function(options) { var config = loadConfig(options); @@ -18,7 +20,13 @@ module.exports = function(options) { var pd = detectProjectRoot(options.cwd); if (pd) { try { - var rules = cjson.load(resolveProjectPath(options.cwd, config.rules)); + var rulesPath = resolveProjectPath(pd, config.rules); + + if (path.extname(rulesPath) === '.bolt') { + return parseBoltRules(rulesPath); + } + + var rules = cjson.load(rulesPath); return rules; } catch (e) { if (e.code === 'ENOENT') { diff --git a/lib/parseBoltRules.js b/lib/parseBoltRules.js new file mode 100644 index 00000000..f1698b32 --- /dev/null +++ b/lib/parseBoltRules.js @@ -0,0 +1,26 @@ +'use strict'; + +var fs = require('fs'); +var spawnSync = require('child_process').spawnSync; +var FirebaseError = require('./error'); +var chalk = require('chalk'); + +module.exports = function(filename) { + var ruleSrc = fs.readFileSync(filename); + + var result = spawnSync('firebase-bolt', { + input: ruleSrc, + timeout: 10000, + encoding: 'utf-8' + }); + + if (result.error && result.error.code === 'ENOENT') { + throw new FirebaseError('Bolt not installed, run ' + chalk.bold('npm install -g firebase-bolt'), {exit: 1}); + } else if (result.error) { + throw new FirebaseError('Unexpected error parsing Bolt rules file', {exit: 2}); + } else if (result.status > 0) { + throw new FirebaseError(result.stderr, {exit: 1}); + } + + return JSON.parse(result.stdout); +};