mirror of
https://github.com/Lissy93/dashy.git
synced 2024-11-23 04:34:44 +03:00
Fixed lint errors caused by octal character, and added missing yaml dependency
This commit is contained in:
parent
a6ecb29734
commit
dbc2fae6d6
@ -18,6 +18,7 @@
|
|||||||
"connect": "^3.7.0",
|
"connect": "^3.7.0",
|
||||||
"crypto-js": "^4.0.0",
|
"crypto-js": "^4.0.0",
|
||||||
"highlight.js": "^11.0.0",
|
"highlight.js": "^11.0.0",
|
||||||
|
"js-yaml": "^4.1.0",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"prismjs": "^1.23.0",
|
"prismjs": "^1.23.0",
|
||||||
"register-service-worker": "^1.6.2",
|
"register-service-worker": "^1.6.2",
|
||||||
|
40
server.js
40
server.js
@ -1,3 +1,5 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
|
/* This is a simple Node.js http server, that is used to serve up the contents of ./dist */
|
||||||
const connect = require('connect');
|
const connect = require('connect');
|
||||||
const serveStatic = require('serve-static');
|
const serveStatic = require('serve-static');
|
||||||
|
|
||||||
@ -12,28 +14,19 @@ const isDocker = !!process.env.IS_DOCKER;
|
|||||||
/* Checks env var for port. If undefined, will use Port 80 for Docker, or 4000 for metal */
|
/* Checks env var for port. If undefined, will use Port 80 for Docker, or 4000 for metal */
|
||||||
const port = process.env.PORT || isDocker ? 80 : 4000;
|
const port = process.env.PORT || isDocker ? 80 : 4000;
|
||||||
|
|
||||||
/* eslint no-console: 0 */
|
|
||||||
const printWelcomeMessage = () => {
|
|
||||||
getLocalIp().then(({ address }) => {
|
|
||||||
const ip = address || 'localhost';
|
|
||||||
console.log(overComplicatedMessage(ip, port));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const getLocalIp = () => {
|
const getLocalIp = () => {
|
||||||
const dnsLookup = util.promisify(dns.lookup);
|
const dnsLookup = util.promisify(dns.lookup);
|
||||||
return dnsLookup(os.hostname());
|
return dnsLookup(os.hostname());
|
||||||
}
|
};
|
||||||
|
|
||||||
const overComplicatedMessage = (ip, port) => {
|
const overComplicatedMessage = (ip) => {
|
||||||
let msg = '';
|
let msg = '';
|
||||||
const chars = {
|
const chars = {
|
||||||
RESET: '\x1b[0m',
|
RESET: '\x1b[0m',
|
||||||
CYAN: '\x1b[36m',
|
CYAN: '\x1b[36m',
|
||||||
GREEN: '\x1b[32m',
|
GREEN: '\x1b[32m',
|
||||||
BLUE: '\x1b[34m',
|
BLUE: '\x1b[34m',
|
||||||
UNDERLINE: '\033[4m',
|
BRIGHT: '\x1b[1m',
|
||||||
BOLD: '\033[1m',
|
|
||||||
BR: '\n',
|
BR: '\n',
|
||||||
};
|
};
|
||||||
const stars = (count) => new Array(count).fill('*').join('');
|
const stars = (count) => new Array(count).fill('*').join('');
|
||||||
@ -42,32 +35,39 @@ const overComplicatedMessage = (ip, port) => {
|
|||||||
if (isDocker) {
|
if (isDocker) {
|
||||||
const containerId = process.env.HOSTNAME || undefined;
|
const containerId = process.env.HOSTNAME || undefined;
|
||||||
msg = `${chars.BLUE}${stars(91)}${chars.BR}${chars.RESET}`
|
msg = `${chars.BLUE}${stars(91)}${chars.BR}${chars.RESET}`
|
||||||
+ `${chars.CYAN}${chars.BOLD}Welcome to Dashy! 🚀${chars.RESET}${chars.BR}`
|
+ `${chars.CYAN}Welcome to Dashy! 🚀${chars.RESET}${chars.BR}`
|
||||||
+ `${chars.GREEN}Your new dashboard is now up and running `
|
+ `${chars.GREEN}Your new dashboard is now up and running `
|
||||||
+ `${containerId ? `in container ID ${containerId}` : 'with Docker'}${chars.BR}`
|
+ `${containerId ? `in container ID ${containerId}` : 'with Docker'}${chars.BR}`
|
||||||
+ `${chars.GREEN}After updating your config file, run `
|
+ `${chars.GREEN}After updating your config file, run `
|
||||||
+ `'${chars.UNDERLINE}docker exec -it ${containerId || '[container-id]'} yarn build`
|
+ `'${chars.BRIGHT}docker exec -it ${containerId || '[container-id]'} yarn build`
|
||||||
+ `${chars.RESET}${chars.GREEN}' to rebuild${chars.BR}`
|
+ `${chars.RESET}${chars.GREEN}' to rebuild${chars.BR}`
|
||||||
+ `${chars.BLUE}${stars(91)}${chars.BR}${chars.RESET}`;
|
+ `${chars.BLUE}${stars(91)}${chars.BR}${chars.RESET}`;
|
||||||
} else {
|
} else {
|
||||||
msg = `${chars.GREEN}┏${line(75)}┓${chars.BR}`
|
msg = `${chars.GREEN}┏${line(75)}┓${chars.BR}`
|
||||||
+ `┃ ${chars.CYAN}${chars.BOLD}Welcome to Dashy! 🚀${blanks(55)}${chars.GREEN}┃${chars.BR}`
|
+ `┃ ${chars.CYAN}Welcome to Dashy! 🚀${blanks(55)}${chars.GREEN}┃${chars.BR}`
|
||||||
+ `┃ ${chars.CYAN}Your new dashboard is now up and running at ${chars.UNDERLINE}`
|
+ `┃ ${chars.CYAN}Your new dashboard is now up and running at ${chars.BRIGHT}`
|
||||||
+ `http://${ip}:${port}${chars.RESET}${blanks(18 - ip.length)}${chars.GREEN}┃${chars.BR}`
|
+ `http://${ip}:${port}${chars.RESET}${blanks(18 - ip.length)}${chars.GREEN}┃${chars.BR}`
|
||||||
+ `┃ ${chars.CYAN}After updating your config file, run '${chars.UNDERLINE}yarn build`
|
+ `┃ ${chars.CYAN}After updating your config file, run '${chars.BRIGHT}yarn build`
|
||||||
+ `${chars.RESET}${chars.CYAN}' to rebuild the app${blanks(6)}${chars.GREEN}┃${chars.BR}`
|
+ `${chars.RESET}${chars.CYAN}' to rebuild the app${blanks(6)}${chars.GREEN}┃${chars.BR}`
|
||||||
+ `┗${line(75)}┛${chars.BR}${chars.BR}`;
|
+ `┗${line(75)}┛${chars.BR}${chars.BR}`;
|
||||||
}
|
}
|
||||||
return msg;
|
return msg;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
/* eslint no-console: 0 */
|
||||||
|
const printWelcomeMessage = () => {
|
||||||
|
getLocalIp().then(({ address }) => {
|
||||||
|
const ip = address || 'localhost';
|
||||||
|
console.log(overComplicatedMessage(ip));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
connect()
|
connect()
|
||||||
.use(serveStatic(`${__dirname}/dist`))
|
.use(serveStatic(`${__dirname}/dist`))
|
||||||
.use(serveStatic(`${__dirname}/public`, { index: 'default.html' }))
|
.use(serveStatic(`${__dirname}/public`, { index: 'default.html' }))
|
||||||
.listen(port, () => {
|
.listen(port, () => {
|
||||||
try { printWelcomeMessage(port); }
|
try { printWelcomeMessage(); } catch (e) { console.log('Dashy is Starting...'); }
|
||||||
catch (e) { console.log('Dashy is Starting...'); }
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('Sorry, an error occurred ', error);
|
console.log('Sorry, an error occurred ', error);
|
||||||
|
@ -1,58 +1,63 @@
|
|||||||
const Ajv = require('ajv');
|
/* eslint-disable no-console */
|
||||||
const yaml = require('js-yaml');
|
/* Script that validates the conf.yml file against Dashy's schema, and outputs any issues */
|
||||||
const fs = require('fs');
|
const Ajv = require('ajv');
|
||||||
|
const yaml = require('js-yaml');
|
||||||
const schema = require('./ConfigSchema.json');
|
const fs = require('fs');
|
||||||
|
|
||||||
const validatorOptions = {
|
const schema = require('./ConfigSchema.json');
|
||||||
strict: true,
|
|
||||||
allowUnionTypes: true,
|
const validatorOptions = {
|
||||||
allErrors: true,
|
strict: true,
|
||||||
};
|
allowUnionTypes: true,
|
||||||
|
allErrors: true,
|
||||||
const ajv = new Ajv(validatorOptions);
|
};
|
||||||
|
|
||||||
/* Message printed when validation was successful */
|
const ajv = new Ajv(validatorOptions);
|
||||||
const successMsg = () => {
|
|
||||||
return '\x1b[1m\x1b[32m\033[1mNo issues found, your configuration is valid :)\x1b[0m\n';
|
/* Message printed when validation was successful */
|
||||||
}
|
const successMsg = () => '\x1b[1m\x1b[32mNo issues found, your configuration is valid :)\x1b[0m\n';
|
||||||
|
|
||||||
/* Formats error message. ready for printing to the console */
|
/* Formats error message. ready for printing to the console */
|
||||||
const errorMsg = (output) => {
|
const errorMsg = (output) => {
|
||||||
const warningFont = '\033[1m\x1b[103m\x1b[34m';
|
const warningFont = '\x1b[103m\x1b[34m';
|
||||||
const line = `${warningFont}${new Array(42).fill('━').join('')}\x1b[0m`;
|
const line = `${warningFont}${new Array(42).fill('━').join('')}\x1b[0m`;
|
||||||
let msg = `\n${line}\n${warningFont} Warning: ${output.length} `
|
let msg = `\n${line}\n${warningFont} Warning: ${output.length} `
|
||||||
+ `issue${output.length > 1 ? 's' : ''} found in config file \x1b[0m\n${line}\n`;
|
+ `issue${output.length > 1 ? 's' : ''} found in config file \x1b[0m\n${line}\n`;
|
||||||
output.forEach((details, index) => {
|
output.forEach((details, index) => {
|
||||||
msg += `${'\033[1m\x1b[36m'}${index + 1}. ${details.keyword} ${details.message} `
|
msg += `${'\x1b[36m'}${index + 1}. ${details.keyword} ${details.message} `
|
||||||
+ `in ${'\033[4m'}${details.instancePath}\x1b[0m\n`;
|
+ `in ${details.instancePath}\x1b[0m\n`;
|
||||||
});
|
});
|
||||||
return msg;
|
return msg;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Error message printed when the file could not be opened */
|
/* Error message printed when the file could not be opened */
|
||||||
const bigError = () => {
|
const bigError = () => {
|
||||||
const formatting = '\033[31m\033[1m\033[47m';
|
const formatting = '\x1b[30m\x1b[43m';
|
||||||
const line = `${formatting}${new Array(41).fill('━').join('')}\x1b[0m\n`;
|
const line = `${formatting}${new Array(38).fill('━').join('')}\x1b[0m\n`;
|
||||||
const msg = `${formatting} Error, unable to find / open 'conf.yml' \x1b[0m\n`;
|
const msg = `${formatting} Error, unable to validate 'conf.yml' \x1b[0m\n`;
|
||||||
return `${line}${msg}${line}\n`;
|
return `\n${line}${msg}${line}\n`;
|
||||||
}
|
};
|
||||||
|
|
||||||
/* Start the validation */
|
/* Start the validation */
|
||||||
const validate = (config, schema) => {
|
const validate = (config) => {
|
||||||
console.log('\nChecking config file against schema...');
|
console.log('\nChecking config file against schema...');
|
||||||
const valid = ajv.validate(schema, config);
|
const valid = ajv.validate(schema, config);
|
||||||
if (valid) {
|
if (valid) {
|
||||||
console.log(successMsg());
|
console.log(successMsg());
|
||||||
} else {
|
} else {
|
||||||
console.log(errorMsg(ajv.errors));
|
console.log(errorMsg(ajv.errors));
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const config = yaml.safeLoad(fs.readFileSync('./public/conf.yml', 'utf8'));
|
const config = yaml.load(fs.readFileSync('./public/conf.yml', 'utf8'));
|
||||||
validate(config, schema);
|
validate(config);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(bigError(), e);
|
console.log(bigError());
|
||||||
}
|
console.log('Please ensure that your config file is present, '
|
||||||
|
+ 'has the correct access rights and is parsable. '
|
||||||
|
+ 'If this warning persists, it may be an issue with the '
|
||||||
|
+ 'validator function. Please raise an issue, and include the following stack trace:\n');
|
||||||
|
console.warn('\x1b[33mStack Trace for ConfigValidators.js:\x1b[0m\n', e);
|
||||||
|
console.log('\n\n');
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user