aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@eficode.com>2018-07-12 14:20:28 +0300
committerJan Tuomi <jan.tuomi@eficode.com>2018-07-12 14:20:28 +0300
commit0f28ca26c3a0b8b4d4e15f742be7ef4ec40c590e (patch)
tree492378645eb9a4c4d20a4fc192a26fe4fd7e99a9
parentd4a8d80bc2c7b60b26e1e62e8287257972c2febd (diff)
Fix xo offenses
-rwxr-xr-xcli.js55
-rw-r--r--index.js15
-rw-r--r--models.js6
3 files changed, 40 insertions, 36 deletions
diff --git a/cli.js b/cli.js
index 6319e4c..6cb56b9 100755
--- a/cli.js
+++ b/cli.js
@@ -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();
});
diff --git a/index.js b/index.js
index 6804fbe..2c7a373 100644
--- a/index.js
+++ b/index.js
@@ -1,19 +1,20 @@
require('dotenv').config();
-const chalk = require('chalk');
const path = require('path');
+const chalk = require('chalk');
if (!process.env.MONGO_URL) {
throw new Error('No MONGO_URL set in .env!');
}
const db = require('monk')(process.env.MONGO_URL);
-const models = require('./models')(db);
const express = require('express');
+
const app = express();
const requireFromString = require('require-from-string');
+const models = require('./models')(db);
-app.get('/', function(req, res) {
- res.sendFile(path.join(__dirname + '/frontpage.html'));
+app.get('/', (req, res) => {
+ res.sendFile(path.join(__dirname, '/frontpage.html'));
});
app.all('/:path*', async (req, res) => {
@@ -23,17 +24,17 @@ app.all('/:path*', async (req, res) => {
res.send('Empty endpoint.');
}
- const endpoint = await models.endpoints.findOne({ name: path });
+ const endpoint = await models.endpoints.findOne({name: path});
if (!endpoint) {
res.status(400);
res.send('No such endpoint.');
}
console.info(`${chalk.green(req.path)}, running endpoint "${chalk.yellow(endpoint.name)}".`);
- const code = endpoint.code;
+ const {code} = endpoint;
try {
const func = requireFromString(code);
- const output = func(req, res);
+ func(req, res);
} catch (err) {
res.status(500);
console.error(chalk.red(`Error in endpoint ${endpoint.name}! Details below.`));
diff --git a/models.js b/models.js
index 343d1f6..cf5137d 100644
--- a/models.js
+++ b/models.js
@@ -1,8 +1,8 @@
module.exports = db => {
const endpoints = db.get('endpoints');
- endpoints.createIndex({ name: 1 }, { unique: true });
+ endpoints.createIndex({name: 1}, {unique: true});
return {
- endpoints,
+ endpoints
};
-}
+};