Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linting #105

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": ["plugin:prettier/recommended"],
"env": {
"node": true,
"es6": true,
"browser": true
},
"rules": {
"no-unused-vars": "off",
"semi": "error",
"no-console": "off",
"quote-props": ["error", "always"]
},
"parserOptions": {
"ecmaVersion": 2021
}
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Project
.idea/
node_modules/
67 changes: 38 additions & 29 deletions bin/mockserver.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
#!/usr/bin/env node

var http = require('http');
var mockserver = require('./../mockserver');
var argv = require('yargs').argv;
var colors = require('colors')
var info = require('./../package.json');
var mocks = argv.m || argv.mocks;
var port = argv.p || argv.port;
var verbose = !(argv.q || argv.quiet);
const http = require('http');
const mockserver = require('./../mockserver');
const argv = require('yargs').argv;
const colors = require('colors');
const info = require('./../package.json');
const mocks = argv.m || argv.mocks;
const port = argv.p || argv.port;
const verbose = !(argv.q || argv.quiet);

if (!mocks || !port) {
console.log([
"Mockserver v" + info.version,
"",
"Usage:",
" mockserver [-q] -p PORT -m PATH",
"",
"Options:",
" -p, --port=PORT - Port to listen on",
" -m, --mocks=PATH - Path to mock files",
" -q, --quiet - Do not output anything",
"",
"Example:",
" mockserver -p 8080 -m './mocks'"
].join("\n"));
console.log(
[
'Mockserver v' + info.version,
'',
'Usage:',
' mockserver [-q] -p PORT -m PATH',
'',
'Options:',
' -p, --port=PORT - Port to listen on',
' -m, --mocks=PATH - Path to mock files',
' -q, --quiet - Do not output anything',
'',
'Example:',
" mockserver -p 8080 -m './mocks'"
].join('\n')
);
} else {
http.createServer(mockserver(mocks, verbose)).listen(port);
http.createServer(mockserver(mocks, verbose)).listen(port);

if (verbose) {
console.log('Mockserver serving mocks {'
+ 'verbose'.yellow + ':' + (verbose && 'true'.green || 'false')
+ '} under "' + mocks.green + '" at '
+ 'http://localhost:'.green + port.toString().green);
}
if (verbose) {
console.log(
'Mockserver serving mocks {' +
'verbose'.yellow +
':' +
((verbose && 'true'.green) || 'false') +
'} under "' +
mocks.green +
'" at ' +
'http://localhost:'.green +
port.toString().green
);
}
}
14 changes: 7 additions & 7 deletions handlers/evalHandler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = function evalHandler(value, request) {
if (!/^#eval/m.test(value)) return value;
return value
.replace(/^#eval (.*);/m, function (statement, val) {
return eval(val);
})
.replace(/\r\n?/g, '\n');
}
if (!/^#eval/m.test(value)) return value;
return value
.replace(/^#eval (.*);/m, function (statement, val) {
return eval(val);
})
.replace(/\r\n?/g, '\n');
};
16 changes: 8 additions & 8 deletions handlers/headerHandler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = function headerHandler(value, request) {
if (!/^#header/m.test(value)) return value;
return value
.replace(/^#header (.*);/m, function (statement, val) {
const expression = val.replace(/[${}]/g, '');
return eval(expression);
})
.replace(/\r\n?/g, '\n');
}
if (!/^#header/m.test(value)) return value;
return value
.replace(/^#header (.*);/m, function (statement, val) {
const expression = val.replace(/[${}]/g, '');
return eval(expression);
})
.replace(/\r\n?/g, '\n');
};
26 changes: 13 additions & 13 deletions handlers/importHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ const fs = require('fs');
const path = require('path');

module.exports = function importHandler(value, context, request) {
if (!/^#import/m.test(value)) return value;
if (!/^#import/m.test(value)) return value;

return value
.replace(/^#import (.*);/m, function (includeStatement, file) {
const importThisFile = file.replace(/['"]/g, '');
const content = fs.readFileSync(path.join(context, importThisFile));
if (importThisFile.endsWith('.js')) {
return JSON.stringify(eval(content.toString()));
} else {
return content;
}
})
.replace(/\r\n?/g, '\n');
}
return value
.replace(/^#import (.*);/m, function (includeStatement, file) {
const importThisFile = file.replace(/['"]/g, '');
const content = fs.readFileSync(path.join(context, importThisFile));
if (importThisFile.endsWith('.js')) {
return JSON.stringify(eval(content.toString()));
} else {
return content;
}
})
.replace(/\r\n?/g, '\n');
};
Loading