From cd760f0cf83cb00b9c4b372ed1b6ed5baf1b6272 Mon Sep 17 00:00:00 2001 From: Mhykol Date: Mon, 3 Jun 2024 05:26:09 -0400 Subject: Added error handling for missing directories and examples --- examples/index.js | 21 +++++++++++++++ examples/shop.js | 51 +++++++++++++++++++++++++++++++++++ source/blog.js | 76 +++++++++++++++++++++++++++------------------------- source/controller.js | 42 ++++++++++++++++++----------- 4 files changed, 138 insertions(+), 52 deletions(-) create mode 100644 examples/index.js create mode 100644 examples/shop.js diff --git a/examples/index.js b/examples/index.js new file mode 100644 index 0000000..64a619b --- /dev/null +++ b/examples/index.js @@ -0,0 +1,21 @@ +class Index { + constructor() {} + + async Main() { + return { + html: ` +
+

Welcome to Cosmic!

+

Please read the README.md

+
+ `, + meta: { + keywords: 'Cosmic', + description: 'Welcome to Cosmic!' + } + } + } +} + +module.exports = (x) => {return new Index(x)} + diff --git a/examples/shop.js b/examples/shop.js new file mode 100644 index 0000000..e75d289 --- /dev/null +++ b/examples/shop.js @@ -0,0 +1,51 @@ +const database = require('../source/database') +const estore = database('EStore') + +class Shop { + constructor() {} + + async Main() { + return estore.Query('SELECT * FROM Products').then(output => { + const data = { + start: ` +
+
+

This is the Shop page

+ `, + body: '', + end: ` +
+
+ ` + } + if (output) { + for (let i = 0; i < output[0].length; i++) { + data.body += ` +
+

${output[0][i].ProductName}

+

ID: ${output[0][i].ID}

+
+ ` + } + } else { + data.body = ` + There are no items in the database. + ` + } + return data.start + data.body + data.end + }) + } + Product() { + const data = ` +
+
+

This is the Product page

+

Description

+
+
+ ` + return data + } +} + +module.exports = () => {return new Shop()} diff --git a/source/blog.js b/source/blog.js index cc654dd..56f76a4 100644 --- a/source/blog.js +++ b/source/blog.js @@ -16,52 +16,54 @@ class Blog { async ReadBlogs() {return this.#LoadBlogs().then(output => {return output})} + // ! Check if file is a markdown file async #LoadBlogs() { return new Promise((res, rej) => { - fs.readdir('./blog/', (err, files) => { - if (err) { - logger.Error(`Error reading directory ${err}`) - rej(null) - } else { - this.blog = new Map() - files.forEach(file => { - try { + if (fs.existsSync('./blog/')) { + fs.readdir('./blog/', (err, files) => { + if (err) { + logger.Error(`${err.code}: Error reading directory`) + rej(`${err.code}: Error reading directory`) + } else { + this.blog = new Map() + files.forEach(file => { this.blog.set(file.replace('.md', ''), fs.readFileSync(path.resolve(`./blog/${file}`), 'utf-8', (err, data) => { if (err) { - logger.Error(err) + logger.Error(`${err.code}: Failed to read ${file}`) + rej(`${err.code}: Failed to read ${file}`) return } return data })) - } catch (err) { - logger.Error(err) - rej(null) - } - }) - this.blog.forEach((value, key) => { - const regex = /# (.*)\nDate: (.*)\n---(.*?)---/s - const match = value.match(regex) + }) + this.blog.forEach((value, key) => { + const regex = /# (.*)\nDate: (.*)\n---(.*?)---/s + const match = value.match(regex) - if (match) { - const [, title, date, description] = match - this.blog.set(key, { - title: title, - date: new Date(date), - short: description, - body: md.render(value) - }) - } else { - this.blog.set(key, { - title: undefined, - date: undefined, - short: undefined, - body: md.render(value) - }) - } - }) - res(this.#OrderByDate(this.blog)) - } - }) + if (match) { + const [, title, date, description] = match + this.blog.set(key, { + title: title, + date: new Date(date), + short: description, + body: md.render(value) + }) + } else { + this.blog.set(key, { + title: undefined, + date: undefined, + short: undefined, + body: md.render(value) + }) + } + }) + res(this.#OrderByDate(this.blog)) + } + }) + } else { + logger.Error('ERROR: blog directory does not exist') + rej('ERROR: blog directory does not exist') + } }) } diff --git a/source/controller.js b/source/controller.js index c66df01..889e868 100644 --- a/source/controller.js +++ b/source/controller.js @@ -1,4 +1,5 @@ const fs = require('fs') +const path = require('path') const Method = require('./method') const logger = require('./logger')() @@ -6,23 +7,33 @@ const logger = require('./logger')() class Controller { constructor() { this.main = new Promise((res, rej) => { - fs.readdir('./scripts/', (err, files) => { - if (err) { - logger.Error(`${err.code}: Failed to read directory`) - rej(`${err.code}: Failed to read directory`) - } + if (fs.existsSync('./scripts/')) { + fs.readdir('./scripts/', (err, files) => { + if (err) { + logger.Error(`${err.code}: Failed to read directory`) + rej(`${err.code}: Failed to read directory`) + } - this.require = new Map() - files.forEach(file => { - try { - this.require.set(file.replace('.js', ''), require(`../scripts/${file}`)()) - } catch (err) { - logger.Error(`${err.code}: Failed to read ${file}`) - rej(`${err.code}: Failed to read ${file}`) + this.require = new Map() + files.forEach(file => { + try { + this.require.set(file.replace('.js', ''), require(`../scripts/${file}`)()) + } catch (err) { + logger.Error(`${err.code}: Failed to read ${file}`) + rej(`${err.code}: Failed to read ${file}`) + } + }) + res(this.require) + }) + } else { + fs.mkdirSync('./scripts/') + fs.writeFile(path.join('./scripts/', 'index.js'), fs.readFileSync('./examples/index.js', 'utf-8'), (err) => { + if (err) { + logger.Error(`${err.code}: Error writing to file`) + rej(`${err.code}: Error writing to file`) } }) - res(this.require) - }) + } }).then(output => { return new Promise((res, rej) => { const map = output @@ -95,4 +106,5 @@ class Controller { } } -module.exports = Controller +module.exports = Controller + -- cgit v1.2.3-70-g09d2