Skip to content

Commit

Permalink
improve: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
daywiss committed Aug 24, 2020
1 parent 3bb54eb commit cc980dc
Show file tree
Hide file tree
Showing 12 changed files with 1,490 additions and 346 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
"env": {
"node": true,
"commonjs": true,
"es2020": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
}
};
61 changes: 29 additions & 32 deletions bin/openservice.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@
#!/usr/bin/env node

require('dotenv').config()
const {parseEnv} = require('../libs/parseEnv')
const args = require('minimist')(process.argv.slice(2))
const assert = require('assert')
const lodash = require('lodash')
const App = require('../libs/app')
const Path = require('path')
const openEnv = require('openenv')

const paths = args._ || []
require("dotenv").config();
const args = require("minimist")(process.argv.slice(2));
const lodash = require("lodash");
const App = require("../libs/app");
const Path = require("path");
const openEnv = require("openenv");

const paths = args._ || [];
// reference for env regex filtering
// const isLower = '^[a-z0-9]'

let config = lodash.reduce(
paths,
(result, fn) => {
const path = Path.resolve(process.cwd(),fn)
return lodash.merge(result,require(path))
const path = Path.resolve(process.cwd(), fn);
return lodash.merge(result, require(path));
},
{}
)
);

const env = openEnv(process.env,{regex:process.env.envRegex})
const env = openEnv(process.env, { regex: process.env.envRegex });

config = lodash.merge(config, env)
config = lodash.merge(config, env);

App(config)
.then(x => {
console.log(config.name, 'Online')
.then(() => {
console.log(config.name, "Online");
})
.catch(e => {
console.log('Error Loading', config.name, e)
process.exit(1)
})

process.on('unhandledRejection', function(err, promise) {
console.log(err)
process.exit(1)
})

process.on('uncaughtException', function(err) {
console.log(err.stack)
process.exit(1)
})

.catch((e) => {
console.log("Error Loading", config.name, e);
process.exit(1);
});

process.on("unhandledRejection", function (err, promise) {
console.log(err, promise);
process.exit(1);
});

process.on("uncaughtException", function (err) {
console.log(err.stack);
process.exit(1);
});
146 changes: 63 additions & 83 deletions libs/utils.js
Original file line number Diff line number Diff line change
@@ -1,144 +1,124 @@
const lodash = require('lodash')
const assert = require('assert')
const lodash = require("lodash");
const assert = require("assert");

exports.ONE_MINUTE_MS = 60 * 1000
exports.ONE_HOUR_MS = 60 * exports.ONE_MINUTE_MS
exports.ONE_DAY_MAS = 24 * exports.ONE_HOUR_MS
exports.ONE_MINUTE_MS = 60 * 1000;
exports.ONE_HOUR_MS = 60 * exports.ONE_MINUTE_MS;
exports.ONE_DAY_MAS = 24 * exports.ONE_HOUR_MS;

exports.objectFilter = (filter = {}) => obj => {
exports.objectFilter = (filter = {}) => (obj) => {
return lodash.reduce(
filter,
(result, value, key) => {
//did the filter pass
if (result === false) return false
if (result === false) return false;
//cast filter params to an array
value = lodash.castArray(value)
value = lodash.castArray(value);
//check the object for the key
const check = lodash.get(obj, key)
const check = lodash.get(obj, key);

if (lodash.isArray(check)) {
if (check.length === 0 && value.length === 0) return true
if (check.length === 0 && value.length === 0) return true;
//this is saying if we specified an empty array to filter
//only match if the event has an empty array
// console.log('filter',filter,'check',check,'key',key,'value',value)
if (value.length === 0) return false
if (value.length === 0) return false;
// return lodash.intersection(check, value).length > 0
// check each value in the path to ensure complete match.
return value.reduce((result, val, i) => {
if (val == check[i]) return result
return false
}, true)
if (val == check[i]) return result;
return false;
}, true);
} else {
return value.includes(check)
return value.includes(check);
}
},
true
)
}
);
};

exports.timeout = (promise, delay, message) => {
return Promise.race([
promise,
new Promise((res, rej) => setTimeout(x => rej(message), delay)),
])
}
new Promise((res, rej) => setTimeout(() => rej(message), delay)),
]);
};

exports.loopWhile = async (promise, delay, ...args) => {
const result = await promise(...args)
if (!result) return
await new Promise((resolve, reject) => setTimeout(resolve, delay))
return exports.loopWhile(promise, delay, ...args)
}
const result = await promise(...args);
if (!result) return;
await new Promise((resolve) => setTimeout(resolve, delay));
return exports.loopWhile(promise, delay, ...args);
};
exports.delay = (delay = 0) => {
return new Promise(resolve => setTimeout(resolve, delay))
}
return new Promise((resolve) => setTimeout(resolve, delay));
};

exports.loop = async (fn, delay = 1000, max, count = 0, result) => {
assert(lodash.isFunction(fn), 'loop requires a function')
if (max && count >= max) return result
result = await fn(count)
await new Promise(res => setTimeout(res, delay))
return exports.loop(fn, delay, max, count + 1, result)
}
assert(lodash.isFunction(fn), "loop requires a function");
if (max && count >= max) return result;
result = await fn(count);
await new Promise((res) => setTimeout(res, delay));
return exports.loop(fn, delay, max, count + 1, result);
};

exports.difference = (oldState, newState) => {
return lodash.omitBy(newState, function(v, k) {
return lodash.isEqual(oldState[k], v)
})
}
return lodash.omitBy(newState, function (v, k) {
return lodash.isEqual(oldState[k], v);
});
};

exports.flattenJson = (json = {}, path = [], result = []) => {
if (!lodash.isObject(json) || lodash.isArray(json)) {
return result.push(path.join('.') + '=' + json)
return result.push(path.join(".") + "=" + json);
}

lodash.each(json, (value, key) => {
exports.flattenJson(value, path.concat(key), result)
})
exports.flattenJson(value, path.concat(key), result);
});

return result
}
return result;
};

exports.CleanStack = (ignore = []) => (stack, start = 0, end) => {
return stack
.split('\n')
.split("\n")
.slice(start, end)
.filter(line => {
.filter((line) => {
return ignore.reduce((result, regex) => {
return result && !regex.test(line)
}, true)
return result && !regex.test(line);
}, true);
})
.join('\n')
}
.join("\n");
};

const cleanStack = exports.CleanStack([
/node_modules/,
/streamify/,
/calls\.js/,
/client\.js/,
/mock\.js/,
/\<anonymous\>/,
/\internal\/process/,
])
/<anonymous>/,
/internal\/process/,
]);

exports.cleanStack = cleanStack
exports.cleanStack = cleanStack;

exports.parseError = err => {
assert(err.message, 'requires error message')
exports.parseError = (err) => {
assert(err.message, "requires error message");
try {
return {
message: err.message,
code: err.code || null,
stack: cleanStack(err.stack || ''),
}
stack: cleanStack(err.stack || ""),
};
} catch (e) {
console.log(e)
console.log(e);
}
}
};

exports.RelativePath = mypath => path => {
exports.RelativePath = (mypath) => (path) => {
return path.reduce((result, part, i) => {
if (result.length > 0 || mypath[i] !== part) result.push(part)
return result
}, [])
}


exports.paginate = function(collection = [], page = 1, numItems = 100) {
assert(collection, 'the provided collection is not an array!')
if (lodash.isObject(collection)) {
collection = lodash.values(collection)
}
var currentPage = parseInt(page)
var perPage = parseInt(numItems)
var offset = (page - 1) * perPage
var paginatedItems = collection.slice(offset, offset + perPage)

return {
currentPage: currentPage,
perPage: perPage,
total: collection.length,
totalPages: Math.ceil(collection.length / perPage),
data: paginatedItems,
}
}
if (result.length > 0 || mypath[i] !== part) result.push(part);
return result;
}, []);
};
24 changes: 12 additions & 12 deletions libs/validate.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const Validator = require('fastest-validator')
const Validator = require("fastest-validator");

module.exports = schema => {
const v = new Validator()
var check = v.compile(schema)
module.exports = (schema) => {
const v = new Validator();
var check = v.compile(schema);

return function(data) {
const result = check(data)
if (result === true) return data
return function (data) {
const result = check(data);
if (result === true) return data;
const message = result
.map(x => x.message || 'Validation Failure')
.join('\n')
throw new Error(message)
.map((x) => x.message || "Validation Failure")
.join("\n");
throw new Error(message);

// const [first] = result
// console.log(first.message)
// throw new Error('validation error')
// throw new Error(first.message)
}
}
};
};
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
"bin": {
"openservice": "./bin/openservice.js"
},
"scripts": {
"lint": "prettier **/*.js --write && eslint --fix **/*.js"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"libs/**/*.js": [
"eslint --ext .js",
"prettier --write",
"git add"
]
},
"bugs": {
"url": "https://github.com/daywiss/openservice/issues"
},
Expand All @@ -28,6 +43,10 @@
"cqrs"
],
"devDependencies": {
"eslint": "^7.7.0",
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
"tape": "^4.10.2"
},
"dependencies": {
Expand Down
Loading

0 comments on commit cc980dc

Please sign in to comment.