Skip to content

Commit

Permalink
Add lint task and fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kierzniak committed Mar 15, 2018
1 parent 4ea5bdb commit b162d10
Show file tree
Hide file tree
Showing 30 changed files with 2,697 additions and 1,067 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{lib,tasks,test}/**.js]
indent_style = space
indent_size = 2
30 changes: 17 additions & 13 deletions lib/config/config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import path from 'path';
import fs from 'fs';
import path from "path";
import fs from "fs";

import dotenv from 'dotenv';
import config from 'nconf';
import dotenv from "dotenv";
import config from "nconf";

dotenv.config();

config.argv() // Load config from command line
.env({ lowerCase: true, separator: '__' }); // Load config from enviromental variables and dotenv
config
.argv() // Load config from command line
.env({ lowerCase: true, separator: "__" }); // Load config from enviromental variables and dotenv

let defaultConfig = path.resolve( path.dirname(__filename), 'config.json');
let enviromentalConfig = path.resolve( path.dirname(__filename), config.get( 'NODE_ENV' ) + '.json');
let defaultConfig = path.resolve(path.dirname(__filename), "config.json");
let enviromentalConfig = path.resolve(
path.dirname(__filename),
config.get("NODE_ENV") + ".json"
);

// Load enviromental config from file. Enviromental config must be loaded before default config.
if( fs.existsSync(enviromentalConfig) ) {
config.file('enviromental', { file: enviromentalConfig });
if (fs.existsSync(enviromentalConfig)) {
config.file("enviromental", { file: enviromentalConfig });
}

// Load default config from file. Default config must be loaded last to not overwrite enviromental config.
if( fs.existsSync(defaultConfig) ) {
config.file('default', { file: defaultConfig });
if (fs.existsSync(defaultConfig)) {
config.file("default", { file: defaultConfig });
}

export default config;
export default config;
8 changes: 4 additions & 4 deletions lib/config/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from './config.js';
import mongodburl from './mongodb.js';
import redisurl from './redis.js';
import config from "./config.js";
import mongodburl from "./mongodb.js";
import redisurl from "./redis.js";

export { config as default, mongodburl, redisurl };
export { config as default, mongodburl, redisurl };
50 changes: 25 additions & 25 deletions lib/config/mongodb.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import config from 'nconf';
import { URL } from 'url';
import config from "nconf";
import { URL } from "url";

let env, mongodburl;

/**
* Some cloud services provide single environment variable to connect
* Some cloud services provide single environment variable to connect
* mongodb instance. We do not want to hard code this variable so it
* is configurable.
*/
env = config.get('mongodb:env');
env = config.get("mongodb:env");

if( typeof env !== 'undefined' ) {
mongodburl = config.get(env.toLowerCase());
if (typeof env !== "undefined") {
mongodburl = config.get(env.toLowerCase());
}

/**
* If there is no provided environment variable build url with config
*/
if( typeof mongodburl === 'undefined' ) {
mongodburl = new URL('mongodb://localhost');
if (typeof mongodburl === "undefined") {
mongodburl = new URL("mongodb://localhost");

if( typeof config.get('mongodb:host') !== 'undefined' ) {
mongodburl.hostname = config.get('mongodb:host');
}
if (typeof config.get("mongodb:host") !== "undefined") {
mongodburl.hostname = config.get("mongodb:host");
}

if( typeof config.get('mongodb:username') !== 'undefined' ) {
mongodburl.username = config.get('mongodb:username');
}
if (typeof config.get("mongodb:username") !== "undefined") {
mongodburl.username = config.get("mongodb:username");
}

if( typeof config.get('mongodb:password') !== 'undefined' ) {
mongodburl.password = config.get('mongodb:password');
}
if (typeof config.get("mongodb:password") !== "undefined") {
mongodburl.password = config.get("mongodb:password");
}

if( typeof config.get('mongodb:port') !== 'undefined' ) {
mongodburl.port = config.get('mongodb:port');
}
if (typeof config.get("mongodb:port") !== "undefined") {
mongodburl.port = config.get("mongodb:port");
}

if( typeof config.get('mongodb:name') !== 'undefined' ) {
mongodburl.pathname = config.get('mongodb:name');
}
if (typeof config.get("mongodb:name") !== "undefined") {
mongodburl.pathname = config.get("mongodb:name");
}

mongodburl = mongodburl.toString();
mongodburl = mongodburl.toString();
}

export default mongodburl;
export default mongodburl;
44 changes: 22 additions & 22 deletions lib/config/redis.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import config from 'nconf';
import { URL } from 'url';
import config from "nconf";
import { URL } from "url";

let env, redisurl;

/**
* Some cloud services provide single environment variable to connect
* Some cloud services provide single environment variable to connect
* redis instance. We do not want to hard code this variable so it
* is configurable.
*/
env = config.get('redis:env');
env = config.get("redis:env");

if( typeof env !== 'undefined' ) {
redisurl = config.get(env.toLowerCase());
if (typeof env !== "undefined") {
redisurl = config.get(env.toLowerCase());
}

/**
* If there is no provided environment variable build url with config
*/
if( typeof redisurl === 'undefined' ) {
redisurl = new URL('redis://localhost');
if (typeof redisurl === "undefined") {
redisurl = new URL("redis://localhost");

if( typeof config.get('redis:host') !== 'undefined' ) {
redisurl.hostname = config.get('redis:host');
}
if (typeof config.get("redis:host") !== "undefined") {
redisurl.hostname = config.get("redis:host");
}

if( typeof config.get('redis:username') !== 'undefined' ) {
redisurl.username = config.get('redis:username');
}
if (typeof config.get("redis:username") !== "undefined") {
redisurl.username = config.get("redis:username");
}

if( typeof config.get('redis:password') !== 'undefined' ) {
redisurl.password = config.get('redis:password');
}
if (typeof config.get("redis:password") !== "undefined") {
redisurl.password = config.get("redis:password");
}

if( typeof config.get('redis:port') !== 'undefined' ) {
redisurl.port = config.get('redis:port');
}
if (typeof config.get("redis:port") !== "undefined") {
redisurl.port = config.get("redis:port");
}

redisurl = redisurl.toString();
redisurl = redisurl.toString();
}

export default redisurl;
export default redisurl;
2 changes: 1 addition & 1 deletion lib/db/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import './mongodb.js';
import "./mongodb.js";
6 changes: 3 additions & 3 deletions lib/db/mongodb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose from 'mongoose';
import mongoose from "mongoose";

import { mongodburl } from './../config/';
import { mongodburl } from "./../config/";

mongoose.connect(mongodburl);
mongoose.connect(mongodburl);
48 changes: 29 additions & 19 deletions lib/entities/file.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import fs from 'fs';
import fs from "fs";

import readChunk from 'read-chunk';
import fileType from 'file-type';
import readChunk from "read-chunk";
import fileType from "file-type";

/**
* Entity class used for mongoose model
*/
export default class File {

resolveSize() {

let stats = fs.statSync(this.path);

return stats.size;
}

resolveMimeType() {

let buffer = readChunk.sync(this.path, 0, 4100);
let type = fileType(buffer);

return type.mime;
}
}
/**
* Resolve size from file path.
*
* @returns {number} File size.
*/
resolveSize() {
let stats = fs.statSync(this.path);

return stats.size;
}

/**
* Resolve mime type from file chunk.
*
* @returns {string} File mime type.
*/
resolveMimeType() {
let buffer = readChunk.sync(this.path, 0, 4100);
let type = fileType(buffer);

return type.mime;
}
}
7 changes: 5 additions & 2 deletions lib/entities/image.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import File from './file';
import File from "./file";

export default class Image extends File {}
/**
* Entity class used for mongoose model
*/
export default class Image extends File {}
28 changes: 15 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import config from './config/';
import db from './db/';
import config from "./config/";
import "./db/";

import web from './web/';
import { optimize } from './worker/';
import web from "./web/";
import { optimize } from "./worker/";

import logger from 'winston';
import logger from "winston";

let type = config.get('type');
let type = config.get("type");

logger.info(`Starting '${type}' process`, { pid: process.pid })
logger.info(`Starting '${type}' process`, { pid: process.pid });

if (type === 'web') {
web();
} else if (type === 'worker') {
optimize();
if (type === "web") {
web();
} else if (type === "worker") {
optimize();
} else {
throw new Error(`"${type}" is an unsupported process type. Use one of: "web", "worker"!`);
}
throw new Error(
`"${type}" is an unsupported process type. Use one of: "web", "worker"!`
);
}
20 changes: 10 additions & 10 deletions lib/models/image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';
import toJson from '@meanie/mongoose-to-json';
import mongoose from "mongoose";
import toJson from "@meanie/mongoose-to-json";

import Image from './../entities/image';
import Image from "./../entities/image";

const ImageSchema = new mongoose.Schema({
filename: {
Expand All @@ -19,28 +19,28 @@ const ImageSchema = new mongoose.Schema({
path: {
type: String,
required: true,
private: true,
private: true
},
optimized: {
type: Boolean,
required: true,
default: false,
default: false
},
optimized_size: Number,
optimized_path: {
type: String,
private: true,
private: true
},
optimized_url: {
type: String,
type: String
},
callback_url: {
type: String,
private: true,
},
private: true
}
});

ImageSchema.plugin(toJson);
ImageSchema.loadClass(Image);

export default mongoose.model('Image', ImageSchema);
export default mongoose.model("Image", ImageSchema);
Loading

0 comments on commit b162d10

Please sign in to comment.