diff options
Diffstat (limited to 'cli.js')
| -rwxr-xr-x | cli.js | 55 |
1 files changed, 29 insertions, 26 deletions
@@ -3,24 +3,29 @@ const path = require('path'); const fs = require('fs'); const monk = require('monk'); const express = require('express'); + let serverInstance; const requireFromString = require('require-from-string'); const userConfigPath = path.join(process.env.HOME, '.faas.config'); require('dotenv').config({ - path: userConfigPath, + path: userConfigPath }); + let mongoURL = process.env.MONGO_URL; +let db; +let models; const chalk = require('chalk'); const vorpal = require('vorpal')(); + vorpal.delimiter(chalk.blue('faas $')); const showMongoNotSetError = v => { v.log(chalk.red('MongoDB URL is not set or is invalid. The URL is needed to authorize the CLI.')); - v.log(chalk.red('Please use the command "authorize <url>" to set the URL.')); -} + v.log(chalk.red('Please use the command "authorize <url>" to set the URL.')); +}; const checkConnection = async (url, v) => { return new Promise((resolve, reject) => { @@ -43,11 +48,9 @@ const checkConnection = async (url, v) => { reject(err); }); }); -} - -let db, models; +}; -(async function() { +(async function () { try { await checkConnection(mongoURL, vorpal); db = monk(mongoURL); @@ -59,7 +62,7 @@ let db, models; vorpal .command('authorize <url>', 'Set the MongoDB URL to authorize the CLI.') - .action(async function(args, callback) { + .action(async function (args, callback) { if (!args.url) { this.log('Please give the Mongo URL.'); return callback(); @@ -79,7 +82,7 @@ vorpal vorpal .command('create <name>', 'Create new endpoint called "name".') - .action(async function(args, callback) { + .action(async function (args, callback) { try { await checkConnection(mongoURL, this); } catch (err) { @@ -94,7 +97,7 @@ vorpal try { await models.endpoints.insert({ name: args.name, - code: `module.exports = function (req, res) { res.send('Hello ${args.name}!') }`, + code: `module.exports = function (req, res) { res.send('Hello ${args.name}!') }` }); this.log(`Created endpoint ${args.name}.`); } catch (err) { @@ -110,7 +113,7 @@ vorpal vorpal .command('remove <name>', 'Remove endpoint called "name".') - .action(async function(args, callback) { + .action(async function (args, callback) { try { await checkConnection(mongoURL, this); } catch (err) { @@ -124,7 +127,7 @@ vorpal } try { await models.endpoints.remove({ - name: args.name, + name: args.name }); } catch (err) { this.log(chalk.red(`Failed to remove endpoint ${args.name}!`)); @@ -136,7 +139,7 @@ vorpal vorpal .command('list', 'List all endpoints.') - .action(async function(args, callback) { + .action(async function (args, callback) { try { await checkConnection(mongoURL, this); } catch (err) { @@ -147,13 +150,13 @@ vorpal const endpoints = await models.endpoints.find({}); endpoints.forEach(endpoint => { this.log(`${chalk(endpoint.name)}`); - }) + }); callback(); }); vorpal .command('upload <file> <endpoint>', 'Upload "file" to endpoint "endpoint".') - .action(async function(args, callback) { + .action(async function (args, callback) { try { await checkConnection(mongoURL, this); } catch (err) { @@ -166,7 +169,7 @@ vorpal return callback(); } - const existing = await models.endpoints.findOne({ name: args.endpoint }); + const existing = await models.endpoints.findOne({name: args.endpoint}); if (!existing) { this.log(chalk.red(`Endpoint ${args.endpoint} doesn't exist!`)); return callback(); @@ -176,23 +179,23 @@ vorpal this.log(chalk.yellow(`Uploading function from file "${args.file}"...`)); const data = fs.readFileSync(path.join(process.cwd(), args.file), 'utf-8'); await models.endpoints.update( - { name: args.endpoint }, - { code: data, name: args.endpoint }, - { upsert: true }); + {name: args.endpoint}, + {code: data, name: args.endpoint}, + {upsert: true}); this.log(chalk.green(`Uploaded function successfully to endpoint ${args.endpoint}!`)); } catch (err) { this.log(chalk.red(`Failed to upload to endpoint ${args.endpoint}!`)); - this.log(chalk.red(String(err))); + this.log(chalk.red(String(err))); } callback(); }); vorpal .command('start dev <file>', 'Run a function locally for development purposes.') - .action(async function(args, callback) { - if (!args.file ) { - this.log('Please provide the "file" to run.'); - return callback(); + .action(async function (args, callback) { + if (!args.file) { + this.log('Please provide the "file" to run.'); + return callback(); } const code = fs.readFileSync(path.join(process.cwd(), args.file), 'utf-8'); if (serverInstance) { @@ -212,7 +215,7 @@ vorpal vorpal .command('stop dev', 'Stop the development server.') - .action(async function(args, callback) { + .action(async function (args, callback) { if (serverInstance) { serverInstance.close(); this.log(chalk.green('Server stopped.')); @@ -224,7 +227,7 @@ vorpal vorpal .command('info', 'Show information about the session') - .action(async function(args, callback) { + .action(async function (args, callback) { this.log('Mongo URL: ' + chalk.yellow(mongoURL)); callback(); }); |
